db_res.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * $Id$
  3. *
  4. * POSTGRES module, portions of this code were templated using
  5. * the mysql module, thus it's similarity.
  6. *
  7. *
  8. * Copyright (C) 2003 August.Net Services, LLC
  9. *
  10. * This file is part of ser, a free SIP server.
  11. *
  12. * ser is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version
  16. *
  17. * For a license to use the ser software under conditions
  18. * other than those described here, or to purchase support for this
  19. * software, please contact iptel.org by e-mail at the following addresses:
  20. * [email protected]
  21. *
  22. * ser is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. *
  31. * ---
  32. *
  33. * History
  34. * -------
  35. * 2003-04-06 initial code written (Greg Fausak/Andy Fullford)
  36. *
  37. */
  38. #include <stdlib.h>
  39. #include "../../db/db_res.h"
  40. #include "../../dprint.h"
  41. #include "../../mem/mem.h"
  42. #include "defs.h"
  43. #include "con_postgres.h"
  44. #include "pg_type.h"
  45. #include "aug_std.h"
  46. int str2valp(db_type_t _t, db_val_t* _v, const char* _s, int _l, void *_p);
  47. /*
  48. * Create a new result structure and initialize it
  49. */
  50. db_res_t* new_result_pg(char *parent)
  51. {
  52. db_res_t* r;
  53. r = (db_res_t*)aug_alloc(sizeof(db_res_t), parent);
  54. RES_NAMES(r) = 0;
  55. RES_TYPES(r) = 0;
  56. RES_COL_N(r) = 0;
  57. RES_ROWS(r) = 0;
  58. RES_ROW_N(r) = 0;
  59. return r;
  60. }
  61. /*
  62. * Get and convert columns from a result
  63. */
  64. static inline int get_columns(db_con_t* _h, db_res_t* _r)
  65. {
  66. int n, i;
  67. n = PQnfields(CON_RESULT(_h));
  68. if (!n) {
  69. LOG(L_ERR, "get_columns(): No columns\n");
  70. return -2;
  71. }
  72. RES_NAMES(_r) = (db_key_t*)aug_alloc(sizeof(db_key_t) * n, _r);
  73. RES_TYPES(_r) = (db_type_t*)aug_alloc(sizeof(db_type_t) * n, _r);
  74. RES_COL_N(_r) = n;
  75. for(i = 0; i < n; i++) {
  76. int ft;
  77. RES_NAMES(_r)[i] = aug_strdup(PQfname(CON_RESULT(_h),i),
  78. RES_NAMES(_r));
  79. switch(ft = PQftype(CON_RESULT(_h),i))
  80. {
  81. case INT2OID:
  82. case INT4OID:
  83. case INT8OID:
  84. RES_TYPES(_r)[i] = DB_INT;
  85. break;
  86. case FLOAT4OID:
  87. case FLOAT8OID:
  88. case NUMERICOID:
  89. RES_TYPES(_r)[i] = DB_DOUBLE;
  90. break;
  91. case DATEOID:
  92. case TIMESTAMPOID:
  93. case TIMESTAMPTZOID:
  94. RES_TYPES(_r)[i] = DB_DATETIME;
  95. break;
  96. case VARCHAROID:
  97. RES_TYPES(_r)[i] = DB_STRING;
  98. break;
  99. default:
  100. LOG(L_ERR, "unknown type %d\n", ft);
  101. RES_TYPES(_r)[i] = DB_STRING;
  102. break;
  103. }
  104. }
  105. return 0;
  106. }
  107. /*
  108. * Convert a row from result into db API representation
  109. */
  110. int convert_row_pg(db_con_t* _h, db_res_t* _res, db_row_t* _r, char **row_buf,
  111. char *parent)
  112. {
  113. int i;
  114. ROW_VALUES(_r) = (db_val_t*)aug_alloc(
  115. sizeof(db_val_t) * RES_COL_N(_res), parent);
  116. ROW_N(_r) = RES_COL_N(_res);
  117. /* I'm not sure about this */
  118. /* PQfsize() gives us the native length in the database, so */
  119. /* an int would be 4, not the strlen of the value */
  120. /* however, strlen of the value would be easy to strlen() */
  121. for(i = 0; i < RES_COL_N(_res); i++) {
  122. if (str2valp(RES_TYPES(_res)[i], &(ROW_VALUES(_r)[i]),
  123. row_buf[i],
  124. PQfsize(CON_RESULT(_h),i),
  125. (void *) ROW_VALUES(_r)) < 0) {
  126. LOG(L_ERR, "convert_row_pg(): Error while converting value\n");
  127. return -3;
  128. }
  129. }
  130. return 0;
  131. }
  132. /*
  133. * Convert rows from postgres to db API representation
  134. */
  135. static inline int convert_rows(db_con_t* _h, db_res_t* _r)
  136. {
  137. int n, i, j, k;
  138. char **row_buf, *s;
  139. n = PQntuples(CON_RESULT(_h));
  140. RES_ROW_N(_r) = n;
  141. if (!n) {
  142. RES_ROWS(_r) = 0;
  143. return 0;
  144. }
  145. RES_ROWS(_r) = (struct db_row*)aug_alloc(sizeof(db_row_t) * n,_r);
  146. j = RES_COL_N(_r);
  147. row_buf = (char **) aug_alloc(sizeof(char *) * (j + 1), CON_SQLURL(_h));
  148. /* j is the number of columns in the answer set */
  149. /* n is the number of rows in the answer set */
  150. for(i = 0; i < n; i++) {
  151. for(k = 0; k < j; k++) {
  152. if(PQgetisnull(CON_RESULT(_h), i, k)) {
  153. /*
  154. ** I don't believe there is a NULL
  155. ** representation, so, I'll cheat
  156. */
  157. s = "";
  158. } else {
  159. s = PQgetvalue(CON_RESULT(_h), i, k);
  160. }
  161. *(row_buf + k) = aug_strdup(s, row_buf);
  162. }
  163. *(row_buf + k) = (char *) 0;
  164. /*
  165. ** ASSERT: row_buf contains an entire row in strings
  166. */
  167. if (convert_row_pg(_h, _r, &(RES_ROWS(_r)[i]), row_buf,
  168. (char *) RES_ROWS(_r)) < 0) {
  169. LOG(L_ERR, "convert_rows(): Error converting row #%d\n",
  170. i);
  171. RES_ROW_N(_r) = i;
  172. aug_free(row_buf);
  173. return -4;
  174. }
  175. }
  176. aug_free(row_buf);
  177. return 0;
  178. }
  179. /*
  180. * Fill the structure with data from database
  181. */
  182. int convert_result(db_con_t* _h, db_res_t* _r)
  183. {
  184. if (get_columns(_h, _r) < 0) {
  185. LOG(L_ERR, "convert_result(): Error getting column names\n");
  186. return -2;
  187. }
  188. if (convert_rows(_h, _r) < 0) {
  189. LOG(L_ERR, "convert_result(): Error while converting rows\n");
  190. return -3;
  191. }
  192. return 0;
  193. }
  194. /*
  195. * Release memory used by a result structure
  196. */
  197. int free_result(db_res_t* _r)
  198. {
  199. aug_free(_r);
  200. return 0;
  201. }