1 /*
2 * MySQL module result related functions
3 *
4 * Copyright (C) 2001-2003 FhG Fokus
5 * Copyright (C) 2007-2008 1&1 Internet AG
6 *
7 * This file is part of Kamailio, a free SIP server.
8 *
9 * Kamailio is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version
13 *
14 * Kamailio is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24
25 /*! \file
26 * \brief DB_MYSQL :: Result related functions
27 * \ingroup db_mysql
28 * Module: \ref db_mysql
29 */
30
31
32 #include <string.h>
33 #include <mysql.h>
34 #include "../../lib/srdb1/db_res.h"
35 #include "../../core/mem/mem.h"
36 #include "../../core/dprint.h"
37 #include "km_row.h"
38 #include "km_my_con.h"
39 #include "km_res.h"
40
41 extern int db_mysql_unsigned_type;
42
43 /*!
44 * \brief Get and convert columns from a result
45 *
46 * Get and convert columns from a result, fills the result structure
47 * with data from the database.
48 * \param _h database connection
49 * \param _r database result set
50 * \return 0 on success, negative on failure
51 */
db_mysql_get_columns(const db1_con_t * _h,db1_res_t * _r)52 int db_mysql_get_columns(const db1_con_t* _h, db1_res_t* _r)
53 {
54 int col;
55 MYSQL_FIELD* fields;
56
57 if ((!_h) || (!_r)) {
58 LM_ERR("invalid parameter\n");
59 return -1;
60 }
61
62 RES_COL_N(_r) = mysql_field_count(CON_CONNECTION(_h));
63 if (!RES_COL_N(_r)) {
64 LM_ERR("no columns returned from the query\n");
65 return -2;
66 } else {
67 LM_DBG("%d columns returned from the query\n", RES_COL_N(_r));
68 }
69
70 if (db_allocate_columns(_r, RES_COL_N(_r)) != 0) {
71 RES_COL_N(_r) = 0;
72 LM_ERR("could not allocate columns\n");
73 return -3;
74 }
75
76 fields = mysql_fetch_fields(RES_RESULT(_r));
77 for(col = 0; col < RES_COL_N(_r); col++) {
78 RES_NAMES(_r)[col] = (str*)pkg_malloc(sizeof(str));
79 if (! RES_NAMES(_r)[col]) {
80 PKG_MEM_ERROR;
81 db_free_columns(_r);
82 return -4;
83 }
84 LM_DBG("allocate %lu bytes for RES_NAMES[%d] at %p\n",
85 (unsigned long)sizeof(str), col, RES_NAMES(_r)[col]);
86
87 /* The pointer that is here returned is part of the result structure. */
88 RES_NAMES(_r)[col]->s = fields[col].name;
89 RES_NAMES(_r)[col]->len = strlen(fields[col].name);
90
91 LM_DBG("RES_NAMES(%p)[%d]=[%.*s]\n", RES_NAMES(_r)[col], col,
92 RES_NAMES(_r)[col]->len, RES_NAMES(_r)[col]->s);
93
94 switch(fields[col].type) {
95 case MYSQL_TYPE_TINY:
96 case MYSQL_TYPE_SHORT:
97 case MYSQL_TYPE_LONG:
98 case MYSQL_TYPE_INT24:
99 case MYSQL_TYPE_TIMESTAMP:
100 if ((db_mysql_unsigned_type != 0)
101 && (fields[col].flags & UNSIGNED_FLAG)) {
102 LM_DBG("use DB1_UINT result type\n");
103 RES_TYPES(_r)[col] = DB1_UINT;
104 } else {
105 LM_DBG("use DB1_INT result type\n");
106 RES_TYPES(_r)[col] = DB1_INT;
107 }
108 break;
109
110 case MYSQL_TYPE_LONGLONG:
111 if ((db_mysql_unsigned_type != 0)
112 && (fields[col].flags & UNSIGNED_FLAG)) {
113 LM_DBG("use DB1_UBIGINT result type\n");
114 RES_TYPES(_r)[col] = DB1_UBIGINT;
115 } else {
116 LM_DBG("use DB1_BIGINT result type\n");
117 RES_TYPES(_r)[col] = DB1_BIGINT;
118 }
119 break;
120
121 case MYSQL_TYPE_FLOAT:
122 case MYSQL_TYPE_DOUBLE:
123 LM_DBG("use DB1_DOUBLE result type\n");
124 RES_TYPES(_r)[col] = DB1_DOUBLE;
125 break;
126
127 case MYSQL_TYPE_DATETIME:
128 LM_DBG("use DB1_DATETIME result type\n");
129 RES_TYPES(_r)[col] = DB1_DATETIME;
130 break;
131
132 case MYSQL_TYPE_BLOB:
133 LM_DBG("use DB1_BLOB result type\n");
134 RES_TYPES(_r)[col] = DB1_BLOB;
135 break;
136
137 case FIELD_TYPE_SET:
138 LM_DBG("use DB1_BITMAP result type\n");
139 RES_TYPES(_r)[col] = DB1_BITMAP;
140 break;
141
142 case MYSQL_TYPE_DECIMAL:
143 #if MYSQL_VERSION_ID > 49999
144 case MYSQL_TYPE_NEWDECIMAL:
145 #endif
146 case MYSQL_TYPE_STRING:
147 case MYSQL_TYPE_VAR_STRING:
148 LM_DBG("use DB1_STRING result type\n");
149 RES_TYPES(_r)[col] = DB1_STRING;
150 break;
151
152 default:
153 LM_WARN("unhandled data type column (%.*s) type id (%d), "
154 "use DB1_STRING as default\n", RES_NAMES(_r)[col]->len,
155 RES_NAMES(_r)[col]->s, fields[col].type);
156 RES_TYPES(_r)[col] = DB1_STRING;
157 break;
158 }
159 }
160 return 0;
161 }
162
163
164 /*!
165 * \brief Convert rows from mysql to db API representation
166 * \param _h database connection
167 * \param _r database result set
168 * \return 0 on success, negative on failure
169 */
db_mysql_convert_rows(const db1_con_t * _h,db1_res_t * _r)170 static inline int db_mysql_convert_rows(const db1_con_t* _h, db1_res_t* _r)
171 {
172 int row;
173
174 if ((!_h) || (!_r)) {
175 LM_ERR("invalid parameter\n");
176 return -1;
177 }
178
179 RES_ROW_N(_r) = mysql_num_rows(RES_RESULT(_r));
180 if (!RES_ROW_N(_r)) {
181 LM_DBG("no rows returned from the query\n");
182 RES_ROWS(_r) = 0;
183 return 0;
184 }
185
186 if (db_allocate_rows(_r) < 0) {
187 LM_ERR("could not allocate rows");
188 RES_ROW_N(_r) = 0;
189 return -2;
190 }
191
192 for(row = 0; row < RES_ROW_N(_r); row++) {
193 RES_ROW(_r) = mysql_fetch_row(RES_RESULT(_r));
194 if (!RES_ROW(_r)) {
195 LM_ERR("driver error: %s\n", mysql_error(CON_CONNECTION(_h)));
196 RES_ROW_N(_r) = row;
197 db_free_rows(_r);
198 return -3;
199 }
200 if (db_mysql_convert_row(_h, _r, &(RES_ROWS(_r)[row])) < 0) {
201 LM_ERR("error while converting row #%d\n", row);
202 RES_ROW_N(_r) = row;
203 db_free_rows(_r);
204 return -4;
205 }
206 }
207 return 0;
208 }
209
210
211 /*!
212 * \brief Fill the result structure with data from database
213 * \param _h database connection
214 * \param _r database result
215 * \return 0 on success, negative on failure
216 */
db_mysql_convert_result(const db1_con_t * _h,db1_res_t * _r)217 int db_mysql_convert_result(const db1_con_t* _h, db1_res_t* _r)
218 {
219 if ((!_h) || (!_r)) {
220 LM_ERR("invalid parameter\n");
221 return -1;
222 }
223
224 if (db_mysql_get_columns(_h, _r) < 0) {
225 LM_ERR("error while getting column names\n");
226 return -2;
227 }
228
229 if (db_mysql_convert_rows(_h, _r) < 0) {
230 LM_ERR("error while converting rows\n");
231 db_free_columns(_r);
232 return -3;
233 }
234 return 0;
235 }
236
237
238 /*!
239 * \brief Allocate new result set with private structure
240 * \return db1_res_t object on success, NULL on failure
241 */
db_mysql_new_result(void)242 db1_res_t* db_mysql_new_result(void)
243 {
244 db1_res_t* obj;
245
246 obj = db_new_result();
247 if (!obj)
248 return NULL;
249 RES_PTR(obj) = pkg_malloc(sizeof(struct my_res));
250 if (!RES_PTR(obj)) {
251 PKG_MEM_ERROR;
252 db_free_result(obj);
253 return NULL;
254 }
255 return obj;
256 }
257