db_mysql/km_val.c

Go to the documentation of this file.
00001 /* 
00002  * $Id$ 
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  * Copyright (C) 2008 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 
00030 #include "../../dprint.h"
00031 #include "../../lib/srdb1/db_ut.h"
00032 #include "km_val.h"
00033 #include "km_my_con.h"
00034 
00035 
00046 int db_mysql_val2str(const db1_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 DB1_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'; /* FIXME */
00067                         *_len = _s - old_s;
00068                         return 0;
00069                 }
00070                 break;
00071 
00072         case DB1_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 DB1_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 }