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) (unidiff)
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) (ignore 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.
13 To reduce the number of connections to the @acronym{MySQL} server, 13 To reduce the number of connections to the @acronym{MySQL} server,
14the @acronym{MySQL} database will be opened at the module level and 14the @acronym{MySQL} database will be opened at the module level and
15shared between the two smap databases. Thus, the module 15shared between the two smap databases. Thus, the module
16initialization in @file{smapd.conf} looks like: 16initialization in @file{smapd.conf} looks like:
17 17
18@example 18@example
19module mysql mysql open config-group=smap 19module mysql mysql config-group=smap
20@end example 20@end example
21 21
22The @samp{open} parameter instructs the module to open the requested 22The @samp{config-group} parameter refers to a group name in the
23databases. The @samp{config-group} parameter refers to a group 23default @file{/etc/my.cnf} file that contains information about the
24name in the default @file{/etc/my.cnf} file that contains information 24@acronym{MySQL} database and credentials for accessing it.
25about the @acronym{MySQL} database and credentials for accessing it.
26The following is a sample snippet from @file{/etc/my.cnf}: 25The following is a sample snippet from @file{/etc/my.cnf}:
27 26
28@example 27@example
29[smap] 28[smap]
30database = Mail 29database = Mail
31user = smap 30user = smap
@@ -58,13 +57,12 @@ CREATE TABLE userdb (
58 57
59The smap database is defined as follows: 58The smap database is defined as follows:
60 59
61@example 60@example
62@group 61@group
63database userdb mysql \ 62database userdb mysql \
64 defaultdb
65 query="SELECT user FROM userdb WHERE user='$key'" 63 query="SELECT user FROM userdb WHERE user='$key'"
66 positive-reply=OK 64 positive-reply=OK
67@end group 65@end group
68@end example 66@end example
69 67
70The @samp{defaultdb} parameter tells it to use the default SQL 68The @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 @@
28#include <smap/diag.h> 28#include <smap/diag.h>
29#include <smap/module.h> 29#include <smap/module.h>
30#include <smap/parseopt.h> 30#include <smap/parseopt.h>
31#include <smap/stream.h> 31#include <smap/stream.h>
32#include <smap/wordsplit.h> 32#include <smap/wordsplit.h>
33 33
34#define MDB_INIT 0x01 34#define MDB_OPEN 0x01
35#define MDB_OPEN 0x02 35#define MDB_DEFDB 0x02
36#define MDB_DEFDB 0x04
37 36
38struct mod_mysql_db { 37struct mod_mysql_db {
39 int flags; 38 int flags;
40 unsigned refcnt; 39 unsigned refcnt;
41 MYSQL mysql; 40 MYSQL mysql;
42 const char *name; 41 const char *name;
@@ -87,18 +86,12 @@ opendb(struct mod_mysql_db *db)
87{ 86{
88 if (db->flags & MDB_OPEN) { 87 if (db->flags & MDB_OPEN) {
89 db->refcnt++; 88 db->refcnt++;
90 return 0; 89 return 0;
91 } 90 }
92 91
93 if (!(db->flags & MDB_INIT)) {
94 smap_error("%s: module settings do not include opendb flag",
95 db->name);
96 return 1;
97 }
98
99 if (!mysql_init(&db->mysql)) { 92 if (!mysql_init(&db->mysql)) {
100 smap_error("%s: not enough memory", db->name); 93 smap_error("%s: not enough memory", db->name);
101 return 1; 94 return 1;
102 } 95 }
103 96
104 if (db->config_file) 97 if (db->config_file)
@@ -153,21 +146,33 @@ freedb(struct mod_mysql_db *db)
153 free(db->positive_reply); 146 free(db->positive_reply);
154 free(db->negative_reply); 147 free(db->negative_reply);
155 free(db->onerror_reply); 148 free(db->onerror_reply);
156} 149}
157 150
158static int 151static int
152dbdeclared(struct mod_mysql_db *db)
153{
154 return db->config_file ||
155 db->config_group ||
156 db->ssl_ca ||
157 db->host ||
158 db->user ||
159 db->password ||
160 db->database ||
161 db->port ||
162 db->socket;
163}
164
165
166static int
159mod_init(int argc, char **argv) 167mod_init(int argc, char **argv)
160{ 168{
161 int rc; 169 int rc;
162 int opendb = 0;
163 170
164 dbgid = smap_debug_alloc("mysql"); 171 dbgid = smap_debug_alloc("mysql");
165 struct smap_option init_option[] = { 172 struct smap_option init_option[] = {
166 { SMAP_OPTSTR(open), smap_opt_bool,
167 &opendb },
168 { SMAP_OPTSTR(config-file), smap_opt_string, 173 { SMAP_OPTSTR(config-file), smap_opt_string,
169 &def_db.config_file }, 174 &def_db.config_file },
170 { SMAP_OPTSTR(config-group), smap_opt_string, 175 { SMAP_OPTSTR(config-group), smap_opt_string,
171 &def_db.config_group }, 176 &def_db.config_group },
172 { SMAP_OPTSTR(ssl-ca), smap_opt_string, 177 { SMAP_OPTSTR(ssl-ca), smap_opt_string,
173 &def_db.ssl_ca }, 178 &def_db.ssl_ca },
@@ -194,14 +199,13 @@ mod_init(int argc, char **argv)
194 &def_db.onerror_reply }, 199 &def_db.onerror_reply },
195 { NULL } 200 { NULL }
196 }; 201 };
197 rc = smap_parseopt(init_option, argc, argv, 0, NULL); 202 rc = smap_parseopt(init_option, argc, argv, 0, NULL);
198 if (rc) 203 if (rc)
199 return rc; 204 return rc;
200 if (opendb) 205 def_db.flags = 0;
201 def_db.flags = MDB_INIT;
202 return 0; 206 return 0;
203} 207}
204 208
205static smap_database_t 209static smap_database_t
206mod_init_db(const char *dbid, int argc, char **argv) 210mod_init_db(const char *dbid, int argc, char **argv)
207{ 211{
@@ -259,13 +263,13 @@ mod_init_db(const char *dbid, int argc, char **argv)
259 db = calloc(1, sizeof(*db)); 263 db = calloc(1, sizeof(*db));
260 if (!db) { 264 if (!db) {
261 smap_error("%s: not enough memory", dbid); 265 smap_error("%s: not enough memory", dbid);
262 return NULL; 266 return NULL;
263 } 267 }
264 268
265 db->flags = MDB_INIT | flags; 269 db->flags = flags;
266 db->name = dbid; 270 db->name = dbid;
267 db->config_file = config_file; 271 db->config_file = config_file;
268 db->config_group = config_group; 272 db->config_group = config_group;
269 db->ssl_ca = ssl_ca; 273 db->ssl_ca = ssl_ca;
270 db->host = host; 274 db->host = host;
271 db->user = user; 275 db->user = user;
@@ -275,12 +279,15 @@ mod_init_db(const char *dbid, int argc, char **argv)
275 db->socket = socket; 279 db->socket = socket;
276 db->template = query; 280 db->template = query;
277 db->positive_reply = positive_reply; 281 db->positive_reply = positive_reply;
278 db->negative_reply = negative_reply; 282 db->negative_reply = negative_reply;
279 db->onerror_reply = onerror_reply; 283 db->onerror_reply = onerror_reply;
280 284
285 if (!dbdeclared(db))
286 db->flags |= MDB_DEFDB;
287
281 return (smap_database_t) db; 288 return (smap_database_t) db;
282} 289}
283 290
284static int 291static int
285mod_free_db(smap_database_t dbp) 292mod_free_db(smap_database_t dbp)
286{ 293{
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 @@
29#include <smap/diag.h> 29#include <smap/diag.h>
30#include <smap/module.h> 30#include <smap/module.h>
31#include <smap/parseopt.h> 31#include <smap/parseopt.h>
32#include <smap/stream.h> 32#include <smap/stream.h>
33#include <smap/wordsplit.h> 33#include <smap/wordsplit.h>
34 34
35#define MDB_INIT 0x01 35#define MDB_OPEN 0x01
36#define MDB_OPEN 0x02 36#define MDB_DEFDB 0x02
37#define MDB_DEFDB 0x04
38 37
39struct modpg_db { 38struct modpg_db {
40 int flags; 39 int flags;
41 unsigned refcnt; 40 unsigned refcnt;
42 const char *name; 41 const char *name;
43 PGconn *pgconn; 42 PGconn *pgconn;
@@ -80,18 +79,12 @@ opendb(struct modpg_db *db)
80{ 79{
81 if (db->flags & MDB_OPEN) { 80 if (db->flags & MDB_OPEN) {
82 db->refcnt++; 81 db->refcnt++;
83 return 0; 82 return 0;
84 } 83 }
85 84
86 if (!(db->flags & MDB_INIT)) {
87 smap_error("%s: module settings do not include opendb flag",
88 db->name);
89 return 1;
90 }
91
92 db->pgconn = PQconnectdb(db->conninfo); 85 db->pgconn = PQconnectdb(db->conninfo);
93 if (!db->pgconn) { 86 if (!db->pgconn) {
94 smap_error("%s: out of memory", db->name); 87 smap_error("%s: out of memory", db->name);
95 return 1; 88 return 1;
96 } 89 }
97 90
@@ -199,18 +192,15 @@ create_conninfo(int argc, char **argv)
199 192
200static int 193static int
201modpg_init(int argc, char **argv) 194modpg_init(int argc, char **argv)
202{ 195{
203 int i; 196 int i;
204 int rc; 197 int rc;
205 int opendb = 0;
206 198
207 dbgid = smap_debug_alloc("postgres"); 199 dbgid = smap_debug_alloc("postgres");
208 struct smap_option init_option[] = { 200 struct smap_option init_option[] = {
209 { SMAP_OPTSTR(open), smap_opt_bool,
210 &opendb },
211 { SMAP_OPTSTR(query), smap_opt_string, 201 { SMAP_OPTSTR(query), smap_opt_string,
212 &def_db.template }, 202 &def_db.template },
213 { SMAP_OPTSTR(positive-reply), smap_opt_string, 203 { SMAP_OPTSTR(positive-reply), smap_opt_string,
214 &def_db.positive_reply }, 204 &def_db.positive_reply },
215 { SMAP_OPTSTR(negative-reply), smap_opt_string, 205 { SMAP_OPTSTR(negative-reply), smap_opt_string,
216 &def_db.negative_reply }, 206 &def_db.negative_reply },
@@ -224,19 +214,13 @@ modpg_init(int argc, char **argv)
224 if (i < argc) { 214 if (i < argc) {
225 def_db.conninfo = create_conninfo(argc - i, argv + i); 215 def_db.conninfo = create_conninfo(argc - i, argv + i);
226 if (!def_db.conninfo) { 216 if (!def_db.conninfo) {
227 smap_error("out of memory"); 217 smap_error("out of memory");
228 return 1; 218 return 1;
229 } 219 }
230 } 220 def_db.flags = 0;
231 if (opendb) {
232 if (!def_db.conninfo) {
233 smap_error("no connection info given");
234 return 1;
235 }
236 def_db.flags = MDB_INIT;
237 def_db.name = "postgres"; 221 def_db.name = "postgres";
238 } 222 }
239 return 0; 223 return 0;
240} 224}
241 225
242static smap_database_t 226static smap_database_t
@@ -248,14 +232,12 @@ modpg_init_db(const char *dbid, int argc, char **argv)
248 char *onerror_reply = NULL; 232 char *onerror_reply = NULL;
249 char *query = NULL; 233 char *query = NULL;
250 int flags = 0; 234 int flags = 0;
251 int i; 235 int i;
252 236
253 struct smap_option init_option[] = { 237 struct smap_option init_option[] = {
254 { SMAP_OPTSTR(defaultdb), smap_opt_bitmask,
255 &flags, { MDB_DEFDB } },
256 { SMAP_OPTSTR(query), smap_opt_string, 238 { SMAP_OPTSTR(query), smap_opt_string,
257 &query }, 239 &query },
258 { SMAP_OPTSTR(positive-reply), smap_opt_string, 240 { SMAP_OPTSTR(positive-reply), smap_opt_string,
259 &positive_reply }, 241 &positive_reply },
260 { SMAP_OPTSTR(negative-reply), smap_opt_string, 242 { SMAP_OPTSTR(negative-reply), smap_opt_string,
261 &negative_reply }, 243 &negative_reply },
@@ -276,18 +258,16 @@ modpg_init_db(const char *dbid, int argc, char **argv)
276 db->conninfo = create_conninfo(argc - i, argv + i); 258 db->conninfo = create_conninfo(argc - i, argv + i);
277 if (!db->conninfo) { 259 if (!db->conninfo) {
278 smap_error("out of memory"); 260 smap_error("out of memory");
279 free(db); 261 free(db);
280 return NULL; 262 return NULL;
281 } 263 }
282 } else if (!(flags & MDB_DEFDB)) { 264 } else
283 smap_error("%s: no connection info given", dbid); 265 flags |= MDB_DEFDB;
284 return NULL;
285 }
286 266
287 db->flags = MDB_INIT | flags; 267 db->flags = flags;
288 db->name = dbid; 268 db->name = dbid;
289 db->template = query; 269 db->template = query;
290 db->positive_reply = positive_reply; 270 db->positive_reply = positive_reply;
291 db->negative_reply = negative_reply; 271 db->negative_reply = negative_reply;
292 db->onerror_reply = onerror_reply; 272 db->onerror_reply = onerror_reply;
293 273

Return to:

Send suggestions and report system problems to the System administrator.