diff options
Diffstat (limited to 'modules/mysql/mysql.c')
-rw-r--r-- | modules/mysql/mysql.c | 37 |
1 files changed, 22 insertions, 15 deletions
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 | ||
38 | struct mod_mysql_db { | 37 | struct 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 | ||
158 | static int | 151 | static int |
152 | dbdeclared(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 | |||
166 | static int | ||
159 | mod_init(int argc, char **argv) | 167 | mod_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 | ||
205 | static smap_database_t | 209 | static smap_database_t |
206 | mod_init_db(const char *dbid, int argc, char **argv) | 210 | mod_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 | ||
284 | static int | 291 | static int |
285 | mod_free_db(smap_database_t dbp) | 292 | mod_free_db(smap_database_t dbp) |
286 | { | 293 | { |