db_val.c

00001 /*
00002  * $Id$ 
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  * Copyright (C) 2008-2009 1&1 Internet AG
00006  *
00007  * This file is part of Kamailio, a free SIP server.
00008  *
00009  * Kamailio is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version
00013  *
00014  * Kamailio is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License 
00020  * along with this program; if not, write to the Free Software 
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  */
00023 
00024 #include "db_ut.h"
00025 
00026 #include <stdio.h>
00027 #include <time.h>
00028 
00045 int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
00046                 const unsigned int _cpy)
00047 {
00048         static str dummy_string = {"", 0};
00049         
00050         if (!_v) {
00051                 LM_ERR("invalid parameter value\n");
00052                 return -1;
00053         }
00054         /* A NULL string is a SQL NULL value, otherwise its an empty value */
00055         if (!_s) {
00056                 LM_DBG("converting NULL value");
00057                 memset(_v, 0, sizeof(db_val_t));
00058                         /* Initialize the string pointers to a dummy empty
00059                          * string so that we do not crash when the NULL flag
00060                          * is set but the module does not check it properly
00061                          */
00062                 VAL_STRING(_v) = dummy_string.s;
00063                 VAL_STR(_v) = dummy_string;
00064                 VAL_BLOB(_v) = dummy_string;
00065                 VAL_TYPE(_v) = _t;
00066                 VAL_NULL(_v) = 1;
00067                 return 0;
00068         }
00069         VAL_NULL(_v) = 0;
00070 
00071         switch(_t) {
00072         case DB1_INT:
00073                 LM_DBG("converting INT [%s]\n", _s);
00074                 if (db_str2int(_s, &VAL_INT(_v)) < 0) {
00075                         LM_ERR("error while converting integer value from string\n");
00076                         return -2;
00077                 } else {
00078                         VAL_TYPE(_v) = DB1_INT;
00079                         return 0;
00080                 }
00081                 break;
00082 
00083         case DB1_BIGINT:
00084                 LM_DBG("converting BIGINT [%s]\n", _s);
00085                 if (db_str2longlong(_s, &VAL_BIGINT(_v)) < 0) {
00086                         LM_ERR("error while converting big integer value from string\n");
00087                         return -3;
00088                 } else {
00089                         VAL_TYPE(_v) = DB1_BIGINT;
00090                         return 0;
00091                 }
00092                 break;
00093 
00094         case DB1_BITMAP:
00095                 LM_DBG("converting BITMAP [%s]\n", _s);
00096                 if (db_str2int(_s, &VAL_INT(_v)) < 0) {
00097                         LM_ERR("error while converting bitmap value from string\n");
00098                         return -4;
00099                 } else {
00100                         VAL_TYPE(_v) = DB1_BITMAP;
00101                         return 0;
00102                 }
00103                 break;
00104         
00105         case DB1_DOUBLE:
00106                 LM_DBG("converting DOUBLE [%s]\n", _s);
00107                 if (db_str2double(_s, &VAL_DOUBLE(_v)) < 0) {
00108                         LM_ERR("error while converting double value from string\n");
00109                         return -5;
00110                 } else {
00111                         VAL_TYPE(_v) = DB1_DOUBLE;
00112                         return 0;
00113                 }
00114                 break;
00115 
00116         case DB1_STRING:
00117                 LM_DBG("converting STRING [%s]\n", _s);
00118 
00119                 if (_cpy == 0) {
00120                         VAL_STRING(_v) = _s;
00121                 } else {
00122                         VAL_STRING(_v) = pkg_malloc(_l + 1);
00123                         if (VAL_STRING(_v) == NULL) {
00124                                 LM_ERR("no private memory left\n");
00125                                 return -6;
00126                         }
00127                         LM_DBG("allocate %d bytes memory for STRING at %p", _l + 1, VAL_STRING(_v));
00128                         strncpy((char*)VAL_STRING(_v), _s, _l);
00129                         ((char*)VAL_STRING(_v))[_l] = '\0';
00130                         VAL_FREE(_v) = 1;
00131                 }
00132 
00133                 VAL_TYPE(_v) = DB1_STRING;
00134                 return 0;
00135 
00136         case DB1_STR:
00137                 LM_DBG("converting STR [%.*s]\n", _l, _s);
00138 
00139                 if (_cpy == 0) {
00140                         VAL_STR(_v).s = (char*) _s;
00141                 } else {
00142                         VAL_STR(_v).s = pkg_malloc(_l);
00143                         if (VAL_STR(_v).s == NULL) {
00144                                 LM_ERR("no private memory left\n");
00145                                 return -7;
00146                         }
00147                         LM_DBG("allocate %d bytes memory for STR at %p", _l, VAL_STR(_v).s);
00148                         strncpy(VAL_STR(_v).s, _s, _l);
00149                         VAL_FREE(_v) = 1;
00150                 }
00151 
00152                 VAL_STR(_v).len = _l;
00153                 VAL_TYPE(_v) = DB1_STR;
00154                 return 0;
00155 
00156         case DB1_DATETIME:
00157                 LM_DBG("converting DATETIME [%s]\n", _s);
00158                 if (db_str2time(_s, &VAL_TIME(_v)) < 0) {
00159                         LM_ERR("error while converting datetime value from string\n");
00160                         return -8;
00161                 } else {
00162                         VAL_TYPE(_v) = DB1_DATETIME;
00163                         return 0;
00164                 }
00165                 break;
00166 
00167         case DB1_BLOB:
00168                 LM_DBG("converting BLOB [%.*s]\n", _l, _s);
00169 
00170                 if (_cpy == 0) {
00171                         VAL_BLOB(_v).s = (char*) _s;
00172                 } else {
00173                         VAL_BLOB(_v).s = pkg_malloc(_l);
00174                         if (VAL_BLOB(_v).s == NULL) {
00175                                 LM_ERR("no private memory left\n");
00176                                 return -9;
00177                         }
00178                         LM_DBG("allocate %d bytes memory for BLOB at %p", _l, VAL_BLOB(_v).s);
00179                         strncpy(VAL_BLOB(_v).s, _s, _l);
00180                         VAL_FREE(_v) = 1;
00181                 }
00182 
00183                 VAL_BLOB(_v).len = _l;
00184                 VAL_TYPE(_v) = DB1_BLOB;
00185                 return 0;
00186         }
00187         return -10;
00188 }
00189 
00190 
00202 int db_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
00203 {
00204         if (!_c || !_v || !_s || !_len || !*_len) {
00205                 LM_ERR("invalid parameter value\n");
00206                 return -1;
00207         }
00208 
00209         if (VAL_NULL(_v)) {
00210                 if (*_len < sizeof("NULL")) {
00211                         LM_ERR("buffer too small\n");
00212                         return -1;
00213                 }
00214                 *_len = snprintf(_s, *_len, "NULL");
00215                 return 0;
00216         }
00217         
00218         switch(VAL_TYPE(_v)) {
00219         case DB1_INT:
00220                 if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
00221                         LM_ERR("error while converting string to int\n");
00222                         return -2;
00223                 } else {
00224                         return 0;
00225                 }
00226                 break;
00227 
00228         case DB1_BIGINT:
00229                 if (db_longlong2str(VAL_BIGINT(_v), _s, _len) < 0) {
00230                         LM_ERR("error while converting string to big int\n");
00231                         return -3;
00232                 } else {
00233                         return 0;
00234                 }
00235                 break;
00236 
00237         case DB1_BITMAP:
00238                 if (db_int2str(VAL_BITMAP(_v), _s, _len) < 0) {
00239                         LM_ERR("error while converting string to int\n");
00240                         return -4;
00241                 } else {
00242                         return 0;
00243                 }
00244                 break;
00245 
00246         case DB1_DOUBLE:
00247                 if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
00248                         LM_ERR("error while converting string to double\n");
00249                         return -5;
00250                 } else {
00251                         return 0;
00252                 }
00253                 break;
00254 
00255         case DB1_DATETIME:
00256                 if (db_time2str(VAL_TIME(_v), _s, _len) < 0) {
00257                         LM_ERR("failed to convert string to time_t\n");
00258                         return -8;
00259                 } else {
00260                         return 0;
00261                 }
00262                 break;
00263 
00264         default:
00265                 return 1;
00266         }
00267 }