summaryrefslogtreecommitdiffabout
authorSergey Poznyakoff <gray@gnu.org.ua>2010-06-29 21:36:06 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2010-06-29 21:38:17 (GMT)
commitc0992a1981165a975148b2a334f2556c3efb6fdf (patch) (side-by-side diff)
tree6586cf46eabb4a48188e45179782c2176b5a1839
parent2f831a4b8b76074284c9f8321ad1f8249664834e (diff)
downloadsmap-c0992a1981165a975148b2a334f2556c3efb6fdf.tar.gz
smap-c0992a1981165a975148b2a334f2556c3efb6fdf.tar.bz2
Improve SQL interface.
* modules/mysql/mysql.c: Always open database, if enough data are given to do so. Remove spurious options defaultdb and open. * modules/postgres/postgres.c: Likewise. * doc/ex-meta1.texi: Update.
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--doc/ex-meta1.texi10
-rw-r--r--modules/mysql/mysql.c37
-rw-r--r--modules/postgres/postgres.c32
3 files changed, 32 insertions, 47 deletions
diff --git a/doc/ex-meta1.texi b/doc/ex-meta1.texi
index d74b49c..a62b4be 100644
--- a/doc/ex-meta1.texi
+++ b/doc/ex-meta1.texi
@@ -13,19 +13,18 @@ of which uses a separate configuration file.
To reduce the number of connections to the @acronym{MySQL} server,
the @acronym{MySQL} database will be opened at the module level and
shared between the two smap databases. Thus, the module
initialization in @file{smapd.conf} looks like:
@example
-module mysql mysql open config-group=smap
+module mysql mysql config-group=smap
@end example
-The @samp{open} parameter instructs the module to open the requested
-databases. The @samp{config-group} parameter refers to a group
-name in the default @file{/etc/my.cnf} file that contains information
-about the @acronym{MySQL} database and credentials for accessing it.
+The @samp{config-group} parameter refers to a group name in the
+default @file{/etc/my.cnf} file that contains information about the
+@acronym{MySQL} database and credentials for accessing it.
The following is a sample snippet from @file{/etc/my.cnf}:
@example
[smap]
database = Mail
user = smap
@@ -58,13 +57,12 @@ CREATE TABLE userdb (
The smap database is defined as follows:
@example
@group
database userdb mysql \
- defaultdb
query="SELECT user FROM userdb WHERE user='$key'"
positive-reply=OK
@end group
@end example
The @samp{defaultdb} parameter tells it to use the default SQL
diff --git a/modules/mysql/mysql.c b/modules/mysql/mysql.c
index 3d24542..315dd8b 100644
--- a/modules/mysql/mysql.c
+++ b/modules/mysql/mysql.c
@@ -28,15 +28,14 @@
#include <smap/diag.h>
#include <smap/module.h>
#include <smap/parseopt.h>
#include <smap/stream.h>
#include <smap/wordsplit.h>
-#define MDB_INIT 0x01
-#define MDB_OPEN 0x02
-#define MDB_DEFDB 0x04
+#define MDB_OPEN 0x01
+#define MDB_DEFDB 0x02
struct mod_mysql_db {
int flags;
unsigned refcnt;
MYSQL mysql;
const char *name;
@@ -87,18 +86,12 @@ opendb(struct mod_mysql_db *db)
{
if (db->flags & MDB_OPEN) {
db->refcnt++;
return 0;
}
- if (!(db->flags & MDB_INIT)) {
- smap_error("%s: module settings do not include opendb flag",
- db->name);
- return 1;
- }
-
if (!mysql_init(&db->mysql)) {
smap_error("%s: not enough memory", db->name);
return 1;
}
if (db->config_file)
@@ -153,21 +146,33 @@ freedb(struct mod_mysql_db *db)
free(db->positive_reply);
free(db->negative_reply);
free(db->onerror_reply);
}
static int
+dbdeclared(struct mod_mysql_db *db)
+{
+ return db->config_file ||
+ db->config_group ||
+ db->ssl_ca ||
+ db->host ||
+ db->user ||
+ db->password ||
+ db->database ||
+ db->port ||
+ db->socket;
+}
+
+
+static int
mod_init(int argc, char **argv)
{
int rc;
- int opendb = 0;
dbgid = smap_debug_alloc("mysql");
struct smap_option init_option[] = {
- { SMAP_OPTSTR(open), smap_opt_bool,
- &opendb },
{ SMAP_OPTSTR(config-file), smap_opt_string,
&def_db.config_file },
{ SMAP_OPTSTR(config-group), smap_opt_string,
&def_db.config_group },
{ SMAP_OPTSTR(ssl-ca), smap_opt_string,
&def_db.ssl_ca },
@@ -194,14 +199,13 @@ mod_init(int argc, char **argv)
&def_db.onerror_reply },
{ NULL }
};
rc = smap_parseopt(init_option, argc, argv, 0, NULL);
if (rc)
return rc;
- if (opendb)
- def_db.flags = MDB_INIT;
+ def_db.flags = 0;
return 0;
}
static smap_database_t
mod_init_db(const char *dbid, int argc, char **argv)
{
@@ -259,13 +263,13 @@ mod_init_db(const char *dbid, int argc, char **argv)
db = calloc(1, sizeof(*db));
if (!db) {
smap_error("%s: not enough memory", dbid);
return NULL;
}
- db->flags = MDB_INIT | flags;
+ db->flags = flags;
db->name = dbid;
db->config_file = config_file;
db->config_group = config_group;
db->ssl_ca = ssl_ca;
db->host = host;
db->user = user;
@@ -275,12 +279,15 @@ mod_init_db(const char *dbid, int argc, char **argv)
db->socket = socket;
db->template = query;
db->positive_reply = positive_reply;
db->negative_reply = negative_reply;
db->onerror_reply = onerror_reply;
+ if (!dbdeclared(db))
+ db->flags |= MDB_DEFDB;
+
return (smap_database_t) db;
}
static int
mod_free_db(smap_database_t dbp)
{
diff --git a/modules/postgres/postgres.c b/modules/postgres/postgres.c
index 3e696ca..1218564 100644
--- a/modules/postgres/postgres.c
+++ b/modules/postgres/postgres.c
@@ -29,15 +29,14 @@
#include <smap/diag.h>
#include <smap/module.h>
#include <smap/parseopt.h>
#include <smap/stream.h>
#include <smap/wordsplit.h>
-#define MDB_INIT 0x01
-#define MDB_OPEN 0x02
-#define MDB_DEFDB 0x04
+#define MDB_OPEN 0x01
+#define MDB_DEFDB 0x02
struct modpg_db {
int flags;
unsigned refcnt;
const char *name;
PGconn *pgconn;
@@ -80,18 +79,12 @@ opendb(struct modpg_db *db)
{
if (db->flags & MDB_OPEN) {
db->refcnt++;
return 0;
}
- if (!(db->flags & MDB_INIT)) {
- smap_error("%s: module settings do not include opendb flag",
- db->name);
- return 1;
- }
-
db->pgconn = PQconnectdb(db->conninfo);
if (!db->pgconn) {
smap_error("%s: out of memory", db->name);
return 1;
}
@@ -199,18 +192,15 @@ create_conninfo(int argc, char **argv)
static int
modpg_init(int argc, char **argv)
{
int i;
int rc;
- int opendb = 0;
dbgid = smap_debug_alloc("postgres");
struct smap_option init_option[] = {
- { SMAP_OPTSTR(open), smap_opt_bool,
- &opendb },
{ SMAP_OPTSTR(query), smap_opt_string,
&def_db.template },
{ SMAP_OPTSTR(positive-reply), smap_opt_string,
&def_db.positive_reply },
{ SMAP_OPTSTR(negative-reply), smap_opt_string,
&def_db.negative_reply },
@@ -224,19 +214,13 @@ modpg_init(int argc, char **argv)
if (i < argc) {
def_db.conninfo = create_conninfo(argc - i, argv + i);
if (!def_db.conninfo) {
smap_error("out of memory");
return 1;
}
- }
- if (opendb) {
- if (!def_db.conninfo) {
- smap_error("no connection info given");
- return 1;
- }
- def_db.flags = MDB_INIT;
+ def_db.flags = 0;
def_db.name = "postgres";
}
return 0;
}
static smap_database_t
@@ -248,14 +232,12 @@ modpg_init_db(const char *dbid, int argc, char **argv)
char *onerror_reply = NULL;
char *query = NULL;
int flags = 0;
int i;
struct smap_option init_option[] = {
- { SMAP_OPTSTR(defaultdb), smap_opt_bitmask,
- &flags, { MDB_DEFDB } },
{ SMAP_OPTSTR(query), smap_opt_string,
&query },
{ SMAP_OPTSTR(positive-reply), smap_opt_string,
&positive_reply },
{ SMAP_OPTSTR(negative-reply), smap_opt_string,
&negative_reply },
@@ -276,18 +258,16 @@ modpg_init_db(const char *dbid, int argc, char **argv)
db->conninfo = create_conninfo(argc - i, argv + i);
if (!db->conninfo) {
smap_error("out of memory");
free(db);
return NULL;
}
- } else if (!(flags & MDB_DEFDB)) {
- smap_error("%s: no connection info given", dbid);
- return NULL;
- }
+ } else
+ flags |= MDB_DEFDB;
- db->flags = MDB_INIT | flags;
+ db->flags = flags;
db->name = dbid;
db->template = query;
db->positive_reply = positive_reply;
db->negative_reply = negative_reply;
db->onerror_reply = onerror_reply;

Return to:

Send suggestions and report system problems to the System administrator.