pg_mod.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * $Id$
  3. *
  4. * PostgreSQL Database Driver for SER
  5. *
  6. * Portions Copyright (C) 2001-2003 FhG FOKUS
  7. * Copyright (C) 2003 August.Net Services, LLC
  8. * Portions Copyright (C) 2005-2008 iptelorg GmbH
  9. *
  10. * This file is part of SER, a free SIP server.
  11. *
  12. * SER is free software; you can redistribute it and/or modify it under the
  13. * terms of the GNU General Public License as published by the Free Software
  14. * Foundation; either version 2 of the License, or (at your option) any later
  15. * version
  16. *
  17. * For a license to use the ser software under conditions other than those
  18. * described here, or to purchase support for this software, please contact
  19. * iptel.org by e-mail at the following addresses: [email protected]
  20. *
  21. * SER is distributed in the hope that it will be useful, but WITHOUT ANY
  22. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  23. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  24. * details.
  25. *
  26. * You should have received a copy of the GNU General Public License along
  27. * with this program; if not, write to the Free Software Foundation, Inc., 59
  28. * Temple Place, Suite 330, Boston, MA 02111-1307 USA
  29. */
  30. /** \addtogroup postgres
  31. * @{
  32. */
  33. /** \file
  34. * Postgres module interface.
  35. */
  36. #include "pg_mod.h"
  37. #include "pg_uri.h"
  38. #include "pg_con.h"
  39. #include "pg_cmd.h"
  40. #include "pg_res.h"
  41. #include "pg_fld.h"
  42. #include "km_db_postgres.h"
  43. #include "../../sr_module.h"
  44. #ifdef PG_TEST
  45. #include <limits.h>
  46. #include <float.h>
  47. #endif
  48. MODULE_VERSION
  49. static int pg_mod_init(void);
  50. static void pg_mod_destroy(void);
  51. int pg_connect_timeout = 0; /* Default is unlimited */
  52. int pg_retries = 2; /* How many times should the module try re-execute failed commands.
  53. * 0 disables reconnecting */
  54. int pg_lockset = 4;
  55. /*
  56. * Postgres module interface
  57. */
  58. static cmd_export_t cmds[] = {
  59. {"db_ctx", (cmd_function)NULL, 0, 0, 0},
  60. {"db_con", (cmd_function)pg_con, 0, 0, 0},
  61. {"db_uri", (cmd_function)pg_uri, 0, 0, 0},
  62. {"db_cmd", (cmd_function)pg_cmd, 0, 0, 0},
  63. {"db_put", (cmd_function)pg_cmd_exec, 0, 0, 0},
  64. {"db_del", (cmd_function)pg_cmd_exec, 0, 0, 0},
  65. {"db_get", (cmd_function)pg_cmd_exec, 0, 0, 0},
  66. {"db_upd", (cmd_function)pg_cmd_exec, 0, 0, 0},
  67. {"db_sql", (cmd_function)pg_cmd_exec, 0, 0, 0},
  68. {"db_res", (cmd_function)pg_res, 0, 0, 0},
  69. {"db_fld", (cmd_function)pg_fld, 0, 0, 0},
  70. {"db_first", (cmd_function)pg_cmd_first, 0, 0, 0},
  71. {"db_next", (cmd_function)pg_cmd_next, 0, 0, 0},
  72. {"db_setopt", (cmd_function)pg_setopt, 0, 0, 0},
  73. {"db_getopt", (cmd_function)pg_getopt, 0, 0, 0},
  74. {"db_bind_api", (cmd_function)db_postgres_bind_api, 0, 0, 0},
  75. {0, 0, 0, 0, 0}
  76. };
  77. /*
  78. * Exported parameters
  79. */
  80. static param_export_t params[] = {
  81. {"retries", PARAM_INT, &pg_retries },
  82. {"lockset", PARAM_INT, &pg_lockset },
  83. {0, 0, 0}
  84. };
  85. struct module_exports exports = {
  86. "db_postgres",
  87. cmds,
  88. 0, /* RPC method */
  89. params, /* module parameters */
  90. pg_mod_init, /* module initialization function */
  91. 0, /* response function*/
  92. pg_mod_destroy, /* destroy function */
  93. 0, /* oncancel function */
  94. 0 /* per-child init function */
  95. };
  96. /*
  97. CREATE TABLE test (
  98. col_bool BOOL,
  99. col_bytea BYTEA,
  100. col_char CHAR,
  101. col_int8 INT8,
  102. col_int4 INT4,
  103. col_int2 INT2,
  104. col_text TEXT,
  105. col_float4 FLOAT4,
  106. col_float8 FLOAT8,
  107. col_inet INET,
  108. col_bpchar BPCHAR,
  109. col_varchar VARCHAR,
  110. col_timestamp TIMESTAMP,
  111. col_timestamptz TIMESTAMPTZ,
  112. col_bit BIT(32),
  113. col_varbit VARBIT
  114. );
  115. */
  116. #ifdef PG_TEST
  117. int pg_test(void)
  118. {
  119. int i, row;
  120. db_ctx_t* db;
  121. db_cmd_t* put, *del, *get;
  122. db_res_t* result;
  123. db_rec_t* rec;
  124. char* times;
  125. db_fld_t int_vals[] = {
  126. {.name = "col_bool", .type = DB_INT},
  127. {.name = "col_int8", .type = DB_INT},
  128. {.name = "col_int4", .type = DB_INT},
  129. {.name = "col_inet", .type = DB_INT},
  130. {.name = "col_timestamp", .type = DB_INT},
  131. {.name = "col_timestamptz", .type = DB_INT},
  132. {.name = "col_bit", .type = DB_INT},
  133. {.name = "col_varbit", .type = DB_INT},
  134. {.name = NULL}
  135. };
  136. db_fld_t datetime_vals[] = {
  137. {.name = "col_int8", .type = DB_INT},
  138. {.name = "col_int4", .type = DB_INT},
  139. {.name = "col_timestamp", .type = DB_INT},
  140. {.name = "col_timestamptz", .type = DB_INT},
  141. {.name = NULL}
  142. };
  143. db_fld_t bitmap_vals[] = {
  144. {.name = "col_int8", .type = DB_INT},
  145. {.name = "col_int4", .type = DB_INT},
  146. {.name = "col_bit", .type = DB_INT},
  147. {.name = "col_varbit", .type = DB_INT},
  148. {.name = NULL}
  149. };
  150. db_fld_t float_vals[] = {
  151. {.name = "col_float4", .type = DB_FLOAT},
  152. {.name = "col_float8", .type = DB_FLOAT},
  153. {.name = NULL}
  154. };
  155. db_fld_t double_vals[] = {
  156. {.name = "col_float8", .type = DB_DOUBLE},
  157. {.name = NULL}
  158. };
  159. db_fld_t str_vals[] = {
  160. {.name = "col_varchar", .type = DB_STR},
  161. {.name = "col_bytea", .type = DB_STR},
  162. {.name = "col_text", .type = DB_STR},
  163. {.name = "col_bpchar", .type = DB_STR},
  164. {.name = "col_char", .type = DB_STR},
  165. {.name = NULL}
  166. };
  167. db_fld_t cstr_vals[] = {
  168. {.name = "col_varchar", .type = DB_CSTR},
  169. {.name = "col_bytea", .type = DB_CSTR},
  170. {.name = "col_text", .type = DB_CSTR},
  171. {.name = "col_bpchar", .type = DB_CSTR},
  172. {.name = "col_char", .type = DB_CSTR},
  173. {.name = NULL}
  174. };
  175. db_fld_t blob_vals[] = {
  176. {.name = "col_bytea", .type = DB_BLOB},
  177. {.name = NULL}
  178. };
  179. db_fld_t res[] = {
  180. {.name = "col_bool", .type = DB_INT},
  181. {.name = "col_bytea", .type = DB_BLOB},
  182. {.name = "col_char", .type = DB_STR},
  183. {.name = "col_int8", .type = DB_INT},
  184. {.name = "col_int4", .type = DB_INT},
  185. {.name = "col_int2", .type = DB_INT},
  186. {.name = "col_text", .type = DB_STR},
  187. {.name = "col_float4", .type = DB_FLOAT},
  188. {.name = "col_float8", .type = DB_DOUBLE},
  189. {.name = "col_inet", .type = DB_INT},
  190. {.name = "col_bpchar", .type = DB_STR},
  191. {.name = "col_varchar", .type = DB_STR},
  192. {.name = "col_timestamp", .type = DB_DATETIME},
  193. {.name = "col_timestamptz", .type = DB_DATETIME},
  194. {.name = "col_bit", .type = DB_BITMAP},
  195. {.name = "col_varbit", .type = DB_BITMAP},
  196. {.name = NULL}
  197. };
  198. db = db_ctx("postgres");
  199. if (db == NULL) {
  200. ERR("Error while initializing database layer\n");
  201. goto error;
  202. }
  203. if (db_add_db(db, "postgres://janakj:heslo@localhost/ser") < 0) goto error;
  204. if (db_connect(db) < 0) goto error;
  205. del = db_cmd(DB_DEL, db, "test", NULL, NULL, NULL);
  206. if (del == NULL) {
  207. ERR("Error while building delete * query\n");
  208. goto error;
  209. }
  210. put = db_cmd(DB_PUT, db, "test", NULL, NULL, int_vals);
  211. if (put == NULL) {
  212. ERR("Error while building test query\n");
  213. goto error;
  214. }
  215. if (db_exec(NULL, del)) {
  216. ERR("Error while deleting rows from test table\n");
  217. goto error;
  218. }
  219. put->vals[0].v.int4 = 0xffffffff;
  220. put->vals[1].v.int4 = 0xffffffff;
  221. put->vals[2].v.int4 = 0xffffffff;
  222. put->vals[3].v.int4 = 0xffffffff;
  223. put->vals[4].v.int4 = 0xffffffff;
  224. put->vals[5].v.int4 = 0xffffffff;
  225. put->vals[6].v.int4 = 0xffffffff;
  226. put->vals[7].v.int4 = 0xffffffff;
  227. if (db_exec(NULL, put)) {
  228. ERR("Error while executing database command\n");
  229. goto error;
  230. }
  231. put->vals[0].v.int4 = 0;
  232. put->vals[1].v.int4 = 0;
  233. put->vals[2].v.int4 = 0;
  234. put->vals[3].v.int4 = 0;
  235. put->vals[4].v.int4 = 0;
  236. put->vals[5].v.int4 = 0;
  237. put->vals[6].v.int4 = 0;
  238. put->vals[7].v.int4 = 0;
  239. if (db_exec(NULL, put)) {
  240. ERR("Error while executing database command\n");
  241. goto error;
  242. }
  243. db_cmd_free(put);
  244. put = db_cmd(DB_PUT, db, "test", NULL, NULL, bitmap_vals);
  245. if (put == NULL) {
  246. ERR("Error while building bitmap test query\n");
  247. goto error;
  248. }
  249. put->vals[0].v.int4 = 0xffffffff;
  250. put->vals[1].v.int4 = 0xffffffff;
  251. put->vals[2].v.int4 = 0xffffffff;
  252. put->vals[3].v.int4 = 0xffffffff;
  253. put->vals[4].v.int4 = 0xffffffff;
  254. if (db_exec(NULL, put)) {
  255. ERR("Error while executing database command\n");
  256. goto error;
  257. }
  258. put->vals[0].v.int4 = 0;
  259. put->vals[1].v.int4 = 0;
  260. put->vals[2].v.int4 = 0;
  261. put->vals[3].v.int4 = 0;
  262. put->vals[4].v.int4 = 0;
  263. if (db_exec(NULL, put)) {
  264. ERR("Error while executing database command\n");
  265. goto error;
  266. }
  267. db_cmd_free(put);
  268. put = db_cmd(DB_PUT, db, "test", NULL, NULL, float_vals);
  269. if (put == NULL) {
  270. ERR("Error while building float test query\n");
  271. goto error;
  272. }
  273. put->vals[0].v.flt = FLT_MAX;
  274. put->vals[1].v.flt = FLT_MAX;
  275. if (db_exec(NULL, put)) {
  276. ERR("Error while executing database command\n");
  277. goto error;
  278. }
  279. put->vals[0].v.flt = FLT_MIN;
  280. put->vals[1].v.flt = FLT_MIN;
  281. if (db_exec(NULL, put)) {
  282. ERR("Error while executing database command\n");
  283. goto error;
  284. }
  285. db_cmd_free(put);
  286. put = db_cmd(DB_PUT, db, "test", NULL, NULL, double_vals);
  287. if (put == NULL) {
  288. ERR("Error while building double test query\n");
  289. goto error;
  290. }
  291. put->vals[0].v.dbl = DBL_MAX;
  292. if (db_exec(NULL, put)) {
  293. ERR("Error while executing database command\n");
  294. goto error;
  295. }
  296. put->vals[0].v.dbl = DBL_MIN;
  297. if (db_exec(NULL, put)) {
  298. ERR("Error while executing database command\n");
  299. goto error;
  300. }
  301. db_cmd_free(put);
  302. put = db_cmd(DB_PUT, db, "test", NULL, NULL, str_vals);
  303. if (put == NULL) {
  304. ERR("Error while building str test query\n");
  305. goto error;
  306. }
  307. put->vals[0].v.lstr.s = "";
  308. put->vals[0].v.lstr.len = 0;
  309. put->vals[1].v.lstr.s = "";
  310. put->vals[1].v.lstr.len = 0;
  311. put->vals[2].v.lstr.s = "";
  312. put->vals[2].v.lstr.len = 0;
  313. put->vals[3].v.lstr.s = "";
  314. put->vals[3].v.lstr.len = 0;
  315. put->vals[4].v.lstr.s = "";
  316. put->vals[4].v.lstr.len = 0;
  317. if (db_exec(NULL, put)) {
  318. ERR("Error while executing database command\n");
  319. goto error;
  320. }
  321. put->vals[0].v.lstr.s = "abc should not be there";
  322. put->vals[0].v.lstr.len = 3;
  323. put->vals[1].v.lstr.s = "abc should not be there";
  324. put->vals[1].v.lstr.len = 3;
  325. put->vals[2].v.lstr.s = "abc should not be there";
  326. put->vals[2].v.lstr.len = 3;
  327. put->vals[3].v.lstr.s = "abc should not be there";
  328. put->vals[3].v.lstr.len = 3;
  329. put->vals[4].v.lstr.s = "a should not be there";
  330. put->vals[4].v.lstr.len = 1;
  331. if (db_exec(NULL, put)) {
  332. ERR("Error while executing database command\n");
  333. goto error;
  334. }
  335. db_cmd_free(put);
  336. put = db_cmd(DB_PUT, db, "test", NULL, NULL, cstr_vals);
  337. if (put == NULL) {
  338. ERR("Error while building cstr test query\n");
  339. goto error;
  340. }
  341. put->vals[0].v.cstr = "";
  342. put->vals[1].v.cstr = "";
  343. put->vals[2].v.cstr = "";
  344. put->vals[3].v.cstr = "";
  345. put->vals[4].v.cstr = "";
  346. if (db_exec(NULL, put)) {
  347. ERR("Error while executing database command\n");
  348. goto error;
  349. }
  350. put->vals[0].v.cstr = "def";
  351. put->vals[1].v.cstr = "def";
  352. put->vals[2].v.cstr = "def";
  353. put->vals[3].v.cstr = "def";
  354. put->vals[4].v.cstr = "d";
  355. if (db_exec(NULL, put)) {
  356. ERR("Error while executing database command\n");
  357. goto error;
  358. }
  359. db_cmd_free(put);
  360. put = db_cmd(DB_PUT, db, "test", NULL, NULL, blob_vals);
  361. if (put == NULL) {
  362. ERR("Error while building blob test query\n");
  363. goto error;
  364. }
  365. put->vals[0].v.blob.s = "\0\0\0\0";
  366. put->vals[0].v.blob.len = 4;
  367. if (db_exec(NULL, put)) {
  368. ERR("Error while executing database command\n");
  369. goto error;
  370. }
  371. db_cmd_free(put);
  372. put = db_cmd(DB_PUT, db, "test", NULL, NULL, datetime_vals);
  373. if (put == NULL) {
  374. ERR("Error while building datetime test query\n");
  375. goto error;
  376. }
  377. put->vals[0].v.time = 0xffffffff;
  378. put->vals[1].v.time = 0xffffffff;
  379. put->vals[2].v.time = 0xffffffff;
  380. put->vals[3].v.time = 0xffffffff;
  381. if (db_exec(NULL, put)) {
  382. ERR("Error while executing database command\n");
  383. goto error;
  384. }
  385. put->vals[0].v.time = 0;
  386. put->vals[1].v.time = 0;
  387. put->vals[2].v.time = 0;
  388. put->vals[3].v.time = 0;
  389. if (db_exec(NULL, put)) {
  390. ERR("Error while executing database command\n");
  391. goto error;
  392. }
  393. if (put) db_cmd_free(put);
  394. if (del) db_cmd_free(del);
  395. put = NULL;
  396. del = NULL;
  397. get = db_cmd(DB_GET, db, "test", res, NULL, NULL);
  398. if (get == NULL) {
  399. ERR("Error while building select query\n");
  400. goto error;
  401. }
  402. if (db_exec(&result, get)) {
  403. ERR("Error while executing select query\n");
  404. goto error;
  405. }
  406. rec = db_first(result);
  407. row = 1;
  408. while(rec) {
  409. ERR("row: %d\n", row);
  410. for(i = 0; !DB_FLD_LAST(rec->fld[i]); i++) {
  411. if (rec->fld[i].flags & DB_NULL) {
  412. ERR("%s: NULL\n", rec->fld[i].name);
  413. } else {
  414. switch(rec->fld[i].type) {
  415. case DB_INT:
  416. case DB_BITMAP:
  417. ERR("%s: %d\n", rec->fld[i].name, rec->fld[i].v.int4);
  418. break;
  419. case DB_DATETIME:
  420. times = ctime(&rec->fld[i].v.time);
  421. ERR("%s: %d:%.*s\n", rec->fld[i].name, rec->fld[i].v.time, strlen(times) - 1, times);
  422. break;
  423. case DB_DOUBLE:
  424. ERR("%s: %f\n", rec->fld[i].name, rec->fld[i].v.dbl);
  425. break;
  426. case DB_FLOAT:
  427. ERR("%s: %f\n", rec->fld[i].name, rec->fld[i].v.flt);
  428. break;
  429. case DB_STR:
  430. case DB_BLOB:
  431. ERR("%s: %.*s\n", rec->fld[i].name, rec->fld[i].v.lstr.len, rec->fld[i].v.lstr.s);
  432. break;
  433. case DB_CSTR:
  434. ERR("%s: %s\n", rec->fld[i].name, rec->fld[i].v.cstr);
  435. break;
  436. }
  437. }
  438. }
  439. ERR("\n");
  440. rec = db_next(result);
  441. row++;
  442. }
  443. db_res_free(result);
  444. db_cmd_free(get);
  445. db_disconnect(db);
  446. db_ctx_free(db);
  447. return 0;
  448. error:
  449. if (get) db_cmd_free(get);
  450. if (put) db_cmd_free(put);
  451. if (del) db_cmd_free(del);
  452. db_disconnect(db);
  453. db_ctx_free(db);
  454. return -1;
  455. }
  456. #endif /* PG_TEST */
  457. int mod_register(char *path, int *dlflags, void *p1, void *p2)
  458. {
  459. if(db_api_init()<0)
  460. return -1;
  461. return 0;
  462. }
  463. static int pg_mod_init(void)
  464. {
  465. #ifdef PG_TEST
  466. if (pg_test() == 0) {
  467. ERR("postgres: Testing successful\n");
  468. } else {
  469. ERR("postgres: Testing failed\n");
  470. }
  471. return -1;
  472. #endif /* PG_TEST */
  473. if(pg_init_lock_set(pg_lockset)<0)
  474. return -1;
  475. return km_postgres_mod_init();
  476. }
  477. static void pg_mod_destroy(void)
  478. {
  479. pg_destroy_lock_set();
  480. }
  481. /** @} */