db_res.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 "../../db/db_con.h"
  41. #include "../../dprint.h"
  42. #include "../../mem/mem.h"
  43. #include "defs.h"
  44. #include "con_postgres.h"
  45. #include "pg_type.h"
  46. #include "aug_std.h"
  47. int str2valp(db_type_t _t, db_val_t* _v, const char* _s, int _l, void *_p);
  48. /*
  49. * Create a new result structure and initialize it
  50. */
  51. db_res_t* new_result_pg(char *parent)
  52. {
  53. db_res_t* r;
  54. r = (db_res_t*)aug_alloc(sizeof(db_res_t), parent);
  55. RES_NAMES(r) = 0;
  56. RES_TYPES(r) = 0;
  57. RES_COL_N(r) = 0;
  58. RES_ROWS(r) = 0;
  59. RES_ROW_N(r) = 0;
  60. return r;
  61. }
  62. /*
  63. * Get and convert columns from a result
  64. */
  65. static inline int get_columns(db_con_t* _h, db_res_t* _r)
  66. {
  67. int n, i;
  68. n = PQnfields(CON_RESULT(_h));
  69. if (!n) {
  70. LOG(L_ERR, "get_columns(): No columns\n");
  71. return -2;
  72. }
  73. RES_NAMES(_r) = (db_key_t*)aug_alloc(sizeof(db_key_t) * n, _r);
  74. RES_TYPES(_r) = (db_type_t*)aug_alloc(sizeof(db_type_t) * n, _r);
  75. RES_COL_N(_r) = n;
  76. for(i = 0; i < n; i++) {
  77. int ft;
  78. RES_NAMES(_r)[i] = aug_strdup(PQfname(CON_RESULT(_h),i),
  79. RES_NAMES(_r));
  80. switch(ft = PQftype(CON_RESULT(_h),i))
  81. {
  82. case INT2OID:
  83. case INT4OID:
  84. case INT8OID:
  85. RES_TYPES(_r)[i] = DB_INT;
  86. break;
  87. case FLOAT4OID:
  88. case FLOAT8OID:
  89. case NUMERICOID:
  90. RES_TYPES(_r)[i] = DB_DOUBLE;
  91. break;
  92. case DATEOID:
  93. case TIMESTAMPOID:
  94. case TIMESTAMPTZOID:
  95. RES_TYPES(_r)[i] = DB_DATETIME;
  96. break;
  97. case VARCHAROID:
  98. RES_TYPES(_r)[i] = DB_STRING;
  99. break;
  100. default:
  101. LOG(L_ERR, "unknown type %d\n", ft);
  102. RES_TYPES(_r)[i] = DB_STRING;
  103. break;
  104. }
  105. }
  106. return 0;
  107. }
  108. /*
  109. * Convert a row from result into db API representation
  110. */
  111. int convert_row_pg(db_con_t* _h, db_res_t* _res, db_row_t* _r, char **row_buf,
  112. char *parent)
  113. {
  114. int i;
  115. ROW_VALUES(_r) = (db_val_t*)aug_alloc(
  116. sizeof(db_val_t) * RES_COL_N(_res), parent);
  117. ROW_N(_r) = RES_COL_N(_res);
  118. /* I'm not sure about this */
  119. /* PQfsize() gives us the native length in the database, so */
  120. /* an int would be 4, not the strlen of the value */
  121. /* however, strlen of the value would be easy to strlen() */
  122. for(i = 0; i < RES_COL_N(_res); i++) {
  123. if (str2valp(RES_TYPES(_res)[i], &(ROW_VALUES(_r)[i]),
  124. row_buf[i],
  125. PQfsize(CON_RESULT(_h),i),
  126. (void *) ROW_VALUES(_r)) < 0) {
  127. LOG(L_ERR, "convert_row_pg(): Error while converting value\n");
  128. return -3;
  129. }
  130. }
  131. return 0;
  132. }
  133. /*
  134. * Convert rows from postgres to db API representation
  135. */
  136. static inline int convert_rows(db_con_t* _h, db_res_t* _r)
  137. {
  138. int n, i, j, k;
  139. char **row_buf, *s;
  140. n = PQntuples(CON_RESULT(_h));
  141. RES_ROW_N(_r) = n;
  142. if (!n) {
  143. RES_ROWS(_r) = 0;
  144. return 0;
  145. }
  146. RES_ROWS(_r) = (struct db_row*)aug_alloc(sizeof(db_row_t) * n,_r);
  147. j = RES_COL_N(_r);
  148. row_buf = (char **) aug_alloc(sizeof(char *) * (j + 1), CON_SQLURL(_h));
  149. /* j is the number of columns in the answer set */
  150. /* n is the number of rows in the answer set */
  151. for(i = 0; i < n; i++) {
  152. for(k = 0; k < j; k++) {
  153. if(PQgetisnull(CON_RESULT(_h), i, k)) {
  154. /*
  155. ** I don't believe there is a NULL
  156. ** representation, so, I'll cheat
  157. */
  158. s = "";
  159. } else {
  160. s = PQgetvalue(CON_RESULT(_h), i, k);
  161. }
  162. *(row_buf + k) = aug_strdup(s, row_buf);
  163. }
  164. *(row_buf + k) = (char *) 0;
  165. /*
  166. ** ASSERT: row_buf contains an entire row in strings
  167. */
  168. if (convert_row_pg(_h, _r, &(RES_ROWS(_r)[i]), row_buf,
  169. (char *) RES_ROWS(_r)) < 0) {
  170. LOG(L_ERR, "convert_rows(): Error converting row #%d\n",
  171. i);
  172. RES_ROW_N(_r) = i;
  173. aug_free(row_buf);
  174. return -4;
  175. }
  176. }
  177. aug_free(row_buf);
  178. return 0;
  179. }
  180. /*
  181. * Fill the structure with data from database
  182. */
  183. int convert_result(db_con_t* _h, db_res_t* _r)
  184. {
  185. if (get_columns(_h, _r) < 0) {
  186. LOG(L_ERR, "convert_result(): Error getting column names\n");
  187. return -2;
  188. }
  189. if (convert_rows(_h, _r) < 0) {
  190. LOG(L_ERR, "convert_result(): Error while converting rows\n");
  191. return -3;
  192. }
  193. return 0;
  194. }
  195. /*
  196. * Release memory used by a result structure
  197. */
  198. int free_result(db_res_t* _r)
  199. {
  200. aug_free(_r);
  201. return 0;
  202. }