diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2010-06-30 00:36:06 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2010-06-30 00:38:17 +0300 |
commit | c0992a1981165a975148b2a334f2556c3efb6fdf (patch) | |
tree | 6586cf46eabb4a48188e45179782c2176b5a1839 | |
parent | 2f831a4b8b76074284c9f8321ad1f8249664834e (diff) | |
download | smap-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.
-rw-r--r-- | doc/ex-meta1.texi | 10 | ||||
-rw-r--r-- | modules/mysql/mysql.c | 37 | ||||
-rw-r--r-- | 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 @@ -7,31 +7,30 @@ module (@pxref{mysql,mysql module}) to configure local user and alias maps for @acronym{MeTA1}. For this purpose, we will assume that the actual data is stored in two tables in a @acronym{MySQL} database. The two maps will be served by two separate databases, each 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 password = guessme socket = /tmp/mysql.sock @end example @menu * userdb-meta1:: Configure local_user_map. @@ -52,25 +51,24 @@ CREATE TABLE userdb ( user varchar(32) NOT NULL default '', mailbox text PRIMARY KEY (user) ); @end group @end example 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 database opened in the module initialization instruction. The @samp{query} parameter supplies the SQL query to run (the @samp{$@{key@}} variable will be expanded to the value of the actual lookup key, prior to executing the query). Finally, @samp{positive-reply} defines the reply to give if the query returns some tuples. The database only verifies whether the user is present 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 @@ -22,27 +22,26 @@ #include <regex.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <mysql/mysql.h> #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; char *config_file; char *config_group; char *ssl_ca; char *host; char *user; char *password; @@ -81,30 +80,24 @@ moddb_negative_reply(struct mod_mysql_db *db) db->negative_reply : (def_db.negative_reply ? def_db.negative_reply : "NOTFOUND"); } static int 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) mysql_options(&db->mysql, MYSQL_READ_DEFAULT_FILE, db->config_file); if (db->config_group) mysql_options (&db->mysql, MYSQL_READ_DEFAULT_GROUP, db->config_group); @@ -147,33 +140,45 @@ freedb(struct mod_mysql_db *db) free(db->host); free(db->user); free(db->password); free(db->database); free(db->socket); free(db->template); 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 }, { SMAP_OPTSTR(host), smap_opt_string, &def_db.host }, { SMAP_OPTSTR(user), smap_opt_string, &def_db.user }, { SMAP_OPTSTR(password), smap_opt_string, &def_db.password }, @@ -188,26 +193,25 @@ mod_init(int argc, char **argv) &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 }, { SMAP_OPTSTR(onerror-reply), smap_opt_string, &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) { struct mod_mysql_db *db; char *positive_reply = NULL; char *negative_reply = NULL; char *onerror_reply = NULL; char *query = NULL; char *config_file = NULL; @@ -253,40 +257,43 @@ mod_init_db(const char *dbid, int argc, char **argv) { NULL } }; if (smap_parseopt(init_option, argc, argv, 0, NULL)) return NULL; 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; db->password = password; db->database = database; db->port = port; 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) { struct mod_mysql_db *db = (struct mod_mysql_db *)dbp; freedb(db); free(db); return 0; } 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 @@ -23,27 +23,26 @@ #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <libpq-fe.h> #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; char *conninfo; char *template; char *positive_reply; char *negative_reply; char *onerror_reply; }; @@ -74,30 +73,24 @@ modpg_negative_reply(struct modpg_db *db) db->negative_reply : (def_db.negative_reply ? def_db.negative_reply : "NOTFOUND"); } static int 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; } if (PQstatus(db->pgconn) == CONNECTION_BAD) { smap_error("%s: cannot connect: %s", db->name, PQerrorMessage(db->pgconn)); PQfinish(db->pgconn); db->pgconn = NULL; return 1; @@ -193,75 +186,64 @@ create_conninfo(int argc, char **argv) } *p++ = ' '; } p[-1] = 0; return conninfo; } 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 }, { SMAP_OPTSTR(onerror-reply), smap_opt_string, &def_db.onerror_reply }, { NULL } }; rc = smap_parseopt(init_option, argc, argv, SMAP_PARSEOPT_PERMUTE, &i); if (rc) return rc; 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 modpg_init_db(const char *dbid, int argc, char **argv) { struct modpg_db *db; char *positive_reply = NULL; char *negative_reply = NULL; 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 }, { SMAP_OPTSTR(onerror-reply), smap_opt_string, &onerror_reply }, { NULL } }; if (smap_parseopt(init_option, argc, argv, SMAP_PARSEOPT_PERMUTE, &i)) @@ -270,30 +252,28 @@ modpg_init_db(const char *dbid, int argc, char **argv) if (!db) { smap_error("%s: not enough memory", dbid); return NULL; } if (i < argc) { 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 (smap_database_t) db; } static int modpg_free_db(smap_database_t dbp) { |