pg_mod.c 13 KB

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