aboutsummaryrefslogtreecommitdiff
path: root/modules/postgres/postgres.c
diff options
context:
space:
mode:
Diffstat (limited to 'modules/postgres/postgres.c')
-rw-r--r--modules/postgres/postgres.c32
1 files changed, 6 insertions, 26 deletions
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.