bdb_val.c

Go to the documentation of this file.
00001 /*
00002  * $Id: bdb_val.c 4986 2008-09-24 11:02:42Z henningw $
00003  *
00004  * db_berkeley module, portions of this code were templated using
00005  * the dbtext and postgres modules.
00006 
00007  * Copyright (C) 2007 Cisco Systems
00008  *
00009  * This file is part of Kamailio, a free SIP server.
00010  *
00011  * Kamailio is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version
00015  *
00016  * Kamailio is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License 
00022  * along with this program; if not, write to the Free Software 
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  * 
00025  * History:
00026  * --------
00027  * 2007-09-19  genesis (wiquan)
00028  */
00029  
00030 
00031 #include "../../db/db_val.h"
00032 #include "../../db/db_ut.h"
00033 #include "db_berkeley.h"
00034 #include "bdb_res.h"
00035 #include "bdb_val.h"
00036 #include <string.h>
00037 
00038 /**
00039  * A copy of db_ut::db_time2str EXCEPT does not wrap the date in single-quotes
00040  *
00041  * Convert a time_t value to string (w.o single-quote)
00042  * \param _v source value
00043  * \param _s target string
00044  * \param _l available length and target length
00045  * \return -1 on error, 0 on success
00046  * \todo This functions add quotes to the time value. This
00047  * should be done in the val2str function, as some databases
00048  * like db_berkeley don't need or like this at all.
00049  */
00050 inline int bdb_time2str(time_t _v, char* _s, int* _l)
00051 {
00052    struct tm* t;
00053    int l;
00054 
00055    if ((!_s) || (!_l) || (*_l < 2)) {
00056       LM_ERR("Invalid parameter value\n");
00057       return -1;
00058    }
00059 
00060 // *_s++ = '\'';
00061 
00062    /* Convert time_t structure to format accepted by the database */
00063    t = localtime(&_v);
00064    l = strftime(_s, *_l -1, "%Y-%m-%d %H:%M:%S", t);
00065 
00066    if (l == 0) {
00067       LM_ERR("Error during time conversion\n");
00068       /* the value of _s is now unspecified */
00069       _s = NULL;
00070       _l = 0;
00071       return -1;
00072    }
00073    *_l = l;
00074 
00075 // *(_s + l) = '\'';
00076 // *_l = l + 2;
00077    return 0;
00078 }
00079 
00080 /**
00081  * Does not copy strings
00082  */
00083 int bdb_str2val(db_type_t _t, db_val_t* _v, char* _s, int _l)
00084 {
00085 
00086    static str dummy_string = {"", 0};
00087 
00088    if(!_s)
00089    {
00090       memset(_v, 0, sizeof(db_val_t));
00091       /* Initialize the string pointers to a dummy empty
00092        * string so that we do not crash when the NULL flag
00093        * is set but the module does not check it properly
00094        */
00095       VAL_STRING(_v) = dummy_string.s;
00096       VAL_STR(_v) = dummy_string;
00097       VAL_BLOB(_v) = dummy_string;
00098       VAL_TYPE(_v) = _t;
00099       VAL_NULL(_v) = 1;
00100       return 0;
00101    }
00102    VAL_NULL(_v) = 0;
00103 
00104    switch(_t) {
00105    case DB_INT:
00106       if (db_str2int(_s, &VAL_INT(_v)) < 0) {
00107          LM_ERR("Error while converting INT value from string\n");
00108          return -2;
00109       } else {
00110          VAL_TYPE(_v) = DB_INT;
00111          return 0;
00112       }
00113       break;
00114 
00115    case DB_BIGINT:
00116          LM_ERR("BIGINT not supported");
00117          return -1;
00118 
00119    case DB_BITMAP:
00120       if (db_str2int(_s, &VAL_INT(_v)) < 0) {
00121          LM_ERR("Error while converting BITMAP value from string\n");
00122          return -3;
00123       } else {
00124          VAL_TYPE(_v) = DB_BITMAP;
00125          return 0;
00126       }
00127       break;
00128 
00129    case DB_DOUBLE:
00130       if (db_str2double(_s, &VAL_DOUBLE(_v)) < 0) {
00131          LM_ERR("Error while converting DOUBLE value from string\n");
00132          return -4;
00133       } else {
00134          VAL_TYPE(_v) = DB_DOUBLE;
00135          return 0;
00136       }
00137       break;
00138 
00139    case DB_STRING:
00140       VAL_STRING(_v) = _s;
00141       VAL_TYPE(_v) = DB_STRING;
00142       VAL_FREE(_v) = 1;
00143       
00144       if( strlen(_s)==4 && !strncasecmp(_s, "NULL", 4) )
00145          VAL_NULL(_v) = 1;
00146       
00147       return 0;
00148 
00149    case DB_STR:
00150       VAL_STR(_v).s = (char*)_s;
00151       VAL_STR(_v).len = _l;
00152       VAL_TYPE(_v) = DB_STR;
00153       VAL_FREE(_v) = 1;
00154 
00155       if( strlen(_s)==4 && !strncasecmp(_s, "NULL", 4) )
00156          VAL_NULL(_v) = 1;
00157 
00158       return 0;
00159 
00160    case DB_DATETIME:
00161       if (db_str2time(_s, &VAL_TIME(_v)) < 0) {
00162          LM_ERR("Error converting datetime\n");
00163          return -5;
00164       } else {
00165          VAL_TYPE(_v) = DB_DATETIME;
00166          return 0;
00167       }
00168       break;
00169 
00170    case DB_BLOB:
00171       VAL_BLOB(_v).s = _s;
00172       VAL_TYPE(_v) = DB_BLOB;
00173       LM_DBG("got blob len %d\n", _l);
00174       return 0;
00175    }
00176 
00177    return -6;
00178 }
00179 
00180 
00181 /*
00182  * Used when converting result from a query
00183  */
00184 int bdb_val2str(db_val_t* _v, char* _s, int* _len)
00185 {
00186    int l;
00187 
00188    if (VAL_NULL(_v)) 
00189    {
00190       *_len = snprintf(_s, *_len, "NULL");
00191       return 0;
00192    }
00193    
00194    switch(VAL_TYPE(_v)) {
00195    case DB_INT:
00196       if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
00197          LM_ERR("Error while converting int to string\n");
00198          return -2;
00199       } else {
00200          LM_DBG("Converted int to string\n");
00201          return 0;
00202       }
00203       break;
00204 
00205    case DB_BITMAP:
00206       if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
00207          LM_ERR("Error while converting bitmap to string\n");
00208          return -3;
00209       } else {
00210          LM_DBG("Converted bitmap to string\n");
00211          return 0;
00212       }
00213       break;
00214 
00215    case DB_DOUBLE:
00216       if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
00217          LM_ERR("Error while converting double  to string\n");
00218          return -3;
00219       } else {
00220          LM_DBG("Converted double to string\n");
00221          return 0;
00222       }
00223       break;
00224 
00225    case DB_STRING:
00226       l = strlen(VAL_STRING(_v));
00227       if (*_len < l ) 
00228       {  LM_ERR("Destination buffer too short for string\n");
00229          return -4;
00230       } 
00231       else 
00232       {  LM_DBG("Converted string to string\n");
00233          strncpy(_s, VAL_STRING(_v) , l);
00234          _s[l] = 0;
00235          *_len = l;
00236          return 0;
00237       }
00238       break;
00239 
00240    case DB_STR:
00241       l = VAL_STR(_v).len;
00242       if (*_len < l) 
00243       {
00244          LM_ERR("Destination buffer too short for str\n");
00245          return -5;
00246       } 
00247       else 
00248       {
00249          LM_DBG("Converted str to string\n");
00250          strncpy(_s, VAL_STR(_v).s , VAL_STR(_v).len);
00251          *_len = VAL_STR(_v).len;
00252          return 0;
00253       }
00254       break;
00255 
00256    case DB_DATETIME:
00257       if (bdb_time2str(VAL_TIME(_v), _s, _len) < 0) {
00258          LM_ERR("Error while converting time_t to string\n");
00259          return -6;
00260       } else {
00261          LM_DBG("Converted time_t to string\n");
00262          return 0;
00263       }
00264       break;
00265 
00266    case DB_BLOB:
00267       l = VAL_BLOB(_v).len;
00268       if (*_len < l) 
00269       {
00270          LM_ERR("Destination buffer too short for blob\n");
00271          return -7;
00272       } 
00273       else 
00274       {
00275          LM_DBG("Converting BLOB [%s]\n", _s);
00276          _s = VAL_BLOB(_v).s;
00277          *_len = 0;
00278          return -8;
00279       }
00280       break;
00281 
00282    default:
00283       LM_DBG("Unknown data type\n");
00284       return -8;
00285    }
00286 }

Generated on Thu May 17 12:00:25 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6