modules/db_unixodbc/val.c
e3649ef3
 /* 
  * $Id$ 
  *
  * UNIXODBC module
  *
  * Copyright (C) 2005-2006 Marco Lorrai
4c7d0961
  * Copyright (C) 2008 1&1 Internet AG
e3649ef3
  *
27642a08
  * This file is part of Kamailio, a free SIP server.
e3649ef3
  *
27642a08
  * Kamailio is free software; you can redistribute it and/or modify
e3649ef3
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
27642a08
  * Kamailio is distributed in the hope that it will be useful,
e3649ef3
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License 
  * along with this program; if not, write to the Free Software 
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
e3649ef3
  *
  *
  * History:
  * --------
  *  2005-12-01  initial commit (chgen)
  */
 
 
 #include "../../dprint.h"
a013ec89
 #include "../../lib/kcore/strcommon.h"
02fb0169
 #include "../../lib/srdb1/db_ut.h"
5408633b
 #include "db_unixodbc.h"
e3649ef3
 #include "val.h"
c6819b9a
 #include "connection.h"
e3649ef3
 
9031d695
 /*
  * Used when converting the query to a result
  */
 int db_unixodbc_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
 		const unsigned int _cpy)
 {
 	/* db_unixodbc uses the NULL string for NULL SQL values */
 	if (_v && _s && !strcmp(_s, "NULL")) {
 		LM_DBG("converting NULL value");
 		static str dummy_string = {"", 0};
 		memset(_v, 0, sizeof(db_val_t));
 			/* Initialize the string pointers to a dummy empty
 			 * string so that we do not crash when the NULL flag
 			 * is set but the module does not check it properly
 			 */
 		VAL_STRING(_v) = dummy_string.s;
 		VAL_STR(_v) = dummy_string;
 		VAL_BLOB(_v) = dummy_string;
 		VAL_TYPE(_v) = _t;
 		VAL_NULL(_v) = 1;
 		return 0;
 	} else {
 		return db_str2val(_t, _v, _s, _l, _cpy);
 	}
 }
f0174d09
 
e3649ef3
 /*
9031d695
  * Used when converting a result from the query
e3649ef3
  */
02fb0169
 int db_unixodbc_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
e3649ef3
 {
a4a1a8f0
 	int l, tmp;
4c8c7b62
 	char* old_s;
e3649ef3
 
9031d695
 	/* db_unixodbc uses a custom escape function */
a4a1a8f0
 	tmp = db_val2str(_c, _v, _s, _len);
 	if (tmp < 1)
 		return tmp;
e3649ef3
 
 	switch(VAL_TYPE(_v))
 	{
02fb0169
 		case DB1_STRING:
e3649ef3
 			l = strlen(VAL_STRING(_v));
 			if (*_len < (l * 2 + 3))
 			{
de7fe5e9
 				LM_ERR("destination buffer too short\n");
c4ba69a1
 				return -6;
e3649ef3
 			}
 			else
 			{
4c8c7b62
 				old_s = _s;
e3649ef3
 				*_s++ = '\'';
4c8c7b62
 				if(use_escape_common)
 				{
 					_s += escape_common(_s, (char*)VAL_STRING(_v), l);
 				} else {
 					memcpy(_s, VAL_STRING(_v), l);
 					_s += l;
 				}
 				*_s++ = '\'';
 				*_s = '\0'; /* FIXME */
 				*_len = _s - old_s;
e3649ef3
 				return 0;
 			}
 			break;
 
02fb0169
 		case DB1_STR:
e3649ef3
 			l = VAL_STR(_v).len;
 			if (*_len < (l * 2 + 3))
 			{
de7fe5e9
 				LM_ERR("destination buffer too short\n");
c4ba69a1
 				return -7;
e3649ef3
 			}
 			else
 			{
4c8c7b62
 				old_s = _s;
 				*_s++ = '\'';
 				if(use_escape_common)
 				{
 					_s += escape_common(_s, VAL_STR(_v).s, l);
 				} else {
 					memcpy(_s, VAL_STR(_v).s, l);
 					_s += l;
 				}
e3649ef3
 				*_s++ = '\'';
4c8c7b62
 				*_s = '\0'; /* FIXME */
 				*_len = _s - old_s;
e3649ef3
 				return 0;
 			}
 			break;
 
02fb0169
 		case DB1_BLOB:
e3649ef3
 			l = VAL_BLOB(_v).len;
 			if (*_len < (l * 2 + 3))
 			{
de7fe5e9
 				LM_ERR("destination buffer too short\n");
c4ba69a1
 				return -9;
e3649ef3
 			}
 			else
 			{
4c8c7b62
 				old_s = _s;
 				*_s++ = '\'';
 				if(use_escape_common)
 				{
 					_s += escape_common(_s, VAL_BLOB(_v).s, l);
 				} else {
 					memcpy(_s, VAL_BLOB(_v).s, l);
 					_s += l;
 				}
e3649ef3
 				*_s++ = '\'';
4c8c7b62
 				*_s = '\0'; /* FIXME */
 				*_len = _s - old_s;
e3649ef3
 				return 0;
 			}
 			break;
 
 		default:
de7fe5e9
 			LM_DBG("unknown data type\n");
c4ba69a1
 			return -10;
e3649ef3
 	}
 }