dbase.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * $Id$
  3. *
  4. * Oracle module core functions
  5. *
  6. * Copyright (C) 2007,2008 TRUNK MOBILE
  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. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <time.h>
  28. #include <oci.h>
  29. #include "../../mem/mem.h"
  30. #include "../../dprint.h"
  31. #include "../../lib/srdb1/db_pool.h"
  32. #include "../../lib/srdb1/db_ut.h"
  33. #include "../../lib/srdb1/db_res.h"
  34. #include "../../lib/srdb1/db_query.h"
  35. #include "val.h"
  36. #include "ora_con.h"
  37. #include "res.h"
  38. #include "asynch.h"
  39. #include "dbase.h"
  40. #define MAX_BIND_HANDLES 128
  41. char st_buf[STATIC_BUF_LEN];
  42. /*
  43. * Make error message. Always return negative value
  44. */
  45. int sql_buf_small(void)
  46. {
  47. LM_ERR("static buffer too small\n");
  48. return -11;
  49. }
  50. /*
  51. * Decode error
  52. */
  53. static char errbuf[512];
  54. static const char* db_oracle_errorinfo(ora_con_t* con)
  55. {
  56. sword errcd;
  57. if (OCIErrorGet(con->errhp, 1, NULL, &errcd,
  58. (OraText*)errbuf, sizeof(errbuf),
  59. OCI_HTYPE_ERROR) != OCI_SUCCESS) errbuf[0] = '\0';
  60. else switch (errcd) {
  61. case 28: /* your session has been killed */
  62. case 30: /* session ID does not exists */
  63. case 31: /* session marked for kill */
  64. case 41: /* active time limit exceeded session terminated */
  65. case 107: /* failed to connect to oracle listener */
  66. case 115: /* connection refused; dispatcher table is full */
  67. case 1033: /* init/shutdown in progress */
  68. case 1034: /* not available (startup) */
  69. case 1089: /* server shutdown */
  70. case 1090: /* shutdown wait after command */
  71. case 1092: /* oracle instance terminated. Disconnection forced */
  72. case 1573: /* shutdown instance, no futher change allowed */
  73. case 2049: /* timeout: distributed transaction waiting lock */
  74. case 3113: /* EOF on communication channel */
  75. case 3114: /* not connected */
  76. case 3135: /* lost connection */
  77. case 6033: /* connect failed, partner rejected connection */
  78. case 6034: /* connect failed, partner exited unexpectedly */
  79. case 6037: /* connect failed, node unrecheable */
  80. case 6039: /* connect failed */
  81. case 6042: /* msgrcv failure (DNT) */
  82. case 6043: /* msgsend failure (DNT) */
  83. case 6107: /* network server not found */
  84. case 6108: /* connect to host failed */
  85. case 6109: /* msgrcv failure (TCP) */
  86. case 6110: /* msgsend failure (TCP) */
  87. case 6114: /* SID lookup failure */
  88. case 6124: /* TCP timeout */
  89. case 6135: /* connect rejected; server is stopping (TCP) */
  90. case 6144: /* SID unavaliable (TCP) */
  91. case 6413: /* connection not open */
  92. case 12150: /* tns can't send data, probably disconnect */
  93. case 12152: /* tns unable to send break message */
  94. case 12153: /* tns not connected */
  95. case 12161: /* tns internal error */
  96. case 12170: /* tns connect timeout */
  97. case 12224: /* tns no listener */
  98. case 12225: /* tns destination host unrecheable */
  99. case 12230: /* tns network error */
  100. case 12525: /* tns (internal) timeout */
  101. case 12521: /* tns can't resolve db name */
  102. case 12537: /* tns connection cloed */
  103. case 12541: /* tns not running */
  104. case 12543: /* tns destination host unrecheable */
  105. case 12547: /* tns lost contact */
  106. case 12560: /* tns protocol(transport) error */
  107. case 12561: /* tns unknown error */
  108. case 12608: /* tns send timeount */
  109. case 12609: /* tns receive timeount */
  110. LM_ERR("conneciom dropped\n");
  111. db_oracle_disconnect(con);
  112. default:
  113. break;
  114. }
  115. return errbuf;
  116. }
  117. const char* db_oracle_error(ora_con_t* con, sword status)
  118. {
  119. switch (status) {
  120. case OCI_SUCCESS:
  121. return "internal (success)";
  122. case OCI_SUCCESS_WITH_INFO:
  123. case OCI_ERROR:
  124. return db_oracle_errorinfo(con);
  125. case OCI_NEED_DATA:
  126. return "need data";
  127. case OCI_NO_DATA:
  128. return "no data";
  129. case OCI_INVALID_HANDLE:
  130. return "invalid handle";
  131. case OCI_STILL_EXECUTING: // ORA-3123
  132. return "executing (logic)";
  133. case OCI_CONTINUE:
  134. return "continue (library)";
  135. default:
  136. snprintf(errbuf, sizeof(errbuf),
  137. "unknown status %u", status);
  138. return errbuf;
  139. }
  140. }
  141. /*
  142. * Initialize database module
  143. * No function should be called before this
  144. */
  145. db1_con_t* db_oracle_init(const str* _url)
  146. {
  147. return db_do_init(_url, (void *)db_oracle_new_connection);
  148. }
  149. /*
  150. * Shut down database module
  151. * No function should be called after this
  152. */
  153. void db_oracle_close(db1_con_t* _h)
  154. {
  155. db_do_close(_h, db_oracle_free_connection);
  156. }
  157. /*
  158. * Release a result set from memory
  159. */
  160. int db_oracle_free_result(db1_con_t* _h, db1_res_t* _r)
  161. {
  162. if (!_h || !_r) {
  163. LM_ERR("invalid parameter value\n");
  164. return -1;
  165. }
  166. if (db_free_result(_r) < 0)
  167. {
  168. LM_ERR("failed to free result structure\n");
  169. return -1;
  170. }
  171. return 0;
  172. }
  173. /*
  174. * Send an SQL query to the server
  175. */
  176. static int db_oracle_submit_query(const db1_con_t* _h, const str* _s)
  177. {
  178. OCIBind* bind[MAX_BIND_HANDLES];
  179. OCIDate odt[sizeof(bind)/sizeof(bind[0])];
  180. str tmps;
  181. sword status;
  182. int pass;
  183. ora_con_t* con = CON_ORA(_h);
  184. query_data_t* pqd = con->pqdata;
  185. size_t hc = pqd->_n + pqd->_nw;
  186. OCIStmt *stmthp;
  187. if (hc >= sizeof(bind)/sizeof(bind[0])) {
  188. LM_ERR("too many bound. Rebuild with MAX_BIND_HANDLES >= %u\n",
  189. (unsigned)hc);
  190. return -1;
  191. }
  192. if (!pqd->_rs) {
  193. /*
  194. * This method is at ~25% faster as set OCI_COMMIT_ON_SUCCESS
  195. * in StmtExecute
  196. */
  197. tmps.len = snprintf(st_buf, sizeof(st_buf),
  198. "begin %.*s; commit write batch nowait; end;",
  199. _s->len, _s->s);
  200. if ((unsigned)tmps.len >= sizeof(st_buf))
  201. return sql_buf_small();
  202. tmps.s = st_buf;
  203. _s = &tmps;
  204. }
  205. pass = 1;
  206. if (!con->connected) {
  207. status = db_oracle_reconnect(con);
  208. if (status != OCI_SUCCESS) {
  209. LM_ERR("can't restore connection: %s\n", db_oracle_error(con, status));
  210. return -2;
  211. }
  212. LM_INFO("connection restored\n");
  213. --pass;
  214. }
  215. repeat:
  216. stmthp = NULL;
  217. status = OCIHandleAlloc(con->envhp, (dvoid**)(dvoid*)&stmthp,
  218. OCI_HTYPE_STMT, 0, NULL);
  219. if (status != OCI_SUCCESS)
  220. goto ora_err;
  221. status = OCIStmtPrepare(stmthp, con->errhp, (text*)_s->s, _s->len,
  222. OCI_NTV_SYNTAX, OCI_DEFAULT);
  223. if (status != OCI_SUCCESS)
  224. goto ora_err;
  225. if (hc) {
  226. bmap_t bmap;
  227. size_t pos = 1;
  228. int i;
  229. memset(bind, 0, hc*sizeof(bind[0]));
  230. for (i = 0; i < pqd->_n; i++) {
  231. if (db_oracle_val2bind(&bmap, &pqd->_v[i], &odt[pos]) < 0)
  232. goto bind_err;
  233. status = OCIBindByPos(stmthp, &bind[pos], con->errhp,
  234. pos, bmap.addr, bmap.size, bmap.type,
  235. NULL, NULL, NULL, 0, NULL, OCI_DEFAULT);
  236. if (status != OCI_SUCCESS)
  237. goto ora_err;
  238. ++pos;
  239. }
  240. for (i = 0; i < pqd->_nw; i++) {
  241. if (db_oracle_val2bind(&bmap, &pqd->_w[i], &odt[pos]) < 0) {
  242. bind_err:
  243. OCIHandleFree(stmthp, OCI_HTYPE_STMT);
  244. LM_ERR("can't map values\n");
  245. return -3;
  246. }
  247. status = OCIBindByPos(stmthp, &bind[pos], con->errhp,
  248. pos, bmap.addr, bmap.size, bmap.type,
  249. NULL, NULL, NULL, 0, NULL, OCI_DEFAULT);
  250. if (status != OCI_SUCCESS)
  251. goto ora_err;
  252. ++pos;
  253. }
  254. }
  255. // timelimited operation
  256. status = begin_timelimit(con, 0);
  257. if (status != OCI_SUCCESS) goto ora_err;
  258. do status = OCIStmtExecute(con->svchp, stmthp, con->errhp,
  259. !pqd->_rs, 0, NULL, NULL,
  260. pqd->_rs ? OCI_STMT_SCROLLABLE_READONLY : OCI_DEFAULT);
  261. while (wait_timelimit(con, status));
  262. if (done_timelimit(con, status)) goto stop_exec;
  263. switch (status) {
  264. case OCI_SUCCESS_WITH_INFO:
  265. LM_WARN("driver: %s\n", db_oracle_errorinfo(con));
  266. //PASS THRU
  267. case OCI_SUCCESS:
  268. if (pqd->_rs)
  269. *pqd->_rs = stmthp;
  270. else
  271. OCIHandleFree(stmthp, OCI_HTYPE_STMT);
  272. return 0;
  273. default:
  274. pass = -pass;
  275. break;
  276. }
  277. ora_err:
  278. LM_ERR("driver: %s\n", db_oracle_error(con, status));
  279. stop_exec:
  280. if (stmthp)
  281. OCIHandleFree(stmthp, OCI_HTYPE_STMT);
  282. if (pass == -1 && !con->connected) {
  283. /* Attemtp to reconnect */
  284. if (db_oracle_reconnect(con) == OCI_SUCCESS) {
  285. LM_NOTICE("attempt repeat after reconnect\n");
  286. pass = 0;
  287. goto repeat;
  288. }
  289. LM_ERR("connection loss\n");
  290. }
  291. return -4;
  292. }
  293. /*
  294. * Query table for specified rows
  295. * _h: structure representing database connection
  296. * _k: key names
  297. * _op: operators
  298. * _v: values of the keys that must match
  299. * _c: column names to return
  300. * _n: number of key=values pairs to compare
  301. * _nc: number of columns to return
  302. * _o: order by the specified column
  303. */
  304. int db_oracle_query(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _op,
  305. const db_val_t* _v, const db_key_t* _c, int _n, int _nc,
  306. const db_key_t _o, db1_res_t** _r)
  307. {
  308. query_data_t cb;
  309. OCIStmt* reshp;
  310. int rc;
  311. if (!_h || !CON_TABLE(_h) || !_r) {
  312. LM_ERR("invalid parameter value\n");
  313. return -1;
  314. }
  315. cb._rs = &reshp;
  316. cb._v = _v;
  317. cb._n = _n;
  318. cb._w = NULL;
  319. cb._nw = 0;
  320. CON_ORA(_h)->pqdata = &cb;
  321. CON_ORA(_h)->bindpos = 0;
  322. rc = db_do_query(_h, _k, _op, _v, _c, _n, _nc, _o, _r,
  323. db_oracle_val2str, db_oracle_submit_query, db_oracle_store_result);
  324. CON_ORA(_h)->pqdata = NULL; /* paranoid for next call */
  325. return rc;
  326. }
  327. /*
  328. * Execute a raw SQL query
  329. */
  330. int db_oracle_raw_query(const db1_con_t* _h, const str* _s, db1_res_t** _r)
  331. {
  332. query_data_t cb;
  333. OCIStmt* reshp;
  334. int len;
  335. const char *p;
  336. if (!_h || !_s || !_s->s) {
  337. badparam:
  338. LM_ERR("invalid parameter value\n");
  339. return -1;
  340. }
  341. memset(&cb, 0, sizeof(cb));
  342. p = _s->s;
  343. len = _s->len;
  344. while (len && *p == ' ') ++p, --len;
  345. #define _S_DIFF(p, l, S) (l <= sizeof(S)-1 || strncasecmp(p, S, sizeof(S)-1))
  346. if (!_S_DIFF(p, len, "select ")) {
  347. if (!_r) goto badparam;
  348. cb._rs = &reshp;
  349. } else {
  350. if ( _S_DIFF(p, len, "insert ")
  351. && _S_DIFF(p, len, "delete ")
  352. && _S_DIFF(p, len, "update "))
  353. {
  354. LM_ERR("unknown raw_query: '%.*s'\n", _s->len, _s->s);
  355. return -2;
  356. }
  357. #undef _S_DIFF
  358. if (_r) goto badparam;
  359. cb._rs = NULL;
  360. }
  361. len = db_do_raw_query(_h, _s, _r, db_oracle_submit_query, db_oracle_store_result);
  362. CON_ORA(_h)->pqdata = NULL; /* paranoid for next call */
  363. return len;
  364. }
  365. /*
  366. * Insert a row into specified table
  367. * _h: structure representing database connection
  368. * _k: key names
  369. * _v: values of the keys
  370. * _n: number of key=value pairs
  371. */
  372. int db_oracle_insert(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v,
  373. int _n)
  374. {
  375. query_data_t cb;
  376. int rc;
  377. if (!_h || !CON_TABLE(_h)) {
  378. LM_ERR("invalid parameter value\n");
  379. return -1;
  380. }
  381. cb._rs = NULL;
  382. cb._v = _v;
  383. cb._n = _n;
  384. cb._w = NULL;
  385. cb._nw = 0;
  386. CON_ORA(_h)->pqdata = &cb;
  387. CON_ORA(_h)->bindpos = 0;
  388. rc = db_do_insert(_h, _k, _v, _n, db_oracle_val2str, db_oracle_submit_query);
  389. CON_ORA(_h)->pqdata = NULL; /* paranoid for next call */
  390. return rc;
  391. }
  392. /*
  393. * Delete a row from the specified table
  394. * _h: structure representing database connection
  395. * _k: key names
  396. * _o: operators
  397. * _v: values of the keys that must match
  398. * _n: number of key=value pairs
  399. */
  400. int db_oracle_delete(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
  401. const db_val_t* _v, int _n)
  402. {
  403. query_data_t cb;
  404. int rc;
  405. if (!_h || !CON_TABLE(_h)) {
  406. LM_ERR("invalid parameter value\n");
  407. return -1;
  408. }
  409. cb._rs = NULL;
  410. cb._v = _v;
  411. cb._n = _n;
  412. cb._w = NULL;
  413. cb._nw = 0;
  414. CON_ORA(_h)->pqdata = &cb;
  415. CON_ORA(_h)->bindpos = 0;
  416. rc = db_do_delete(_h, _k, _o, _v, _n, db_oracle_val2str, db_oracle_submit_query);
  417. CON_ORA(_h)->pqdata = NULL; /* paranoid for next call */
  418. return rc;
  419. }
  420. /*
  421. * Update some rows in the specified table
  422. * _h: structure representing database connection
  423. * _k: key names
  424. * _o: operators
  425. * _v: values of the keys that must match
  426. * _uk: updated columns
  427. * _uv: updated values of the columns
  428. * _n: number of key=value pairs
  429. * _un: number of columns to update
  430. */
  431. int db_oracle_update(const db1_con_t* _h, const db_key_t* _k, const db_op_t* _o,
  432. const db_val_t* _v, const db_key_t* _uk, const db_val_t* _uv,
  433. int _n, int _un)
  434. {
  435. query_data_t cb;
  436. int rc;
  437. if (!_h || !CON_TABLE(_h)) {
  438. LM_ERR("invalid parameter value\n");
  439. return -1;
  440. }
  441. cb._rs = NULL;
  442. cb._v = _uv;
  443. cb._n = _un;
  444. cb._w = _v;
  445. cb._nw = _n;
  446. CON_ORA(_h)->pqdata = &cb;
  447. CON_ORA(_h)->bindpos = 0;
  448. rc = db_do_update(_h, _k, _o, _v, _uk, _uv, _n, _un,
  449. db_oracle_val2str, db_oracle_submit_query);
  450. CON_ORA(_h)->pqdata = NULL; /* paranoid for next call */
  451. return rc;
  452. }
  453. /*
  454. * Store name of table that will be used by
  455. * subsequent database functions
  456. */
  457. int db_oracle_use_table(db1_con_t* _h, const str* _t)
  458. {
  459. return db_use_table(_h, _t);
  460. }