km_res.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * $Id$
  3. *
  4. * POSTGRES module, portions of this code were templated using
  5. * the mysql module, thus it's similarity.
  6. *
  7. * Copyright (C) 2003 August.Net Services, LLC
  8. * Copyright (C) 2006 Norman Brandinger
  9. * Copyright (C) 2008 1&1 Internet AG
  10. *
  11. * This file is part of openser, a free SIP server.
  12. *
  13. * openser is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version
  17. *
  18. * openser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. * ---
  28. *
  29. * History
  30. * -------
  31. * 2003-04-06 initial code written (Greg Fausak/Andy Fullford)
  32. *
  33. * 2006-07-26 added BPCHAROID as a valid type for DB_STRING conversions
  34. * this removes the "unknown type 1042" log messages (norm)
  35. *
  36. * 2006-10-27 Added fetch support (norm)
  37. * Removed dependency on aug_* memory routines (norm)
  38. * Added connection pooling support (norm)
  39. * Standardized API routines to pg_* names (norm)
  40. *
  41. */
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include "../../db/db_id.h"
  45. #include "../../db/db_res.h"
  46. #include "../../db/db_con.h"
  47. #include "../../dprint.h"
  48. #include "../../mem/mem.h"
  49. #include "res.h"
  50. #include "val.h"
  51. #include "pg_con.h"
  52. #include "pg_type.h"
  53. /**
  54. * Fill the result structure with data from the query
  55. */
  56. int db_postgres_convert_result(const db_con_t* _h, db_res_t* _r)
  57. {
  58. if (!_h || !_r) {
  59. LM_ERR("invalid parameter value\n");
  60. return -1;
  61. }
  62. if (db_postgres_get_columns(_h, _r) < 0) {
  63. LM_ERR("failed to get column names\n");
  64. return -2;
  65. }
  66. if (db_postgres_convert_rows(_h, _r) < 0) {
  67. LM_ERR("failed to convert rows\n");
  68. db_free_columns(_r);
  69. return -3;
  70. }
  71. return 0;
  72. }
  73. /**
  74. * Get and convert columns from a result set
  75. */
  76. int db_postgres_get_columns(const db_con_t* _h, db_res_t* _r)
  77. {
  78. int col, datatype;
  79. if (!_h || !_r) {
  80. LM_ERR("invalid parameter value\n");
  81. return -1;
  82. }
  83. /* Get the number of columns (fields) in each row of the query result. */
  84. RES_COL_N(_r) = PQnfields(CON_RESULT(_h));
  85. if (!RES_COL_N(_r)) {
  86. LM_DBG("no columns returned from the query\n");
  87. return -2;
  88. } else {
  89. LM_DBG("%d columns returned from the query\n", RES_COL_N(_r));
  90. }
  91. if (db_allocate_columns(_r, RES_COL_N(_r)) != 0) {
  92. LM_ERR("could not allocate columns");
  93. return -3;
  94. }
  95. /* For each column both the name and the OID number of the data type are saved. */
  96. for(col = 0; col < RES_COL_N(_r); col++) {
  97. RES_NAMES(_r)[col] = (str*)pkg_malloc(sizeof(str));
  98. if (! RES_NAMES(_r)[col]) {
  99. LM_ERR("no private memory left\n");
  100. db_free_columns(_r);
  101. return -4;
  102. }
  103. LM_DBG("allocate %d bytes for RES_NAMES[%d] at %p", sizeof(str), col,
  104. RES_NAMES(_r)[col]);
  105. /* The pointer that is here returned is part of the result structure. */
  106. RES_NAMES(_r)[col]->s = PQfname(CON_RESULT(_h), col);
  107. RES_NAMES(_r)[col]->len = strlen(PQfname(CON_RESULT(_h), col));
  108. LM_DBG("RES_NAMES(%p)[%d]=[%.*s]\n", RES_NAMES(_r)[col], col,
  109. RES_NAMES(_r)[col]->len, RES_NAMES(_r)[col]->s);
  110. /* get the datatype of the column */
  111. switch(datatype = PQftype(CON_RESULT(_h),col))
  112. {
  113. case INT2OID:
  114. case INT4OID:
  115. case INT8OID:
  116. LM_DBG("use DB_INT result type");
  117. RES_TYPES(_r)[col] = DB_INT;
  118. break;
  119. case FLOAT4OID:
  120. case FLOAT8OID:
  121. case NUMERICOID:
  122. LM_DBG("use DB_DOUBLE result type");
  123. RES_TYPES(_r)[col] = DB_DOUBLE;
  124. break;
  125. case DATEOID:
  126. case TIMESTAMPOID:
  127. case TIMESTAMPTZOID:
  128. LM_DBG("use DB_DATETIME result type");
  129. RES_TYPES(_r)[col] = DB_DATETIME;
  130. break;
  131. case BOOLOID:
  132. case CHAROID:
  133. case VARCHAROID:
  134. case BPCHAROID:
  135. LM_DBG("use DB_STRING result type");
  136. RES_TYPES(_r)[col] = DB_STRING;
  137. break;
  138. case TEXTOID:
  139. case BYTEAOID:
  140. LM_DBG("use DB_BLOB result type");
  141. RES_TYPES(_r)[col] = DB_BLOB;
  142. break;
  143. case BITOID:
  144. case VARBITOID:
  145. LM_DBG("use DB_BITMAP result type");
  146. RES_TYPES(_r)[col] = DB_BITMAP;
  147. break;
  148. default:
  149. LM_WARN("unhandled data type column (%.*s) type id (%d), "
  150. "use DB_STRING as default\n", RES_NAMES(_r)[col]->len,
  151. RES_NAMES(_r)[col]->s, datatype);
  152. RES_TYPES(_r)[col] = DB_STRING;
  153. break;
  154. }
  155. }
  156. return 0;
  157. }
  158. /**
  159. * Convert rows from PostgreSQL to db API representation
  160. */
  161. int db_postgres_convert_rows(const db_con_t* _h, db_res_t* _r)
  162. {
  163. char **row_buf, *s;
  164. int row, col, len;
  165. if (!_h || !_r) {
  166. LM_ERR("invalid parameter\n");
  167. return -1;
  168. }
  169. RES_ROW_N(_r) = PQntuples(CON_RESULT(_h));
  170. if (!RES_ROW_N(_r)) {
  171. LM_DBG("no rows returned from the query\n");
  172. RES_ROWS(_r) = 0;
  173. return 0;
  174. }
  175. len = sizeof(db_row_t) * RES_COL_N(_r);
  176. row_buf = (char**)pkg_malloc(len);
  177. if (!row_buf) {
  178. LM_ERR("no private memory left\n");
  179. return -1;
  180. }
  181. LM_DBG("allocate for %d columns %d bytes in row buffer at %p\n", RES_COL_N(_r), len, row_buf);
  182. memset(row_buf, 0, len);
  183. /* Allocate a row structure for each row in the current fetch. */
  184. len = sizeof(db_row_t) * RES_ROW_N(_r);
  185. RES_ROWS(_r) = (db_row_t*)pkg_malloc(len);
  186. LM_DBG("allocate %d bytes for %d rows at %p\n", len, RES_ROW_N(_r), RES_ROWS(_r));
  187. if (!RES_ROWS(_r)) {
  188. LM_ERR("no private memory left\n");
  189. return -1;
  190. }
  191. memset(RES_ROWS(_r), 0, len);
  192. for(row = 0; row < RES_ROW_N(_r); row++) {
  193. for(col = 0; col < RES_COL_N(_r); col++) {
  194. /*
  195. * The row data pointer returned by PQgetvalue points to storage
  196. * that is part of the PGresult structure. One should not modify
  197. * the data it points to, and one must explicitly copy the data
  198. * into other storage if it is to be used past the lifetime of
  199. * the PGresult structure itself.
  200. */
  201. s = PQgetvalue(CON_RESULT(_h), row, col);
  202. LM_DBG("PQgetvalue(%p,%d,%d)=[%s]\n", _h, row, col, s);
  203. len = strlen(s);
  204. row_buf[col] = pkg_malloc(len+1);
  205. if (!row_buf[col]) {
  206. LM_ERR("no private memory left");
  207. return -1;
  208. }
  209. memset(row_buf[col], 0, len+1);
  210. LM_DBG("allocated %d bytes for row_buf[%d] at %p\n", len, col, row_buf[col]);
  211. strncpy(row_buf[col], s, len);
  212. LM_DBG("[%d][%d] Column[%.*s]=[%s]\n",
  213. row, col, RES_NAMES(_r)[col]->len, RES_NAMES(_r)[col]->s, row_buf[col]);
  214. }
  215. /*
  216. ** ASSERT: row_buf contains an entire row in strings
  217. */
  218. if(db_postgres_convert_row(_h, _r, &(RES_ROWS(_r)[row]), row_buf)<0){
  219. LM_ERR("failed to convert row #%d\n", row);
  220. RES_ROW_N(_r) = row - RES_LAST_ROW(_r);
  221. for (col = 0; col < RES_COL_N(_r); col++) {
  222. LM_DBG("freeing row_buf[%d] at %p\n", col, row_buf[col]);
  223. pkg_free(row_buf[col]);
  224. }
  225. LM_DBG("freeing row buffer at %p\n", row_buf);
  226. pkg_free(row_buf);
  227. return -4;
  228. }
  229. /*
  230. * pkg_free() must be done for the above allocations now that the row
  231. * has been converted. During pg_convert_row (and subsequent pg_str2val)
  232. * processing, data types that don't need to be converted (namely STRINGS
  233. * and STR) have their addresses saved. These data types should not have
  234. * their pkg_malloc() allocations freed here because they are still
  235. * needed. However, some data types (ex: INT, DOUBLE) should have their
  236. * pkg_malloc() allocations freed because during the conversion process,
  237. * their converted values are saved in the union portion of the db_val_t
  238. * structure. BLOB will be copied during PQunescape in str2val, thus it
  239. * has to be freed here AND in pg_free_row().
  240. *
  241. * Warning: when the converted row is no longer needed, the data types
  242. * whose addresses were saved in the db_val_t structure must be freed
  243. * or a memory leak will happen. This processing should happen in the
  244. * pg_free_row() subroutine. The caller of this routine should ensure
  245. * that pg_free_rows(), pg_free_row() or pg_free_result() is eventually
  246. * called.
  247. */
  248. for (col = 0; col < RES_COL_N(_r); col++) {
  249. switch (RES_TYPES(_r)[col]) {
  250. case DB_STRING:
  251. case DB_STR:
  252. break;
  253. default:
  254. LM_DBG("[%d][%d] Col[%.*s] Type[%d] "
  255. "Freeing row_buf[%p]\n", row, col,
  256. RES_NAMES(_r)[col]->len, RES_NAMES(_r)[col]->s,
  257. RES_TYPES(_r)[col], row_buf[col]);
  258. LM_DBG("freeing row_buf[%d] at %p\n", col, row_buf[col]);
  259. pkg_free(row_buf[col]);
  260. }
  261. /*
  262. * The following housekeeping may not be technically required, but it
  263. * is a good practice to NULL pointer fields that are no longer valid.
  264. * Note that DB_STRING fields have not been pkg_free(). NULLing DB_STRING
  265. * fields would normally not be good to do because a memory leak would
  266. * occur. However, the pg_convert_row() routine has saved the DB_STRING
  267. * pointer in the db_val_t structure. The db_val_t structure will
  268. * eventually be used to pkg_free() the DB_STRING storage.
  269. */
  270. row_buf[col] = (char *)NULL;
  271. }
  272. }
  273. LM_DBG("freeing row buffer at %p\n", row_buf);
  274. pkg_free(row_buf);
  275. row_buf = NULL;
  276. return 0;
  277. }
  278. /**
  279. * Convert a row from the result query into db API representation
  280. */
  281. int db_postgres_convert_row(const db_con_t* _h, db_res_t* _r, db_row_t* _row,
  282. char **row_buf)
  283. {
  284. int col, len;
  285. if (!_h || !_r || !_row) {
  286. LM_ERR("invalid parameter value\n");
  287. return -1;
  288. }
  289. /*
  290. * Allocate storage to hold the data type value converted from a string
  291. * because PostgreSQL returns (most) data as strings
  292. */
  293. len = sizeof(db_val_t) * RES_COL_N(_r);
  294. ROW_VALUES(_row) = (db_val_t*)pkg_malloc(len);
  295. if (!ROW_VALUES(_row)) {
  296. LM_ERR("no private memory left\n");
  297. return -1;
  298. }
  299. LM_DBG("allocate %d bytes for row values at %p\n", len, ROW_VALUES(_row));
  300. ROW_N(_row) = RES_COL_N(_r);
  301. memset(ROW_VALUES(_row), 0, len);
  302. /* Save the number of columns in the ROW structure */
  303. ROW_N(_row) = RES_COL_N(_r);
  304. /* For each column in the row */
  305. for(col = 0; col < ROW_N(_row); col++) {
  306. /* Convert the string representation into the value representation */
  307. if (db_postgres_str2val(RES_TYPES(_r)[col], &(ROW_VALUES(_row)[col]),
  308. row_buf[col], strlen(row_buf[col])) < 0) {
  309. LM_ERR("failed to convert value\n");
  310. LM_DBG("free row at %pn", _row);
  311. db_free_row(_row);
  312. return -3;
  313. }
  314. }
  315. return 0;
  316. }