dbt_api.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * $Id$
  3. *
  4. * DBText library
  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. * 2003-02-05 created by Daniel
  27. *
  28. */
  29. #include <string.h>
  30. #include "../../lib/srdb1/db.h"
  31. #include "../../mem/mem.h"
  32. #include "dbt_res.h"
  33. #include "dbt_api.h"
  34. int dbt_use_table(db1_con_t* _h, const str* _t)
  35. {
  36. return db_use_table(_h, _t);
  37. }
  38. /*
  39. * Get and convert columns from a result
  40. */
  41. static int dbt_get_columns(db1_res_t* _r, dbt_result_p _dres)
  42. {
  43. int col;
  44. if (!_r || !_dres) {
  45. LM_ERR("invalid parameter\n");
  46. return -1;
  47. }
  48. RES_COL_N(_r) = _dres->nrcols;
  49. if (!RES_COL_N(_r)) {
  50. LM_ERR("no columns\n");
  51. return -2;
  52. }
  53. if (db_allocate_columns(_r, RES_COL_N(_r)) != 0) {
  54. LM_ERR("could not allocate columns");
  55. return -3;
  56. }
  57. for(col = 0; col < RES_COL_N(_r); col++) {
  58. /*
  59. * Its would be not necessary to allocate here new memory, because of
  60. * the internal structure of the db_text module. But we do this anyway
  61. * to stay confirm to the other database modules.
  62. */
  63. RES_NAMES(_r)[col] = (str*)pkg_malloc(sizeof(str));
  64. if (! RES_NAMES(_r)[col]) {
  65. LM_ERR("no private memory left\n");
  66. db_free_columns(_r);
  67. return -4;
  68. }
  69. LM_DBG("allocate %d bytes for RES_NAMES[%d] at %p",
  70. (int)sizeof(str), col,
  71. RES_NAMES(_r)[col]);
  72. RES_NAMES(_r)[col]->s = _dres->colv[col].name.s;
  73. RES_NAMES(_r)[col]->len = _dres->colv[col].name.len;
  74. switch(_dres->colv[col].type)
  75. {
  76. case DB1_STR:
  77. case DB1_STRING:
  78. case DB1_BLOB:
  79. case DB1_INT:
  80. case DB1_DATETIME:
  81. case DB1_DOUBLE:
  82. RES_TYPES(_r)[col] = _dres->colv[col].type;
  83. break;
  84. default:
  85. LM_WARN("unhandled data type column (%.*s) type id (%d), "
  86. "use STR as default\n", RES_NAMES(_r)[col]->len,
  87. RES_NAMES(_r)[col]->s, _dres->colv[col].type);
  88. RES_TYPES(_r)[col] = DB1_STR;
  89. break;
  90. }
  91. }
  92. return 0;
  93. }
  94. /*
  95. * Convert a row from result into db API representation
  96. */
  97. static int dbt_convert_row(db1_res_t* _res, db_row_t* _r, dbt_row_p _r1)
  98. {
  99. int i;
  100. if (!_r || !_res || !_r1) {
  101. LM_ERR("invalid parameter value\n");
  102. return -1;
  103. }
  104. if (db_allocate_row(_res, _r) != 0) {
  105. LM_ERR("could not allocate row");
  106. return -2;
  107. }
  108. for(i = 0; i < RES_COL_N(_res); i++) {
  109. (ROW_VALUES(_r)[i]).nul = _r1->fields[i].nul;
  110. switch(RES_TYPES(_res)[i])
  111. {
  112. case DB1_INT:
  113. VAL_INT(&(ROW_VALUES(_r)[i])) =
  114. _r1->fields[i].val.int_val;
  115. VAL_TYPE(&(ROW_VALUES(_r)[i])) = DB1_INT;
  116. break;
  117. case DB1_BIGINT:
  118. LM_ERR("BIGINT not supported");
  119. return -1;
  120. case DB1_DOUBLE:
  121. VAL_DOUBLE(&(ROW_VALUES(_r)[i])) =
  122. _r1->fields[i].val.double_val;
  123. VAL_TYPE(&(ROW_VALUES(_r)[i])) = DB1_DOUBLE;
  124. break;
  125. case DB1_STRING:
  126. VAL_STR(&(ROW_VALUES(_r)[i])).s =
  127. _r1->fields[i].val.str_val.s;
  128. VAL_STR(&(ROW_VALUES(_r)[i])).len =
  129. _r1->fields[i].val.str_val.len;
  130. VAL_TYPE(&(ROW_VALUES(_r)[i])) = DB1_STRING;
  131. VAL_FREE(&(ROW_VALUES(_r)[i])) = 0;
  132. break;
  133. case DB1_STR:
  134. VAL_STR(&(ROW_VALUES(_r)[i])).s =
  135. _r1->fields[i].val.str_val.s;
  136. VAL_STR(&(ROW_VALUES(_r)[i])).len =
  137. _r1->fields[i].val.str_val.len;
  138. VAL_TYPE(&(ROW_VALUES(_r)[i])) = DB1_STR;
  139. VAL_FREE(&(ROW_VALUES(_r)[i])) = 0;
  140. break;
  141. case DB1_DATETIME:
  142. VAL_INT(&(ROW_VALUES(_r)[i])) =
  143. _r1->fields[i].val.int_val;
  144. VAL_TYPE(&(ROW_VALUES(_r)[i])) = DB1_DATETIME;
  145. break;
  146. case DB1_BLOB:
  147. VAL_STR(&(ROW_VALUES(_r)[i])).s =
  148. _r1->fields[i].val.str_val.s;
  149. VAL_STR(&(ROW_VALUES(_r)[i])).len =
  150. _r1->fields[i].val.str_val.len;
  151. VAL_TYPE(&(ROW_VALUES(_r)[i])) = DB1_BLOB;
  152. VAL_FREE(&(ROW_VALUES(_r)[i])) = 0;
  153. break;
  154. case DB1_BITMAP:
  155. VAL_INT(&(ROW_VALUES(_r)[i])) =
  156. _r1->fields[i].val.bitmap_val;
  157. VAL_TYPE(&(ROW_VALUES(_r)[i])) = DB1_INT;
  158. break;
  159. default:
  160. LM_ERR("val type [%d] not supported", RES_TYPES(_res)[i]);
  161. return -1;
  162. }
  163. }
  164. return 0;
  165. }
  166. /*
  167. * Convert rows from internal to db API representation
  168. */
  169. static int dbt_convert_rows(db1_res_t* _r, dbt_result_p _dres)
  170. {
  171. int row;
  172. dbt_row_p _rp = NULL;
  173. if (!_r || !_dres) {
  174. LM_ERR("invalid parameter\n");
  175. return -1;
  176. }
  177. RES_ROW_N(_r) = _dres->nrrows;
  178. if (!RES_ROW_N(_r)) {
  179. return 0;
  180. }
  181. if (db_allocate_rows(_r) < 0) {
  182. LM_ERR("could not allocate rows");
  183. return -2;
  184. }
  185. row = 0;
  186. _rp = _dres->rows;
  187. while(_rp) {
  188. if (dbt_convert_row(_r, &(RES_ROWS(_r)[row]), _rp) < 0) {
  189. LM_ERR("failed to convert row #%d\n", row);
  190. RES_ROW_N(_r) = row;
  191. db_free_rows(_r);
  192. return -4;
  193. }
  194. row++;
  195. _rp = _rp->next;
  196. }
  197. return 0;
  198. }
  199. /*
  200. * Fill the structure with data from database
  201. */
  202. static int dbt_convert_result(db1_res_t* _r, dbt_result_p _dres)
  203. {
  204. if (!_r || !_dres) {
  205. LM_ERR("invalid parameter\n");
  206. return -1;
  207. }
  208. if (dbt_get_columns(_r, _dres) < 0) {
  209. LM_ERR("failed to get column names\n");
  210. return -2;
  211. }
  212. if (dbt_convert_rows(_r, _dres) < 0) {
  213. LM_ERR("failed to convert rows\n");
  214. db_free_columns(_r);
  215. return -3;
  216. }
  217. return 0;
  218. }
  219. /*
  220. * Retrieve result set
  221. */
  222. int dbt_get_result(db1_res_t** _r, dbt_result_p _dres)
  223. {
  224. if ( !_r) {
  225. LM_ERR("invalid parameter value\n");
  226. return -1;
  227. }
  228. if (!_dres)
  229. {
  230. LM_ERR("failed to get result\n");
  231. *_r = 0;
  232. return -3;
  233. }
  234. *_r = db_new_result();
  235. if (*_r == 0)
  236. {
  237. LM_ERR("no private memory left\n");
  238. return -2;
  239. }
  240. if (dbt_convert_result(*_r, _dres) < 0)
  241. {
  242. LM_ERR("failed to convert result\n");
  243. pkg_free(*_r);
  244. return -4;
  245. }
  246. (*_r)->ptr = _dres;
  247. return 0;
  248. }