pg_mod.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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_bit", .type = DB_INT},
  127. {.name = "col_varbit", .type = DB_INT},
  128. {.name = NULL}
  129. };
  130. db_fld_t datetime_vals[] = {
  131. {.name = "col_int8", .type = DB_INT},
  132. {.name = "col_int4", .type = DB_INT},
  133. {.name = "col_timestamp", .type = DB_INT},
  134. {.name = NULL}
  135. };
  136. db_fld_t bitmap_vals[] = {
  137. {.name = "col_int8", .type = DB_INT},
  138. {.name = "col_int4", .type = DB_INT},
  139. {.name = "col_bit", .type = DB_INT},
  140. {.name = "col_varbit", .type = DB_INT},
  141. {.name = NULL}
  142. };
  143. db_fld_t float_vals[] = {
  144. {.name = "col_float4", .type = DB_FLOAT},
  145. {.name = "col_float8", .type = DB_FLOAT},
  146. {.name = NULL}
  147. };
  148. db_fld_t double_vals[] = {
  149. {.name = "col_float8", .type = DB_DOUBLE},
  150. {.name = NULL}
  151. };
  152. db_fld_t str_vals[] = {
  153. {.name = "col_varchar", .type = DB_STR},
  154. {.name = "col_bytea", .type = DB_STR},
  155. {.name = "col_text", .type = DB_STR},
  156. {.name = "col_bpchar", .type = DB_STR},
  157. {.name = "col_char", .type = DB_STR},
  158. {.name = NULL}
  159. };
  160. db_fld_t cstr_vals[] = {
  161. {.name = "col_varchar", .type = DB_CSTR},
  162. {.name = "col_bytea", .type = DB_CSTR},
  163. {.name = "col_text", .type = DB_CSTR},
  164. {.name = "col_bpchar", .type = DB_CSTR},
  165. {.name = "col_char", .type = DB_CSTR},
  166. {.name = NULL}
  167. };
  168. db_fld_t blob_vals[] = {
  169. {.name = "col_bytea", .type = DB_BLOB},
  170. {.name = NULL}
  171. };
  172. db_fld_t res[] = {
  173. {.name = "col_bool", .type = DB_INT},
  174. {.name = "col_bytea", .type = DB_BLOB},
  175. {.name = "col_char", .type = DB_STR},
  176. {.name = "col_int8", .type = DB_INT},
  177. {.name = "col_int4", .type = DB_INT},
  178. {.name = "col_int2", .type = DB_INT},
  179. {.name = "col_text", .type = DB_STR},
  180. {.name = "col_float4", .type = DB_FLOAT},
  181. {.name = "col_float8", .type = DB_DOUBLE},
  182. {.name = "col_inet", .type = DB_INT},
  183. {.name = "col_bpchar", .type = DB_STR},
  184. {.name = "col_varchar", .type = DB_STR},
  185. {.name = "col_timestamp", .type = DB_DATETIME},
  186. {.name = "col_bit", .type = DB_BITMAP},
  187. {.name = "col_varbit", .type = DB_BITMAP},
  188. {.name = NULL}
  189. };
  190. db = db_ctx("postgres");
  191. if (db == NULL) {
  192. ERR("Error while initializing database layer\n");
  193. goto error;
  194. }
  195. if (db_add_db(db, "postgres://janakj:honzacvut@localhost/ser") < 0) goto error;
  196. if (db_connect(db) < 0) goto error;
  197. del = db_cmd(DB_DEL, db, "test", NULL, NULL, NULL);
  198. if (del == NULL) {
  199. ERR("Error while building delete * query\n");
  200. goto error;
  201. }
  202. put = db_cmd(DB_PUT, db, "test", NULL, NULL, int_vals);
  203. if (put == NULL) {
  204. ERR("Error while building test query\n");
  205. goto error;
  206. }
  207. if (db_exec(NULL, del)) {
  208. ERR("Error while deleting rows from test table\n");
  209. goto error;
  210. }
  211. put->vals[0].v.int4 = 0xffffffff;
  212. put->vals[1].v.int4 = 0xffffffff;
  213. put->vals[2].v.int4 = 0xffffffff;
  214. put->vals[3].v.int4 = 0xffffffff;
  215. put->vals[4].v.int4 = 0xffffffff;
  216. put->vals[5].v.int4 = 0xffffffff;
  217. put->vals[6].v.int4 = 0xffffffff;
  218. if (db_exec(NULL, put)) {
  219. ERR("Error while executing database command\n");
  220. goto error;
  221. }
  222. put->vals[0].v.int4 = 0;
  223. put->vals[1].v.int4 = 0;
  224. put->vals[2].v.int4 = 0;
  225. put->vals[3].v.int4 = 0;
  226. put->vals[4].v.int4 = 0;
  227. put->vals[5].v.int4 = 0;
  228. put->vals[6].v.int4 = 0;
  229. if (db_exec(NULL, put)) {
  230. ERR("Error while executing database command\n");
  231. goto error;
  232. }
  233. db_cmd_free(put);
  234. put = db_cmd(DB_PUT, db, "test", NULL, NULL, bitmap_vals);
  235. if (put == NULL) {
  236. ERR("Error while building bitmap test query\n");
  237. goto error;
  238. }
  239. put->vals[0].v.int4 = 0xffffffff;
  240. put->vals[1].v.int4 = 0xffffffff;
  241. put->vals[2].v.int4 = 0xffffffff;
  242. put->vals[3].v.int4 = 0xffffffff;
  243. if (db_exec(NULL, put)) {
  244. ERR("Error while executing database command\n");
  245. goto error;
  246. }
  247. put->vals[0].v.int4 = 0;
  248. put->vals[1].v.int4 = 0;
  249. put->vals[2].v.int4 = 0;
  250. put->vals[3].v.int4 = 0;
  251. if (db_exec(NULL, put)) {
  252. ERR("Error while executing database command\n");
  253. goto error;
  254. }
  255. db_cmd_free(put);
  256. put = db_cmd(DB_PUT, db, "test", NULL, NULL, float_vals);
  257. if (put == NULL) {
  258. ERR("Error while building float test query\n");
  259. goto error;
  260. }
  261. put->vals[0].v.flt = FLT_MAX;
  262. put->vals[1].v.flt = FLT_MAX;
  263. if (db_exec(NULL, put)) {
  264. ERR("Error while executing database command\n");
  265. goto error;
  266. }
  267. put->vals[0].v.flt = FLT_MIN;
  268. put->vals[1].v.flt = FLT_MIN;
  269. if (db_exec(NULL, put)) {
  270. ERR("Error while executing database command\n");
  271. goto error;
  272. }
  273. db_cmd_free(put);
  274. put = db_cmd(DB_PUT, db, "test", NULL, NULL, double_vals);
  275. if (put == NULL) {
  276. ERR("Error while building double test query\n");
  277. goto error;
  278. }
  279. put->vals[0].v.dbl = DBL_MAX;
  280. if (db_exec(NULL, put)) {
  281. ERR("Error while executing database command\n");
  282. goto error;
  283. }
  284. put->vals[0].v.dbl = DBL_MIN;
  285. if (db_exec(NULL, put)) {
  286. ERR("Error while executing database command\n");
  287. goto error;
  288. }
  289. db_cmd_free(put);
  290. put = db_cmd(DB_PUT, db, "test", NULL, NULL, str_vals);
  291. if (put == NULL) {
  292. ERR("Error while building str test query\n");
  293. goto error;
  294. }
  295. put->vals[0].v.lstr.s = "";
  296. put->vals[0].v.lstr.len = 0;
  297. put->vals[1].v.lstr.s = "";
  298. put->vals[1].v.lstr.len = 0;
  299. put->vals[2].v.lstr.s = "";
  300. put->vals[2].v.lstr.len = 0;
  301. put->vals[3].v.lstr.s = "";
  302. put->vals[3].v.lstr.len = 0;
  303. put->vals[4].v.lstr.s = "";
  304. put->vals[4].v.lstr.len = 0;
  305. if (db_exec(NULL, put)) {
  306. ERR("Error while executing database command\n");
  307. goto error;
  308. }
  309. put->vals[0].v.lstr.s = "abc should not be there";
  310. put->vals[0].v.lstr.len = 3;
  311. put->vals[1].v.lstr.s = "abc should not be there";
  312. put->vals[1].v.lstr.len = 3;
  313. put->vals[2].v.lstr.s = "abc should not be there";
  314. put->vals[2].v.lstr.len = 3;
  315. put->vals[3].v.lstr.s = "abc should not be there";
  316. put->vals[3].v.lstr.len = 3;
  317. put->vals[4].v.lstr.s = "a should not be there";
  318. put->vals[4].v.lstr.len = 1;
  319. if (db_exec(NULL, put)) {
  320. ERR("Error while executing database command\n");
  321. goto error;
  322. }
  323. db_cmd_free(put);
  324. put = db_cmd(DB_PUT, db, "test", NULL, NULL, cstr_vals);
  325. if (put == NULL) {
  326. ERR("Error while building cstr test query\n");
  327. goto error;
  328. }
  329. put->vals[0].v.cstr = "";
  330. put->vals[1].v.cstr = "";
  331. put->vals[2].v.cstr = "";
  332. put->vals[3].v.cstr = "";
  333. put->vals[4].v.cstr = "";
  334. if (db_exec(NULL, put)) {
  335. ERR("Error while executing database command\n");
  336. goto error;
  337. }
  338. put->vals[0].v.cstr = "def";
  339. put->vals[1].v.cstr = "def";
  340. put->vals[2].v.cstr = "def";
  341. put->vals[3].v.cstr = "def";
  342. put->vals[4].v.cstr = "d";
  343. if (db_exec(NULL, put)) {
  344. ERR("Error while executing database command\n");
  345. goto error;
  346. }
  347. db_cmd_free(put);
  348. put = db_cmd(DB_PUT, db, "test", NULL, NULL, blob_vals);
  349. if (put == NULL) {
  350. ERR("Error while building blob test query\n");
  351. goto error;
  352. }
  353. put->vals[0].v.blob.s = "\0\0\0\0";
  354. put->vals[0].v.blob.len = 4;
  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, datetime_vals);
  361. if (put == NULL) {
  362. ERR("Error while building datetime test query\n");
  363. goto error;
  364. }
  365. put->vals[0].v.time = 0xffffffff;
  366. put->vals[1].v.time = 0xffffffff;
  367. put->vals[2].v.time = 0xffffffff;
  368. if (db_exec(NULL, put)) {
  369. ERR("Error while executing database command\n");
  370. goto error;
  371. }
  372. put->vals[0].v.time = 0;
  373. put->vals[1].v.time = 0;
  374. put->vals[2].v.time = 0;
  375. if (db_exec(NULL, put)) {
  376. ERR("Error while executing database command\n");
  377. goto error;
  378. }
  379. if (put) db_cmd_free(put);
  380. if (del) db_cmd_free(del);
  381. put = NULL;
  382. del = NULL;
  383. get = db_cmd(DB_GET, db, "test", res, NULL, NULL);
  384. if (get == NULL) {
  385. ERR("Error while building select query\n");
  386. goto error;
  387. }
  388. if (db_exec(&result, get)) {
  389. ERR("Error while executing select query\n");
  390. goto error;
  391. }
  392. rec = db_first(result);
  393. row = 1;
  394. while(rec) {
  395. ERR("row: %d\n", row);
  396. for(i = 0; !DB_FLD_LAST(rec->fld[i]); i++) {
  397. if (rec->fld[i].flags & DB_NULL) {
  398. ERR("%s: NULL\n", rec->fld[i].name);
  399. } else {
  400. switch(rec->fld[i].type) {
  401. case DB_INT:
  402. case DB_BITMAP:
  403. ERR("%s: %d\n", rec->fld[i].name, rec->fld[i].v.int4);
  404. break;
  405. case DB_DATETIME:
  406. times = ctime(&rec->fld[i].v.time);
  407. ERR("%s: %d:%.*s\n", rec->fld[i].name, rec->fld[i].v.time, strlen(times) - 1, times);
  408. break;
  409. case DB_DOUBLE:
  410. ERR("%s: %f\n", rec->fld[i].name, rec->fld[i].v.dbl);
  411. break;
  412. case DB_FLOAT:
  413. ERR("%s: %f\n", rec->fld[i].name, rec->fld[i].v.flt);
  414. break;
  415. case DB_STR:
  416. case DB_BLOB:
  417. ERR("%s: %.*s\n", rec->fld[i].name, rec->fld[i].v.lstr.len, rec->fld[i].v.lstr.s);
  418. break;
  419. case DB_CSTR:
  420. ERR("%s: %s\n", rec->fld[i].name, rec->fld[i].v.cstr);
  421. break;
  422. }
  423. }
  424. }
  425. ERR("\n");
  426. rec = db_next(result);
  427. row++;
  428. }
  429. db_res_free(result);
  430. db_cmd_free(get);
  431. db_disconnect(db);
  432. db_ctx_free(db);
  433. return 0;
  434. error:
  435. if (get) db_cmd_free(get);
  436. if (put) db_cmd_free(put);
  437. if (del) db_cmd_free(del);
  438. db_disconnect(db);
  439. db_ctx_free(db);
  440. return -1;
  441. }
  442. #endif /* PG_TEST */
  443. static int pg_mod_init(void)
  444. {
  445. #ifdef PG_TEST
  446. if (pg_test() == 0) {
  447. ERR("postgres: Testing successful\n");
  448. } else {
  449. ERR("postgres: Testing failed\n");
  450. }
  451. return -1;
  452. #endif /* PG_TEST */
  453. return 0;
  454. }
  455. /** @} */