db_mysql/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 #include "../../dprint.h"
00031 #include "../../db/db_ut.h"
00032 #include "val.h"
00033 #include "my_con.h"
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 int db_mysql_val2str(const db_con_t* _c, const db_val_t* _v, char* _s, int* _len)
00047 {
00048 int l, tmp;
00049 char* old_s;
00050
00051 tmp = db_val2str(_c, _v, _s, _len);
00052 if (tmp < 1)
00053 return tmp;
00054
00055 switch(VAL_TYPE(_v)) {
00056 case DB_STRING:
00057 l = strlen(VAL_STRING(_v));
00058 if (*_len < (l * 2 + 3)) {
00059 LM_ERR("destination buffer too short\n");
00060 return -6;
00061 } else {
00062 old_s = _s;
00063 *_s++ = '\'';
00064 _s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STRING(_v), l);
00065 *_s++ = '\'';
00066 *_s = '\0';
00067 *_len = _s - old_s;
00068 return 0;
00069 }
00070 break;
00071
00072 case DB_STR:
00073 if (*_len < (VAL_STR(_v).len * 2 + 3)) {
00074 LM_ERR("destination buffer too short\n");
00075 return -7;
00076 } else {
00077 old_s = _s;
00078 *_s++ = '\'';
00079 _s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STR(_v).s, VAL_STR(_v).len);
00080 *_s++ = '\'';
00081 *_s = '\0';
00082 *_len = _s - old_s;
00083 return 0;
00084 }
00085 break;
00086
00087 case DB_BLOB:
00088 l = VAL_BLOB(_v).len;
00089 if (*_len < (l * 2 + 3)) {
00090 LM_ERR("destination buffer too short\n");
00091 return -9;
00092 } else {
00093 old_s = _s;
00094 *_s++ = '\'';
00095 _s += mysql_real_escape_string(CON_CONNECTION(_c), _s, VAL_STR(_v).s, l);
00096 *_s++ = '\'';
00097 *_s = '\0';
00098 *_len = _s - old_s;
00099 return 0;
00100 }
00101 break;
00102
00103 default:
00104 LM_DBG("unknown data type\n");
00105 return -10;
00106 }
00107 }