1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
/*
* $Id$
*
* DBText library
*
* Copyright (C) 2001-2003 FhG Fokus
*
* This file is part of Kamailio, a free SIP server.
*
* Kamailio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* Kamailio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* History:
* --------
* 2003-02-05 created by Daniel
*
*/
#include <string.h>
#include "../../lib/srdb1/db.h"
#include "../../mem/mem.h"
#include "dbt_res.h"
#include "dbt_api.h"
int dbt_use_table(db1_con_t* _h, const str* _t)
{
return db_use_table(_h, _t);
}
/*
* Get and convert columns from a result
*/
static int dbt_get_columns(db1_res_t* _r, dbt_result_p _dres)
{
int col;
if (!_r || !_dres) {
LM_ERR("invalid parameter\n");
return -1;
}
RES_COL_N(_r) = _dres->nrcols;
if (!RES_COL_N(_r)) {
LM_ERR("no columns\n");
return -2;
}
if (db_allocate_columns(_r, RES_COL_N(_r)) != 0) {
LM_ERR("could not allocate columns");
return -3;
}
for(col = 0; col < RES_COL_N(_r); col++) {
/*
* Its would be not necessary to allocate here new memory, because of
* the internal structure of the db_text module. But we do this anyway
* to stay confirm to the other database modules.
*/
RES_NAMES(_r)[col] = (str*)pkg_malloc(sizeof(str));
if (! RES_NAMES(_r)[col]) {
LM_ERR("no private memory left\n");
db_free_columns(_r);
return -4;
}
LM_DBG("allocate %d bytes for RES_NAMES[%d] at %p",
(int)sizeof(str), col,
RES_NAMES(_r)[col]);
RES_NAMES(_r)[col]->s = _dres->colv[col].name.s;
RES_NAMES(_r)[col]->len = _dres->colv[col].name.len;
switch(_dres->colv[col].type)
{
case DB1_STR:
case DB1_STRING:
case DB1_BLOB:
case DB1_INT:
case DB1_DATETIME:
case DB1_DOUBLE:
RES_TYPES(_r)[col] = _dres->colv[col].type;
break;
default:
LM_WARN("unhandled data type column (%.*s) type id (%d), "
"use STR as default\n", RES_NAMES(_r)[col]->len,
RES_NAMES(_r)[col]->s, _dres->colv[col].type);
RES_TYPES(_r)[col] = DB1_STR;
break;
}
}
return 0;
}
/*
* Convert a row from result into db API representation
*/
static int dbt_convert_row(db1_res_t* _res, db_row_t* _r, dbt_row_p _r1)
{
int i;
if (!_r || !_res || !_r1) {
LM_ERR("invalid parameter value\n");
return -1;
}
if (db_allocate_row(_res, _r) != 0) {
LM_ERR("could not allocate row");
return -2;
}
for(i = 0; i < RES_COL_N(_res); i++) {
(ROW_VALUES(_r)[i]).nul = _r1->fields[i].nul;
switch(RES_TYPES(_res)[i])
{
case DB1_INT:
VAL_INT(&(ROW_VALUES(_r)[i])) =
_r1->fields[i].val.int_val;
VAL_TYPE(&(ROW_VALUES(_r)[i])) = DB1_INT;
break;
case DB1_BIGINT:
LM_ERR("BIGINT not supported");
return -1;
case DB1_DOUBLE:
VAL_DOUBLE(&(ROW_VALUES(_r)[i])) =
_r1->fields[i].val.double_val;
VAL_TYPE(&(ROW_VALUES(_r)[i])) = DB1_DOUBLE;
break;
case DB1_STRING:
VAL_STR(&(ROW_VALUES(_r)[i])).s =
_r1->fields[i].val.str_val.s;
VAL_STR(&(ROW_VALUES(_r)[i])).len =
_r1->fields[i].val.str_val.len;
VAL_TYPE(&(ROW_VALUES(_r)[i])) = DB1_STRING;
VAL_FREE(&(ROW_VALUES(_r)[i])) = 0;
break;
case DB1_STR:
VAL_STR(&(ROW_VALUES(_r)[i])).s =
_r1->fields[i].val.str_val.s;
VAL_STR(&(ROW_VALUES(_r)[i])).len =
_r1->fields[i].val.str_val.len;
VAL_TYPE(&(ROW_VALUES(_r)[i])) = DB1_STR;
VAL_FREE(&(ROW_VALUES(_r)[i])) = 0;
break;
case DB1_DATETIME:
VAL_INT(&(ROW_VALUES(_r)[i])) =
_r1->fields[i].val.int_val;
VAL_TYPE(&(ROW_VALUES(_r)[i])) = DB1_DATETIME;
break;
case DB1_BLOB:
VAL_STR(&(ROW_VALUES(_r)[i])).s =
_r1->fields[i].val.str_val.s;
VAL_STR(&(ROW_VALUES(_r)[i])).len =
_r1->fields[i].val.str_val.len;
VAL_TYPE(&(ROW_VALUES(_r)[i])) = DB1_BLOB;
VAL_FREE(&(ROW_VALUES(_r)[i])) = 0;
break;
case DB1_BITMAP:
VAL_INT(&(ROW_VALUES(_r)[i])) =
_r1->fields[i].val.bitmap_val;
VAL_TYPE(&(ROW_VALUES(_r)[i])) = DB1_INT;
break;
default:
LM_ERR("val type [%d] not supported", RES_TYPES(_res)[i]);
return -1;
}
}
return 0;
}
/*
* Convert rows from internal to db API representation
*/
static int dbt_convert_rows(db1_res_t* _r, dbt_result_p _dres)
{
int row;
dbt_row_p _rp = NULL;
if (!_r || !_dres) {
LM_ERR("invalid parameter\n");
return -1;
}
RES_ROW_N(_r) = _dres->nrrows;
if (!RES_ROW_N(_r)) {
return 0;
}
if (db_allocate_rows(_r) < 0) {
LM_ERR("could not allocate rows");
return -2;
}
row = 0;
_rp = _dres->rows;
while(_rp) {
if (dbt_convert_row(_r, &(RES_ROWS(_r)[row]), _rp) < 0) {
LM_ERR("failed to convert row #%d\n", row);
RES_ROW_N(_r) = row;
db_free_rows(_r);
return -4;
}
row++;
_rp = _rp->next;
}
return 0;
}
/*
* Fill the structure with data from database
*/
static int dbt_convert_result(db1_res_t* _r, dbt_result_p _dres)
{
if (!_r || !_dres) {
LM_ERR("invalid parameter\n");
return -1;
}
if (dbt_get_columns(_r, _dres) < 0) {
LM_ERR("failed to get column names\n");
return -2;
}
if (dbt_convert_rows(_r, _dres) < 0) {
LM_ERR("failed to convert rows\n");
db_free_columns(_r);
return -3;
}
return 0;
}
/*
* Retrieve result set
*/
int dbt_get_result(db1_res_t** _r, dbt_result_p _dres)
{
if ( !_r) {
LM_ERR("invalid parameter value\n");
return -1;
}
if (!_dres)
{
LM_ERR("failed to get result\n");
*_r = 0;
return -3;
}
*_r = db_new_result();
if (*_r == 0)
{
LM_ERR("no private memory left\n");
return -2;
}
if (dbt_convert_result(*_r, _dres) < 0)
{
LM_ERR("failed to convert result\n");
pkg_free(*_r);
return -4;
}
(*_r)->ptr = _dres;
return 0;
}
|