2
0

km_res.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2003 August.Net Services, LLC
  5. * Copyright (C) 2006 Norman Brandinger
  6. * Copyright (C) 2008 1&1 Internet AG
  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-04-06 initial code written (Greg Fausak/Andy Fullford)
  27. *
  28. * 2006-07-26 added BPCHAROID as a valid type for DB1_STRING conversions
  29. * this removes the "unknown type 1042" log messages (norm)
  30. *
  31. * 2006-10-27 Added fetch support (norm)
  32. * Removed dependency on aug_* memory routines (norm)
  33. * Added connection pooling support (norm)
  34. * Standardized API routines to pg_* names (norm)
  35. */
  36. /*! \file
  37. * \brief DB_POSTGRES :: Core
  38. * \ingroup db_postgres
  39. * Module: \ref db_postgres
  40. */
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include "../../lib/srdb1/db_id.h"
  44. #include "../../lib/srdb1/db_res.h"
  45. #include "../../lib/srdb1/db_con.h"
  46. #include "../../dprint.h"
  47. #include "../../mem/mem.h"
  48. #include "km_res.h"
  49. #include "km_val.h"
  50. #include "km_pg_con.h"
  51. #include "km_pg_type.h"
  52. /*!
  53. * \brief Fill the result structure with data from the query
  54. * \param _h database connection
  55. * \param _r result set
  56. * \return 0 on success, negative on error
  57. */
  58. int db_postgres_convert_result(const db1_con_t* _h, db1_res_t* _r)
  59. {
  60. if (!_h || !_r) {
  61. LM_ERR("invalid parameter value\n");
  62. return -1;
  63. }
  64. if (db_postgres_get_columns(_h, _r) < 0) {
  65. LM_ERR("failed to get column names\n");
  66. return -2;
  67. }
  68. if (db_postgres_convert_rows(_h, _r) < 0) {
  69. LM_ERR("failed to convert rows\n");
  70. db_free_columns(_r);
  71. return -3;
  72. }
  73. return 0;
  74. }
  75. /*!
  76. * \brief Get and convert columns from a result set
  77. * \param _h database connection
  78. * \param _r result set
  79. * \return 0 on success, negative on error
  80. */
  81. int db_postgres_get_columns(const db1_con_t* _h, db1_res_t* _r)
  82. {
  83. int col, datatype;
  84. if (!_h || !_r) {
  85. LM_ERR("invalid parameter value\n");
  86. return -1;
  87. }
  88. /* Get the number of rows (tuples) in the query result. */
  89. RES_ROW_N(_r) = PQntuples(CON_RESULT(_h));
  90. /* Get the number of columns (fields) in each row of the query result. */
  91. RES_COL_N(_r) = PQnfields(CON_RESULT(_h));
  92. if (!RES_COL_N(_r)) {
  93. LM_DBG("no columns returned from the query\n");
  94. return -2;
  95. } else {
  96. LM_DBG("%d columns returned from the query\n", RES_COL_N(_r));
  97. }
  98. if (db_allocate_columns(_r, RES_COL_N(_r)) != 0) {
  99. LM_ERR("could not allocate columns\n");
  100. return -3;
  101. }
  102. /* For each column both the name and the OID number of the data type are saved. */
  103. for(col = 0; col < RES_COL_N(_r); col++) {
  104. RES_NAMES(_r)[col] = (str*)pkg_malloc(sizeof(str));
  105. if (! RES_NAMES(_r)[col]) {
  106. LM_ERR("no private memory left\n");
  107. db_free_columns(_r);
  108. return -4;
  109. }
  110. LM_DBG("allocate %d bytes for RES_NAMES[%d] at %p\n", (unsigned int) sizeof(str), col,
  111. RES_NAMES(_r)[col]);
  112. /* The pointer that is here returned is part of the result structure. */
  113. RES_NAMES(_r)[col]->s = PQfname(CON_RESULT(_h), col);
  114. RES_NAMES(_r)[col]->len = strlen(PQfname(CON_RESULT(_h), col));
  115. LM_DBG("RES_NAMES(%p)[%d]=[%.*s]\n", RES_NAMES(_r)[col], col,
  116. RES_NAMES(_r)[col]->len, RES_NAMES(_r)[col]->s);
  117. /* get the datatype of the column */
  118. switch(datatype = PQftype(CON_RESULT(_h),col))
  119. {
  120. case INT2OID:
  121. case INT4OID:
  122. LM_DBG("use DB1_INT result type\n");
  123. RES_TYPES(_r)[col] = DB1_INT;
  124. break;
  125. case INT8OID:
  126. LM_DBG("use DB1_BIGINT result type\n");
  127. RES_TYPES(_r)[col] = DB1_BIGINT;
  128. case FLOAT4OID:
  129. case FLOAT8OID:
  130. case NUMERICOID:
  131. LM_DBG("use DB1_DOUBLE result type\n");
  132. RES_TYPES(_r)[col] = DB1_DOUBLE;
  133. break;
  134. case DATEOID:
  135. case TIMESTAMPOID:
  136. case TIMESTAMPTZOID:
  137. LM_DBG("use DB1_DATETIME result type\n");
  138. RES_TYPES(_r)[col] = DB1_DATETIME;
  139. break;
  140. case BOOLOID:
  141. case CHAROID:
  142. case VARCHAROID:
  143. case BPCHAROID:
  144. LM_DBG("use DB1_STRING result type\n");
  145. RES_TYPES(_r)[col] = DB1_STRING;
  146. break;
  147. case TEXTOID:
  148. case BYTEAOID:
  149. LM_DBG("use DB1_BLOB result type\n");
  150. RES_TYPES(_r)[col] = DB1_BLOB;
  151. break;
  152. case BITOID:
  153. case VARBITOID:
  154. LM_DBG("use DB1_BITMAP result type\n");
  155. RES_TYPES(_r)[col] = DB1_BITMAP;
  156. break;
  157. default:
  158. LM_WARN("unhandled data type column (%.*s) type id (%d), "
  159. "use DB1_STRING as default\n", RES_NAMES(_r)[col]->len,
  160. RES_NAMES(_r)[col]->s, datatype);
  161. RES_TYPES(_r)[col] = DB1_STRING;
  162. break;
  163. }
  164. }
  165. return 0;
  166. }
  167. /*!
  168. * \brief Convert rows from PostgreSQL to db API representation
  169. * \param _h database connection
  170. * \param _r result set
  171. * \return 0 on success, negative on error
  172. */
  173. int db_postgres_convert_rows(const db1_con_t* _h, db1_res_t* _r)
  174. {
  175. char **row_buf, *s;
  176. int row, col, len;
  177. if (!_h || !_r) {
  178. LM_ERR("invalid parameter\n");
  179. return -1;
  180. }
  181. if (!RES_ROW_N(_r)) {
  182. LM_DBG("no rows returned from the query\n");
  183. RES_ROWS(_r) = 0;
  184. return 0;
  185. }
  186. /*Allocate an array of pointers per column to holds the string representation */
  187. len = sizeof(char *) * RES_COL_N(_r);
  188. row_buf = (char**)pkg_malloc(len);
  189. if (!row_buf) {
  190. LM_ERR("no private memory left\n");
  191. return -1;
  192. }
  193. LM_DBG("allocate for %d columns %d bytes in row buffer at %p\n", RES_COL_N(_r), len, row_buf);
  194. if (db_allocate_rows(_r) < 0) {
  195. LM_ERR("could not allocate rows\n");
  196. LM_DBG("freeing row buffer at %p\n", row_buf);
  197. pkg_free(row_buf);
  198. return -2;
  199. }
  200. for(row = RES_LAST_ROW(_r); row < (RES_LAST_ROW(_r) + RES_ROW_N(_r)); row++) {
  201. /* reset row buf content */
  202. memset(row_buf, 0, len);
  203. for(col = 0; col < RES_COL_N(_r); col++) {
  204. /*
  205. * The row data pointer returned by PQgetvalue points to storage
  206. * that is part of the PGresult structure. One should not modify
  207. * the data it points to, and one must explicitly copy the data
  208. * into other storage if it is to be used past the lifetime of
  209. * the PGresult structure itself.
  210. */
  211. s = PQgetvalue(CON_RESULT(_h), row, col);
  212. LM_DBG("PQgetvalue(%p,%d,%d)=[%s]\n", _h, row, col, s);
  213. /*
  214. * A empty string can be a NULL value, or just an empty string.
  215. * This differs from the mysql behaviour, that further processing
  216. * steps expect. So we need to simulate this here unfortunally.
  217. */
  218. if (PQgetisnull(CON_RESULT(_h), row, col) == 0) {
  219. row_buf[col] = s;
  220. LM_DBG("[%d][%d] Column[%.*s]=[%s]\n",
  221. row, col, RES_NAMES(_r)[col]->len, RES_NAMES(_r)[col]->s, row_buf[col]);
  222. }
  223. }
  224. /* ASSERT: row_buf contains an entire row in strings */
  225. if(db_postgres_convert_row(_h, _r, &(RES_ROWS(_r)[row - RES_LAST_ROW(_r)]), row_buf)<0){
  226. LM_ERR("failed to convert row #%d\n", row);
  227. RES_ROW_N(_r) = row - RES_LAST_ROW(_r);
  228. LM_DBG("freeing row buffer at %p\n", row_buf);
  229. pkg_free(row_buf);
  230. db_free_rows(_r);
  231. return -4;
  232. }
  233. }
  234. LM_DBG("freeing row buffer at %p\n", row_buf);
  235. pkg_free(row_buf);
  236. row_buf = NULL;
  237. return 0;
  238. }
  239. /*!
  240. * \brief Convert a row from the result query into db API representation
  241. * \param _h database connection
  242. * \param _r result set
  243. * \param _row row
  244. * \param row_buf row buffer
  245. * \return 0 on success, negative on error
  246. */
  247. int db_postgres_convert_row(const db1_con_t* _h, db1_res_t* _r, db_row_t* _row,
  248. char **row_buf)
  249. {
  250. int col, col_len;
  251. if (!_h || !_r || !_row) {
  252. LM_ERR("invalid parameter value\n");
  253. return -1;
  254. }
  255. if (db_allocate_row(_r, _row) != 0) {
  256. LM_ERR("could not allocate row\n");
  257. return -2;
  258. }
  259. /* For each column in the row */
  260. for(col = 0; col < ROW_N(_row); col++) {
  261. /* because it can contain NULL */
  262. if (!row_buf[col]) {
  263. col_len = 0;
  264. } else {
  265. col_len = strlen(row_buf[col]);
  266. }
  267. /* Convert the string representation into the value representation */
  268. if (db_postgres_str2val(RES_TYPES(_r)[col], &(ROW_VALUES(_row)[col]),
  269. row_buf[col], col_len) < 0) {
  270. LM_ERR("failed to convert value\n");
  271. LM_DBG("free row at %p\n", _row);
  272. db_free_row(_row);
  273. return -3;
  274. }
  275. }
  276. return 0;
  277. }