MCPcopy Create free account
hub / github.com/OpenSIPS/opensips / db_sqlite_get_columns

Function db_sqlite_get_columns

modules/db_sqlite/res.c:184–256  ·  view source on GitHub ↗

* Get and convert columns from a result */

Source from the content-addressed store, hash-verified

182 * Get and convert columns from a result
183 */
184int db_sqlite_get_columns(const db_con_t* _h, db_res_t* _r)
185{
186 int col;
187 int autoincrement;
188 const char *decltype;
189 const char* name;
190 char stable[256];
191
192 if ((!_h) || (!_r)) {
193 LM_ERR("invalid parameter\n");
194 return -1;
195 }
196
197 RES_COL_N(_r) = sqlite3_column_count(CON_SQLITE_PS(_h));
198
199 if (!RES_COL_N(_r)) {
200 LM_ERR("no columns returned from the query\n");
201 return -2;
202 } else {
203 LM_DBG("%d columns returned from the query\n", RES_COL_N(_r));
204 }
205
206 if (db_allocate_columns(_r, RES_COL_N(_r)) != 0) {
207 LM_ERR("could not allocate columns\n");
208 return -3;
209 }
210
211 for(col = 0; col < RES_COL_N(_r); col++) {
212 /* The pointer that is here returned is part of the result structure */
213
214 name = sqlite3_column_name(CON_SQLITE_PS(_h), col);
215 RES_NAMES(_r)[col]->s = *((char**)&name);
216 RES_NAMES(_r)[col]->len = strlen(RES_NAMES(_r)[col]->s);
217
218 /* check if column is autoincrement only for normal queries;
219 * for raw queries we can't know the table name */
220 if (!CON_RAW_QUERY(_h)) {
221 /* sanity check */
222 if (CON_TABLE(_h)->len > 255) {
223 LM_ERR("table name too big [%d]\n", CON_TABLE(_h)->len);
224 return -1;
225 }
226
227 /* fix possible non '\0' terminated table name */
228 memcpy(stable, CON_TABLE(_h)->s, CON_TABLE(_h)->len);
229 stable[CON_TABLE(_h)->len] = '\0';
230
231 if (sqlite3_table_column_metadata(
232 CON_CONNECTION(_h), NULL, /* db name*/ stable, /* table name */
233 name, /* column name */ NULL, NULL, NULL, NULL,
234 &autoincrement) != 0) {
235 LM_ERR("failed to fetch metadata for column [%s]\n", name);
236 return -1;
237 }
238 }
239
240 /* since DB_BITMAP not used in SQLITE we will use it
241 * here to know if value is PRIMARY KEY AUTOINCREMENT */

Callers 2

db_sqlite_convert_resultFunction · 0.85
db_sqlite_fetch_resultFunction · 0.85

Calls 2

db_allocate_columnsFunction · 0.85
get_type_from_decltypeFunction · 0.85

Tested by

no test coverage detected