db_oracle/val.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2007,2008 TRUNK MOBILE
00005  *
00006  * This file is part of Kamailio, a free SIP server.
00007  *
00008  * Kamailio is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * Kamailio is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00023 
00024 #include <stdio.h>
00025 #include <string.h>
00026 #include <time.h>
00027 #include <oci.h>
00028 #include "../../dprint.h"
00029 #include "ora_con.h"
00030 #include "dbase.h"
00031 #include "val.h"
00032 
00033 
00034 /*
00035  * Convert value to sql-string as db bind index
00036  */
00037 int db_oracle_val2str(const db_con_t* _c, const db_val_t* _v, char* _s, int* _len)
00038 {
00039    int ret;
00040 
00041    if (!_c || !_v || !_s || !_len || *_len <= 0) {
00042       LM_ERR("invalid parameter value\n");
00043       return -1;
00044    }
00045 
00046    ret = snprintf(_s, *_len, ":%u", ++CON_ORA(_c)->bindpos);
00047    if ((unsigned)ret >= (unsigned)*_len)
00048       return sql_buf_small();
00049    *_len = ret;
00050    return 0;
00051 }
00052 
00053 /*
00054  * Called after val2str to realy binding
00055  */
00056 int db_oracle_val2bind(bmap_t* _m, const db_val_t* _v, OCIDate* _o)
00057 {
00058    if (VAL_NULL(_v)) {
00059       _m->addr = NULL;
00060       _m->size = 0;
00061       _m->type = SQLT_NON;
00062       return 0;
00063    }
00064 
00065    switch (VAL_TYPE(_v)) {
00066    case DB_INT:
00067       _m->addr = (int*)&VAL_INT(_v);
00068       _m->size = sizeof(VAL_INT(_v));
00069       _m->type = SQLT_INT;
00070       break;
00071 
00072    case DB_BIGINT:
00073       LM_ERR("BIGINT not supported");
00074       return -1;
00075 
00076    case DB_BITMAP:
00077       _m->addr = (unsigned*)&VAL_BITMAP(_v);
00078       _m->size = sizeof(VAL_BITMAP(_v));
00079       _m->type = SQLT_UIN;
00080       break;
00081 
00082    case DB_DOUBLE:
00083       _m->addr = (double*)&VAL_DOUBLE(_v);
00084       _m->size = sizeof(VAL_DOUBLE(_v));
00085       _m->type = SQLT_FLT;
00086       break;
00087 
00088    case DB_STRING:
00089       _m->addr = (char*)VAL_STRING(_v);
00090       _m->size = strlen(VAL_STRING(_v))+1;
00091       _m->type = SQLT_STR;
00092       break;
00093 
00094    case DB_STR:
00095       {
00096          unsigned len = VAL_STR(_v).len;
00097          char *estr, *pstr = VAL_STR(_v).s;
00098 
00099          estr = (char*)memchr(pstr, 0, len);
00100          if (estr) {
00101             LM_WARN("truncate STR len from %u to: '%s'\n",
00102                len, pstr);
00103             len = (unsigned)(estr - pstr) + 1;
00104          }
00105          _m->size = len;
00106          _m->addr = pstr;
00107          _m->type = SQLT_CHR;
00108       }
00109       break;
00110 
00111    case DB_DATETIME:
00112       {
00113          struct tm* tm = localtime(&VAL_TIME(_v));
00114          if (tm->tm_sec == 60)
00115             --tm->tm_sec;
00116          OCIDateSetDate(_o, (ub2)(tm->tm_year + 1900),
00117             (ub1)(tm->tm_mon + 1), (ub1)tm->tm_mday);
00118          OCIDateSetTime(_o, (ub1)tm->tm_hour, (ub1)tm->tm_min,
00119             (ub1)tm->tm_sec);
00120          _m->addr = _o;
00121          _m->size = sizeof(*_o);
00122          _m->type = SQLT_ODT;
00123       }
00124       break;
00125 
00126    case DB_BLOB:
00127       _m->addr = VAL_BLOB(_v).s;
00128       _m->size = VAL_BLOB(_v).len;
00129       _m->type = SQLT_CLOB;
00130       break;
00131 
00132    default:
00133        LM_ERR("unknown data type\n");
00134        return -1;
00135    }
00136    return 0;
00137 }

Generated on Tue May 22 14:00:32 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6