From c0992a1981165a975148b2a334f2556c3efb6fdf Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Wed, 30 Jun 2010 00:36:06 +0300 Subject: 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. --- doc/ex-meta1.texi | 10 ++++------ modules/mysql/mysql.c | 37 ++++++++++++++++++++++--------------- modules/postgres/postgres.c | 32 ++++++-------------------------- 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 @@ -16,13 +16,12 @@ 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 @@ -61,7 +60,6 @@ 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 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 @@ -31,9 +31,8 @@ #include #include -#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; @@ -90,12 +89,6 @@ opendb(struct mod_mysql_db *db) 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; @@ -155,16 +148,28 @@ freedb(struct mod_mysql_db *db) 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, @@ -197,8 +202,7 @@ mod_init(int argc, char **argv) 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; } @@ -262,7 +266,7 @@ mod_init_db(const char *dbid, int argc, char **argv) return NULL; } - db->flags = MDB_INIT | flags; + db->flags = flags; db->name = dbid; db->config_file = config_file; db->config_group = config_group; @@ -278,6 +282,9 @@ mod_init_db(const char *dbid, int argc, char **argv) db->negative_reply = negative_reply; db->onerror_reply = onerror_reply; + if (!dbdeclared(db)) + db->flags |= MDB_DEFDB; + return (smap_database_t) db; } 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 @@ -32,9 +32,8 @@ #include #include -#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; @@ -83,12 +82,6 @@ opendb(struct modpg_db *db) 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); @@ -202,12 +195,9 @@ 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, @@ -227,13 +217,7 @@ modpg_init(int argc, char **argv) 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; @@ -251,8 +235,6 @@ modpg_init_db(const char *dbid, int argc, char **argv) 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, @@ -279,12 +261,10 @@ modpg_init_db(const char *dbid, int argc, char **argv) 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; -- cgit v1.2.1