db_unixodbc/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
00032 #include "../../dprint.h"
00033 #include "../../strcommon.h"
00034 #include "../../db/db_ut.h"
00035 #include "db_unixodbc.h"
00036 #include "val.h"
00037 #include "con.h"
00038
00039
00040
00041
00042 int db_unixodbc_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
00043 const unsigned int _cpy)
00044 {
00045
00046 if (_v && _s && !strcmp(_s, "NULL")) {
00047 LM_DBG("converting NULL value");
00048 static str dummy_string = {"", 0};
00049 memset(_v, 0, sizeof(db_val_t));
00050
00051
00052
00053
00054 VAL_STRING(_v) = dummy_string.s;
00055 VAL_STR(_v) = dummy_string;
00056 VAL_BLOB(_v) = dummy_string;
00057 VAL_TYPE(_v) = _t;
00058 VAL_NULL(_v) = 1;
00059 return 0;
00060 } else {
00061 return db_str2val(_t, _v, _s, _l, _cpy);
00062 }
00063 }
00064
00065
00066
00067
00068 int db_unixodbc_val2str(const db_con_t* _c, const db_val_t* _v, char* _s, int* _len)
00069 {
00070 int l, tmp;
00071 char* old_s;
00072
00073
00074 tmp = db_val2str(_c, _v, _s, _len);
00075 if (tmp < 1)
00076 return tmp;
00077
00078 switch(VAL_TYPE(_v))
00079 {
00080 case DB_STRING:
00081 l = strlen(VAL_STRING(_v));
00082 if (*_len < (l * 2 + 3))
00083 {
00084 LM_ERR("destination buffer too short\n");
00085 return -6;
00086 }
00087 else
00088 {
00089 old_s = _s;
00090 *_s++ = '\'';
00091 if(use_escape_common)
00092 {
00093 _s += escape_common(_s, (char*)VAL_STRING(_v), l);
00094 } else {
00095 memcpy(_s, VAL_STRING(_v), l);
00096 _s += l;
00097 }
00098 *_s++ = '\'';
00099 *_s = '\0';
00100 *_len = _s - old_s;
00101 return 0;
00102 }
00103 break;
00104
00105 case DB_STR:
00106 l = VAL_STR(_v).len;
00107 if (*_len < (l * 2 + 3))
00108 {
00109 LM_ERR("destination buffer too short\n");
00110 return -7;
00111 }
00112 else
00113 {
00114 old_s = _s;
00115 *_s++ = '\'';
00116 if(use_escape_common)
00117 {
00118 _s += escape_common(_s, VAL_STR(_v).s, l);
00119 } else {
00120 memcpy(_s, VAL_STR(_v).s, l);
00121 _s += l;
00122 }
00123 *_s++ = '\'';
00124 *_s = '\0';
00125 *_len = _s - old_s;
00126 return 0;
00127 }
00128 break;
00129
00130 case DB_BLOB:
00131 l = VAL_BLOB(_v).len;
00132 if (*_len < (l * 2 + 3))
00133 {
00134 LM_ERR("destination buffer too short\n");
00135 return -9;
00136 }
00137 else
00138 {
00139 old_s = _s;
00140 *_s++ = '\'';
00141 if(use_escape_common)
00142 {
00143 _s += escape_common(_s, VAL_BLOB(_v).s, l);
00144 } else {
00145 memcpy(_s, VAL_BLOB(_v).s, l);
00146 _s += l;
00147 }
00148 *_s++ = '\'';
00149 *_s = '\0';
00150 *_len = _s - old_s;
00151 return 0;
00152 }
00153 break;
00154
00155 default:
00156 LM_DBG("unknown data type\n");
00157 return -10;
00158 }
00159 }