bdb_val.c
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
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
00040
00041
00042
00043
00044
00045
00046
00047
00048
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
00061
00062
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
00069 _s = NULL;
00070 _l = 0;
00071 return -1;
00072 }
00073 *_l = l;
00074
00075
00076
00077 return 0;
00078 }
00079
00080
00081
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
00092
00093
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
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 }