Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,271 +0,0 @@
1
-/*
2
- * Copyright (C) 2001-2003 FhG Fokus
3
- * Copyright (C) 2008-2009 1&1 Internet AG
4
- *
5
- * This file is part of Kamailio, a free SIP server.
6
- *
7
- * Kamailio is free software; you can redistribute it and/or modify
8
- * it under the terms of the GNU General Public License as published by
9
- * the Free Software Foundation; either version 2 of the License, or
10
- * (at your option) any later version
11
- *
12
- * Kamailio is distributed in the hope that it will be useful,
13
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
- * GNU General Public License for more details.
16
- *
17
- * You should have received a copy of the GNU General Public License 
18
- * along with this program; if not, write to the Free Software 
19
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20
- */
21
-
22
-#include "db_ut.h"
23
-
24
-#include <stdio.h>
25
-#include <time.h>
26
-
27
-/*!
28
- * \brief Convert a str to a db value
29
- *
30
- * Convert a str to a db value, copy strings if _cpy is not zero.
31
- * Copying is not necessary if the result from the database client library
32
- * is freed after the result inside the server is processed. If the result
33
- * is freed earlier, e.g. because its saved inside some temporary storage,
34
- * then it must be copied in order to be use it reliable.
35
- *
36
- * \param _t destination value type
37
- * \param _v destination value
38
- * \param _s source string
39
- * \param _l string length
40
- * \param _cpy when set to zero does not copy strings, otherwise copy strings
41
- * \return 0 on success, negative on error
42
- */
43
-int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
44
-		const unsigned int _cpy)
45
-{
46
-	static str dummy_string = {"", 0};
47
-	static char dummy_string_buf[2];
48
-	
49
-	if (!_v) {
50
-		LM_ERR("invalid parameter value\n");
51
-		return -1;
52
-	}
53
-	/* A NULL string is a SQL NULL value, otherwise its an empty value */
54
-	if (!_s) {
55
-		LM_DBG("converting NULL value\n");
56
-		memset(_v, 0, sizeof(db_val_t));
57
-			/* Initialize the string pointers to a dummy empty
58
-			 * string so that we do not crash when the NULL flag
59
-			 * is set but the module does not check it properly
60
-			 */
61
-		dummy_string_buf[0] = '\0';
62
-		dummy_string.s = dummy_string_buf;
63
-		VAL_STRING(_v) = dummy_string.s;
64
-		VAL_STR(_v) = dummy_string;
65
-		VAL_BLOB(_v) = dummy_string;
66
-		VAL_TYPE(_v) = _t;
67
-		VAL_NULL(_v) = 1;
68
-		return 0;
69
-	}
70
-	VAL_NULL(_v) = 0;
71
-
72
-	switch(_t) {
73
-	case DB1_INT:
74
-		LM_DBG("converting INT [%s]\n", _s);
75
-		if (db_str2int(_s, &VAL_INT(_v)) < 0) {
76
-			LM_ERR("error while converting integer value from string\n");
77
-			return -2;
78
-		} else {
79
-			VAL_TYPE(_v) = DB1_INT;
80
-			return 0;
81
-		}
82
-		break;
83
-
84
-	case DB1_BIGINT:
85
-		LM_DBG("converting BIGINT [%s]\n", _s);
86
-		if (db_str2longlong(_s, &VAL_BIGINT(_v)) < 0) {
87
-			LM_ERR("error while converting big integer value from string\n");
88
-			return -3;
89
-		} else {
90
-			VAL_TYPE(_v) = DB1_BIGINT;
91
-			return 0;
92
-		}
93
-		break;
94
-
95
-	case DB1_BITMAP:
96
-		LM_DBG("converting BITMAP [%s]\n", _s);
97
-		if (db_str2int(_s, &VAL_INT(_v)) < 0) {
98
-			LM_ERR("error while converting bitmap value from string\n");
99
-			return -4;
100
-		} else {
101
-			VAL_TYPE(_v) = DB1_BITMAP;
102
-			return 0;
103
-		}
104
-		break;
105
-	
106
-	case DB1_DOUBLE:
107
-		LM_DBG("converting DOUBLE [%s]\n", _s);
108
-		if (db_str2double(_s, &VAL_DOUBLE(_v)) < 0) {
109
-			LM_ERR("error while converting double value from string\n");
110
-			return -5;
111
-		} else {
112
-			VAL_TYPE(_v) = DB1_DOUBLE;
113
-			return 0;
114
-		}
115
-		break;
116
-
117
-	case DB1_STRING:
118
-		LM_DBG("converting STRING [%s]\n", _s);
119
-
120
-		if (_cpy == 0) {
121
-			VAL_STRING(_v) = _s;
122
-		} else {
123
-			VAL_STRING(_v) = pkg_malloc(_l + 1);
124
-			if (VAL_STRING(_v) == NULL) {
125
-				LM_ERR("no private memory left\n");
126
-				return -6;
127
-			}
128
-			LM_DBG("allocate %d bytes memory for STRING at %p\n", _l + 1, VAL_STRING(_v));
129
-			strncpy((char*)VAL_STRING(_v), _s, _l);
130
-			((char*)VAL_STRING(_v))[_l] = '\0';
131
-			VAL_FREE(_v) = 1;
132
-		}
133
-
134
-		VAL_TYPE(_v) = DB1_STRING;
135
-		return 0;
136
-
137
-	case DB1_STR:
138
-		LM_DBG("converting STR [%.*s]\n", _l, _s);
139
-
140
-		if (_cpy == 0) {
141
-			VAL_STR(_v).s = (char*) _s;
142
-		} else {
143
-			VAL_STR(_v).s = pkg_malloc(_l);
144
-			if (VAL_STR(_v).s == NULL) {
145
-				LM_ERR("no private memory left\n");
146
-				return -7;
147
-			}
148
-			LM_DBG("allocate %d bytes memory for STR at %p\n", _l, VAL_STR(_v).s);
149
-			strncpy(VAL_STR(_v).s, _s, _l);
150
-			VAL_FREE(_v) = 1;
151
-		}
152
-
153
-		VAL_STR(_v).len = _l;
154
-		VAL_TYPE(_v) = DB1_STR;
155
-		return 0;
156
-
157
-	case DB1_DATETIME:
158
-		LM_DBG("converting DATETIME [%s]\n", _s);
159
-		if (db_str2time(_s, &VAL_TIME(_v)) < 0) {
160
-			LM_ERR("error while converting datetime value from string\n");
161
-			return -8;
162
-		} else {
163
-			VAL_TYPE(_v) = DB1_DATETIME;
164
-			return 0;
165
-		}
166
-		break;
167
-
168
-	case DB1_BLOB:
169
-		LM_DBG("converting BLOB [%.*s]\n", _l, _s);
170
-
171
-		if (_cpy == 0) {
172
-			VAL_BLOB(_v).s = (char*) _s;
173
-		} else {
174
-			VAL_BLOB(_v).s = pkg_malloc(_l);
175
-			if (VAL_BLOB(_v).s == NULL) {
176
-				LM_ERR("no private memory left\n");
177
-				return -9;
178
-			}
179
-			LM_DBG("allocate %d bytes memory for BLOB at %p\n", _l, VAL_BLOB(_v).s);
180
-			strncpy(VAL_BLOB(_v).s, _s, _l);
181
-			VAL_FREE(_v) = 1;
182
-		}
183
-
184
-		VAL_BLOB(_v).len = _l;
185
-		VAL_TYPE(_v) = DB1_BLOB;
186
-		return 0;
187
-
188
-	default:
189
-		return -10;
190
-	}
191
-	return -10;
192
-}
193
-
194
-
195
-/*!
196
- * \brief Convert a numerical value to a string
197
- *
198
- * Convert a numerical value to a string, used when converting result from a query.
199
- * Implement common functionality needed from the databases, does parameter checking.
200
- * \param _c database connection
201
- * \param _v source value
202
- * \param _s target string
203
- * \param _len target string length
204
- * \return 0 on success, negative on error, 1 if value must be converted by other means
205
- */
206
-int db_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
207
-{
208
-	if (!_c || !_v || !_s || !_len || !*_len) {
209
-		LM_ERR("invalid parameter value\n");
210
-		return -1;
211
-	}
212
-
213
-	if (VAL_NULL(_v)) {
214
-		if (*_len < sizeof("NULL")) {
215
-			LM_ERR("buffer too small\n");
216
-			return -1;
217
-		}
218
-		*_len = snprintf(_s, *_len, "NULL");
219
-		return 0;
220
-	}
221
-	
222
-	switch(VAL_TYPE(_v)) {
223
-	case DB1_INT:
224
-		if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
225
-			LM_ERR("error while converting string to int\n");
226
-			return -2;
227
-		} else {
228
-			return 0;
229
-		}
230
-		break;
231
-
232
-	case DB1_BIGINT:
233
-		if (db_longlong2str(VAL_BIGINT(_v), _s, _len) < 0) {
234
-			LM_ERR("error while converting string to big int\n");
235
-			return -3;
236
-		} else {
237
-			return 0;
238
-		}
239
-		break;
240
-
241
-	case DB1_BITMAP:
242
-		if (db_int2str(VAL_BITMAP(_v), _s, _len) < 0) {
243
-			LM_ERR("error while converting string to int\n");
244
-			return -4;
245
-		} else {
246
-			return 0;
247
-		}
248
-		break;
249
-
250
-	case DB1_DOUBLE:
251
-		if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
252
-			LM_ERR("error while converting string to double\n");
253
-			return -5;
254
-		} else {
255
-			return 0;
256
-		}
257
-		break;
258
-
259
-	case DB1_DATETIME:
260
-		if (db_time2str(VAL_TIME(_v), _s, _len) < 0) {
261
-			LM_ERR("failed to convert string to time_t\n");
262
-			return -8;
263
-		} else {
264
-			return 0;
265
-		}
266
-		break;
267
-
268
-	default:
269
-		return 1;
270
-	}
271
-}
Browse code

lib/srdb1: dummy string for null values pointed to static buffer

- allow editing in upper layers, avoiding innaproriate access to a constant

Daniel-Constantin Mierla authored on 18/07/2015 07:52:28
Showing 1 changed files
... ...
@@ -44,6 +44,7 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
44 44
 		const unsigned int _cpy)
45 45
 {
46 46
 	static str dummy_string = {"", 0};
47
+	static char dummy_string_buf[2];
47 48
 	
48 49
 	if (!_v) {
49 50
 		LM_ERR("invalid parameter value\n");
... ...
@@ -57,6 +58,8 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
57 58
 			 * string so that we do not crash when the NULL flag
58 59
 			 * is set but the module does not check it properly
59 60
 			 */
61
+		dummy_string_buf[0] = '\0';
62
+		dummy_string.s = dummy_string_buf;
60 63
 		VAL_STRING(_v) = dummy_string.s;
61 64
 		VAL_STR(_v) = dummy_string;
62 65
 		VAL_BLOB(_v) = dummy_string;
Browse code

srdb1 Remove SVN id's and history

Olle E. Johansson authored on 04/01/2015 10:10:14
Showing 1 changed files
... ...
@@ -1,6 +1,4 @@
1 1
 /*
2
- * $Id$ 
3
- *
4 2
  * Copyright (C) 2001-2003 FhG Fokus
5 3
  * Copyright (C) 2008-2009 1&1 Internet AG
6 4
  *
Browse code

lib/srdb1: end of line after several debug messages

Daniel-Constantin Mierla authored on 05/10/2014 19:55:51
Showing 1 changed files
... ...
@@ -53,7 +53,7 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
53 53
 	}
54 54
 	/* A NULL string is a SQL NULL value, otherwise its an empty value */
55 55
 	if (!_s) {
56
-		LM_DBG("converting NULL value");
56
+		LM_DBG("converting NULL value\n");
57 57
 		memset(_v, 0, sizeof(db_val_t));
58 58
 			/* Initialize the string pointers to a dummy empty
59 59
 			 * string so that we do not crash when the NULL flag
... ...
@@ -124,7 +124,7 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
124 124
 				LM_ERR("no private memory left\n");
125 125
 				return -6;
126 126
 			}
127
-			LM_DBG("allocate %d bytes memory for STRING at %p", _l + 1, VAL_STRING(_v));
127
+			LM_DBG("allocate %d bytes memory for STRING at %p\n", _l + 1, VAL_STRING(_v));
128 128
 			strncpy((char*)VAL_STRING(_v), _s, _l);
129 129
 			((char*)VAL_STRING(_v))[_l] = '\0';
130 130
 			VAL_FREE(_v) = 1;
... ...
@@ -144,7 +144,7 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
144 144
 				LM_ERR("no private memory left\n");
145 145
 				return -7;
146 146
 			}
147
-			LM_DBG("allocate %d bytes memory for STR at %p", _l, VAL_STR(_v).s);
147
+			LM_DBG("allocate %d bytes memory for STR at %p\n", _l, VAL_STR(_v).s);
148 148
 			strncpy(VAL_STR(_v).s, _s, _l);
149 149
 			VAL_FREE(_v) = 1;
150 150
 		}
... ...
@@ -175,7 +175,7 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
175 175
 				LM_ERR("no private memory left\n");
176 176
 				return -9;
177 177
 			}
178
-			LM_DBG("allocate %d bytes memory for BLOB at %p", _l, VAL_BLOB(_v).s);
178
+			LM_DBG("allocate %d bytes memory for BLOB at %p\n", _l, VAL_BLOB(_v).s);
179 179
 			strncpy(VAL_BLOB(_v).s, _s, _l);
180 180
 			VAL_FREE(_v) = 1;
181 181
 		}
Browse code

lib/srdb1: fix clang compiler warning

Ovidiu Sas authored on 03/09/2014 17:15:35
Showing 1 changed files
... ...
@@ -183,6 +183,9 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
183 183
 		VAL_BLOB(_v).len = _l;
184 184
 		VAL_TYPE(_v) = DB1_BLOB;
185 185
 		return 0;
186
+
187
+	default:
188
+		return -10;
186 189
 	}
187 190
 	return -10;
188 191
 }
Browse code

all: updated FSF address in GPL text

Anthony Messina authored on 04/07/2014 09:36:37 • Daniel-Constantin Mierla committed on 04/07/2014 09:37:36
Showing 1 changed files
... ...
@@ -18,7 +18,7 @@
18 18
  *
19 19
  * You should have received a copy of the GNU General Public License 
20 20
  * along with this program; if not, write to the Free Software 
21
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 22
  */
23 23
 
24 24
 #include "db_ut.h"
Browse code

Database type enum values (such as DB_INT) renamed to DB1_*.

Both database libraries define the same database value type names. We
need to rename them in one of the libraries so that both libraries can
be used at the same time. Here we chose to rename the enum values in
libsrdb1 to DB1_*. The same change needs to be done in SER modules
using this version of the database library.

Jan Janak authored on 12/02/2009 19:21:15
Showing 1 changed files
... ...
@@ -69,51 +69,51 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
69 69
 	VAL_NULL(_v) = 0;
70 70
 
71 71
 	switch(_t) {
72
-	case DB_INT:
72
+	case DB1_INT:
73 73
 		LM_DBG("converting INT [%s]\n", _s);
74 74
 		if (db_str2int(_s, &VAL_INT(_v)) < 0) {
75 75
 			LM_ERR("error while converting integer value from string\n");
76 76
 			return -2;
77 77
 		} else {
78
-			VAL_TYPE(_v) = DB_INT;
78
+			VAL_TYPE(_v) = DB1_INT;
79 79
 			return 0;
80 80
 		}
81 81
 		break;
82 82
 
83
-	case DB_BIGINT:
83
+	case DB1_BIGINT:
84 84
 		LM_DBG("converting BIGINT [%s]\n", _s);
85 85
 		if (db_str2longlong(_s, &VAL_BIGINT(_v)) < 0) {
86 86
 			LM_ERR("error while converting big integer value from string\n");
87 87
 			return -3;
88 88
 		} else {
89
-			VAL_TYPE(_v) = DB_BIGINT;
89
+			VAL_TYPE(_v) = DB1_BIGINT;
90 90
 			return 0;
91 91
 		}
92 92
 		break;
93 93
 
94
-	case DB_BITMAP:
94
+	case DB1_BITMAP:
95 95
 		LM_DBG("converting BITMAP [%s]\n", _s);
96 96
 		if (db_str2int(_s, &VAL_INT(_v)) < 0) {
97 97
 			LM_ERR("error while converting bitmap value from string\n");
98 98
 			return -4;
99 99
 		} else {
100
-			VAL_TYPE(_v) = DB_BITMAP;
100
+			VAL_TYPE(_v) = DB1_BITMAP;
101 101
 			return 0;
102 102
 		}
103 103
 		break;
104 104
 	
105
-	case DB_DOUBLE:
105
+	case DB1_DOUBLE:
106 106
 		LM_DBG("converting DOUBLE [%s]\n", _s);
107 107
 		if (db_str2double(_s, &VAL_DOUBLE(_v)) < 0) {
108 108
 			LM_ERR("error while converting double value from string\n");
109 109
 			return -5;
110 110
 		} else {
111
-			VAL_TYPE(_v) = DB_DOUBLE;
111
+			VAL_TYPE(_v) = DB1_DOUBLE;
112 112
 			return 0;
113 113
 		}
114 114
 		break;
115 115
 
116
-	case DB_STRING:
116
+	case DB1_STRING:
117 117
 		LM_DBG("converting STRING [%s]\n", _s);
118 118
 
119 119
 		if (_cpy == 0) {
... ...
@@ -130,10 +130,10 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
130 130
 			VAL_FREE(_v) = 1;
131 131
 		}
132 132
 
133
-		VAL_TYPE(_v) = DB_STRING;
133
+		VAL_TYPE(_v) = DB1_STRING;
134 134
 		return 0;
135 135
 
136
-	case DB_STR:
136
+	case DB1_STR:
137 137
 		LM_DBG("converting STR [%.*s]\n", _l, _s);
138 138
 
139 139
 		if (_cpy == 0) {
... ...
@@ -150,21 +150,21 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
150 150
 		}
151 151
 
152 152
 		VAL_STR(_v).len = _l;
153
-		VAL_TYPE(_v) = DB_STR;
153
+		VAL_TYPE(_v) = DB1_STR;
154 154
 		return 0;
155 155
 
156
-	case DB_DATETIME:
156
+	case DB1_DATETIME:
157 157
 		LM_DBG("converting DATETIME [%s]\n", _s);
158 158
 		if (db_str2time(_s, &VAL_TIME(_v)) < 0) {
159 159
 			LM_ERR("error while converting datetime value from string\n");
160 160
 			return -8;
161 161
 		} else {
162
-			VAL_TYPE(_v) = DB_DATETIME;
162
+			VAL_TYPE(_v) = DB1_DATETIME;
163 163
 			return 0;
164 164
 		}
165 165
 		break;
166 166
 
167
-	case DB_BLOB:
167
+	case DB1_BLOB:
168 168
 		LM_DBG("converting BLOB [%.*s]\n", _l, _s);
169 169
 
170 170
 		if (_cpy == 0) {
... ...
@@ -181,7 +181,7 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
181 181
 		}
182 182
 
183 183
 		VAL_BLOB(_v).len = _l;
184
-		VAL_TYPE(_v) = DB_BLOB;
184
+		VAL_TYPE(_v) = DB1_BLOB;
185 185
 		return 0;
186 186
 	}
187 187
 	return -10;
... ...
@@ -216,7 +216,7 @@ int db_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
216 216
 	}
217 217
 	
218 218
 	switch(VAL_TYPE(_v)) {
219
-	case DB_INT:
219
+	case DB1_INT:
220 220
 		if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
221 221
 			LM_ERR("error while converting string to int\n");
222 222
 			return -2;
... ...
@@ -225,7 +225,7 @@ int db_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
225 225
 		}
226 226
 		break;
227 227
 
228
-	case DB_BIGINT:
228
+	case DB1_BIGINT:
229 229
 		if (db_longlong2str(VAL_BIGINT(_v), _s, _len) < 0) {
230 230
 			LM_ERR("error while converting string to big int\n");
231 231
 			return -3;
... ...
@@ -234,7 +234,7 @@ int db_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
234 234
 		}
235 235
 		break;
236 236
 
237
-	case DB_BITMAP:
237
+	case DB1_BITMAP:
238 238
 		if (db_int2str(VAL_BITMAP(_v), _s, _len) < 0) {
239 239
 			LM_ERR("error while converting string to int\n");
240 240
 			return -4;
... ...
@@ -243,7 +243,7 @@ int db_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
243 243
 		}
244 244
 		break;
245 245
 
246
-	case DB_DOUBLE:
246
+	case DB1_DOUBLE:
247 247
 		if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
248 248
 			LM_ERR("error while converting string to double\n");
249 249
 			return -5;
... ...
@@ -252,7 +252,7 @@ int db_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
252 252
 		}
253 253
 		break;
254 254
 
255
-	case DB_DATETIME:
255
+	case DB1_DATETIME:
256 256
 		if (db_time2str(VAL_TIME(_v), _s, _len) < 0) {
257 257
 			LM_ERR("failed to convert string to time_t\n");
258 258
 			return -8;
Browse code

Rename db_con_t to db1_con_t to avoid conflicts with libsrdb2.

Both libraries libsrdb1 and libsrdb2 contain the data structure
db_con_t and in one if them the data structure needs to be renamed so
that both libraries can be used from one SER module at the same time.

Jan Janak authored on 12/02/2009 19:14:43
Showing 1 changed files
... ...
@@ -199,7 +199,7 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
199 199
  * \param _len target string length
200 200
  * \return 0 on success, negative on error, 1 if value must be converted by other means
201 201
  */
202
-int db_val2str(const db_con_t* _c, const db_val_t* _v, char* _s, int* _len)
202
+int db_val2str(const db1_con_t* _c, const db_val_t* _v, char* _s, int* _len)
203 203
 {
204 204
 	if (!_c || !_v || !_s || !_len || !*_len) {
205 205
 		LM_ERR("invalid parameter value\n");
Browse code

- add one debug probe for SQL NULL values, like for the other datatypes

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@5518 689a6050-402a-0410-94f2-e92a70836424

Henning Westerholt authored on 26/01/2009 18:07:14
Showing 1 changed files
... ...
@@ -53,6 +53,7 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
53 53
 	}
54 54
 	/* A NULL string is a SQL NULL value, otherwise its an empty value */
55 55
 	if (!_s) {
56
+		LM_DBG("converting NULL value");
56 57
 		memset(_v, 0, sizeof(db_val_t));
57 58
 			/* Initialize the string pointers to a dummy empty
58 59
 			 * string so that we do not crash when the NULL flag
Browse code

- partial revert of commit rev5359 for db_mysql module - generalize db_str2val function in the DB API core, the string copying behaviour can now configured (its done for db_postgres, db_unixodbc, not done for db_mysql like in the 1.3, 1.4 branches) - still TODO: fix NULL case in db_unixodbc, try to fix double copying

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@5423 689a6050-402a-0410-94f2-e92a70836424

Henning Westerholt authored on 05/01/2009 18:29:35
Showing 1 changed files
... ...
@@ -27,16 +27,23 @@
27 27
 #include <time.h>
28 28
 
29 29
 /*!
30
- * \brief Convert a str to a db value, copy strings
30
+ * \brief Convert a str to a db value
31
+ *
32
+ * Convert a str to a db value, copy strings if _cpy is not zero.
33
+ * Copying is not necessary if the result from the database client library
34
+ * is freed after the result inside the server is processed. If the result
35
+ * is freed earlier, e.g. because its saved inside some temporary storage,
36
+ * then it must be copied in order to be use it reliable.
31 37
  *
32
- * Convert a str to a db value, does not copy strings.
33 38
  * \param _t destination value type
34 39
  * \param _v destination value
35 40
  * \param _s source string
36 41
  * \param _l string length
42
+ * \param _cpy when set to zero does not copy strings, otherwise copy strings
37 43
  * \return 0 on success, negative on error
38 44
  */
39
-int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l)
45
+int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l,
46
+		const unsigned int _cpy)
40 47
 {
41 48
 	static str dummy_string = {"", 0};
42 49
 	
... ...
@@ -44,7 +51,7 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l)
44 51
 		LM_ERR("invalid parameter value\n");
45 52
 		return -1;
46 53
 	}
47
-	/* A NULL string is a NULL value, otherwise its an empty value */
54
+	/* A NULL string is a SQL NULL value, otherwise its an empty value */
48 55
 	if (!_s) {
49 56
 		memset(_v, 0, sizeof(db_val_t));
50 57
 			/* Initialize the string pointers to a dummy empty
... ...
@@ -107,37 +114,42 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l)
107 114
 
108 115
 	case DB_STRING:
109 116
 		LM_DBG("converting STRING [%s]\n", _s);
110
-		/*
111
-		 * Normally we could just use the string returned from the DB library,
112
-		 * as its usually not immediately freed after the free_result. But as
113
-		 * there exists some corner cases where this string gets invalid, we
114
-		 * need to copy it here.
115
-		 */
116
-		VAL_STRING(_v) = pkg_malloc(_l + 1);
117
-		if (VAL_STRING(_v) == NULL) {
118
-			LM_ERR("no private memory left\n");
119
-			return -6;
117
+
118
+		if (_cpy == 0) {
119
+			VAL_STRING(_v) = _s;
120
+		} else {
121
+			VAL_STRING(_v) = pkg_malloc(_l + 1);
122
+			if (VAL_STRING(_v) == NULL) {
123
+				LM_ERR("no private memory left\n");
124
+				return -6;
125
+			}
126
+			LM_DBG("allocate %d bytes memory for STRING at %p", _l + 1, VAL_STRING(_v));
127
+			strncpy((char*)VAL_STRING(_v), _s, _l);
128
+			((char*)VAL_STRING(_v))[_l] = '\0';
129
+			VAL_FREE(_v) = 1;
120 130
 		}
121
-		LM_DBG("allocate %d bytes memory for STRING at %p", _l + 1, VAL_STRING(_v));
122
-		strncpy((char*)VAL_STRING(_v), _s, _l);
123
-		((char*)VAL_STRING(_v))[_l] = '\0';
131
+
124 132
 		VAL_TYPE(_v) = DB_STRING;
125
-		VAL_FREE(_v) = 1;
126 133
 		return 0;
127 134
 
128 135
 	case DB_STR:
129 136
 		LM_DBG("converting STR [%.*s]\n", _l, _s);
130
-		/* same problem as DB_STRING.. */
131
-		VAL_STR(_v).s = pkg_malloc(_l);
132
-		if (VAL_STR(_v).s == NULL) {
133
-			LM_ERR("no private memory left\n");
134
-			return -7;
137
+
138
+		if (_cpy == 0) {
139
+			VAL_STR(_v).s = (char*) _s;
140
+		} else {
141
+			VAL_STR(_v).s = pkg_malloc(_l);
142
+			if (VAL_STR(_v).s == NULL) {
143
+				LM_ERR("no private memory left\n");
144
+				return -7;
145
+			}
146
+			LM_DBG("allocate %d bytes memory for STR at %p", _l, VAL_STR(_v).s);
147
+			strncpy(VAL_STR(_v).s, _s, _l);
148
+			VAL_FREE(_v) = 1;
135 149
 		}
136
-		LM_DBG("allocate %d bytes memory for STR at %p", _l, VAL_STR(_v).s);
137
-		strncpy(VAL_STR(_v).s, _s, _l);
150
+
138 151
 		VAL_STR(_v).len = _l;
139 152
 		VAL_TYPE(_v) = DB_STR;
140
-		VAL_FREE(_v) = 1;
141 153
 		return 0;
142 154
 
143 155
 	case DB_DATETIME:
... ...
@@ -153,17 +165,22 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l)
153 165
 
154 166
 	case DB_BLOB:
155 167
 		LM_DBG("converting BLOB [%.*s]\n", _l, _s);
156
-		/* same problem as DB_STRING.. */
157
-		VAL_BLOB(_v).s = pkg_malloc(_l);
158
-		if (VAL_BLOB(_v).s == NULL) {
159
-			LM_ERR("no private memory left\n");
160
-			return -9;
168
+
169
+		if (_cpy == 0) {
170
+			VAL_BLOB(_v).s = (char*) _s;
171
+		} else {
172
+			VAL_BLOB(_v).s = pkg_malloc(_l);
173
+			if (VAL_BLOB(_v).s == NULL) {
174
+				LM_ERR("no private memory left\n");
175
+				return -9;
176
+			}
177
+			LM_DBG("allocate %d bytes memory for BLOB at %p", _l, VAL_BLOB(_v).s);
178
+			strncpy(VAL_BLOB(_v).s, _s, _l);
179
+			VAL_FREE(_v) = 1;
161 180
 		}
162
-		LM_DBG("allocate %d bytes memory for BLOB at %p", _l, VAL_BLOB(_v).s);
163
-		strncpy(VAL_BLOB(_v).s, _s, _l);
181
+
164 182
 		VAL_BLOB(_v).len = _l;
165 183
 		VAL_TYPE(_v) = DB_BLOB;
166
-		VAL_FREE(_v) = 1;
167 184
 		return 0;
168 185
 	}
169 186
 	return -10;
Browse code

- change behaviour of db_str2val, this now copy strings - this change was necessary because in some circumstances the memory that is returned from the database driver is used by some other means too fast, causes crashed or wrong results later on, this bug is timing related - this means for mysql and unixodbc that the performance will be decreased a bit, postgres already copied the string, so here nothing change - add a new function to DB core API, db_val2str that, usable to convert numerical values to a string, and common things like NULL values conversion, parameter checks - convert mysql, postgres and unixodbc module to use this new function - convert postgres function to use the core db_str2val function, the copying is now done at a different place, cleanup the code somewhat - remove unnecessary assignment if a NULL value is encountered in postgres - TODO: fix NULL handling and double copying that is done now for unixodbc

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@5359 689a6050-402a-0410-94f2-e92a70836424

Henning Westerholt authored on 15/12/2008 15:07:12
Showing 1 changed files
... ...
@@ -23,6 +23,9 @@
23 23
 
24 24
 #include "db_ut.h"
25 25
 
26
+#include <stdio.h>
27
+#include <time.h>
28
+
26 29
 /*!
27 30
  * \brief Convert a str to a db value, copy strings
28 31
  *
... ...
@@ -102,37 +105,145 @@ int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l)
102 105
 		}
103 106
 		break;
104 107
 
105
-		case DB_STRING:
106
-			LM_DBG("converting STRING [%s]\n", _s);
107
-			VAL_STRING(_v) = _s;
108
-			VAL_TYPE(_v) = DB_STRING;
108
+	case DB_STRING:
109
+		LM_DBG("converting STRING [%s]\n", _s);
110
+		/*
111
+		 * Normally we could just use the string returned from the DB library,
112
+		 * as its usually not immediately freed after the free_result. But as
113
+		 * there exists some corner cases where this string gets invalid, we
114
+		 * need to copy it here.
115
+		 */
116
+		VAL_STRING(_v) = pkg_malloc(_l + 1);
117
+		if (VAL_STRING(_v) == NULL) {
118
+			LM_ERR("no private memory left\n");
119
+			return -6;
120
+		}
121
+		LM_DBG("allocate %d bytes memory for STRING at %p", _l + 1, VAL_STRING(_v));
122
+		strncpy((char*)VAL_STRING(_v), _s, _l);
123
+		((char*)VAL_STRING(_v))[_l] = '\0';
124
+		VAL_TYPE(_v) = DB_STRING;
125
+		VAL_FREE(_v) = 1;
126
+		return 0;
127
+
128
+	case DB_STR:
129
+		LM_DBG("converting STR [%.*s]\n", _l, _s);
130
+		/* same problem as DB_STRING.. */
131
+		VAL_STR(_v).s = pkg_malloc(_l);
132
+		if (VAL_STR(_v).s == NULL) {
133
+			LM_ERR("no private memory left\n");
134
+			return -7;
135
+		}
136
+		LM_DBG("allocate %d bytes memory for STR at %p", _l, VAL_STR(_v).s);
137
+		strncpy(VAL_STR(_v).s, _s, _l);
138
+		VAL_STR(_v).len = _l;
139
+		VAL_TYPE(_v) = DB_STR;
140
+		VAL_FREE(_v) = 1;
141
+		return 0;
142
+
143
+	case DB_DATETIME:
144
+		LM_DBG("converting DATETIME [%s]\n", _s);
145
+		if (db_str2time(_s, &VAL_TIME(_v)) < 0) {
146
+			LM_ERR("error while converting datetime value from string\n");
147
+			return -8;
148
+		} else {
149
+			VAL_TYPE(_v) = DB_DATETIME;
109 150
 			return 0;
151
+		}
152
+		break;
153
+
154
+	case DB_BLOB:
155
+		LM_DBG("converting BLOB [%.*s]\n", _l, _s);
156
+		/* same problem as DB_STRING.. */
157
+		VAL_BLOB(_v).s = pkg_malloc(_l);
158
+		if (VAL_BLOB(_v).s == NULL) {
159
+			LM_ERR("no private memory left\n");
160
+			return -9;
161
+		}
162
+		LM_DBG("allocate %d bytes memory for BLOB at %p", _l, VAL_BLOB(_v).s);
163
+		strncpy(VAL_BLOB(_v).s, _s, _l);
164
+		VAL_BLOB(_v).len = _l;
165
+		VAL_TYPE(_v) = DB_BLOB;
166
+		VAL_FREE(_v) = 1;
167
+		return 0;
168
+	}
169
+	return -10;
170
+}
110 171
 
111
-		case DB_STR:
112
-			LM_DBG("converting STR [%.*s]\n", _l, _s);
113
-			VAL_STR(_v).s = (char*)_s;
114
-			VAL_STR(_v).len = _l;
115
-			VAL_TYPE(_v) = DB_STR;
172
+
173
+/*!
174
+ * \brief Convert a numerical value to a string
175
+ *
176
+ * Convert a numerical value to a string, used when converting result from a query.
177
+ * Implement common functionality needed from the databases, does parameter checking.
178
+ * \param _c database connection
179
+ * \param _v source value
180
+ * \param _s target string
181
+ * \param _len target string length
182
+ * \return 0 on success, negative on error, 1 if value must be converted by other means
183
+ */
184
+int db_val2str(const db_con_t* _c, const db_val_t* _v, char* _s, int* _len)
185
+{
186
+	if (!_c || !_v || !_s || !_len || !*_len) {
187
+		LM_ERR("invalid parameter value\n");
188
+		return -1;
189
+	}
190
+
191
+	if (VAL_NULL(_v)) {
192
+		if (*_len < sizeof("NULL")) {
193
+			LM_ERR("buffer too small\n");
194
+			return -1;
195
+		}
196
+		*_len = snprintf(_s, *_len, "NULL");
197
+		return 0;
198
+	}
199
+	
200
+	switch(VAL_TYPE(_v)) {
201
+	case DB_INT:
202
+		if (db_int2str(VAL_INT(_v), _s, _len) < 0) {
203
+			LM_ERR("error while converting string to int\n");
204
+			return -2;
205
+		} else {
116 206
 			return 0;
207
+		}
208
+		break;
117 209
 
118
-		case DB_DATETIME:
119
-			LM_DBG("converting DATETIME [%s]\n", _s);
120
-			if (db_str2time(_s, &VAL_TIME(_v)) < 0) {
121
-				LM_ERR("error while converting datetime value from string\n");
122
-				return -6;
123
-			} else {
124
-				VAL_TYPE(_v) = DB_DATETIME;
125
-				return 0;
126
-			}
127
-			break;
128
-
129
-		case DB_BLOB:
130
-			LM_DBG("converting BLOB [%.*s]\n", _l, _s);
131
-			VAL_BLOB(_v).s = (char*)_s;
132
-			VAL_BLOB(_v).len = _l;
133
-			VAL_TYPE(_v) = DB_BLOB;
210
+	case DB_BIGINT:
211
+		if (db_longlong2str(VAL_BIGINT(_v), _s, _len) < 0) {
212
+			LM_ERR("error while converting string to big int\n");
213
+			return -3;
214
+		} else {
134 215
 			return 0;
216
+		}
217
+		break;
218
+
219
+	case DB_BITMAP:
220
+		if (db_int2str(VAL_BITMAP(_v), _s, _len) < 0) {
221
+			LM_ERR("error while converting string to int\n");
222
+			return -4;
223
+		} else {
224
+			return 0;
225
+		}
226
+		break;
227
+
228
+	case DB_DOUBLE:
229
+		if (db_double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
230
+			LM_ERR("error while converting string to double\n");
231
+			return -5;
232
+		} else {
233
+			return 0;
234
+		}
235
+		break;
236
+
237
+	case DB_DATETIME:
238
+		if (db_time2str(VAL_TIME(_v), _s, _len) < 0) {
239
+			LM_ERR("failed to convert string to time_t\n");
240
+			return -8;
241
+		} else {
242
+			return 0;
243
+		}
244
+		break;
245
+
246
+	default:
247
+		return 1;
135 248
 	}
136
-	return -7;
137 249
 }
138
-
Browse code

- move db_mysql and db_unixodbc str2val implementation to the DB core, remove the duplicated implementation in the modules (preparation for query copy problem) - introduced a small regression for the unixodbc in the NULL value handling that needs to be fixed

git-svn-id: https://openser.svn.sourceforge.net/svnroot/openser/trunk@5322 689a6050-402a-0410-94f2-e92a70836424

Henning Westerholt authored on 10/12/2008 18:41:09
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,138 @@
1
+/*
2
+ * $Id$ 
3
+ *
4
+ * Copyright (C) 2001-2003 FhG Fokus
5
+ * Copyright (C) 2008-2009 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
+ */
23
+
24
+#include "db_ut.h"
25
+
26
+/*!
27
+ * \brief Convert a str to a db value, copy strings
28
+ *
29
+ * Convert a str to a db value, does not copy strings.
30
+ * \param _t destination value type
31
+ * \param _v destination value
32
+ * \param _s source string
33
+ * \param _l string length
34
+ * \return 0 on success, negative on error
35
+ */
36
+int db_str2val(const db_type_t _t, db_val_t* _v, const char* _s, const int _l)
37
+{
38
+	static str dummy_string = {"", 0};
39
+	
40
+	if (!_v) {
41
+		LM_ERR("invalid parameter value\n");
42
+		return -1;
43
+	}
44
+	/* A NULL string is a NULL value, otherwise its an empty value */
45
+	if (!_s) {
46
+		memset(_v, 0, sizeof(db_val_t));
47
+			/* Initialize the string pointers to a dummy empty
48
+			 * string so that we do not crash when the NULL flag
49
+			 * is set but the module does not check it properly
50
+			 */
51
+		VAL_STRING(_v) = dummy_string.s;
52
+		VAL_STR(_v) = dummy_string;
53
+		VAL_BLOB(_v) = dummy_string;
54
+		VAL_TYPE(_v) = _t;
55
+		VAL_NULL(_v) = 1;
56
+		return 0;
57
+	}
58
+	VAL_NULL(_v) = 0;
59
+
60
+	switch(_t) {