pg_mod.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 "../../sr_module.h"
  43. #ifdef PG_TEST
  44. #include <limits.h>
  45. #include <float.h>
  46. #endif
  47. static int pg_mod_init(void);
  48. MODULE_VERSION
  49. int pg_connect_timeout = 0; /* Default is unlimited */
  50. int pg_retries = 2; /* How many times should the module try re-execute failed commands.
  51. * 0 disables reconnecting */
  52. /*
  53. * Postgres module interface
  54. */
  55. static cmd_export_t cmds[] = {
  56. {"db_ctx", (cmd_function)NULL, 0, 0, 0},
  57. {"db_con", (cmd_function)pg_con, 0, 0, 0},
  58. {"db_uri", (cmd_function)pg_uri, 0, 0, 0},
  59. {"db_cmd", (cmd_function)pg_cmd, 0, 0, 0},
  60. {"db_put", (cmd_function)pg_cmd_exec, 0, 0, 0},
  61. {"db_del", (cmd_function)pg_cmd_exec, 0, 0, 0},
  62. {"db_get", (cmd_function)pg_cmd_exec, 0, 0, 0},
  63. {"db_upd", (cmd_function)pg_cmd_exec, 0, 0, 0},
  64. {"db_sql", (cmd_function)pg_cmd_exec, 0, 0, 0},
  65. {"db_res", (cmd_function)pg_res, 0, 0, 0},
  66. {"db_fld", (cmd_function)pg_fld, 0, 0, 0},
  67. {"db_first", (cmd_function)pg_cmd_first, 0, 0, 0},
  68. {"db_next", (cmd_function)pg_cmd_next, 0, 0, 0},
  69. {"db_setopt", (cmd_function)pg_setopt, 0, 0, 0},
  70. {"db_getopt", (cmd_function)pg_getopt, 0, 0, 0},
  71. {0, 0, 0, 0, 0}
  72. };
  73. /*
  74. * Exported parameters
  75. */
  76. static param_export_t params[] = {
  77. {"retries", PARAM_INT, &pg_retries },
  78. {0, 0, 0}
  79. };
  80. struct module_exports exports = {
  81. "postgres",
  82. cmds,
  83. 0, /* RPC method */
  84. params, /* module parameters */
  85. pg_mod_init, /* module initialization function */
  86. 0, /* response function*/
  87. 0, /* destroy function */
  88. 0, /* oncancel function */
  89. 0 /* per-child init function */
  90. };
  91. /*
  92. CREATE TABLE test (
  93. col_bool BOOL,
  94. col_bytea BYTEA,
  95. col_char CHAR,
  96. col_int8 INT8,
  97. col_int4 INT4,
  98. col_int2 INT2,
  99. col_text TEXT,
  100. col_float4 FLOAT4,
  101. col_float8 FLOAT8,
  102. col_inet INET,
  103. col_bpchar BPCHAR,
  104. col_varchar VARCHAR,
  105. col_timestamp TIMESTAMP,
  106. col_timestamptz TIMESTAMPTZ,
  107. col_bit BIT(32),
  108. col_varbit VARBIT
  109. );
  110. */
  111. #ifdef PG_TEST
  112. int pg_test(void)
  113. {
  114. int i, row;
  115. db_ctx_t* db;
  116. db_cmd_t* put, *del, *get;
  117. db_res_t* result;
  118. db_rec_t* rec;
  119. char* times;
  120. db_fld_t int_vals[] = {
  121. {.name = "col_bool", .type = DB_INT},
  122. {.name = "col_int8", .type = DB_INT},
  123. {.name = "col_int4", .type = DB_INT},
  124. {.name = "col_inet", .type = DB_INT},
  125. {.name = "col_timestamp", .type = DB_INT},
  126. {.name = "col_timestamptz", .type = DB_INT},
  127. {.name = "col_bit", .type = DB_INT},
  128. {.name = "col_varbit", .type = DB_INT},
  129. {.name = NULL}
  130. };
  131. db_fld_t datetime_vals[] = {
  132. {.name = "col_int8", .type = DB_INT},
  133. {.name = "col_int4", .type = DB_INT},
  134. {.name = "col_timestamp", .type = DB_INT},
  135. {.name = "col_timestamptz", .type = DB_INT},
  136. {.name = NULL}
  137. };
  138. db_fld_t bitmap_vals[] = {
  139. {.name = "col_int8", .type = DB_INT},
  140. {.name = "col_int4", .type = DB_INT},
  141. {.name = "col_bit", .type = DB_INT},
  142. {.name = "col_varbit", .type = DB_INT},
  143. {.name = NULL}
  144. };
  145. db_fld_t float_vals[] = {
  146. {.name = "col_float4", .type = DB_FLOAT},
  147. {.name = "col_float8", .type = DB_FLOAT},
  148. {.name = NULL}
  149. };
  150. db_fld_t double_vals[] = {
  151. {.name = "col_float8", .type = DB_DOUBLE},
  152. {.name = NULL}
  153. };
  154. db_fld_t str_vals[] = {
  155. {.name = "col_varchar", .type = DB_STR},
  156. {.name = "col_bytea", .type = DB_STR},
  157. {.name = "col_text", .type = DB_STR},
  158. {.name = "col_bpchar", .type = DB_STR},
  159. {.name = "col_char", .type = DB_STR},
  160. {.name = NULL}
  161. };
  162. db_fld_t cstr_vals[] = {
  163. {.name = "col_varchar", .type = DB_CSTR},
  164. {.name = "col_bytea", .type = DB_CSTR},
  165. {.name = "col_text", .type = DB_CSTR},
  166. {.name = "col_bpchar", .type = DB_CSTR},
  167. {.name = "col_char", .type = DB_CSTR},
  168. {.name = NULL}
  169. };
  170. db_fld_t blob_vals[] = {
  171. {.name = "col_bytea", .type = DB_BLOB},
  172. {.name = NULL}
  173. };
  174. db_fld_t res[] = {
  175. {.name = "col_bool", .type = DB_INT},
  176. {.name = "col_bytea", .type = DB_BLOB},
  177. {.name = "col_char", .type = DB_STR},
  178. {.name = "col_int8", .type = DB_INT},
  179. {.name = "col_int4", .type = DB_INT},
  180. {.name = "col_int2", .type = DB_INT},
  181. {.name = "col_text", .type = DB_STR},
  182. {.name = "col_float4", .type = DB_FLOAT},
  183. {.name = "col_float8", .type = DB_DOUBLE},
  184. {.name = "col_inet", .type = DB_INT},
  185. {.name = "col_bpchar", .type = DB_STR},
  186. {.name = "col_varchar", .type = DB_STR},
  187. {.name = "col_timestamp", .type = DB_DATETIME},
  188. {.name = "col_timestamptz", .type = DB_DATETIME},
  189. {.name = "col_bit", .type = DB_BITMAP},
  190. {.name = "col_varbit", .type = DB_BITMAP},
  191. {.name = NULL}
  192. };
  193. db = db_ctx("postgres");
  194. if (db == NULL) {
  195. ERR("Error while initializing database layer\n");
  196. goto error;
  197. }
  198. if (db_add_db(db, "postgres://janakj:heslo@localhost/ser") < 0) goto error;
  199. if (db_connect(db) < 0) goto error;
  200. del = db_cmd(DB_DEL, db, "test", NULL, NULL, NULL);
  201. if (del == NULL) {
  202. ERR("Error while building delete * query\n");
  203. goto error;
  204. }
  205. put = db_cmd(DB_PUT, db, "test", NULL, NULL, int_vals);
  206. if (put == NULL) {
  207. ERR("Error while building test query\n");
  208. goto error;
  209. }
  210. if (db_exec(NULL, del)) {
  211. ERR("Error while deleting rows from test table\n");
  212. goto error;
  213. }
  214. put->vals[0].v.int4 = 0xffffffff;
  215. put->vals[1].v.int4 = 0xffffffff;
  216. put->vals[2].v.int4 = 0xffffffff;
  217. put->vals[3].v.int4 = 0xffffffff;
  218. put->vals[4].v.int4 = 0xffffffff;
  219. put->vals[5].v.int4 = 0xffffffff;
  220. put->vals[6].v.int4 = 0xffffffff;
  221. put->vals[7].v.int4 = 0xffffffff;
  222. if (db_exec(NULL, put)) {
  223. ERR("Error while executing database command\n");
  224. goto error;
  225. }
  226. put->vals[0].v.int4 = 0;
  227. put->vals[1].v.int4 = 0;
  228. put->vals[2].v.int4 = 0;
  229. put->vals[3].v.int4 = 0;
  230. put->vals[4].v.int4 = 0;
  231. put->vals[5].v.int4 = 0;
  232. put->vals[6].v.int4 = 0;
  233. put->vals[7].v.int4 = 0;
  234. if (db_exec(NULL, put)) {
  235. ERR("Error while executing database command\n");
  236. goto error;
  237. }
  238. db_cmd_free(put);
  239. put = db_cmd(DB_PUT, db, "test", NULL, NULL, bitmap_vals);
  240. if (put == NULL) {
  241. ERR("Error while building bitmap test query\n");
  242. goto error;
  243. }
  244. put->vals[0].v.int4 = 0xffffffff;
  245. put->vals[1].v.int4 = 0xffffffff;
  246. put->vals[2].v.int4 = 0xffffffff;
  247. put->vals[3].v.int4 = 0xffffffff;
  248. put->vals[4].v.int4 = 0xffffffff;
  249. if (db_exec(NULL, put)) {
  250. ERR("Error while executing database command\n");
  251. goto error;
  252. }
  253. put->vals[0].v.int4 = 0;
  254. put->vals[1].v.int4 = 0;
  255. put->vals[2].v.int4 = 0;
  256. put->vals[3].v.int4 = 0;
  257. put->vals[4].v.int4 = 0;
  258. if (db_exec(NULL, put)) {
  259. ERR("Error while executing database command\n");
  260. goto error;
  261. }
  262. db_cmd_free(put);
  263. put = db_cmd(DB_PUT, db, "test", NULL, NULL, float_vals);
  264. if (put == NULL) {
  265. ERR("Error while building float test query\n");
  266. goto error;
  267. }
  268. put->vals[0].v.flt = FLT_MAX;
  269. put->vals[1].v.flt = FLT_MAX;
  270. if (db_exec(NULL, put)) {
  271. ERR("Error while executing database command\n");
  272. goto error;
  273. }
  274. put->vals[0].v.flt = FLT_MIN;
  275. put->vals[1].v.flt = FLT_MIN;
  276. if (db_exec(NULL, put)) {
  277. ERR("Error while executing database command\n");
  278. goto error;
  279. }
  280. db_cmd_free(put);
  281. put = db_cmd(DB_PUT, db, "test", NULL, NULL, double_vals);
  282. if (put == NULL) {
  283. ERR("Error while building double test query\n");
  284. goto error;
  285. }
  286. put->vals[0].v.dbl = DBL_MAX;
  287. if (db_exec(NULL, put)) {
  288. ERR("Error while executing database command\n");
  289. goto error;
  290. }
  291. put->vals[0].v.dbl = DBL_MIN;
  292. if (db_exec(NULL, put)) {
  293. ERR("Error while executing database command\n");
  294. goto error;
  295. }
  296. db_cmd_free(put);
  297. put = db_cmd(DB_PUT, db, "test", NULL, NULL, str_vals);
  298. if (put == NULL) {
  299. ERR("Error while building str test query\n");
  300. goto error;
  301. }
  302. put->vals[0].v.lstr.s = "";
  303. put->vals[0].v.lstr.len = 0;
  304. put->vals[1].v.lstr.s = "";
  305. put->vals[1].v.lstr.len = 0;
  306. put->vals[2].v.lstr.s = "";
  307. put->vals[2].v.lstr.len = 0;
  308. put->vals[3].v.lstr.s = "";
  309. put->vals[3].v.lstr.len = 0;
  310. put->vals[4].v.lstr.s = "";
  311. put->vals[4].v.lstr.len = 0;
  312. if (db_exec(NULL, put)) {
  313. ERR("Error while executing database command\n");
  314. goto error;
  315. }
  316. put->vals[0].v.lstr.s = "abc should not be there";
  317. put->vals[0].v.lstr.len = 3;
  318. put->vals[1].v.lstr.s = "abc should not be there";
  319. put->vals[1].v.lstr.len = 3;
  320. put->vals[2].v.lstr.s = "abc should not be there";
  321. put->vals[2].v.lstr.len = 3;
  322. put->vals[3].v.lstr.s = "abc should not be there";
  323. put->vals[3].v.lstr.len = 3;
  324. put->vals[4].v.lstr.s = "a should not be there";
  325. put->vals[4].v.lstr.len = 1;
  326. if (db_exec(NULL, put)) {
  327. ERR("Error while executing database command\n");
  328. goto error;
  329. }
  330. db_cmd_free(put);
  331. put = db_cmd(DB_PUT, db, "test", NULL, NULL, cstr_vals);
  332. if (put == NULL) {
  333. ERR("Error while building cstr test query\n");
  334. goto error;
  335. }
  336. put->vals[0].v.cstr = "";
  337. put->vals[1].v.cstr = "";
  338. put->vals[2].v.cstr = "";
  339. put->vals[3].v.cstr = "";
  340. put->vals[4].v.cstr = "";
  341. if (db_exec(NULL, put)) {
  342. ERR("Error while executing database command\n");
  343. goto error;
  344. }
  345. put->vals[0].v.cstr = "def";
  346. put->vals[1].v.cstr = "def";
  347. put->vals[2].v.cstr = "def";
  348. put->vals[3].v.cstr = "def";
  349. put->vals[4].v.cstr = "d";
  350. if (db_exec(NULL, put)) {
  351. ERR("Error while executing database command\n");
  352. goto error;
  353. }
  354. db_cmd_free(put);
  355. put = db_cmd(DB_PUT, db, "test", NULL, NULL, blob_vals);
  356. if (put == NULL) {
  357. ERR("Error while building blob test query\n");
  358. goto error;
  359. }
  360. put->vals[0].v.blob.s = "\0\0\0\0";
  361. put->vals[0].v.blob.len = 4;
  362. if (db_exec(NULL, put)) {
  363. ERR("Error while executing database command\n");
  364. goto error;
  365. }
  366. db_cmd_free(put);
  367. put = db_cmd(DB_PUT, db, "test", NULL, NULL, datetime_vals);
  368. if (put == NULL) {
  369. ERR("Error while building datetime test query\n");
  370. goto error;
  371. }
  372. put->vals[0].v.time = 0xffffffff;
  373. put->vals[1].v.time = 0xffffffff;
  374. put->vals[2].v.time = 0xffffffff;
  375. put->vals[3].v.time = 0xffffffff;
  376. if (db_exec(NULL, put)) {
  377. ERR("Error while executing database command\n");
  378. goto error;
  379. }
  380. put->vals[0].v.time = 0;
  381. put->vals[1].v.time = 0;
  382. put->vals[2].v.time = 0;
  383. put->vals[3].v.time = 0;
  384. if (db_exec(NULL, put)) {
  385. ERR("Error while executing database command\n");
  386. goto error;
  387. }
  388. if (put) db_cmd_free(put);
  389. if (del) db_cmd_free(del);
  390. put = NULL;
  391. del = NULL;
  392. get = db_cmd(DB_GET, db, "test", res, NULL, NULL);
  393. if (get == NULL) {
  394. ERR("Error while building select query\n");
  395. goto error;
  396. }
  397. if (db_exec(&result, get)) {
  398. ERR("Error while executing select query\n");
  399. goto error;
  400. }
  401. rec = db_first(result);
  402. row = 1;
  403. while(rec) {
  404. ERR("row: %d\n", row);
  405. for(i = 0; !DB_FLD_LAST(rec->fld[i]); i++) {
  406. if (rec->fld[i].flags & DB_NULL) {
  407. ERR("%s: NULL\n", rec->fld[i].name);
  408. } else {
  409. switch(rec->fld[i].type) {
  410. case DB_INT:
  411. case DB_BITMAP:
  412. ERR("%s: %d\n", rec->fld[i].name, rec->fld[i].v.int4);
  413. break;
  414. case DB_DATETIME:
  415. times = ctime(&rec->fld[i].v.time);
  416. ERR("%s: %d:%.*s\n", rec->fld[i].name, rec->fld[i].v.time, strlen(times) - 1, times);
  417. break;
  418. case DB_DOUBLE:
  419. ERR("%s: %f\n", rec->fld[i].name, rec->fld[i].v.dbl);
  420. break;
  421. case DB_FLOAT:
  422. ERR("%s: %f\n", rec->fld[i].name, rec->fld[i].v.flt);
  423. break;
  424. case DB_STR:
  425. case DB_BLOB:
  426. ERR("%s: %.*s\n", rec->fld[i].name, rec->fld[i].v.lstr.len, rec->fld[i].v.lstr.s);
  427. break;
  428. case DB_CSTR:
  429. ERR("%s: %s\n", rec->fld[i].name, rec->fld[i].v.cstr);
  430. break;
  431. }
  432. }
  433. }
  434. ERR("\n");
  435. rec = db_next(result);
  436. row++;
  437. }
  438. db_res_free(result);
  439. db_cmd_free(get);
  440. db_disconnect(db);
  441. db_ctx_free(db);
  442. return 0;
  443. error:
  444. if (get) db_cmd_free(get);
  445. if (put) db_cmd_free(put);
  446. if (del) db_cmd_free(del);
  447. db_disconnect(db);
  448. db_ctx_free(db);
  449. return -1;
  450. }
  451. #endif /* PG_TEST */
  452. static int pg_mod_init(void)
  453. {
  454. #ifdef PG_TEST
  455. if (pg_test() == 0) {
  456. ERR("postgres: Testing successful\n");
  457. } else {
  458. ERR("postgres: Testing failed\n");
  459. }
  460. return -1;
  461. #endif /* PG_TEST */
  462. return 0;
  463. }
  464. /** @} */