km_dbase.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2003 August.Net Services, LLC
  5. * Copyright (C) 2006 Norman Brandinger
  6. * Copyright (C) 2008 1&1 Internet AG
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History
  25. * -------
  26. * 2003-04-06 initial code written (Greg Fausak/Andy Fullford)
  27. * 2006-07-28 within pg_get_result(): added check to immediatly return of no
  28. * result set was returned added check to only execute
  29. * convert_result() if PGRES_TUPLES_OK added safety check to avoid
  30. * double pg_free_result() (norm)
  31. * 2006-08-07 Rewrote pg_get_result().
  32. * Additional debugging lines have been placed through out the code.
  33. * Added Asynchronous Command Processing (PQsendQuery/PQgetResult)
  34. * instead of PQexec. this was done in preparation of adding FETCH
  35. * support. Note that PQexec returns a result pointer while
  36. * PQsendQuery does not. The result set pointer is obtained from
  37. * a call (or multiple calls) to PQgetResult.
  38. * Removed transaction processing calls (BEGIN/COMMIT/ROLLBACK) as
  39. * they added uneeded overhead. Klaus' testing showed in excess of
  40. * 1ms gain by removing each command. In addition, Kamailio only
  41. * issues single queries and is not, at this time transaction aware.
  42. * The transaction processing routines have been left in place
  43. * should this support be needed in the future.
  44. * Updated logic in pg_query / pg_raw_query to accept a (0) result
  45. * set (_r) parameter. In this case, control is returned
  46. * immediately after submitting the query and no call to
  47. * pg_get_results() is performed. This is a requirement for
  48. * FETCH support. (norm)
  49. * 2006-10-27 Added fetch support (norm)
  50. * Removed dependency on aug_* memory routines (norm)
  51. * Added connection pooling support (norm)
  52. * Standardized API routines to pg_* names (norm)
  53. * 2006-11-01 Updated pg_insert(), pg_delete(), pg_update() and
  54. * pg_get_result() to handle failed queries. Detailed warnings
  55. * along with the text of the failed query is now displayed in the
  56. * log. Callers of these routines can now assume that a non-zero
  57. * rc indicates the query failed and that remedial action may need
  58. * to be taken. (norm)
  59. */
  60. /*! \file
  61. * \brief DB_POSTGRES :: Core
  62. * \ingroup db_postgres
  63. * Module: \ref db_postgres
  64. */
  65. /*! maximum number of columns */
  66. #define MAXCOLUMNS 512
  67. #include <string.h>
  68. #include <stdio.h>
  69. #include <stdlib.h>
  70. #include "../../dprint.h"
  71. #include "../../mem/mem.h"
  72. #include "../../lib/srdb1/db.h"
  73. #include "../../lib/srdb1/db_ut.h"
  74. #include "../../lib/srdb1/db_query.h"
  75. #include "../../locking.h"
  76. #include "../../hashes.h"
  77. #include "km_dbase.h"
  78. #include "km_pg_con.h"
  79. #include "km_val.h"
  80. #include "km_res.h"
  81. #include "pg_mod.h"
  82. static gen_lock_set_t *_pg_lock_set = NULL;
  83. static unsigned int _pg_lock_size = 0;
  84. /*!
  85. * \brief init lock set used to implement SQL REPLACE via UPDATE/INSERT
  86. * \param sz power of two to compute the lock set size
  87. * \return 0 on success, -1 on error
  88. */
  89. int pg_init_lock_set(int sz)
  90. {
  91. if(sz>0 && sz<=10)
  92. {
  93. _pg_lock_size = 1<<sz;
  94. } else {
  95. _pg_lock_size = 1<<4;
  96. }
  97. _pg_lock_set = lock_set_alloc(_pg_lock_size);
  98. if(_pg_lock_set==NULL || lock_set_init(_pg_lock_set)==NULL)
  99. {
  100. LM_ERR("cannot initiate lock set\n");
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. void pg_destroy_lock_set(void)
  106. {
  107. if(_pg_lock_set!=NULL)
  108. {
  109. lock_set_destroy(_pg_lock_set);
  110. lock_set_dealloc(_pg_lock_set);
  111. _pg_lock_set = NULL;
  112. _pg_lock_size = 0;
  113. }
  114. }
  115. static void db_postgres_free_query(const db1_con_t* _con);
  116. /*!
  117. * \brief Initialize database for future queries
  118. * \param _url URL of the database that should be opened
  119. * \return database connection on success, NULL on error
  120. * \note this function must be called prior to any database functions
  121. */
  122. db1_con_t *db_postgres_init(const str* _url)
  123. {
  124. return db_do_init(_url, (void*) db_postgres_new_connection);
  125. }
  126. /*!
  127. * \brief Initialize database for future queries - no pooling
  128. * \param _url URL of the database that should be opened
  129. * \return database connection on success, NULL on error
  130. * \note this function must be called prior to any database functions
  131. */
  132. db1_con_t *db_postgres_init2(const str* _url, db_pooling_t pooling)
  133. {
  134. return db_do_init2(_url, (void*) db_postgres_new_connection, pooling);
  135. }
  136. /*!
  137. * \brief Close database when the database is no longer needed
  138. * \param _h closed connection, as returned from db_postgres_init
  139. * \note free all memory and resources
  140. */
  141. void db_postgres_close(db1_con_t* _h)
  142. {
  143. db_do_close(_h, db_postgres_free_connection);
  144. }
  145. /*!
  146. * \brief Submit_query, run a query
  147. * \param _con database connection
  148. * \param _s query string
  149. * \return 0 on success, negative on failure
  150. */
  151. static int db_postgres_submit_query(const db1_con_t* _con, const str* _s)
  152. {
  153. char *s=NULL;
  154. int i, retries;
  155. ExecStatusType pqresult;
  156. PGresult *res = NULL;
  157. int sock, ret;
  158. fd_set fds;
  159. time_t max_time;
  160. struct timeval wait_time;
  161. if(! _con || !_s || !_s->s)
  162. {
  163. LM_ERR("invalid parameter value\n");
  164. return(-1);
  165. }
  166. /* this bit of nonsense in case our connection get screwed up */
  167. switch(PQstatus(CON_CONNECTION(_con)))
  168. {
  169. case CONNECTION_OK:
  170. break;
  171. case CONNECTION_BAD:
  172. LM_DBG("connection reset\n");
  173. PQreset(CON_CONNECTION(_con));
  174. break;
  175. case CONNECTION_STARTED:
  176. case CONNECTION_MADE:
  177. case CONNECTION_AWAITING_RESPONSE:
  178. case CONNECTION_AUTH_OK:
  179. case CONNECTION_SETENV:
  180. case CONNECTION_SSL_STARTUP:
  181. case CONNECTION_NEEDED:
  182. default:
  183. LM_ERR("%p PQstatus(%s) invalid: %.*s\n", _con,
  184. PQerrorMessage(CON_CONNECTION(_con)), _s->len, _s->s);
  185. return -1;
  186. }
  187. if (CON_TRANSACTION(_con) == 1)
  188. retries = 0;
  189. else
  190. retries = pg_retries;
  191. s = pkg_malloc((_s->len+1)*sizeof(char));
  192. if (s==NULL)
  193. {
  194. LM_ERR("%p db_postgres_submit_query Out of Memory: Query: %.*s\n", _con, _s->len, _s->s);
  195. return -1;
  196. }
  197. memcpy( s, _s->s, _s->len );
  198. s[_s->len] = '\0';
  199. for(i = 0; i <= retries; i++) {
  200. /* free any previous query that is laying about */
  201. db_postgres_free_query(_con);
  202. /* exec the query */
  203. if (PQsendQuery(CON_CONNECTION(_con), s)) {
  204. if (pg_timeout <= 0)
  205. goto do_read;
  206. max_time = time(NULL) + pg_timeout;
  207. while (1) {
  208. sock = PQsocket(CON_CONNECTION(_con));
  209. FD_ZERO(&fds);
  210. FD_SET(sock, &fds);
  211. wait_time.tv_usec = 0;
  212. wait_time.tv_sec = max_time - time(NULL);
  213. if (wait_time.tv_sec <= 0 || wait_time.tv_sec > 0xffffff)
  214. goto timeout;
  215. ret = select(sock + 1, &fds, NULL, NULL, &wait_time);
  216. if (ret < 0) {
  217. if (errno == EINTR)
  218. continue;
  219. LM_WARN("select() error\n");
  220. goto reset;
  221. }
  222. if (!ret) {
  223. timeout:
  224. LM_WARN("timeout waiting for postgres reply\n");
  225. goto reset;
  226. }
  227. if (!PQconsumeInput(CON_CONNECTION(_con))) {
  228. LM_WARN("error reading data from postgres server: %s\n",
  229. PQerrorMessage(CON_CONNECTION(_con)));
  230. goto reset;
  231. }
  232. if (!PQisBusy(CON_CONNECTION(_con)))
  233. break;
  234. }
  235. do_read:
  236. /* Get the result of the query */
  237. while ((res = PQgetResult(CON_CONNECTION(_con))) != NULL) {
  238. db_postgres_free_query(_con);
  239. CON_RESULT(_con) = res;
  240. }
  241. pqresult = PQresultStatus(CON_RESULT(_con));
  242. if((pqresult!=PGRES_FATAL_ERROR)
  243. && (PQstatus(CON_CONNECTION(_con))==CONNECTION_OK))
  244. {
  245. LM_DBG("sending query ok: %p (%d) - [%.*s]\n",
  246. _con, pqresult, _s->len, _s->s);
  247. pkg_free(s);
  248. return 0;
  249. }
  250. LM_WARN("postgres result check failed with code %d (%s)\n",
  251. pqresult, PQresStatus(pqresult));
  252. }
  253. LM_WARN("postgres query command failed, connection status %d,"
  254. " error [%s]\n", PQstatus(CON_CONNECTION(_con)),
  255. PQerrorMessage(CON_CONNECTION(_con)));
  256. if(PQstatus(CON_CONNECTION(_con))!=CONNECTION_OK)
  257. {
  258. reset:
  259. LM_DBG("reseting the connection to postgress server\n");
  260. PQreset(CON_CONNECTION(_con));
  261. }
  262. }
  263. LM_ERR("%p PQsendQuery Error: %s Query: %.*s\n", _con,
  264. PQerrorMessage(CON_CONNECTION(_con)), _s->len, _s->s);
  265. pkg_free(s);
  266. return -1;
  267. }
  268. /*!
  269. * \brief Gets a partial result set, fetch rows from a result
  270. *
  271. * Gets a partial result set, fetch a number of rows from a database result.
  272. * This function initialize the given result structure on the first run, and
  273. * fetches the nrows number of rows. On subsequenting runs, it uses the
  274. * existing result and fetches more rows, until it reaches the end of the
  275. * result set. Because of this the result needs to be null in the first
  276. * invocation of the function. If the number of wanted rows is zero, the
  277. * function returns anything with a result of zero.
  278. * \param _con database connection
  279. * \param _res result set
  280. * \param nrows number of fetches rows
  281. * \return 0 on success, negative on failure
  282. */
  283. int db_postgres_fetch_result(const db1_con_t* _con, db1_res_t** _res, const int nrows)
  284. {
  285. int rows;
  286. ExecStatusType pqresult;
  287. if (!_con || !_res || nrows < 0) {
  288. LM_ERR("invalid parameter value\n");
  289. return -1;
  290. }
  291. /* exit if the fetch count is zero */
  292. if (nrows == 0) {
  293. if (*_res)
  294. db_free_result(*_res);
  295. *_res = 0;
  296. return 0;
  297. }
  298. if (*_res == NULL) {
  299. /* Allocate a new result structure */
  300. *_res = db_new_result();
  301. pqresult = PQresultStatus(CON_RESULT(_con));
  302. LM_DBG("%p PQresultStatus(%s) PQgetResult(%p)\n", _con,
  303. PQresStatus(pqresult), CON_RESULT(_con));
  304. switch(pqresult) {
  305. case PGRES_COMMAND_OK:
  306. /* Successful completion of a command returning no data
  307. * (such as INSERT or UPDATE). */
  308. return 0;
  309. case PGRES_TUPLES_OK:
  310. /* Successful completion of a command returning data
  311. * (such as a SELECT or SHOW). */
  312. if (db_postgres_get_columns(_con, *_res) < 0) {
  313. LM_ERR("failed to get column names\n");
  314. return -2;
  315. }
  316. break;
  317. case PGRES_FATAL_ERROR:
  318. LM_ERR("%p - invalid query, execution aborted\n", _con);
  319. LM_ERR("%p - PQresultStatus(%s)\n", _con,
  320. PQresStatus(pqresult));
  321. LM_ERR("%p: %s\n", _con,
  322. PQresultErrorMessage(CON_RESULT(_con)));
  323. if (*_res)
  324. db_free_result(*_res);
  325. *_res = 0;
  326. return -3;
  327. case PGRES_EMPTY_QUERY:
  328. /* notice or warning */
  329. case PGRES_NONFATAL_ERROR:
  330. /* status for COPY command, not used */
  331. case PGRES_COPY_OUT:
  332. case PGRES_COPY_IN:
  333. /* unexpected response */
  334. case PGRES_BAD_RESPONSE:
  335. default:
  336. LM_ERR("%p - probable invalid query\n", _con);
  337. LM_ERR("%p - PQresultStatus(%s)\n", _con, PQresStatus(pqresult));
  338. LM_ERR("%p: %s\n", _con, PQresultErrorMessage(CON_RESULT(_con)));
  339. if (*_res)
  340. db_free_result(*_res);
  341. *_res = 0;
  342. return -4;
  343. }
  344. } else {
  345. if(RES_ROWS(*_res) != NULL) {
  346. db_free_rows(*_res);
  347. }
  348. RES_ROWS(*_res) = 0;
  349. RES_ROW_N(*_res) = 0;
  350. }
  351. /* Get the number of rows (tuples) in the query result. */
  352. RES_NUM_ROWS(*_res) = PQntuples(CON_RESULT(_con));
  353. /* determine the number of rows remaining to be processed */
  354. rows = RES_NUM_ROWS(*_res) - RES_LAST_ROW(*_res);
  355. /* If there aren't any more rows left to process, exit */
  356. if (rows <= 0)
  357. return 0;
  358. /* if the fetch count is less than the remaining rows to process */
  359. /* set the number of rows to process (during this call) equal to the fetch count */
  360. if (nrows < rows)
  361. rows = nrows;
  362. RES_ROW_N(*_res) = rows;
  363. LM_DBG("converting row %d of %d count %d\n", RES_LAST_ROW(*_res),
  364. RES_NUM_ROWS(*_res), RES_ROW_N(*_res));
  365. if (db_postgres_convert_rows(_con, *_res) < 0) {
  366. LM_ERR("failed to convert rows\n");
  367. if (*_res)
  368. db_free_result(*_res);
  369. *_res = 0;
  370. return -3;
  371. }
  372. /* update the total number of rows processed */
  373. RES_LAST_ROW(*_res) += rows;
  374. return 0;
  375. }
  376. /*!
  377. * \brief Free database and any old query results
  378. * \param _con database connection
  379. */
  380. static void db_postgres_free_query(const db1_con_t* _con)
  381. {
  382. if(CON_RESULT(_con))
  383. {
  384. LM_DBG("PQclear(%p) result set\n", CON_RESULT(_con));
  385. PQclear(CON_RESULT(_con));
  386. CON_RESULT(_con) = 0;
  387. }
  388. }
  389. /*!
  390. * \brief Free the query and the result memory in the core
  391. * \param _con database connection
  392. * \param _r result set
  393. * \return 0 on success, -1 on failure
  394. */
  395. int db_postgres_free_result(db1_con_t* _con, db1_res_t* _r)
  396. {
  397. if ((!_con) || (!_r)) {
  398. LM_ERR("invalid parameter value\n");
  399. return -1;
  400. }
  401. if (db_free_result(_r) < 0) {
  402. LM_ERR("unable to free result structure\n");
  403. return -1;
  404. }
  405. db_postgres_free_query(_con);
  406. return 0;
  407. }
  408. /*!
  409. * \brief Query table for specified rows
  410. * \param _h structure representing database connection
  411. * \param _k key names
  412. * \param _op operators
  413. * \param _v values of the keys that must match
  414. * \param _c column names to return
  415. * \param _n nmber of key=values pairs to compare
  416. * \param _nc number of columns to return
  417. * \param _o order by the specified column
  418. * \param _r result set
  419. * \return 0 on success, negative on failure
  420. */
  421. int db_postgres_query(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
  422. const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc,
  423. const db_key_t _o, db1_res_t** _r)
  424. {
  425. return db_do_query(_h, _k, _op, _v, _c, _n, _nc, _o, _r, db_postgres_val2str,
  426. db_postgres_submit_query, db_postgres_store_result);
  427. }
  428. /*!
  429. * \brief Query table for specified rows and lock them
  430. * \param _h structure representing database connection
  431. * \param _k key names
  432. * \param _op operators
  433. * \param _v values of the keys that must match
  434. * \param _c column names to return
  435. * \param _n nmber of key=values pairs to compare
  436. * \param _nc number of columns to return
  437. * \param _o order by the specified column
  438. * \param _r result set
  439. * \return 0 on success, negative on failure
  440. */
  441. int db_postgres_query_lock(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
  442. const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc,
  443. const db_key_t _o, db1_res_t** _r)
  444. {
  445. if (CON_TRANSACTION(_h) == 0)
  446. {
  447. LM_ERR("transaction not in progress\n");
  448. return -1;
  449. }
  450. return db_do_query_lock(_h, _k, _op, _v, _c, _n, _nc, _o, _r, db_postgres_val2str,
  451. db_postgres_submit_query, db_postgres_store_result);
  452. }
  453. /*!
  454. * Execute a raw SQL query
  455. * \param _h database connection
  456. * \param _s raw query string
  457. * \param _r result set
  458. * \return 0 on success, negative on failure
  459. */
  460. int db_postgres_raw_query(const db1_con_t* _h, const str* _s, db1_res_t** _r)
  461. {
  462. return db_do_raw_query(_h, _s, _r, db_postgres_submit_query,
  463. db_postgres_store_result);
  464. }
  465. /*!
  466. * \brief Retrieve result set
  467. * \param _con structure representing the database connection
  468. * \param _r pointer to a structure represending the result set
  469. * \return 0 If the status of the last command produced a result set and,
  470. * If the result set contains data or the convert_result() routine
  471. * completed successfully. Negative if the status of the last command was
  472. * not handled or if the convert_result() returned an error.
  473. * \note A new result structure is allocated on every call to this routine.
  474. * If this routine returns 0, it is the callers responsbility to free the
  475. * result structure. If this routine returns < 0, then the result structure
  476. * is freed before returning to the caller.
  477. */
  478. int db_postgres_store_result(const db1_con_t* _con, db1_res_t** _r)
  479. {
  480. ExecStatusType pqresult;
  481. int rc = 0;
  482. *_r = db_new_result();
  483. if (*_r==NULL) {
  484. LM_ERR("failed to init new result\n");
  485. rc = -1;
  486. goto done;
  487. }
  488. pqresult = PQresultStatus(CON_RESULT(_con));
  489. LM_DBG("%p PQresultStatus(%s) PQgetResult(%p)\n", _con,
  490. PQresStatus(pqresult), CON_RESULT(_con));
  491. CON_AFFECTED(_con) = 0;
  492. switch(pqresult) {
  493. case PGRES_COMMAND_OK:
  494. /* Successful completion of a command returning no data
  495. * (such as INSERT or UPDATE). */
  496. rc = 0;
  497. CON_AFFECTED(_con) = atoi(PQcmdTuples(CON_RESULT(_con)));
  498. break;
  499. case PGRES_TUPLES_OK:
  500. /* Successful completion of a command returning data
  501. * (such as a SELECT or SHOW). */
  502. if (db_postgres_convert_result(_con, *_r) < 0) {
  503. LM_ERR("error while converting result\n");
  504. LM_DBG("freeing result set at %p\n", _r);
  505. pkg_free(*_r);
  506. *_r = 0;
  507. rc = -4;
  508. break;
  509. }
  510. rc = 0;
  511. CON_AFFECTED(_con) = atoi(PQcmdTuples(CON_RESULT(_con)));
  512. break;
  513. /* query failed */
  514. case PGRES_FATAL_ERROR:
  515. LM_ERR("invalid query, execution aborted\n");
  516. LM_ERR("driver error: %s, %s\n", PQresStatus(pqresult), PQresultErrorMessage(CON_RESULT(_con)));
  517. db_free_result(*_r);
  518. *_r = 0;
  519. rc = -3;
  520. break;
  521. case PGRES_EMPTY_QUERY:
  522. /* notice or warning */
  523. case PGRES_NONFATAL_ERROR:
  524. /* status for COPY command, not used */
  525. case PGRES_COPY_OUT:
  526. case PGRES_COPY_IN:
  527. /* unexpected response */
  528. case PGRES_BAD_RESPONSE:
  529. default:
  530. LM_ERR("probable invalid query, execution aborted\n");
  531. LM_ERR("driver message: %s, %s\n", PQresStatus(pqresult), PQresultErrorMessage(CON_RESULT(_con)));
  532. db_free_result(*_r);
  533. *_r = 0;
  534. rc = -4;
  535. break;
  536. }
  537. done:
  538. db_postgres_free_query(_con);
  539. return (rc);
  540. }
  541. /*!
  542. * \brief Insert a row into specified table
  543. * \param _h structure representing database connection
  544. * \param _k key names
  545. * \param _v values of the keys
  546. * \param _n number of key=value pairs
  547. * \return 0 on success, negative on failure
  548. */
  549. int db_postgres_insert(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
  550. const int _n)
  551. {
  552. db1_res_t* _r = NULL;
  553. int ret = db_do_insert(_h, _k, _v, _n, db_postgres_val2str, db_postgres_submit_query);
  554. // finish the async query, otherwise the next query will not complete
  555. int tmp = db_postgres_store_result(_h, &_r);
  556. if (tmp < 0) {
  557. LM_WARN("unexpected result returned");
  558. ret = tmp;
  559. }
  560. if (_r)
  561. db_free_result(_r);
  562. return ret;
  563. }
  564. /*!
  565. * \brief Delete a row from the specified table
  566. * \param _h structure representing database connection
  567. * \param _k key names
  568. * \param _o operators
  569. * \param _v values of the keys that must match
  570. * \param _n number of key=value pairs
  571. * \return 0 on success, negative on failure
  572. */
  573. int db_postgres_delete(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
  574. const db_val_t* _v, const int _n)
  575. {
  576. db1_res_t* _r = NULL;
  577. int ret = db_do_delete(_h, _k, _o, _v, _n, db_postgres_val2str,
  578. db_postgres_submit_query);
  579. int tmp = db_postgres_store_result(_h, &_r);
  580. if (tmp < 0) {
  581. LM_WARN("unexpected result returned");
  582. ret = tmp;
  583. }
  584. if (_r)
  585. db_free_result(_r);
  586. return ret;
  587. }
  588. /*!
  589. * Update some rows in the specified table
  590. * \param _h structure representing database connection
  591. * \param _k key names
  592. * \param _o operators
  593. * \param _v values of the keys that must match
  594. * \param _uk updated columns
  595. * \param _uv updated values of the columns
  596. * \param _n number of key=value pairs
  597. * \param _un number of columns to update
  598. * \return 0 on success, negative on failure
  599. */
  600. int db_postgres_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
  601. const db_val_t* _v, const db_key_t* _uk, const db_val_t* _uv, const int _n,
  602. const int _un)
  603. {
  604. db1_res_t* _r = NULL;
  605. int ret = db_do_update(_h, _k, _o, _v, _uk, _uv, _n, _un, db_postgres_val2str,
  606. db_postgres_submit_query);
  607. int tmp = db_postgres_store_result(_h, &_r);
  608. if (tmp < 0) {
  609. LM_WARN("unexpected result returned");
  610. ret = tmp;
  611. }
  612. if (_r)
  613. db_free_result(_r);
  614. return ret;
  615. }
  616. /**
  617. * Returns the affected rows of the last query.
  618. * \param _h database handle
  619. * \return returns the affected rows as integer or -1 on error.
  620. */
  621. int db_postgres_affected_rows(const db1_con_t* _h)
  622. {
  623. if (!_h) {
  624. LM_ERR("invalid parameter value\n");
  625. return -1;
  626. }
  627. return CON_AFFECTED(_h);
  628. }
  629. /**
  630. * Starts a single transaction that will consist of one or more queries (SQL BEGIN)
  631. * \param _h database handle
  632. * \return 0 on success, negative on failure
  633. */
  634. int db_postgres_start_transaction(db1_con_t* _h, db_locking_t _l)
  635. {
  636. db1_res_t *res = NULL;
  637. str begin_str = str_init("BEGIN");
  638. str lock_start_str = str_init("LOCK TABLE ");
  639. str lock_write_end_str = str_init(" IN EXCLUSIVE MODE");
  640. str lock_full_end_str = str_init(" IN ACCESS EXCLUSIVE MODE");
  641. str *lock_end_str = &lock_write_end_str;
  642. str lock_str = {0, 0};
  643. if (!_h) {
  644. LM_ERR("invalid parameter value\n");
  645. return -1;
  646. }
  647. if (CON_TRANSACTION(_h) == 1) {
  648. LM_ERR("transaction already started\n");
  649. return -1;
  650. }
  651. if (db_postgres_raw_query(_h, &begin_str, &res) < 0)
  652. {
  653. LM_ERR("executing raw_query\n");
  654. return -1;
  655. }
  656. if (res) db_postgres_free_result(_h, res);
  657. CON_TRANSACTION(_h) = 1;
  658. switch(_l)
  659. {
  660. case DB_LOCKING_NONE:
  661. break;
  662. case DB_LOCKING_FULL:
  663. lock_end_str = &lock_full_end_str;
  664. /* Fall-thru */
  665. case DB_LOCKING_WRITE:
  666. if ((lock_str.s = pkg_malloc((lock_start_str.len + CON_TABLE(_h)->len + lock_end_str->len) * sizeof(char))) == NULL)
  667. {
  668. LM_ERR("allocating pkg memory\n");
  669. goto error;
  670. }
  671. memcpy(lock_str.s, lock_start_str.s, lock_start_str.len);
  672. lock_str.len += lock_start_str.len;
  673. memcpy(lock_str.s + lock_str.len, CON_TABLE(_h)->s, CON_TABLE(_h)->len);
  674. lock_str.len += CON_TABLE(_h)->len;
  675. memcpy(lock_str.s + lock_str.len, lock_end_str->s, lock_end_str->len);
  676. lock_str.len += lock_end_str->len;
  677. if (db_postgres_raw_query(_h, &lock_str, &res) < 0)
  678. {
  679. LM_ERR("executing raw_query\n");
  680. goto error;
  681. }
  682. if (res) db_postgres_free_result(_h, res);
  683. if (lock_str.s) pkg_free(lock_str.s);
  684. break;
  685. default:
  686. LM_WARN("unrecognised lock type\n");
  687. goto error;
  688. }
  689. return 0;
  690. error:
  691. if (lock_str.s) pkg_free(lock_str.s);
  692. db_postgres_abort_transaction(_h);
  693. return -1;
  694. }
  695. /**
  696. * Ends a transaction and commits the changes (SQL COMMIT)
  697. * \param _h database handle
  698. * \return 0 on success, negative on failure
  699. */
  700. int db_postgres_end_transaction(db1_con_t* _h)
  701. {
  702. db1_res_t *res = NULL;
  703. str query_str = str_init("COMMIT");
  704. if (!_h) {
  705. LM_ERR("invalid parameter value\n");
  706. return -1;
  707. }
  708. if (CON_TRANSACTION(_h) == 0) {
  709. LM_ERR("transaction not in progress\n");
  710. return -1;
  711. }
  712. if (db_postgres_raw_query(_h, &query_str, &res) < 0)
  713. {
  714. LM_ERR("executing raw_query\n");
  715. return -1;
  716. }
  717. if (res) db_postgres_free_result(_h, res);
  718. /* Only _end_ the transaction after the raw_query. That way, if the
  719. raw_query fails, and the calling module does an abort_transaction()
  720. to clean-up, a ROLLBACK will be sent to the DB. */
  721. CON_TRANSACTION(_h) = 0;
  722. return 0;
  723. }
  724. /**
  725. * Ends a transaction and rollsback the changes (SQL ROLLBACK)
  726. * \param _h database handle
  727. * \return 1 if there was something to rollback, 0 if not, negative on failure
  728. */
  729. int db_postgres_abort_transaction(db1_con_t* _h)
  730. {
  731. db1_res_t *res = NULL;
  732. str query_str = str_init("ROLLBACK");
  733. if (!_h) {
  734. LM_ERR("invalid parameter value\n");
  735. return -1;
  736. }
  737. if (CON_TRANSACTION(_h) == 0) {
  738. LM_DBG("nothing to rollback\n");
  739. return 0;
  740. }
  741. /* Whether the rollback succeeds or not we need to _end_ the
  742. transaction now or all future starts will fail */
  743. CON_TRANSACTION(_h) = 0;
  744. if (db_postgres_raw_query(_h, &query_str, &res) < 0)
  745. {
  746. LM_ERR("executing raw_query\n");
  747. return -1;
  748. }
  749. if (res) db_postgres_free_result(_h, res);
  750. return 1;
  751. }
  752. /*!
  753. * Store name of table that will be used by subsequent database functions
  754. * \param _con database connection
  755. * \param _t table name
  756. * \return 0 on success, negative on error
  757. */
  758. int db_postgres_use_table(db1_con_t* _con, const str* _t)
  759. {
  760. return db_use_table(_con, _t);
  761. }
  762. /*!
  763. * \brief SQL REPLACE implementation
  764. * \param _h structure representing database connection
  765. * \param _k key names
  766. * \param _v values of the keys
  767. * \param _n number of key=value pairs
  768. * \param _un number of keys to build the unique key, starting from first
  769. * \param _m mode - first update, then insert, or first insert, then update
  770. * \return 0 on success, negative on failure
  771. */
  772. int db_postgres_replace(const db1_con_t* _h, const db_key_t* _k,
  773. const db_val_t* _v, const int _n, const int _un, const int _m)
  774. {
  775. unsigned int pos = 0;
  776. int i;
  777. if(_un > _n)
  778. {
  779. LM_ERR("number of columns for unique key is too high\n");
  780. return -1;
  781. }
  782. if(_un > 0)
  783. {
  784. for(i=0; i<_un; i++)
  785. {
  786. if(!VAL_NULL(&_v[i]))
  787. {
  788. switch(VAL_TYPE(&_v[i]))
  789. {
  790. case DB1_INT:
  791. pos += VAL_UINT(&_v[i]);
  792. break;
  793. case DB1_STR:
  794. pos += get_hash1_raw((VAL_STR(&_v[i])).s,
  795. (VAL_STR(&_v[i])).len);
  796. break;
  797. case DB1_STRING:
  798. pos += get_hash1_raw(VAL_STRING(&_v[i]),
  799. strlen(VAL_STRING(&_v[i])));
  800. break;
  801. default:
  802. break;
  803. }
  804. }
  805. }
  806. pos &= (_pg_lock_size-1);
  807. lock_set_get(_pg_lock_set, pos);
  808. if(db_postgres_update(_h, _k, 0, _v, _k + _un,
  809. _v + _un, _un, _n -_un)< 0)
  810. {
  811. LM_ERR("update failed\n");
  812. lock_set_release(_pg_lock_set, pos);
  813. return -1;
  814. }
  815. if (db_postgres_affected_rows(_h) <= 0)
  816. {
  817. if(db_postgres_insert(_h, _k, _v, _n)< 0)
  818. {
  819. LM_ERR("insert failed\n");
  820. lock_set_release(_pg_lock_set, pos);
  821. return -1;
  822. }
  823. LM_DBG("inserted new record in database table\n");
  824. } else {
  825. LM_DBG("updated record in database table\n");
  826. }
  827. lock_set_release(_pg_lock_set, pos);
  828. } else {
  829. if(db_postgres_insert(_h, _k, _v, _n)< 0)
  830. {
  831. LM_ERR("direct insert failed\n");
  832. return -1;
  833. }
  834. LM_DBG("directly inserted new record in database table\n");
  835. }
  836. return 0;
  837. }