km_dbase.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /*
  2. * $Id$
  3. *
  4. * MySQL module core functions
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. * Copyright (C) 2007-2008 1&1 Internet AG
  8. *
  9. * This file is part of Kamailio, a free SIP server.
  10. *
  11. * Kamailio is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version
  15. *
  16. * Kamailio is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. /*!
  26. * \file
  27. * \brief Implementation of core functions for the MySQL driver.
  28. *
  29. * This file contains the implementation of core functions for the MySQL
  30. * database driver, for example to submit a query or fetch a result.
  31. * \ingroup db_mysql
  32. * Module: \ref db_mysql
  33. */
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <mysql/mysql.h>
  37. #include <mysql/errmsg.h>
  38. #include <mysql/mysql_version.h>
  39. #include "../../mem/mem.h"
  40. #include "../../dprint.h"
  41. #include "../../lib/srdb1/db_query.h"
  42. #include "../../lib/srdb1/db_ut.h"
  43. #include "mysql_mod.h"
  44. #include "km_val.h"
  45. #include "km_my_con.h"
  46. #include "km_res.h"
  47. #include "km_row.h"
  48. #include "km_db_mysql.h"
  49. #include "km_dbase.h"
  50. static char *mysql_sql_buf;
  51. /**
  52. * \brief Send a SQL query to the server.
  53. *
  54. * Send a SQL query to the database server. This methods tries to reconnect
  55. * to the server if the connection is gone and the auto_reconnect parameter is
  56. * enabled. It also issues a mysql_ping before the query to connect again after
  57. * a long waiting period because for some older mysql versions the auto reconnect
  58. * don't work sufficient. If auto_reconnect is enabled and the server supports it,
  59. * then the mysql_ping is probably not necessary, but its safer to do it in this
  60. * cases too.
  61. *
  62. * \param _h handle for the db
  63. * \param _s executed query
  64. * \return zero on success, negative value on failure
  65. */
  66. static int db_mysql_submit_query(const db1_con_t* _h, const str* _s)
  67. {
  68. time_t t;
  69. int i, code;
  70. if (!_h || !_s || !_s->s) {
  71. LM_ERR("invalid parameter value\n");
  72. return -1;
  73. }
  74. if (my_ping_interval) {
  75. t = time(0);
  76. if ((t - CON_TIMESTAMP(_h)) > my_ping_interval) {
  77. if (mysql_ping(CON_CONNECTION(_h))) {
  78. LM_WARN("driver error on ping: %s\n", mysql_error(CON_CONNECTION(_h)));
  79. counter_inc(mysql_cnts_h.driver_err);
  80. }
  81. }
  82. /*
  83. * We're doing later a query anyway that will reset the timout of the server,
  84. * so it makes sense to set the timestamp value to the actual time in order
  85. * to prevent unnecessary pings.
  86. */
  87. CON_TIMESTAMP(_h) = t;
  88. }
  89. /* screws up the terminal when the query contains a BLOB :-( (by bogdan)
  90. * LM_DBG("submit_query(): %.*s\n", _s->len, _s->s);
  91. */
  92. /* When a server connection is lost and a query is attempted, most of
  93. * the time the query will return a CR_SERVER_LOST, then at the second
  94. * attempt to execute it, the mysql lib will reconnect and succeed.
  95. * However is a few cases, the first attempt returns CR_SERVER_GONE_ERROR
  96. * the second CR_SERVER_LOST and only the third succeeds.
  97. * Thus the 3 in the loop count. Increasing the loop count over this
  98. * value shouldn't be needed, but it doesn't hurt either, since the loop
  99. * will most of the time stop at the second or sometimes at the third
  100. * iteration. In the case of CR_SERVER_GONE_ERROR and CR_SERVER_LOST the
  101. * driver error counter is increased
  102. */
  103. for (i=0; i < (db_mysql_auto_reconnect ? 3 : 1); i++) {
  104. if (mysql_real_query(CON_CONNECTION(_h), _s->s, _s->len) == 0) {
  105. return 0;
  106. }
  107. code = mysql_errno(CON_CONNECTION(_h));
  108. if (code != CR_SERVER_GONE_ERROR && code != CR_SERVER_LOST) {
  109. break;
  110. }
  111. counter_inc(mysql_cnts_h.driver_err);
  112. }
  113. LM_ERR("driver error on query: %s\n", mysql_error(CON_CONNECTION(_h)));
  114. return -2;
  115. }
  116. /**
  117. * Initialize the database module.
  118. * No function should be called before this
  119. * \param _url URL used for initialization
  120. * \return zero on success, negative value on failure
  121. */
  122. db1_con_t* db_mysql_init(const str* _url)
  123. {
  124. return db_do_init(_url, (void *)db_mysql_new_connection);
  125. }
  126. /**
  127. * Shut down the database module.
  128. * No function should be called after this
  129. * \param _h handle to the closed connection
  130. * \return zero on success, negative value on failure
  131. */
  132. void db_mysql_close(db1_con_t* _h)
  133. {
  134. db_do_close(_h, db_mysql_free_connection);
  135. }
  136. /**
  137. * Retrieve a result set
  138. * \param _h handle to the database
  139. * \param _r result set that should be retrieved
  140. * \return zero on success, negative value on failure
  141. */
  142. static int db_mysql_store_result(const db1_con_t* _h, db1_res_t** _r)
  143. {
  144. int code;
  145. if ((!_h) || (!_r)) {
  146. LM_ERR("invalid parameter value\n");
  147. return -1;
  148. }
  149. *_r = db_mysql_new_result();
  150. if (*_r == 0) {
  151. LM_ERR("no memory left\n");
  152. return -2;
  153. }
  154. RES_RESULT(*_r) = mysql_store_result(CON_CONNECTION(_h));
  155. if (!RES_RESULT(*_r)) {
  156. if (mysql_field_count(CON_CONNECTION(_h)) == 0) {
  157. (*_r)->col.n = 0;
  158. (*_r)->n = 0;
  159. goto done;
  160. } else {
  161. LM_ERR("driver error: %s\n", mysql_error(CON_CONNECTION(_h)));
  162. code = mysql_errno(CON_CONNECTION(_h));
  163. if (code == CR_SERVER_GONE_ERROR || code == CR_SERVER_LOST) {
  164. counter_inc(mysql_cnts_h.driver_err);
  165. }
  166. db_mysql_free_result(_h, *_r);
  167. *_r = 0;
  168. return -3;
  169. }
  170. }
  171. if (db_mysql_convert_result(_h, *_r) < 0) {
  172. LM_ERR("error while converting result\n");
  173. LM_DBG("freeing result set at %p\n", _r);
  174. /* all mem on Kamailio API side is already freed by
  175. * db_mysql_convert_result in case of error, but we also need
  176. * to free the mem from the mysql lib side, internal pkg for it
  177. * and *_r */
  178. db_mysql_free_result(_h, *_r);
  179. *_r = 0;
  180. #if (MYSQL_VERSION_ID >= 40100)
  181. while( mysql_more_results(CON_CONNECTION(_h)) && mysql_next_result(CON_CONNECTION(_h)) == 0 ) {
  182. MYSQL_RES *res = mysql_store_result( CON_CONNECTION(_h) );
  183. mysql_free_result(res);
  184. }
  185. #endif
  186. return -4;
  187. }
  188. done:
  189. #if (MYSQL_VERSION_ID >= 40100)
  190. while( mysql_more_results(CON_CONNECTION(_h)) && mysql_next_result(CON_CONNECTION(_h)) == 0 ) {
  191. MYSQL_RES *res = mysql_store_result( CON_CONNECTION(_h) );
  192. mysql_free_result(res);
  193. }
  194. #endif
  195. return 0;
  196. }
  197. /**
  198. * Release a result set from memory.
  199. * \param _h handle to the database
  200. * \param _r result set that should be freed
  201. * \return zero on success, negative value on failure
  202. */
  203. int db_mysql_free_result(const db1_con_t* _h, db1_res_t* _r)
  204. {
  205. if ((!_h) || (!_r)) {
  206. LM_ERR("invalid parameter value\n");
  207. return -1;
  208. }
  209. mysql_free_result(RES_RESULT(_r));
  210. RES_RESULT(_r) = 0;
  211. pkg_free(RES_PTR(_r));
  212. if (db_free_result(_r) < 0) {
  213. LM_ERR("unable to free result structure\n");
  214. return -1;
  215. }
  216. return 0;
  217. }
  218. /**
  219. * Query a table for specified rows.
  220. * \param _h structure representing database connection
  221. * \param _k key names
  222. * \param _op operators
  223. *\param _v values of the keys that must match
  224. * \param _c column names to return
  225. * \param _n number of key=values pairs to compare
  226. * \param _nc number of columns to return
  227. * \param _o order by the specified column
  228. * \param _r pointer to a structure representing the result
  229. * \return zero on success, negative value on failure
  230. */
  231. int db_mysql_query(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
  232. const db_val_t* _v, const db_key_t* _c, const int _n, const int _nc,
  233. const db_key_t _o, db1_res_t** _r)
  234. {
  235. return db_do_query(_h, _k, _op, _v, _c, _n, _nc, _o, _r,
  236. db_mysql_val2str, db_mysql_submit_query, db_mysql_store_result);
  237. }
  238. /**
  239. * \brief Gets a partial result set, fetch rows from a result
  240. *
  241. * Gets a partial result set, fetch a number of rows from a database result.
  242. * This function initialize the given result structure on the first run, and
  243. * fetches the nrows number of rows. On subsequenting runs, it uses the
  244. * existing result and fetches more rows, until it reaches the end of the
  245. * result set. Because of this the result needs to be null in the first
  246. * invocation of the function. If the number of wanted rows is zero, the
  247. * function returns anything with a result of zero.
  248. * \param _h structure representing the database connection
  249. * \param _r pointer to a structure representing the result
  250. * \param nrows number of fetched rows
  251. * \return zero on success, negative value on failure
  252. */
  253. int db_mysql_fetch_result(const db1_con_t* _h, db1_res_t** _r, const int nrows)
  254. {
  255. int rows, i, code;
  256. if (!_h || !_r || nrows < 0) {
  257. LM_ERR("Invalid parameter value\n");
  258. return -1;
  259. }
  260. /* exit if the fetch count is zero */
  261. if (nrows == 0) {
  262. db_mysql_free_result(_h, *_r);
  263. *_r = 0;
  264. return 0;
  265. }
  266. if(*_r==0) {
  267. /* Allocate a new result structure */
  268. *_r = db_mysql_new_result();
  269. if (*_r == 0) {
  270. LM_ERR("no memory left\n");
  271. return -2;
  272. }
  273. RES_RESULT(*_r) = mysql_store_result(CON_CONNECTION(_h));
  274. if (!RES_RESULT(*_r)) {
  275. if (mysql_field_count(CON_CONNECTION(_h)) == 0) {
  276. (*_r)->col.n = 0;
  277. (*_r)->n = 0;
  278. return 0;
  279. } else {
  280. LM_ERR("driver error: %s\n", mysql_error(CON_CONNECTION(_h)));
  281. code = mysql_errno(CON_CONNECTION(_h));
  282. if (code == CR_SERVER_GONE_ERROR || code == CR_SERVER_LOST) {
  283. counter_inc(mysql_cnts_h.driver_err);
  284. }
  285. db_mysql_free_result(_h, *_r);
  286. *_r = 0;
  287. return -3;
  288. }
  289. }
  290. if (db_mysql_get_columns(_h, *_r) < 0) {
  291. LM_ERR("error while getting column names\n");
  292. return -4;
  293. }
  294. RES_NUM_ROWS(*_r) = mysql_num_rows(RES_RESULT(*_r));
  295. if (!RES_NUM_ROWS(*_r)) {
  296. LM_DBG("no rows returned from the query\n");
  297. RES_ROWS(*_r) = 0;
  298. return 0;
  299. }
  300. } else {
  301. /* free old rows */
  302. if(RES_ROWS(*_r)!=0)
  303. db_free_rows(*_r);
  304. RES_ROWS(*_r) = 0;
  305. RES_ROW_N(*_r) = 0;
  306. }
  307. /* determine the number of rows remaining to be processed */
  308. rows = RES_NUM_ROWS(*_r) - RES_LAST_ROW(*_r);
  309. /* If there aren't any more rows left to process, exit */
  310. if(rows<=0)
  311. return 0;
  312. /* if the fetch count is less than the remaining rows to process */
  313. /* set the number of rows to process (during this call) equal to the fetch count */
  314. if(nrows < rows)
  315. rows = nrows;
  316. RES_ROW_N(*_r) = rows;
  317. LM_DBG("converting row %d of %d count %d\n", RES_LAST_ROW(*_r),
  318. RES_NUM_ROWS(*_r), RES_ROW_N(*_r));
  319. RES_ROWS(*_r) = (struct db_row*)pkg_malloc(sizeof(db_row_t) * rows);
  320. if (!RES_ROWS(*_r)) {
  321. LM_ERR("no memory left\n");
  322. return -5;
  323. }
  324. for(i = 0; i < rows; i++) {
  325. RES_ROW(*_r) = mysql_fetch_row(RES_RESULT(*_r));
  326. if (!RES_ROW(*_r)) {
  327. LM_ERR("driver error: %s\n", mysql_error(CON_CONNECTION(_h)));
  328. RES_ROW_N(*_r) = i;
  329. db_free_rows(*_r);
  330. return -6;
  331. }
  332. if (db_mysql_convert_row(_h, *_r, &(RES_ROWS(*_r)[i])) < 0) {
  333. LM_ERR("error while converting row #%d\n", i);
  334. RES_ROW_N(*_r) = i;
  335. db_free_rows(*_r);
  336. return -7;
  337. }
  338. }
  339. /* update the total number of rows processed */
  340. RES_LAST_ROW(*_r) += rows;
  341. return 0;
  342. }
  343. /**
  344. * Execute a raw SQL query.
  345. * \param _h handle for the database
  346. * \param _s raw query string
  347. * \param _r result set for storage
  348. * \return zero on success, negative value on failure
  349. */
  350. int db_mysql_raw_query(const db1_con_t* _h, const str* _s, db1_res_t** _r)
  351. {
  352. return db_do_raw_query(_h, _s, _r, db_mysql_submit_query,
  353. db_mysql_store_result);
  354. }
  355. /**
  356. * Insert a row into a specified table.
  357. * \param _h structure representing database connection
  358. * \param _k key names
  359. * \param _v values of the keys
  360. * \param _n number of key=value pairs
  361. * \return zero on success, negative value on failure
  362. */
  363. int db_mysql_insert(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
  364. const int _n)
  365. {
  366. if(unlikely(db_mysql_insert_all_delayed==1))
  367. return db_do_insert_delayed(_h, _k, _v, _n, db_mysql_val2str,
  368. db_mysql_submit_query);
  369. else
  370. return db_do_insert(_h, _k, _v, _n, db_mysql_val2str,
  371. db_mysql_submit_query);
  372. }
  373. /**
  374. * Delete a row from the specified table
  375. * \param _h structure representing database connection
  376. * \param _k key names
  377. * \param _o operators
  378. * \param _v values of the keys that must match
  379. * \param _n number of key=value pairs
  380. * \return zero on success, negative value on failure
  381. */
  382. int db_mysql_delete(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
  383. const db_val_t* _v, const int _n)
  384. {
  385. return db_do_delete(_h, _k, _o, _v, _n, db_mysql_val2str,
  386. db_mysql_submit_query);
  387. }
  388. /**
  389. * Update some rows in the specified table
  390. * \param _h structure representing database connection
  391. * \param _k key names
  392. * \param _o operators
  393. * \param _v values of the keys that must match
  394. * \param _uk updated columns
  395. * \param _uv updated values of the columns
  396. * \param _n number of key=value pairs
  397. * \param _un number of columns to update
  398. * \return zero on success, negative value on failure
  399. */
  400. int db_mysql_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
  401. const db_val_t* _v, const db_key_t* _uk, const db_val_t* _uv, const int _n,
  402. const int _un)
  403. {
  404. return db_do_update(_h, _k, _o, _v, _uk, _uv, _n, _un, db_mysql_val2str,
  405. db_mysql_submit_query);
  406. }
  407. /**
  408. * Just like insert, but replace the row if it exists.
  409. * \param _h database handle
  410. * \param _k key names
  411. * \param _v values of the keys that must match
  412. * \param _n number of key=value pairs
  413. * \return zero on success, negative value on failure
  414. */
  415. int db_mysql_replace(const db1_con_t* _h, const db_key_t* _k,
  416. const db_val_t* _v, const int _n, const int _un, const int _m)
  417. {
  418. return db_do_replace(_h, _k, _v, _n, db_mysql_val2str,
  419. db_mysql_submit_query);
  420. }
  421. /**
  422. * Returns the last inserted ID.
  423. * \param _h database handle
  424. * \return returns the ID as integer or returns 0 if the previous statement
  425. * does not use an AUTO_INCREMENT value.
  426. */
  427. int db_mysql_last_inserted_id(const db1_con_t* _h)
  428. {
  429. if (!_h) {
  430. LM_ERR("invalid parameter value\n");
  431. return -1;
  432. }
  433. return mysql_insert_id(CON_CONNECTION(_h));
  434. }
  435. /**
  436. * Returns the affected rows of the last query.
  437. * \param _h database handle
  438. * \return returns the affected rows as integer or -1 on error.
  439. */
  440. int db_mysql_affected_rows(const db1_con_t* _h)
  441. {
  442. if (!_h) {
  443. LM_ERR("invalid parameter value\n");
  444. return -1;
  445. }
  446. return (int)mysql_affected_rows(CON_CONNECTION(_h));
  447. }
  448. /**
  449. * Starts a single transaction that will consist of one or more queries (SQL BEGIN)
  450. * \param _h database handle
  451. * \return 0 on success, negative on failure
  452. */
  453. int db_mysql_start_transaction(db1_con_t* _h, db_locking_t _l)
  454. {
  455. str begin_str = str_init("SET autocommit=0");
  456. str lock_start_str = str_init("LOCK TABLES ");
  457. str lock_end_str = str_init(" WRITE");
  458. str lock_str = {0, 0};
  459. if (!_h) {
  460. LM_ERR("invalid parameter value\n");
  461. return -1;
  462. }
  463. if (CON_TRANSACTION(_h) == 1) {
  464. LM_ERR("transaction already started\n");
  465. return -1;
  466. }
  467. if (db_mysql_raw_query(_h, &begin_str, NULL) < 0)
  468. {
  469. LM_ERR("executing raw_query\n");
  470. return -1;
  471. }
  472. CON_TRANSACTION(_h) = 1;
  473. switch(_l)
  474. {
  475. case DB_LOCKING_NONE:
  476. break;
  477. case DB_LOCKING_FULL:
  478. /* Fall-thru */
  479. case DB_LOCKING_WRITE:
  480. if ((lock_str.s = pkg_malloc((lock_start_str.len + CON_TABLE(_h)->len + lock_end_str.len) * sizeof(char))) == NULL)
  481. {
  482. LM_ERR("allocating pkg memory\n");
  483. goto error;
  484. }
  485. memcpy(lock_str.s, lock_start_str.s, lock_start_str.len);
  486. lock_str.len += lock_start_str.len;
  487. memcpy(lock_str.s + lock_str.len, CON_TABLE(_h)->s, CON_TABLE(_h)->len);
  488. lock_str.len += CON_TABLE(_h)->len;
  489. memcpy(lock_str.s + lock_str.len, lock_end_str.s, lock_end_str.len);
  490. lock_str.len += lock_end_str.len;
  491. if (db_mysql_raw_query(_h, &lock_str, NULL) < 0)
  492. {
  493. LM_ERR("executing raw_query\n");
  494. goto error;
  495. }
  496. if (lock_str.s) pkg_free(lock_str.s);
  497. CON_LOCKEDTABLES(_h) = 1;
  498. break;
  499. default:
  500. LM_WARN("unrecognised lock type\n");
  501. goto error;
  502. }
  503. return 0;
  504. error:
  505. if (lock_str.s) pkg_free(lock_str.s);
  506. db_mysql_abort_transaction(_h);
  507. return -1;
  508. }
  509. /**
  510. * Unlock tables in the session
  511. * \param _h database handle
  512. * \return 0 on success, negative on failure
  513. */
  514. int db_mysql_unlock_tables(db1_con_t* _h)
  515. {
  516. str query_str = str_init("UNLOCK TABLES");
  517. if (!_h) {
  518. LM_ERR("invalid parameter value\n");
  519. return -1;
  520. }
  521. if (CON_LOCKEDTABLES(_h) == 0) {
  522. LM_DBG("no active locked tables\n");
  523. return 0;
  524. }
  525. if (db_mysql_raw_query(_h, &query_str, NULL) < 0)
  526. {
  527. LM_ERR("executing raw_query\n");
  528. return -1;
  529. }
  530. CON_LOCKEDTABLES(_h) = 0;
  531. return 0;
  532. }
  533. /**
  534. * Ends a transaction and commits the changes (SQL COMMIT)
  535. * \param _h database handle
  536. * \return 0 on success, negative on failure
  537. */
  538. int db_mysql_end_transaction(db1_con_t* _h)
  539. {
  540. str commit_query_str = str_init("COMMIT");
  541. str set_query_str = str_init("SET autocommit=1");
  542. if (!_h) {
  543. LM_ERR("invalid parameter value\n");
  544. return -1;
  545. }
  546. if (CON_TRANSACTION(_h) == 0) {
  547. LM_ERR("transaction not in progress\n");
  548. return -1;
  549. }
  550. if (db_mysql_raw_query(_h, &commit_query_str, NULL) < 0)
  551. {
  552. LM_ERR("executing raw_query\n");
  553. return -1;
  554. }
  555. if (db_mysql_raw_query(_h, &set_query_str, NULL) < 0)
  556. {
  557. LM_ERR("executing raw_query\n");
  558. return -1;
  559. }
  560. /* Only _end_ the transaction after the raw_query. That way, if the
  561. raw_query fails, and the calling module does an abort_transaction()
  562. to clean-up, a ROLLBACK will be sent to the DB. */
  563. CON_TRANSACTION(_h) = 0;
  564. if(db_mysql_unlock_tables(_h)<0)
  565. return -1;
  566. return 0;
  567. }
  568. /**
  569. * Ends a transaction and rollsback the changes (SQL ROLLBACK)
  570. * \param _h database handle
  571. * \return 1 if there was something to rollback, 0 if not, negative on failure
  572. */
  573. int db_mysql_abort_transaction(db1_con_t* _h)
  574. {
  575. str rollback_query_str = str_init("ROLLBACK");
  576. str set_query_str = str_init("SET autocommit=1");
  577. int ret;
  578. if (!_h) {
  579. LM_ERR("invalid parameter value\n");
  580. return -1;
  581. }
  582. if (CON_TRANSACTION(_h) == 0) {
  583. LM_DBG("nothing to rollback\n");
  584. ret = 0;
  585. goto done;
  586. }
  587. /* Whether the rollback succeeds or not we need to _end_ the
  588. transaction now or all future starts will fail */
  589. CON_TRANSACTION(_h) = 0;
  590. if (db_mysql_raw_query(_h, &rollback_query_str, NULL) < 0)
  591. {
  592. LM_ERR("executing raw_query\n");
  593. ret = -1;
  594. goto done;
  595. }
  596. if (db_mysql_raw_query(_h, &set_query_str, NULL) < 0)
  597. {
  598. LM_ERR("executing raw_query\n");
  599. ret = -1;
  600. goto done;
  601. }
  602. ret = 1;
  603. done:
  604. db_mysql_unlock_tables(_h);
  605. return ret;
  606. }
  607. /**
  608. * Insert a row into a specified table, update on duplicate key.
  609. * \param _h structure representing database connection
  610. * \param _k key names
  611. * \param _v values of the keys
  612. * \param _n number of key=value pairs
  613. */
  614. int db_mysql_insert_update(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
  615. const int _n)
  616. {
  617. int off, ret;
  618. static str sql_str;
  619. if ((!_h) || (!_k) || (!_v) || (!_n)) {
  620. LM_ERR("invalid parameter value\n");
  621. return -1;
  622. }
  623. ret = snprintf(mysql_sql_buf, sql_buffer_size, "insert into %.*s (", CON_TABLE(_h)->len, CON_TABLE(_h)->s);
  624. if (ret < 0 || ret >= sql_buffer_size) goto error;
  625. off = ret;
  626. ret = db_print_columns(mysql_sql_buf + off, sql_buffer_size - off, _k, _n);
  627. if (ret < 0) return -1;
  628. off += ret;
  629. ret = snprintf(mysql_sql_buf + off, sql_buffer_size - off, ") values (");
  630. if (ret < 0 || ret >= (sql_buffer_size - off)) goto error;
  631. off += ret;
  632. ret = db_print_values(_h, mysql_sql_buf + off, sql_buffer_size - off, _v, _n, db_mysql_val2str);
  633. if (ret < 0) return -1;
  634. off += ret;
  635. *(mysql_sql_buf + off++) = ')';
  636. ret = snprintf(mysql_sql_buf + off, sql_buffer_size - off, " on duplicate key update ");
  637. if (ret < 0 || ret >= (sql_buffer_size - off)) goto error;
  638. off += ret;
  639. ret = db_print_set(_h, mysql_sql_buf + off, sql_buffer_size - off, _k, _v, _n, db_mysql_val2str);
  640. if (ret < 0) return -1;
  641. off += ret;
  642. sql_str.s = mysql_sql_buf;
  643. sql_str.len = off;
  644. if (db_mysql_submit_query(_h, &sql_str) < 0) {
  645. LM_ERR("error while submitting query\n");
  646. return -2;
  647. }
  648. return 0;
  649. error:
  650. LM_ERR("error while preparing insert_update operation\n");
  651. return -1;
  652. }
  653. /**
  654. * Insert delayed a row into a specified table.
  655. * \param _h structure representing database connection
  656. * \param _k key names
  657. * \param _v values of the keys
  658. * \param _n number of key=value pairs
  659. * \return zero on success, negative value on failure
  660. */
  661. int db_mysql_insert_delayed(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n)
  662. {
  663. return db_do_insert_delayed(_h, _k, _v, _n, db_mysql_val2str,
  664. db_mysql_submit_query);
  665. }
  666. /**
  667. * Store the name of table that will be used by subsequent database functions
  668. * \param _h database handle
  669. * \param _t table name
  670. * \return zero on success, negative value on failure
  671. */
  672. int db_mysql_use_table(db1_con_t* _h, const str* _t)
  673. {
  674. return db_use_table(_h, _t);
  675. }
  676. /**
  677. * Allocate a buffer for database module
  678. * No function should be called before this
  679. * \return zero on success, negative value on failure
  680. */
  681. int db_mysql_alloc_buffer(void)
  682. {
  683. if (db_api_init())
  684. {
  685. LM_ERR("Failed to initialise db api\n");
  686. return -1;
  687. }
  688. mysql_sql_buf = (char*)malloc(sql_buffer_size);
  689. if (mysql_sql_buf == NULL)
  690. return -1;
  691. else
  692. return 0;
  693. }