dbt_res.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /*
  2. * $Id$
  3. *
  4. * DBText module core functions
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  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. * 2009-03-01 added support for ORDER-BY clause by Edgar Holleis
  27. * 2003-06-05 fixed bug: when comparing two values and the first was less than
  28. * the second one, the result of 'dbt_row_match' was always true,
  29. * thanks to Gabriel, (Daniel)
  30. * 2003-02-04 created by Daniel
  31. *
  32. */
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <sys/types.h>
  36. #include <stdlib.h>
  37. #include <setjmp.h>
  38. #include "../../mem/mem.h"
  39. #include "dbt_res.h"
  40. #define SIGN(_i) ((_i) > 0 ? 1 : ((_i) < 0 ? -1 : 0))
  41. dbt_result_p dbt_result_new(dbt_table_p _dtp, int *_lres, int _sz)
  42. {
  43. dbt_result_p _dres = NULL;
  44. int i, n;
  45. char *p;
  46. if(!_dtp || _sz < 0)
  47. return NULL;
  48. if(!_lres)
  49. _sz = _dtp->nrcols;
  50. _dres = (dbt_result_p)pkg_malloc(sizeof(dbt_result_t));
  51. if(!_dres)
  52. return NULL;
  53. _dres->colv = (dbt_column_p)pkg_malloc(_sz*sizeof(dbt_column_t));
  54. if(!_dres->colv)
  55. {
  56. LM_DBG("no pkg memory!\n");
  57. pkg_free(_dres);
  58. return NULL;
  59. }
  60. memset(_dres->colv, 0, _sz*sizeof(dbt_column_t));
  61. LM_DBG("new res with %d cols\n", _sz);
  62. for(i = 0; i < _sz; i++)
  63. {
  64. n = (_lres)?_dtp->colv[_lres[i]]->name.len:_dtp->colv[i]->name.len;
  65. p = (_lres)?_dtp->colv[_lres[i]]->name.s:_dtp->colv[i]->name.s;
  66. _dres->colv[i].name.s = (char*)pkg_malloc((n+1)*sizeof(char));
  67. if(!_dres->colv[i].name.s)
  68. {
  69. LM_DBG("no pkg memory\n");
  70. goto clean;
  71. }
  72. _dres->colv[i].name.len = n;
  73. strncpy(_dres->colv[i].name.s, p, n);
  74. _dres->colv[i].name.s[n] = 0;
  75. _dres->colv[i].type =
  76. (_lres)?_dtp->colv[_lres[i]]->type:_dtp->colv[i]->type;
  77. }
  78. _dres->nrcols = _sz;
  79. _dres->nrrows = 0;
  80. _dres->rows = NULL;
  81. return _dres;
  82. clean:
  83. while(i>=0)
  84. {
  85. if(_dres->colv[i].name.s)
  86. pkg_free(_dres->colv[i].name.s);
  87. i--;
  88. }
  89. pkg_free(_dres->colv);
  90. pkg_free(_dres);
  91. return NULL;
  92. }
  93. int dbt_result_free(dbt_result_p _dres)
  94. {
  95. dbt_row_p _rp=NULL, _rp0=NULL;
  96. int i;
  97. if(!_dres)
  98. return -1;
  99. _rp = _dres->rows;
  100. while(_rp)
  101. {
  102. _rp0=_rp;
  103. _rp=_rp->next;
  104. if(_rp0->fields)
  105. {
  106. for(i=0; i<_dres->nrcols; i++)
  107. {
  108. if((_dres->colv[i].type==DB1_STR
  109. || _dres->colv[i].type==DB1_STRING
  110. || _dres->colv[i].type==DB1_BLOB
  111. )
  112. && _rp0->fields[i].val.str_val.s)
  113. pkg_free(_rp0->fields[i].val.str_val.s);
  114. }
  115. pkg_free(_rp0->fields);
  116. }
  117. pkg_free(_rp0);
  118. }
  119. if(_dres->colv)
  120. {
  121. for(i=0; i<_dres->nrcols; i++)
  122. {
  123. if(_dres->colv[i].name.s)
  124. pkg_free(_dres->colv[i].name.s);
  125. }
  126. pkg_free(_dres->colv);
  127. }
  128. pkg_free(_dres);
  129. return 0;
  130. }
  131. int dbt_result_add_row(dbt_result_p _dres, dbt_row_p _drp)
  132. {
  133. if(!_dres || !_drp)
  134. return -1;
  135. _dres->nrrows++;
  136. if(_dres->rows)
  137. (_dres->rows)->prev = _drp;
  138. _drp->next = _dres->rows;
  139. _dres->rows = _drp;
  140. return 0;
  141. }
  142. int* dbt_get_refs(dbt_table_p _dtp, db_key_t* _k, int _n)
  143. {
  144. int i, j, *_lref=NULL;
  145. if(!_dtp || !_k || _n < 0)
  146. return NULL;
  147. _lref = (int*)pkg_malloc(_n*sizeof(int));
  148. if(!_lref)
  149. return NULL;
  150. for(i=0; i < _n; i++)
  151. {
  152. for(j=0; j<_dtp->nrcols; j++)
  153. {
  154. if(_k[i]->len==_dtp->colv[j]->name.len
  155. && !strncasecmp(_k[i]->s, _dtp->colv[j]->name.s,
  156. _dtp->colv[j]->name.len))
  157. {
  158. _lref[i] = j;
  159. break;
  160. }
  161. }
  162. if(j>=_dtp->nrcols)
  163. {
  164. LM_ERR("column <%.*s> not found\n", _k[i]->len, _k[i]->s);
  165. pkg_free(_lref);
  166. return NULL;
  167. }
  168. }
  169. return _lref;
  170. }
  171. int dbt_row_match(dbt_table_p _dtp, dbt_row_p _drp, int* _lkey,
  172. db_op_t* _op, db_val_t* _v, int _n)
  173. {
  174. int i, res;
  175. if(!_dtp || !_drp)
  176. return 0;
  177. if(!_lkey)
  178. return 1;
  179. for(i=0; i<_n; i++)
  180. {
  181. res = dbt_cmp_val(&_drp->fields[_lkey[i]], &_v[i]);
  182. if(!_op || !strcmp(_op[i], OP_EQ))
  183. {
  184. if(res!=0)
  185. return 0;
  186. }else{
  187. if(!strcmp(_op[i], OP_LT))
  188. {
  189. if(res!=-1)
  190. return 0;
  191. }else{
  192. if(!strcmp(_op[i], OP_GT))
  193. {
  194. if(res!=1)
  195. return 0;
  196. }else{
  197. if(!strcmp(_op[i], OP_LEQ))
  198. {
  199. if(res==1)
  200. return 0;
  201. }else{
  202. if(!strcmp(_op[i], OP_GEQ))
  203. {
  204. if(res==-1)
  205. return 0;
  206. }else{
  207. return 0;
  208. }}}}}
  209. }
  210. return 1;
  211. }
  212. int dbt_result_extract_fields(dbt_table_p _dtp, dbt_row_p _drp,
  213. int* _lres, dbt_result_p _dres)
  214. {
  215. dbt_row_p _rp=NULL;
  216. int i, n;
  217. if(!_dtp || !_drp || !_dres || _dres->nrcols<=0)
  218. return -1;
  219. _rp = dbt_result_new_row(_dres);
  220. if(!_rp)
  221. return -1;
  222. for(i=0; i<_dres->nrcols; i++)
  223. {
  224. n = (_lres)?_lres[i]:i;
  225. if(dbt_is_neq_type(_dres->colv[i].type, _dtp->colv[n]->type))
  226. {
  227. LM_DBG("wrong types!\n");
  228. goto clean;
  229. }
  230. _rp->fields[i].nul = _drp->fields[n].nul;
  231. if(_rp->fields[i].nul)
  232. {
  233. memset(&(_rp->fields[i].val), 0, sizeof(_rp->fields[i].val));
  234. continue;
  235. }
  236. switch(_dres->colv[i].type)
  237. {
  238. case DB1_INT:
  239. case DB1_DATETIME:
  240. case DB1_BITMAP:
  241. _rp->fields[i].type = _dres->colv[i].type;
  242. _rp->fields[i].val.int_val = _drp->fields[n].val.int_val;
  243. break;
  244. case DB1_DOUBLE:
  245. _rp->fields[i].type = DB1_DOUBLE;
  246. _rp->fields[i].val.double_val=_drp->fields[n].val.double_val;
  247. break;
  248. case DB1_STRING:
  249. case DB1_STR:
  250. case DB1_BLOB:
  251. _rp->fields[i].type = _dres->colv[i].type;
  252. _rp->fields[i].val.str_val.len =
  253. _drp->fields[n].val.str_val.len;
  254. _rp->fields[i].val.str_val.s =(char*)pkg_malloc(sizeof(char)*
  255. (_drp->fields[n].val.str_val.len+1));
  256. if(!_rp->fields[i].val.str_val.s)
  257. goto clean;
  258. memcpy(_rp->fields[i].val.str_val.s,
  259. _drp->fields[n].val.str_val.s,
  260. _rp->fields[i].val.str_val.len);
  261. _rp->fields[i].val.str_val.s[_rp->fields[i].val.str_val.len]=0;
  262. break;
  263. default:
  264. goto clean;
  265. }
  266. }
  267. if(_dres->rows)
  268. (_dres->rows)->prev = _rp;
  269. _rp->next = _dres->rows;
  270. _dres->rows = _rp;
  271. _dres->nrrows++;
  272. return 0;
  273. clean:
  274. LM_DBG("make clean!\n");
  275. while(i>=0)
  276. {
  277. if((_rp->fields[i].type == DB1_STRING
  278. || _rp->fields[i].type == DB1_STR
  279. || _rp->fields[i].type == DB1_BLOB)
  280. && !_rp->fields[i].nul
  281. && _rp->fields[i].val.str_val.s)
  282. pkg_free(_rp->fields[i].val.str_val.s);
  283. i--;
  284. }
  285. pkg_free(_rp->fields);
  286. pkg_free(_rp);
  287. return -1;
  288. }
  289. int dbt_result_print(dbt_result_p _dres)
  290. {
  291. #if 0
  292. int i;
  293. FILE *fout = stdout;
  294. dbt_row_p rowp = NULL;
  295. char *p;
  296. if(!_dres || _dres->nrcols<=0)
  297. return -1;
  298. fprintf(fout, "\nContent of result\n");
  299. for(i=0; i<_dres->nrcols; i++)
  300. {
  301. switch(_dres->colv[i].type)
  302. {
  303. case DB1_INT:
  304. fprintf(fout, "%.*s(int", _dres->colv[i].name.len,
  305. _dres->colv[i].name.s);
  306. if(_dres->colv[i].flag & DBT_FLAG_NULL)
  307. fprintf(fout, ",null");
  308. fprintf(fout, ") ");
  309. break;
  310. case DB1_DOUBLE:
  311. fprintf(fout, "%.*s(double", _dres->colv[i].name.len,
  312. _dres->colv[i].name.s);
  313. if(_dres->colv[i].flag & DBT_FLAG_NULL)
  314. fprintf(fout, ",null");
  315. fprintf(fout, ") ");
  316. break;
  317. case DB1_STR:
  318. fprintf(fout, "%.*s(str", _dres->colv[i].name.len,
  319. _dres->colv[i].name.s);
  320. if(_dres->colv[i].flag & DBT_FLAG_NULL)
  321. fprintf(fout, ",null");
  322. fprintf(fout, ") ");
  323. break;
  324. default:
  325. return -1;
  326. }
  327. }
  328. fprintf(fout, "\n");
  329. rowp = _dres->rows;
  330. while(rowp)
  331. {
  332. for(i=0; i<_dres->nrcols; i++)
  333. {
  334. switch(_dres->colv[i].type)
  335. {
  336. case DB1_INT:
  337. if(rowp->fields[i].nul)
  338. fprintf(fout, "N ");
  339. else
  340. fprintf(fout, "%d ",
  341. rowp->fields[i].val.int_val);
  342. break;
  343. case DB1_DOUBLE:
  344. if(rowp->fields[i].nul)
  345. fprintf(fout, "N ");
  346. else
  347. fprintf(fout, "%.2f ",
  348. rowp->fields[i].val.double_val);
  349. break;
  350. case DB1_STR:
  351. fprintf(fout, "\"");
  352. if(!rowp->fields[i].nul)
  353. {
  354. p = rowp->fields[i].val.str_val.s;
  355. while(p < rowp->fields[i].val.str_val.s
  356. + rowp->fields[i].val.str_val.len)
  357. {
  358. switch(*p)
  359. {
  360. case '\n':
  361. fprintf(fout, "\\n");
  362. break;
  363. case '\r':
  364. fprintf(fout, "\\r");
  365. break;
  366. case '\t':
  367. fprintf(fout, "\\t");
  368. break;
  369. case '\\':
  370. fprintf(fout, "\\\\");
  371. break;
  372. case '"':
  373. fprintf(fout, "\\\"");
  374. break;
  375. case '\0':
  376. fprintf(fout, "\\0");
  377. break;
  378. default:
  379. fprintf(fout, "%c", *p);
  380. }
  381. p++;
  382. }
  383. }
  384. fprintf(fout, "\" ");
  385. break;
  386. default:
  387. return -1;
  388. }
  389. }
  390. fprintf(fout, "\n");
  391. rowp = rowp->next;
  392. }
  393. #endif
  394. return 0;
  395. }
  396. int dbt_cmp_val(dbt_val_p _vp, db_val_t* _v)
  397. {
  398. int _l, _n;
  399. if(!_vp && !_v)
  400. return 0;
  401. if(!_v)
  402. return 1;
  403. if(!_vp)
  404. return -1;
  405. if(_vp->nul && _v->nul)
  406. return 0;
  407. if(_v->nul)
  408. return 1;
  409. if(_vp->nul)
  410. return -1;
  411. switch(VAL_TYPE(_v))
  412. {
  413. case DB1_INT:
  414. return (_vp->val.int_val<_v->val.int_val)?-1:
  415. (_vp->val.int_val>_v->val.int_val)?1:0;
  416. case DB1_BIGINT:
  417. LM_ERR("BIGINT not supported");
  418. return -1;
  419. case DB1_DOUBLE:
  420. return (_vp->val.double_val<_v->val.double_val)?-1:
  421. (_vp->val.double_val>_v->val.double_val)?1:0;
  422. case DB1_DATETIME:
  423. return (_vp->val.int_val<_v->val.time_val)?-1:
  424. (_vp->val.int_val>_v->val.time_val)?1:0;
  425. case DB1_STRING:
  426. _l = strlen(_v->val.string_val);
  427. _l = (_l>_vp->val.str_val.len)?_vp->val.str_val.len:_l;
  428. _n = strncasecmp(_vp->val.str_val.s, _v->val.string_val, _l);
  429. if(_n)
  430. return SIGN(_n);
  431. if(_vp->val.str_val.len == strlen(_v->val.string_val))
  432. return 0;
  433. if(_l==_vp->val.str_val.len)
  434. return -1;
  435. return 1;
  436. case DB1_STR:
  437. _l = _v->val.str_val.len;
  438. _l = (_l>_vp->val.str_val.len)?_vp->val.str_val.len:_l;
  439. _n = strncasecmp(_vp->val.str_val.s, _v->val.str_val.s, _l);
  440. if(_n)
  441. return SIGN(_n);
  442. if(_vp->val.str_val.len == _v->val.str_val.len)
  443. return 0;
  444. if(_l==_vp->val.str_val.len)
  445. return -1;
  446. return 1;
  447. case DB1_BLOB:
  448. _l = _v->val.blob_val.len;
  449. _l = (_l>_vp->val.str_val.len)?_vp->val.str_val.len:_l;
  450. _n = strncasecmp(_vp->val.str_val.s, _v->val.blob_val.s, _l);
  451. if(_n)
  452. return SIGN(_n);
  453. if(_vp->val.str_val.len == _v->val.blob_val.len)
  454. return 0;
  455. if(_l==_vp->val.str_val.len)
  456. return -1;
  457. return 1;
  458. case DB1_BITMAP:
  459. return (_vp->val.int_val<_v->val.bitmap_val)?-1:
  460. (_vp->val.int_val>_v->val.bitmap_val)?1:0;
  461. default:
  462. LM_ERR("invalid datatype %d\n", VAL_TYPE(_v));
  463. return -2;
  464. }
  465. return -2;
  466. }
  467. dbt_row_p dbt_result_new_row(dbt_result_p _dres)
  468. {
  469. dbt_row_p _drp = NULL;
  470. if(!_dres || _dres->nrcols<=0)
  471. return NULL;
  472. _drp = (dbt_row_p)pkg_malloc(sizeof(dbt_row_t));
  473. if(!_drp)
  474. return NULL;
  475. memset(_drp, 0, sizeof(dbt_row_t));
  476. _drp->fields = (dbt_val_p)pkg_malloc(_dres->nrcols*sizeof(dbt_val_t));
  477. if(!_drp->fields)
  478. {
  479. pkg_free(_drp);
  480. return NULL;
  481. }
  482. memset(_drp->fields, 0, _dres->nrcols*sizeof(dbt_val_t));
  483. _drp->next = _drp->prev = NULL;
  484. return _drp;
  485. }
  486. /* The _o clause to query is not really a db_key_t, it is SQL (str).
  487. * db_mysql and db_postgres simply paste it into SQL, we need to parse it. */
  488. /* Format of _o: column1 [ASC|DESC], column2 [ASC|DESC], ... */
  489. int dbt_parse_orderbyclause(db_key_t **_o_k, char **_o_op, int *_o_n, db_key_t _o)
  490. {
  491. char *_po, *_ps, *_pe;
  492. char _c = '\0';
  493. char _d[8];
  494. int _n;
  495. int _i;
  496. str *_s;
  497. /* scan _o, count ',' -> upper bound for no of columns */
  498. _n = 1;
  499. for (_i=0; _i < _o->len; _i++)
  500. if (_o->s[_i] == ',')
  501. _n++;
  502. /* *_o_k will include the db_key_ts, the strs, a copy of _o and \0 */
  503. *_o_k = pkg_malloc((sizeof(db_key_t)+sizeof(str)) * _n + _o->len + 1);
  504. if (!*_o_k)
  505. return -1;
  506. _s = (str *)((char *)(*_o_k) + sizeof(db_key_t) * _n);
  507. for (_i=0; _i < _n; _i++)
  508. (*_o_k)[_i] = &_s[_i];
  509. _po = (char *)(*_o_k) + (sizeof(db_key_t) + sizeof(str)) * _n;
  510. memcpy(_po, _o->s, _o->len);
  511. *(_po+_o->len) = '\0';
  512. *_o_op = pkg_malloc(sizeof(char) * _n);
  513. if (!*_o_op)
  514. {
  515. pkg_free(*_o_k);
  516. return -1;
  517. }
  518. *_o_n = 0;
  519. _ps = _po;
  520. while (*_o_n < _n)
  521. {
  522. while (*_ps == ' ') _ps++;
  523. if (*_ps == '\0')
  524. break;
  525. strcpy(_d, " \f\n\r\t\v,"); /* isspace() and comma */
  526. if (*_ps == '"' || *_ps == '\'') /* detect quote */
  527. {
  528. _d[0] = *_ps;
  529. _d[1] = '\0';
  530. _ps++;
  531. }
  532. _pe = strpbrk(_ps, _d); /* search quote, space, comma or eos */
  533. if (!_pe && _d[0] == ' ') /* if token is last token in string */
  534. _pe = _po + _o->len; /* point to end of string */
  535. if (! _pe) /* we were looking for quote but found none */
  536. goto parse_error;
  537. /* _ps points to start of column-name,
  538. * _pe points after the column-name, on quote, space, comma, or '\0' */
  539. _c = *_pe;
  540. *_pe = '\0';
  541. (*_o_k)[*_o_n]->s = _ps;
  542. (*_o_k)[*_o_n]->len = _pe - _ps;
  543. (*_o_op)[*_o_n] = '<'; /* default */
  544. (*_o_n)++;
  545. if (_c == '\0')
  546. break;
  547. /* go beyond current token */
  548. _ps = _pe + 1;
  549. if (_c == ',')
  550. continue;
  551. while (*_ps == ' ') _ps++;
  552. if (*_ps == ',')
  553. {
  554. _ps++;
  555. continue;
  556. }
  557. if (*_ps == '\0')
  558. break;
  559. /* there is ASC OR DESC qualifier */
  560. if (strncasecmp(_ps, "DESC", 4) == 0)
  561. {
  562. (*_o_op)[*_o_n-1] = '>';
  563. _ps += 4;
  564. } else if (strncasecmp(_ps, "ASC", 3) == 0)
  565. {
  566. _ps += 3;
  567. } else goto parse_error;
  568. /* point behind qualifier */
  569. while (*_ps == ' ') _ps++;
  570. if (*_ps == ',')
  571. {
  572. _ps++;
  573. continue;
  574. }
  575. if (*_ps == '\0')
  576. break;
  577. goto parse_error;
  578. }
  579. if (*_ps != '\0' && _c != '\0') /* that means more elements than _tbc->nrcols */
  580. goto parse_error;
  581. if (*_o_n == 0) /* there weren't actually any columns */
  582. {
  583. pkg_free(*_o_k);
  584. pkg_free(*_o_op);
  585. *_o_op = NULL;
  586. *_o_k = NULL;
  587. return 0; /* return success anyway */
  588. }
  589. return 0;
  590. parse_error:
  591. pkg_free(*_o_k);
  592. pkg_free(*_o_op);
  593. *_o_op = NULL;
  594. *_o_k = NULL;
  595. *_o_n = 0;
  596. return -1;
  597. }
  598. /* lres/_nc is the selected columns, _o_l/_o_n is the order-by columns:
  599. * All order-by columns need to be extracted along with the selected columns,
  600. * so any column in _o_l and not lres needs to be added to lres. _o_nc keeps
  601. * track of the number of columns added to lres. */
  602. int dbt_mangle_columnselection(int **_lres, int *_nc, int *_o_nc, int *_o_l, int _o_n)
  603. {
  604. int _i, _j;
  605. *_o_nc = 0;
  606. if (! *_lres)
  607. return 0; /* all columns selected, no need to worry */
  608. /* count how many columns are affected */
  609. for (_i=0; _i < _o_n; _i++) /* loop over order-by columns */
  610. {
  611. for (_j=0; _j < *_nc && (*_lres)[_j] != _o_l[_i]; _j++);
  612. if (_j == *_nc) /* order-by column not found in select columns */
  613. (*_o_nc)++;
  614. }
  615. if (*_o_nc == 0)
  616. return 0; /* all order-by columns also selected, we're fine */
  617. /* make _lres bigger */
  618. *_lres = pkg_realloc(*_lres, sizeof(int) * (*_nc + *_o_nc));
  619. if (! *_lres)
  620. return -1;
  621. /* add oder-by columns to select columns */
  622. for (_i=0; _i < _o_n; _i++) /* loop over order-by columns */
  623. {
  624. for (_j=0; _j < *_nc && (*_lres)[_j] != _o_l[_i]; _j++);
  625. if (_j == *_nc) /* order-by column not found in select columns */
  626. {
  627. (*_lres)[*_nc] = _o_l[_i];
  628. (*_nc)++;
  629. }
  630. }
  631. /* _lres, _nc modified, _o_nc returned */
  632. return 0;
  633. }
  634. /* globals for qsort */
  635. dbt_result_p dbt_sort_dres;
  636. int *dbt_sort_o_l;
  637. char *dbt_sort_o_op;
  638. int dbt_sort_o_n;
  639. jmp_buf dbt_sort_jmpenv;
  640. /* comparison function for qsort */
  641. int dbt_qsort_compar(const void *_a, const void *_b)
  642. {
  643. int _i, _j, _r;
  644. for (_i=0; _i<dbt_sort_o_n; _i++)
  645. {
  646. _j = dbt_sort_o_l[_i];
  647. _r = dbt_cmp_val(&(*(dbt_row_p *)_a)->fields[_j], &(*(dbt_row_p *)_b)->fields[_j]);
  648. if (_r == 0)
  649. continue; /* no result yet, compare next column */
  650. if (_r == +1 || _r == -1)
  651. return (dbt_sort_o_op[_i] == '<') ? _r : -_r; /* ASC OR DESC */
  652. /* error */
  653. longjmp(dbt_sort_jmpenv, _r);
  654. }
  655. /* no result after comparing all columns, same */
  656. return 0;
  657. }
  658. int dbt_sort_result(dbt_result_p _dres, int *_o_l, char *_o_op, int _o_n, int *_lres, int _nc)
  659. {
  660. int _i, _j;
  661. dbt_row_p *_a;
  662. dbt_row_p _el;
  663. /* first we need to rewrite _o_l in terms of _lres */
  664. if (_lres)
  665. {
  666. for (_i=0; _i < _o_n; _i++) /* loop over order-by columns */
  667. {
  668. /* depends on correctness of dbt_mangle_columnselection */
  669. for (_j=0; _lres[_j] != _o_l[_i]; _j++ /*, assert(_j < _nc)*/);
  670. _o_l[_i] = _j;
  671. }
  672. }
  673. /* rewrite linked list to array */
  674. _a = pkg_malloc(sizeof(dbt_row_p) * _dres->nrrows);
  675. if (!_a)
  676. return -1;
  677. for (_el=_dres->rows, _i=0; _el != NULL; _el=_el->next, _i++)
  678. _a[_i] = _el;
  679. /* set globals */
  680. dbt_sort_dres = _dres;
  681. dbt_sort_o_l = _o_l;
  682. dbt_sort_o_op = _o_op;
  683. dbt_sort_o_n = _o_n;
  684. _i = setjmp(dbt_sort_jmpenv); /* exception handling */
  685. if (_i)
  686. {
  687. /* error occured during qsort */
  688. LM_ERR("qsort aborted\n");
  689. pkg_free(_a);
  690. return _i;
  691. }
  692. qsort(_a, _dres->nrrows, sizeof(dbt_row_p), &dbt_qsort_compar);
  693. /* restore linked list */
  694. for (_i=0; _i < _dres->nrrows; _i++)
  695. {
  696. _a[_i]->prev = (_i > 0) ? _a[_i-1] : NULL;
  697. _a[_i]->next = (_i+1 < _dres->nrrows) ? _a[_i+1] : NULL;
  698. }
  699. _dres->rows = _a[0];
  700. pkg_free(_a);
  701. return 0;
  702. }
  703. /* Remove the columns that were added to the result to facilitate sorting.
  704. * The additional columns constitute the end of the queue. Instead of
  705. * actually removing them with realloc, they are simply kept around, but
  706. * hidden. For string-columns, the allocated string is however freed. */
  707. void dbt_project_result(dbt_result_p _dres, int _o_nc)
  708. {
  709. int _i;
  710. dbt_row_p _drp;
  711. if (! _o_nc)
  712. return;
  713. /* check whether there are string columns, free them */
  714. for (_i = _dres->nrcols - _o_nc; _i < _dres->nrcols; _i++)
  715. {
  716. if (_dres->colv[_i].type == DB1_STRING ||
  717. _dres->colv[_i].type == DB1_STR ||
  718. _dres->colv[_i].type == DB1_BLOB)
  719. {
  720. for (_drp=_dres->rows; _drp != NULL; _drp = _drp->next)
  721. {
  722. if (! _drp->fields[_i].nul &&
  723. (_drp->fields[_i].type == DB1_STRING ||
  724. _drp->fields[_i].type == DB1_STR ||
  725. _drp->fields[_i].type == DB1_BLOB ))
  726. {
  727. pkg_free(_drp->fields[_i].val.str_val.s);
  728. _drp->fields[_i].val.str_val.s = NULL;
  729. _drp->fields[_i].val.str_val.len = 0;
  730. }
  731. }
  732. }
  733. /* free the string containing the column name */
  734. pkg_free(_dres->colv[_i].name.s);
  735. _dres->colv[_i].name.s = NULL;
  736. _dres->colv[_i].name.len = 0;
  737. }
  738. /* pretend the columns are gone, in dbt_free_query free will do the right thing */
  739. _dres->nrcols -= _o_nc;
  740. }