Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00037 #include "../../lib/srdb1/db_val.h"
00038 #include "../../lib/srdb1/db_ut.h"
00039 #include "km_db_berkeley.h"
00040 #include "km_bdb_res.h"
00041 #include "km_bdb_val.h"
00042 #include <string.h>
00043
00056 inline int km_bdb_time2str(time_t _v, char* _s, int* _l)
00057 {
00058 struct tm* t;
00059 int l;
00060
00061 if ((!_s) || (!_l) || (*_l < 2)) {
00062 LM_ERR("Invalid parameter value\n");
00063 return -1;
00064 }
00065
00066
00067
00068
00069 t = localtime(&_v);
00070 l = strftime(_s, *_l -1, "%Y-%m-%d %H:%M:%S", t);
00071
00072 if (l == 0) {
00073 LM_ERR("Error during time conversion\n");
00074
00075 _s = NULL;
00076 _l = 0;
00077 return -1;
00078 }
00079 *_l = l;
00080
00081
00082
00083 return 0;
00084 }
00085
00089 int bdb_str2val(db_type_t _t, db_val_t* _v, char* _s, int _l)
00090 {
00091
00092 static str dummy_string = {"", 0};
00093
00094 if(!_s)
00095 {
00096 memset(_v, 0, sizeof(db_val_t));
00097
00098
00099
00100
00101 VAL_STRING(_v) = dummy_string.s;
00102 VAL_STR(_v) = dummy_string;
00103 VAL_BLOB(_v) = dummy_string;
00104 VAL_TYPE(_v) = _t;
00105 VAL_NULL(_v) = 1;
00106 return 0;
00107 }
00108 VAL_NULL(_v) = 0;
00109
00110 switch(_t) {
00111 case DB1_INT:
00112 if (db_str2int(_s, &VAL_INT(_v)) < 0) {
00113 LM_ERR("Error while converting INT value from string\n");
00114 return -2;
00115 } else {
00116 VAL_TYPE(_v) = DB1_INT;
00117 return 0;
00118 }
00119 break;
00120
00121 case DB1_BIGINT:
00122 LM_ERR("BIGINT not supported");
00123 return -1;
00124
00125 case DB1_BITMAP:
00126 if (db_str2int(_s, &VAL_INT(_v)) < 0) {
00127 LM_ERR("Error while converting BITMAP value from string\n");
00128 return -3;
00129 } else {
00130 VAL_TYPE(_v) = DB1_BITMAP;
00131 return 0;
00132 }
00133 break;
00134
00135 case DB1_DOUBLE:
00136 if (db_str2double(_s, &VAL_DOUBLE(_v)) < 0) {
00137 LM_ERR("Error while converting DOUBLE value from string\n");
00138 return -4;
00139 } else {
00140 VAL_TYPE(_v) = DB1_DOUBLE;
00141 return 0;
00142 }
00143 break;
00144
00145 case DB1_STRING:
00146 VAL_STRING(_v) = _s;
00147 VAL_TYPE(_v) = DB1_STRING;
00148 VAL_FREE(_v) = 1;
00149
00150 if( strlen(_s)==4 && !strncasecmp(_s, "NULL", 4) )
00151 VAL_NULL(_v) = 1;
00152
00153 return 0;
00154
00155 case DB1_STR:
00156 VAL_STR(_v).s = (char*)_s;
00157 VAL_STR(_v).len = _l;
00158 VAL_TYPE(_v) = DB1_STR;
00159 VAL_FREE(_v) = 1;
00160
00161 if( strlen(_s)==4 && !strncasecmp(_s, "NULL", 4) )
00162 VAL_NULL(_v) = 1;
00163
00164 return 0;
00165
00166 case DB1_DATETIME:
00167 if (db_str2time(_s, &VAL_TIME(_v)) < 0) {
00168 LM_ERR("Error converting datetime\n");
00169 return -5;
00170 } else {
00171 VAL_TYPE(_v) = DB1_DATETIME;
00172 return 0;
00173 }
00174 break;
00175
00176 case DB1_BLOB:
00177 VAL_BLOB(_v).s = _s;
00178 VAL_TYPE(_v) = DB1_BLOB;
00179 LM_DBG("got blob len %d\n", _l);
00180 return 0;
00181 }
00182
00183 return -6;
00184 }
00185
00186
00187
00188
00189
00190 int km_bdb_val2str(db_val_t* _v, char* _s, int* _len)
00191 {
00192 int l;
00193
00194 if (VAL_NULL(_v))
00195 {
00196 *_len = snprintf(_s, *_len, "NULL");
00197 return 0;
00198 }
00199
00200 switch(VAL_TYPE(_v)) {
00201 case DB1_INT:
00202 if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
00203 LM_ERR("Error while converting int to string\n");
00204 return -2;
00205 } else {
00206 LM_DBG("Converted int to string\n");
00207 return 0;
00208 }
00209 break;
00210
00211 case DB1_BITMAP:
00212 if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
00213 LM_ERR("Error while converting bitmap to string\n");
00214 return -3;
00215 } else {
00216 LM_DBG("Converted bitmap to string\n");
00217 return 0;
00218 }
00219 break;
00220
00221 case DB1_DOUBLE:
00222 if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
00223 LM_ERR("Error while converting double to string\n");
00224 return -3;
00225 } else {
00226 LM_DBG("Converted double to string\n");
00227 return 0;
00228 }
00229 break;
00230
00231 case DB1_STRING:
00232 l = strlen(VAL_STRING(_v));
00233 if (*_len < l )
00234 { LM_ERR("Destination buffer too short for string\n");
00235 return -4;
00236 }
00237 else
00238 { LM_DBG("Converted string to string\n");
00239 strncpy(_s, VAL_STRING(_v) , l);
00240 _s[l] = 0;
00241 *_len = l;
00242 return 0;
00243 }
00244 break;
00245
00246 case DB1_STR:
00247 l = VAL_STR(_v).len;
00248 if (*_len < l)
00249 {
00250 LM_ERR("Destination buffer too short for str\n");
00251 return -5;
00252 }
00253 else
00254 {
00255 LM_DBG("Converted str to string\n");
00256 strncpy(_s, VAL_STR(_v).s , VAL_STR(_v).len);
00257 *_len = VAL_STR(_v).len;
00258 return 0;
00259 }
00260 break;
00261
00262 case DB1_DATETIME:
00263 if (km_bdb_time2str(VAL_TIME(_v), _s, _len) < 0) {
00264 LM_ERR("Error while converting time_t to string\n");
00265 return -6;
00266 } else {
00267 LM_DBG("Converted time_t to string\n");
00268 return 0;
00269 }
00270 break;
00271
00272 case DB1_BLOB:
00273 l = VAL_BLOB(_v).len;
00274 if (*_len < l)
00275 {
00276 LM_ERR("Destination buffer too short for blob\n");
00277 return -7;
00278 }
00279 else
00280 {
00281 LM_DBG("Converting BLOB [%s]\n", _s);
00282 _s = VAL_BLOB(_v).s;
00283 *_len = 0;
00284 return -8;
00285 }
00286 break;
00287
00288 default:
00289 LM_DBG("Unknown data type\n");
00290 return -8;
00291 }
00292 }