1 /*
2 * UNIXODBC module
3 *
4 * Copyright (C) 2005-2006 Marco Lorrai
5 * Copyright (C) 2008 1&1 Internet AG
6 *
7 * This file is part of Kamailio, a free SIP server.
8 *
9 * Kamailio is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version
13 *
14 * Kamailio is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25
26 #include "../../core/dprint.h"
27 #include "../../core/strutils.h"
28 #include "../../lib/srdb1/db_ut.h"
29 #include "db_unixodbc.h"
30 #include "val.h"
31 #include "connection.h"
32
33 /*
34 * Used when converting the query to a result
35 */
db_unixodbc_str2val(const db_type_t _t,db_val_t * _v,const char * _s,const int _l,const unsigned int _cpy)36 int db_unixodbc_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
37 const unsigned int _cpy)
38 {
39 /* db_unixodbc uses the NULL string for NULL SQL values */
40 if (_v && _s && !strcmp(_s, "NULL")) {
41 LM_DBG("converting NULL value");
42 static str dummy_string = {"", 0};
43 memset(_v, 0, sizeof(db_val_t));
44 /* Initialize the string pointers to a dummy empty
45 * string so that we do not crash when the NULL flag
46 * is set but the module does not check it properly
47 */
48 VAL_STRING(_v) = dummy_string.s;
49 VAL_STR(_v) = dummy_string;
50 VAL_BLOB(_v) = dummy_string;
51 VAL_TYPE(_v) = _t;
52 VAL_NULL(_v) = 1;
53 return 0;
54 } else {
55 return db_str2val(_t, _v, _s, _l, _cpy);
56 }
57 }
58
59 /*
60 * Used when converting a result from the query
61 */
db_unixodbc_val2str(const db1_con_t * _c,const db_val_t * _v,char * _s,int * _len)62 int db_unixodbc_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
63 {
64 int l, tmp;
65 char* old_s;
66
67 /* db_unixodbc uses a custom escape function */
68 tmp = db_val2str(_c, _v, _s, _len);
69 if (tmp < 1)
70 return tmp;
71
72 switch(VAL_TYPE(_v))
73 {
74 case DB1_STRING:
75 l = strlen(VAL_STRING(_v));
76 if (*_len < (l * 2 + 3))
77 {
78 LM_ERR("destination buffer too short\n");
79 return -6;
80 }
81 else
82 {
83 old_s = _s;
84 *_s++ = '\'';
85 if(use_escape_common)
86 {
87 _s += escape_common(_s, (char*)VAL_STRING(_v), l);
88 } else {
89 memcpy(_s, VAL_STRING(_v), l);
90 _s += l;
91 }
92 *_s++ = '\'';
93 *_s = '\0'; /* FIXME */
94 *_len = _s - old_s;
95 return 0;
96 }
97 break;
98
99 case DB1_STR:
100 l = VAL_STR(_v).len;
101 if (*_len < (l * 2 + 3))
102 {
103 LM_ERR("destination buffer too short\n");
104 return -7;
105 }
106 else
107 {
108 old_s = _s;
109 *_s++ = '\'';
110 if(use_escape_common)
111 {
112 _s += escape_common(_s, VAL_STR(_v).s, l);
113 } else {
114 memcpy(_s, VAL_STR(_v).s, l);
115 _s += l;
116 }
117 *_s++ = '\'';
118 *_s = '\0'; /* FIXME */
119 *_len = _s - old_s;
120 return 0;
121 }
122 break;
123
124 case DB1_BLOB:
125 l = VAL_BLOB(_v).len;
126 if (*_len < (l * 2 + 3))
127 {
128 LM_ERR("destination buffer too short\n");
129 return -9;
130 }
131 else
132 {
133 old_s = _s;
134 *_s++ = '\'';
135 if(use_escape_common)
136 {
137 _s += escape_common(_s, VAL_BLOB(_v).s, l);
138 } else {
139 memcpy(_s, VAL_BLOB(_v).s, l);
140 _s += l;
141 }
142 *_s++ = '\'';
143 *_s = '\0'; /* FIXME */
144 *_len = _s - old_s;
145 return 0;
146 }
147 break;
148
149 default:
150 LM_ERR("unknown data type\n");
151 return -10;
152 }
153 }
154