bdb_api.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /* $Id$
  2. *
  3. * Copyright (C) 2006-2007 Sippy Software, Inc. <[email protected]>
  4. *
  5. * This file is part of ser, a free SIP server.
  6. *
  7. * ser is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version
  11. *
  12. * For a license to use the ser software under conditions
  13. * other than those described here, or to purchase support for this
  14. * software, please contact iptel.org by e-mail at the following addresses:
  15. * [email protected]
  16. *
  17. * ser is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. *
  26. */
  27. #include "bdb.h"
  28. int bdb_close_table(db_con_t* _h)
  29. {
  30. /* close DB */
  31. if (BDB_CON_DB(_h) == NULL) {
  32. #ifdef BDB_EXTRA_DEBUG
  33. LOG(L_NOTICE, "BDB:bdb_close_table: no need to close\n");
  34. #endif
  35. return 0;
  36. #ifdef BDB_EXTRA_DEBUG
  37. } else {
  38. LOG(L_NOTICE, "BDB:bdb_close_table: '%s'\n", CON_TABLE(_h));
  39. #endif
  40. };
  41. BDB_CON_DB(_h)->close(BDB_CON_DB(_h), 0);
  42. CON_TABLE(_h) = NULL;
  43. BDB_CON_DB(_h) = NULL;
  44. return 0;
  45. };
  46. int bdb_open_table(db_con_t* _h, const char* _t)
  47. {
  48. int ret;
  49. bdb_table_p t;
  50. if ((t = bdb_find_table(_t)) == NULL) {
  51. #ifdef BDB_EXTRA_DEBUG
  52. LOG(L_ERR, "BDB:bdb_open_table: table: '%s' has not been described\n", _t);
  53. #endif
  54. return -1;
  55. }
  56. BDB_CON_COL_NUM(_h) = t->col_num;
  57. CON_TABLE(_h) = t->name.s;
  58. #ifdef BDB_EXTRA_DEBUG
  59. LOG(L_NOTICE, "BDB:bdb_open_table: '%s'\n", CON_TABLE(_h));
  60. #endif
  61. ret = db_create(&BDB_CON_DB(_h), BDB_CON_DBENV(_h), 0);
  62. if (ret != 0) {
  63. LOG(L_ERR, "BDB:bdb_open_table: unable to db_create(): %s\n", db_strerror(ret));
  64. return -1;
  65. };
  66. ret = BDB_CON_DB(_h)->set_flags(BDB_CON_DB(_h), DB_DUP);
  67. if (ret != 0) {
  68. LOG(L_ERR, "BDB:bdb_open_table: unable to set_flags(): %s\n", db_strerror(ret));
  69. return -1;
  70. }
  71. ret = BDB_CON_DB(_h)->open(BDB_CON_DB(_h), NULL, CON_TABLE(_h), NULL, DB_BTREE, DB_CREATE, 0);
  72. if (ret != 0) {
  73. LOG(L_ERR, "BDB:bdb_open_table: unable to open database '%s': %s\n", CON_TABLE(_h), db_strerror(ret));
  74. return -1;
  75. };
  76. return 0;
  77. };
  78. int bdb_use_table(db_con_t* _h, const char* _t)
  79. {
  80. int ret;
  81. if ((!_h) || (!_t)) {
  82. #ifdef BDB_EXTRA_DEBUG
  83. LOG(L_ERR, "BDB:bdb_use_table: Invalid parameter value\n");
  84. #endif
  85. return -1;
  86. }
  87. if (CON_TABLE(_h) != NULL && !strcmp(CON_TABLE(_h), _t)) {
  88. #ifdef BDB_EXTRA_DEBUG
  89. LOG(L_NOTICE, "BDB:bdb_use_table: table '%s' has been already opened\n", _t);
  90. #endif
  91. return 0;
  92. }
  93. /* close table if one was already opened */
  94. if (CON_TABLE(_h) != NULL) {
  95. bdb_close_table(_h);
  96. }
  97. ret = bdb_open_table(_h, _t);
  98. return ret;
  99. }
  100. int bdb_describe_table(modparam_t type, void* val)
  101. {
  102. char *s, *p;
  103. bdb_table_p t;
  104. bdb_column_p c;
  105. #ifdef BDB_EXTRA_DEBUG
  106. LOG(L_NOTICE, "BDB:bdb_describe_table: input string: '%s'\n", (char*)val);
  107. #endif
  108. s = (char*) val;
  109. p = strchr(s, ':');
  110. *p = 0;
  111. if (bdb_find_table(s) != NULL) {
  112. #ifdef BDB_EXTRA_DEBUG
  113. LOG(L_ERR, "BDB:bdb_describe_table: table: '%s' already has been described\n", s);
  114. #endif
  115. return -1;
  116. }
  117. t = pkg_malloc(sizeof(*t));
  118. memset(t, 0, sizeof(*t));
  119. t->name.s = pkg_malloc(strlen(s) + 1);
  120. memcpy(t->name.s, s, strlen(s) + 1);
  121. t->name.len = strlen(s) + 1;
  122. #ifdef BDB_EXTRA_DEBUG
  123. LOG(L_NOTICE, "BDB:bdb_describe_table: table: '%.*s'\n", t->name.len, t->name.s);
  124. #endif
  125. bdb_push_table(t);
  126. s = p + 1;
  127. while ((p = strchr(s, '(')) != NULL) {
  128. *p = 0;
  129. if (bdb_find_column(t, s) != NULL) {
  130. #ifdef BDB_EXTRA_DEBUG
  131. LOG(L_ERR, "BDB:bdb_describe_table: table: '%.*s': dublicated column: '%s', \n",
  132. t->name.len, t->name.s, s);
  133. #endif
  134. return -1;
  135. }
  136. c = pkg_malloc(sizeof(*c));
  137. memset(c, 0, sizeof(*c));
  138. c->name.s = pkg_malloc(strlen(s) + 1);
  139. memcpy(c->name.s, s, strlen(s) + 1);
  140. c->name.len = strlen(s) + 1;
  141. #ifdef BDB_EXTRA_DEBUG
  142. LOG(L_NOTICE, "BDB:bdb_describe_table: column '%.*s'", c->name.len, c->name.s);
  143. #endif
  144. bdb_push_column(t, c);
  145. t->col_num++;
  146. s = ++p;
  147. p = strchr(s, ')');
  148. *p = 0;
  149. #ifdef BDB_EXTRA_DEBUG
  150. LOG(L_NOTICE, ", type: '%s'\n", s);
  151. #endif
  152. if (!strncmp("int", s, strlen(s))) {
  153. c->type = DB_INT;
  154. } else if (!strncmp("float", s, strlen(s))) {
  155. c->type = DB_FLOAT;
  156. } else if (!strncmp("double", s, strlen(s))) {
  157. c->type = DB_DOUBLE;
  158. } else if (!strncmp("string", s, strlen(s))) {
  159. c->type = DB_STRING;
  160. } else if (!strncmp("str", s, strlen(s))) {
  161. c->type = DB_STR;
  162. } else if (!strncmp("datetime", s, strlen(s))) {
  163. c->type = DB_DATETIME;
  164. } else if (!strncmp("blob", s, strlen(s))) {
  165. c->type = DB_BLOB;
  166. } else if (!strncmp("bitmap", s, strlen(s))) {
  167. c->type = DB_BITMAP;
  168. } else {
  169. #ifdef BDB_EXTRA_DEBUG
  170. LOG(L_ERR, "BDB:bdb_describe_table: bad column type: '%s'\n", s);
  171. #endif
  172. return -1;
  173. }
  174. s = ++p;
  175. if ((p = strchr(s, ' ')) != NULL)
  176. s++;
  177. }
  178. return 0;
  179. };
  180. void bdb_push_table(bdb_table_p _t)
  181. {
  182. bdb_table_p t;
  183. if (bdb_tables == NULL) {
  184. bdb_tables = _t;
  185. return;
  186. }
  187. t = bdb_tables;
  188. while (t->next != NULL) {
  189. t = t->next;
  190. }
  191. t->next = _t;
  192. };
  193. void bdb_free_table(bdb_table_p _t)
  194. {
  195. if (_t->name.s) {
  196. pkg_free(_t->name.s);
  197. }
  198. if (_t->cols != NULL) {
  199. bdb_free_column_list(_t->cols);
  200. }
  201. pkg_free(_t);
  202. };
  203. void bdb_free_table_list(bdb_table_p _t)
  204. {
  205. bdb_table_p curr, next;
  206. #ifdef BDB_EXTRA_DEBUG
  207. LOG(L_NOTICE, "BDB:bdb_free_table_list\n");
  208. #endif
  209. for (curr = _t; curr != NULL;) {
  210. next = curr->next;
  211. bdb_free_table(curr);
  212. curr = next;
  213. }
  214. };
  215. bdb_table_p bdb_find_table(const char* _t)
  216. {
  217. bdb_table_p t;
  218. for (t = bdb_tables; t != NULL; t = t->next) {
  219. #ifdef BDB_EXTRA_DEBUG
  220. LOG(L_NOTICE, "BDB:bdb_find_table: search for '%s', found '%s'\n", _t, t->name.s);
  221. #endif
  222. if (!strcmp(_t, t->name.s))
  223. return t;
  224. }
  225. return NULL;
  226. };
  227. void bdb_free_column(bdb_column_p _c)
  228. {
  229. if (_c->name.s) {
  230. pkg_free(_c->name.s);
  231. }
  232. pkg_free(_c);
  233. };
  234. void bdb_free_column_list(bdb_column_p _c)
  235. {
  236. bdb_column_p curr, next;
  237. for (curr = _c; curr != NULL;) {
  238. next = curr->next;
  239. bdb_free_column(curr);
  240. curr = next;
  241. }
  242. };
  243. bdb_column_p bdb_find_column(bdb_table_p _t, const char* _c)
  244. {
  245. bdb_column_p c;
  246. for (c = _t->cols; c != NULL; c = c->next) {
  247. if (!strcmp(_c, c->name.s))
  248. return c;
  249. }
  250. return NULL;
  251. };
  252. void bdb_push_column(bdb_table_p _t, bdb_column_p _c)
  253. {
  254. bdb_column_p c;
  255. if (_t->cols == NULL) {
  256. _t->cols = _c;
  257. return;
  258. }
  259. c = _t->cols;
  260. while (c->next != NULL) {
  261. c = c->next;
  262. }
  263. c->next = _c;
  264. };
  265. int bdb_update_table(db_con_t* _h, bdb_srow_p s_r, bdb_urow_p u_r)
  266. {
  267. DBC *cursorp;
  268. DBT key, *keyp, data;
  269. u_int32_t flags, nflags;
  270. bdb_val_p v;
  271. bdb_row_p r;
  272. int ret;
  273. if (s_r->key.size > 0) {
  274. keyp = &(s_r->key);
  275. flags = DB_SET;
  276. nflags = DB_NEXT_DUP;
  277. } else {
  278. memset(&key, 0, sizeof(DBT));
  279. keyp = &key;
  280. flags = DB_NEXT;
  281. nflags = DB_NEXT;
  282. }
  283. memset(&data, 0, sizeof(DBT));
  284. r = pkg_malloc(sizeof(*r));
  285. memset(r, 0, sizeof(*r));
  286. BDB_CON_DB(_h)->cursor(BDB_CON_DB(_h), NULL, &cursorp, DB_WRITECURSOR);
  287. ret = cursorp->c_get(cursorp, keyp, &data, flags);
  288. while (ret == 0) {
  289. if ((ret = bdb_get_db_row(_h, &data, &v)) < 0) {
  290. if (cursorp != NULL)
  291. cursorp->c_close(cursorp);
  292. bdb_free_row(r);
  293. return -1;
  294. };
  295. /* row content now in v */
  296. ret = bdb_row_match(_h, v, s_r);
  297. if (ret < 0) {
  298. if (cursorp != NULL)
  299. cursorp->c_close(cursorp);
  300. bdb_free_row(r);
  301. return -1;
  302. } else if (ret) { /* match */
  303. if (bdb_set_row(_h, u_r, v, r) < 0) {
  304. if (cursorp != NULL)
  305. cursorp->c_close(cursorp);
  306. bdb_free_row(r);
  307. return -1;
  308. };
  309. ret = cursorp->c_put(cursorp, keyp, &(r->data), DB_CURRENT);
  310. if (ret != 0) {
  311. LOG(L_ERR, "BDB:bdb_update_table: c_put(): %s\n", db_strerror(ret));
  312. if (cursorp != NULL)
  313. cursorp->c_close(cursorp);
  314. bdb_free_row(r);
  315. return -1;
  316. }
  317. if (r->data.data != NULL) {
  318. pkg_free(r->data.data);
  319. }
  320. if (r->tail.s != NULL) {
  321. pkg_free(r->tail.s);
  322. }
  323. if (r->fields != NULL) {
  324. bdb_free_field_list(r->fields);
  325. }
  326. memset(r, 0, sizeof(*r));
  327. #ifdef BDB_EXTRA_DEBUG
  328. } else {
  329. LOG(L_NOTICE, "BDB:bdb_update_table: does not match\n");
  330. #endif
  331. };
  332. ret = cursorp->c_get(cursorp, keyp, &data, nflags);
  333. }
  334. if (ret != DB_NOTFOUND) {
  335. LOG(L_ERR, "BDB:bdb_update_table: %s\n", db_strerror(ret));
  336. if (cursorp != NULL)
  337. cursorp->c_close(cursorp);
  338. bdb_free_row(r);
  339. return -1;
  340. }
  341. if (cursorp != NULL)
  342. cursorp->c_close(cursorp);
  343. bdb_free_row(r);
  344. return 0;
  345. };
  346. int bdb_query_table(db_con_t* _h, bdb_srow_p s_r, bdb_rrow_p r_r, int _n, db_res_t** _r)
  347. {
  348. bdb_table_p t;
  349. bdb_column_p c;
  350. int i, j;
  351. DBC *cursorp;
  352. DBT key, *keyp, data;
  353. u_int32_t flags, nflags;
  354. bdb_val_p v;
  355. db_res_t *res;
  356. int ret;
  357. if (s_r->key.size > 0) {
  358. keyp = &(s_r->key);
  359. flags = DB_SET;
  360. nflags = DB_NEXT_DUP;
  361. } else {
  362. memset(&key, 0, sizeof(DBT));
  363. keyp = &key;
  364. flags = DB_NEXT;
  365. nflags = DB_NEXT;
  366. }
  367. memset(&data, 0, sizeof(DBT));
  368. /* prepare result */
  369. res = pkg_malloc(sizeof(*res));
  370. memset(res, 0, sizeof(*res));
  371. *_r = res;
  372. res->col.n = (_n == 0) ? BDB_CON_COL_NUM(_h) : _n;
  373. t = bdb_find_table(CON_TABLE(_h));
  374. if (_n == 0) { /* return all columns */
  375. res->col.names = pkg_malloc(sizeof(db_key_t) * t->col_num);
  376. res->col.types = pkg_malloc(sizeof(db_type_t) * t->col_num);
  377. for (c = t->cols, i = 0; c != NULL; c = c->next, i++) {
  378. res->col.names[i] = pkg_malloc(c->name.len);
  379. memcpy((void *)res->col.names[i], (void *)c->name.s, c->name.len);
  380. res->col.types[i] = c->type;
  381. }
  382. } else {
  383. res->col.names = pkg_malloc(sizeof(db_key_t) * _n);
  384. res->col.types = pkg_malloc(sizeof(db_type_t) * _n);
  385. for (i = 0; i < _n; i++) {
  386. for (c = t->cols, j = 0; j < r_r[i]; c = c->next, j++);
  387. res->col.names[i] = pkg_malloc(c->name.len);
  388. memcpy((void *)res->col.names[i], (void *)c->name.s, c->name.len);
  389. res->col.types[i] = c->type;
  390. }
  391. }
  392. BDB_CON_DB(_h)->cursor(BDB_CON_DB(_h), NULL, &cursorp, 0);
  393. ret = cursorp->c_get(cursorp, keyp, &data, flags);
  394. while (ret == 0) {
  395. if ((ret = bdb_get_db_row(_h, &data, &v)) < 0) {
  396. if (cursorp != NULL)
  397. cursorp->c_close(cursorp);
  398. bdb_free_result(_h, *_r);
  399. return -1;
  400. };
  401. /* row content now in v */
  402. ret = bdb_row_match(_h, v, s_r);
  403. if (ret < 0) {
  404. if (cursorp != NULL)
  405. cursorp->c_close(cursorp);
  406. bdb_free_result(_h, *_r);
  407. return -1;
  408. } else if (ret) { /* match */
  409. if (bdb_push_res_row(_h, _r, r_r, _n, v) < 0) {
  410. if (cursorp != NULL)
  411. cursorp->c_close(cursorp);
  412. bdb_free_result(_h, *_r);
  413. return -1;
  414. };
  415. #ifdef BDB_EXTRA_DEBUG
  416. } else {
  417. LOG(L_NOTICE, "BDB:bdb_query_table: does not match\n");
  418. #endif
  419. };
  420. ret = cursorp->c_get(cursorp, keyp, &data, nflags);
  421. }
  422. if (ret != DB_NOTFOUND) {
  423. LOG(L_ERR, "BDB:bdb_query_table: %s\n", db_strerror(ret));
  424. if (cursorp != NULL)
  425. cursorp->c_close(cursorp);
  426. bdb_free_result(_h, *_r);
  427. return -1;
  428. }
  429. if (cursorp != NULL)
  430. cursorp->c_close(cursorp);
  431. return 0;
  432. }
  433. int bdb_row_match(db_con_t* _h, bdb_val_p _v, bdb_srow_p s_r)
  434. {
  435. bdb_sval_p s_v;
  436. db_val_t *v, *v2;
  437. int op, l;
  438. s_v = s_r->fields;
  439. while (s_v != NULL) {
  440. v = &(s_v->v); /* row field value*/
  441. v2 = &(_v[s_v->c_idx].v); /* compared value */
  442. op = s_v->op; /* expression operator */
  443. if (VAL_TYPE(v) != VAL_TYPE(v2)) {
  444. LOG(L_ERR, "BDB:bdb_row_match: types mismatch: %d vs %d\n", VAL_TYPE(v), VAL_TYPE(v2));
  445. return -1;
  446. };
  447. if (VAL_NULL(v) && VAL_NULL(v) == VAL_NULL(v2)) {
  448. #ifdef BDB_EXTRA_DEBUG
  449. LOG(L_NOTICE, "BDB:bdb_row_match: NULL == NULL\n");
  450. #endif
  451. return 1;
  452. };
  453. if (VAL_NULL(v) != VAL_NULL(v2)) {
  454. #ifdef BDB_EXTRA_DEBUG
  455. LOG(L_NOTICE, "BDB:bdb_row_match: NULL != NULL\n");
  456. #endif
  457. return 0;
  458. };
  459. switch (VAL_TYPE(v)) {
  460. case DB_INT:
  461. #ifdef BDB_EXTRA_DEBUG
  462. LOG(L_NOTICE, "BDB:bdb_row_match: %d vs %d\n", VAL_INT(v), VAL_INT(v2));
  463. #endif
  464. switch (op) {
  465. case BDB_OP_EQ:
  466. if (VAL_INT(v) != VAL_INT(v2)) return 0;
  467. break;
  468. case BDB_OP_LT:
  469. if (VAL_INT(v) >= VAL_INT(v2)) return 0;
  470. break;
  471. case BDB_OP_GT:
  472. if (VAL_INT(v) <= VAL_INT(v2)) return 0;
  473. break;
  474. case BDB_OP_LEQ:
  475. if (VAL_INT(v) > VAL_INT(v2)) return 0;
  476. break;
  477. case BDB_OP_GEQ:
  478. if (VAL_INT(v) < VAL_INT(v2)) return 0;
  479. break;
  480. }
  481. break;
  482. case DB_FLOAT:
  483. #ifdef BDB_EXTRA_DEBUG
  484. LOG(L_NOTICE, "BDB:bdb_row_match: %f vs %f\n", VAL_FLOAT(v), VAL_FLOAT(v2));
  485. #endif
  486. switch (op) {
  487. case BDB_OP_EQ:
  488. if (VAL_FLOAT(v) != VAL_FLOAT(v2)) return 0;
  489. break;
  490. case BDB_OP_LT:
  491. if (VAL_FLOAT(v) >= VAL_FLOAT(v2)) return 0;
  492. break;
  493. case BDB_OP_GT:
  494. if (VAL_FLOAT(v) <= VAL_FLOAT(v2)) return 0;
  495. break;
  496. case BDB_OP_LEQ:
  497. if (VAL_FLOAT(v) > VAL_FLOAT(v2)) return 0;
  498. break;
  499. case BDB_OP_GEQ:
  500. if (VAL_FLOAT(v) < VAL_FLOAT(v2)) return 0;
  501. break;
  502. }
  503. break;
  504. case DB_DATETIME:
  505. #ifdef BDB_EXTRA_DEBUG
  506. LOG(L_NOTICE, "BDB:bdb_row_match: %d vs %d\n", VAL_TIME(v), VAL_TIME(v2));
  507. #endif
  508. switch (op) {
  509. case BDB_OP_EQ:
  510. if (VAL_TIME(v) != VAL_TIME(v2)) return 0;
  511. break;
  512. case BDB_OP_LT:
  513. if (VAL_TIME(v) >= VAL_TIME(v2)) return 0;
  514. break;
  515. case BDB_OP_GT:
  516. if (VAL_TIME(v) <= VAL_TIME(v2)) return 0;
  517. break;
  518. case BDB_OP_LEQ:
  519. if (VAL_TIME(v) > VAL_TIME(v2)) return 0;
  520. break;
  521. case BDB_OP_GEQ:
  522. if (VAL_TIME(v) < VAL_TIME(v2)) return 0;
  523. break;
  524. }
  525. break;
  526. case DB_DOUBLE:
  527. #ifdef BDB_EXTRA_DEBUG
  528. LOG(L_NOTICE, "BDB:bdb_row_match: %f vs %f\n", VAL_DOUBLE(v), VAL_DOUBLE(v2));
  529. #endif
  530. switch (op) {
  531. case BDB_OP_EQ:
  532. if (VAL_DOUBLE(v) != VAL_DOUBLE(v2)) return 0;
  533. break;
  534. case BDB_OP_LT:
  535. if (VAL_DOUBLE(v) >= VAL_DOUBLE(v2)) return 0;
  536. break;
  537. case BDB_OP_GT:
  538. if (VAL_DOUBLE(v) <= VAL_DOUBLE(v2)) return 0;
  539. break;
  540. case BDB_OP_LEQ:
  541. if (VAL_DOUBLE(v) > VAL_DOUBLE(v2)) return 0;
  542. break;
  543. case BDB_OP_GEQ:
  544. if (VAL_DOUBLE(v) < VAL_DOUBLE(v2)) return 0;
  545. break;
  546. }
  547. break;
  548. case DB_BITMAP:
  549. #ifdef BDB_EXTRA_DEBUG
  550. LOG(L_NOTICE, "BDB:bdb_row_match: %0X vs %0X\n", VAL_BITMAP(v), VAL_BITMAP(v2));
  551. #endif
  552. switch (op) {
  553. case BDB_OP_EQ:
  554. if (VAL_BITMAP(v) != VAL_BITMAP(v2)) return 0;
  555. break;
  556. case BDB_OP_LT:
  557. if (VAL_BITMAP(v) >= VAL_BITMAP(v2)) return 0;
  558. break;
  559. case BDB_OP_GT:
  560. if (VAL_BITMAP(v) <= VAL_BITMAP(v2)) return 0;
  561. break;
  562. case BDB_OP_LEQ:
  563. if (VAL_BITMAP(v) > VAL_BITMAP(v2)) return 0;
  564. break;
  565. case BDB_OP_GEQ:
  566. if (VAL_BITMAP(v) < VAL_BITMAP(v2)) return 0;
  567. break;
  568. }
  569. break;
  570. case DB_STRING:
  571. #ifdef BDB_EXTRA_DEBUG
  572. LOG(L_NOTICE, "BDB:bdb_row_match: %s vs %s\n", VAL_STRING(v), VAL_STRING(v2));
  573. #endif
  574. switch (op) {
  575. case BDB_OP_EQ:
  576. if (strcmp(VAL_STRING(v), VAL_STRING(v2))) return 0;
  577. break;
  578. case BDB_OP_LT:
  579. if (strcmp(VAL_STRING(v), VAL_STRING(v2)) >= 0) return 0;
  580. break;
  581. case BDB_OP_GT:
  582. if (strcmp(VAL_STRING(v), VAL_STRING(v2)) <= 0) return 0;
  583. break;
  584. case BDB_OP_LEQ:
  585. if (strcmp(VAL_STRING(v), VAL_STRING(v2)) > 0) return 0;
  586. break;
  587. case BDB_OP_GEQ:
  588. if (strcmp(VAL_STRING(v), VAL_STRING(v2)) < 0) return 0;
  589. break;
  590. }
  591. break;
  592. case DB_STR:
  593. #ifdef BDB_EXTRA_DEBUG
  594. LOG(L_NOTICE, "BDB:bdb_row_match: %.*s vs %.*s\n", VAL_STR(v).len, VAL_STR(v).s, VAL_STR(v2).len, VAL_STR(v2).s);
  595. #endif
  596. l = VAL_STR(v).len > VAL_STR(v2).len ? VAL_STR(v).len : VAL_STR(v2).len;
  597. switch (op) {
  598. case BDB_OP_EQ:
  599. if (strncmp(VAL_STR(v).s, VAL_STR(v2).s, l)) return 0;
  600. break;
  601. case BDB_OP_LT:
  602. if (strncmp(VAL_STR(v).s, VAL_STR(v2).s, l) >= 0) return 0;
  603. break;
  604. case BDB_OP_GT:
  605. if (strncmp(VAL_STR(v).s, VAL_STR(v2).s, l) <= 0) return 0;
  606. break;
  607. case BDB_OP_LEQ:
  608. if (strncmp(VAL_STR(v).s, VAL_STR(v2).s, l) > 0) return 0;
  609. break;
  610. case BDB_OP_GEQ:
  611. if (strncmp(VAL_STR(v).s, VAL_STR(v2).s, l) < 0) return 0;
  612. break;
  613. }
  614. break;
  615. case DB_BLOB:
  616. #ifdef BDB_EXTRA_DEBUG
  617. LOG(L_NOTICE, "BDB:bdb_row_match: %.*s (len = %d) vs %.*s (len = %d)\n", VAL_BLOB(v).len, VAL_BLOB(v).s, VAL_BLOB(v).len, VAL_BLOB(v2).len, VAL_BLOB(v2).s, VAL_BLOB(v2).len);
  618. #endif
  619. l = VAL_BLOB(v).len > VAL_BLOB(v2).len ? VAL_BLOB(v).len : VAL_BLOB(v2).len;
  620. switch (op) {
  621. case BDB_OP_EQ:
  622. if (memcmp(VAL_BLOB(v).s, VAL_BLOB(v2).s, l)) return 0;
  623. break;
  624. case BDB_OP_LT:
  625. if (memcmp(VAL_BLOB(v).s, VAL_BLOB(v2).s, l) >= 0) return 0;
  626. break;
  627. case BDB_OP_GT:
  628. if (memcmp(VAL_BLOB(v).s, VAL_BLOB(v2).s, l) <= 0) return 0;
  629. break;
  630. case BDB_OP_LEQ:
  631. if (memcmp(VAL_BLOB(v).s, VAL_BLOB(v2).s, l) > 0) return 0;
  632. break;
  633. case BDB_OP_GEQ:
  634. if (memcmp(VAL_BLOB(v).s, VAL_BLOB(v2).s, l) < 0) return 0;
  635. break;
  636. }
  637. break;
  638. default:
  639. return -1; /* is it possible here? */
  640. break;
  641. }
  642. s_v = s_v->next;
  643. }
  644. #ifdef BDB_EXTRA_DEBUG
  645. LOG(L_NOTICE, "BDB:bdb_row_match: match\n");
  646. #endif
  647. return 1; /* match */
  648. };
  649. int bdb_push_res_row(db_con_t* _h, db_res_t** _r, bdb_rrow_p _r_r, int _n, bdb_val_p _v)
  650. {
  651. db_res_t *r;
  652. db_row_t *row;
  653. db_val_t *v;
  654. int i, n;
  655. char *s;
  656. r = *_r;
  657. /*
  658. * use system malloc() to allocate memory for RES_ROWS array
  659. * due to pkg_malloc() memory pool fragmentation problem
  660. */
  661. if (RES_ROW_N(r) == 0) {
  662. if ((row = malloc(sizeof(*(RES_ROWS(r))))) == NULL) {
  663. LOG(L_ERR, "BDB:bdb_push_res_row: unable to allocate %d bytes\n",
  664. sizeof(*(RES_ROWS(r))));
  665. return -1;
  666. };
  667. } else {
  668. if ((row = realloc(RES_ROWS(r), sizeof(*(RES_ROWS(r))) * (RES_ROW_N(r) + 1))) == NULL) {
  669. LOG(L_ERR, "BDB:bdb_push_res_row: unable to reallocate %d bytes\n",
  670. sizeof(*(RES_ROWS(r))) * (RES_ROW_N(r) + 1));
  671. return -1;
  672. };
  673. }
  674. RES_ROWS(r) = row;
  675. row = &RES_ROWS(r)[RES_ROW_N(r)];
  676. RES_ROW_N(r)++;
  677. n = (_n == 0) ? BDB_CON_COL_NUM(_h) : _n;
  678. ROW_VALUES(row) = malloc(sizeof(*(ROW_VALUES(row))) * n);
  679. if (ROW_VALUES(row) == NULL) {
  680. LOG(L_ERR, "BDB:bdb_push_res_row: unable to allocate %d bytes for ROW_VALUES\n",
  681. sizeof(*(ROW_VALUES(row))) * n);
  682. return -1;
  683. }
  684. for (i = 0; i < n; i++) {
  685. v = &ROW_VALUES(row)[i];
  686. memcpy(v, &_v[_r_r[i]].v, sizeof(*v));
  687. if (VAL_TYPE(v) == DB_STRING || VAL_TYPE(v) == DB_STR || VAL_TYPE(v) == DB_BLOB) {
  688. s = malloc(VAL_STR(v).len + 1);
  689. if (s == NULL) {
  690. LOG(L_ERR, "BDB:bdb_push_res_row: unable to allocate %d bytes for VAL_STR\n",
  691. VAL_STR(v).len);
  692. free(ROW_VALUES(row));
  693. return -1;
  694. }
  695. memcpy(s, VAL_STR(v).s, VAL_STR(v).len);
  696. /* some code expect STR value to be NULL terminated */
  697. s[VAL_STR(v).len] = 0;
  698. VAL_STR(v).s = s;
  699. }
  700. }
  701. ROW_N(row) = n;
  702. return 0;
  703. };
  704. int bdb_delete_table(db_con_t* _h, bdb_srow_p s_r)
  705. {
  706. DBC *cursorp;
  707. DBT key, *keyp, data;
  708. u_int32_t flags, nflags;
  709. bdb_val_p v;
  710. int ret;
  711. if (s_r->key.size > 0) {
  712. keyp = &(s_r->key);
  713. flags = DB_SET;
  714. nflags = DB_NEXT_DUP;
  715. } else {
  716. memset(&key, 0, sizeof(DBT));
  717. keyp = &key;
  718. flags = DB_NEXT;
  719. nflags = DB_NEXT;
  720. }
  721. memset(&data, 0, sizeof(DBT));
  722. BDB_CON_DB(_h)->cursor(BDB_CON_DB(_h), NULL, &cursorp, DB_WRITECURSOR);
  723. ret = cursorp->c_get(cursorp, keyp, &data, flags);
  724. while (ret == 0) {
  725. if ((ret = bdb_get_db_row(_h, &data, &v)) < 0) {
  726. return -1;
  727. };
  728. /* row content now in v */
  729. ret = bdb_row_match(_h, v, s_r);
  730. if (ret < 0) {
  731. if (cursorp != NULL)
  732. cursorp->c_close(cursorp);
  733. return -1;
  734. } else if (ret) { /* match */
  735. ret = cursorp->c_del(cursorp, 0);
  736. if (ret != 0) {
  737. LOG(L_ERR, "BDB:bdb_delete_table: c_del(): %s\n", db_strerror(ret));
  738. if (cursorp != NULL)
  739. cursorp->c_close(cursorp);
  740. return -1;
  741. }
  742. #ifdef BDB_EXTRA_DEBUG
  743. } else {
  744. LOG(L_NOTICE, "BDB:bdb_delete_table: does not match\n");
  745. #endif
  746. };
  747. ret = cursorp->c_get(cursorp, keyp, &data, nflags);
  748. }
  749. if (ret != DB_NOTFOUND) {
  750. if (cursorp != NULL)
  751. cursorp->c_close(cursorp);
  752. LOG(L_ERR, "BDB:bdb_delete_table: %s\n", db_strerror(ret));
  753. return -1;
  754. }
  755. if (cursorp != NULL)
  756. cursorp->c_close(cursorp);
  757. return 0;
  758. }