File: db_val.c

package info (click to toggle)
openser 1.1.0-9etch1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,828 kB
  • ctags: 11,809
  • sloc: ansic: 120,528; sh: 5,249; yacc: 1,716; makefile: 1,261; php: 656; perl: 205; sql: 190
file content (367 lines) | stat: -rw-r--r-- 7,914 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * $Id: db_val.c,v 1.7 2006/06/01 09:41:31 bogdan_iancu Exp $
 *
 * POSTGRES module, portions of this code were templated using
 * the mysql module, thus it's similarity.
 *
 * Copyright (C) 2003 August.Net Services, LLC
 *
 * This file is part of openser, a free SIP server.
 *
 * openser is free software; you can redistribute it and/or modify
 * 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
 *
 * openser is distributed in the hope that it will be useful,
 * 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 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * ---
 *
 * History
 * -------
 * 2003-04-06 initial code written (Greg Fausak/Andy Fullford)
 * 2003-04-14 gmtime changed to localtime because mktime later
 *            expects localtime, changed daylight saving bug
 *            previously found in mysql module (janakj)
 *
 */

#include <limits.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../../db/db_val.h"
#include "../../dprint.h"
#include "defs.h"
#include "db_utils.h"
#include "con_postgres.h"
#include "aug_std.h"


char *strptime(const char *s, const char *format, struct tm *tm);

/*
 * Convert a string to integer
 */
static inline int str2int(const char* _s, int* _v)
{
	long tmp;

#ifdef PARANOID
	if ((!_s) || (!_v)) {
		LOG(L_ERR, "str2int(): Invalid parameter value\n");
		return -1;
	}
#endif

	tmp = strtoul(_s, 0, 10);
	if ((tmp == ULONG_MAX && errno == ERANGE) || 
	    (tmp < INT_MIN) || (tmp > UINT_MAX)) {
		printf("str2int: Value out of range\n");
		return -1;
	}

	*_v = (int)tmp;

	return 0;
}


/*
 * Convert a string to double
 */
static inline int str2double(const char* _s, double* _v)
{
#ifdef PARANOID
	if ((!_s) || (!_v)) {
		LOG(L_ERR, "str2double(): Invalid parameter value\n");
		return -1;
	}
#endif
	*_v = atof(_s);
	return 0;
}


/* 
 * Convert a string to time_t
 */
static inline int str2time(const char* _s, time_t* _v)
{
	struct tm t;
#ifdef PARANOID
	if ((!_s) || (!_v)) {
		LOG(L_ERR, "str2time(): Invalid parameter value\n");
		return -1;
	}
#endif

	memset(&t, '\0', sizeof(struct tm));
	strptime(_s,"%Y-%m-%d %H:%M:%S %z",&t);

	     /* Daylight saving information got lost in the database
	      * so let mktime to guess it. This eliminates the bug when
	      * contacts reloaded from the database have different time
	      * of expiration by one hour when daylight saving is used
	      */ 
	t.tm_isdst = -1;   
	*_v = mktime(&t);

	return 0;
}


/*
 * Convert an integer to string
 */
static inline int int2str(int _v, char* _s, int* _l)
{
#ifdef PARANOID
	if ((!_s) || (!_l) || (!*_l)) {
		LOG(L_ERR, "int2str(): Invalid parameter value\n");
		return -1;
	}
#endif
	*_l = snprintf(_s, *_l, "%-d", _v);
	return 0;
}


/*
 * Convert a double to string
 */
static inline int double2str(double _v, char* _s, int* _l)
{
#ifdef PARANOID
	if ((!_s) || (!_l) || (!*_l)) {
		LOG(L_ERR, "double2str(): Invalid parameter value\n");
		return -1;
	}
#endif
	*_l = snprintf(_s, *_l, "%-10.2f", _v);
	return 0;
}


/*
 * Convert time_t to string
 */
static inline int time2str(time_t _v, char* _s, int* _l)
{
	struct tm *t;
	int bl;
#ifdef PARANOID
	if ((!_s) || (!_l) || (*_l < 2))  {
		LOG(L_ERR, "Invalid parameter value\n");
		return -1;
	}
#endif

	t = localtime(&_v);

	if((bl=strftime(_s,(size_t)(*_l)-1,"'%Y-%m-%d %H:%M:%S %z'",t))>0)
		*_l = bl;
	
	return 0;
}

/*
 * Does not copy strings
 */
int str2valp(db_type_t _t, db_val_t* _v, char* _s, int _l, void *_p)
{
#ifdef PARANOID
	if (!_v) {
		LOG(L_ERR, "str2valp(): Invalid parameter value\n");
		return -1;
	}
#endif

	if (!_s) {
		DLOG("str2valp", "got a null value");
		VAL_TYPE(_v) = _t;
		VAL_NULL(_v) = 1;
		return 0;
	}

	switch(_t) {
	case DB_INT:
	case DB_BITMAP:
		DBG("DEBUG:postgres:str2valp: got int %s \n", _s);
		if (str2int(_s, &VAL_INT(_v)) < 0) {
			LOG(L_ERR, "str2valp(): Error while converting integer value "
				"from string\n");
			return -2;
		} else {
			VAL_TYPE(_v) = DB_INT;
			return 0;
		}
	
	case DB_DOUBLE:
		DBG("DEBUG:postgres:str2valp: got double %s \n", _s);
		if (str2double(_s, &VAL_DOUBLE(_v)) < 0) {
			LOG(L_ERR, "str2valp(): Error while converting double value "
				"from string\n");
			return -3;
		} else {
			VAL_TYPE(_v) = DB_DOUBLE;
			return 0;
		}

	case DB_STRING:
		DBG("DEBUG:postgres:str2valp: got string %s \n", _s);
		VAL_STRING(_v) = aug_strdup(_s, _p);
		VAL_TYPE(_v) = DB_STRING;
		return 0;

	case DB_STR:
		VAL_STR(_v).s = aug_alloc(_l + 1, _p);
		memcpy( _s, VAL_STR(_v).s, _l);
		VAL_STR(_v).s[_l] = (char) 0;
		VAL_STR(_v).len = _l;
		VAL_TYPE(_v) = DB_STR;
		DBG("DEBUG:postgres:str2valp: got len string %d %s\n", _l, _s);
		return 0;

	case DB_DATETIME:
		DBG("DEBUG:postgres:str2valp: got time %s \n", _s);
		if (str2time(_s, &VAL_TIME(_v)) < 0) {
			LOG(L_ERR,"str2valp(): Error converting datetime\n");
			return -4;
		} else {
			VAL_TYPE(_v) = DB_DATETIME;
			return 0;
		}

	case DB_BLOB:
		VAL_BLOB(_v).s = (char*)PQunescapeBytea((unsigned char*)_s,
				(size_t*)&(VAL_BLOB(_v).len) );
		VAL_TYPE(_v) = DB_BLOB;
		DBG("DEBUG:postgres:str2valp: got blob len %d\n", _l);
		return 0;
	}
	return -5;
}


/*
 * Used when converting result from a query
 */
int val2str(db_val_t* _v, char* _s, int* _len)
{
	int l;
	char *tmp_s;
	size_t tmp_len;

#ifdef PARANOID
	if ((!_v) || (!_s) || (!_len) || (!*_len)) {
		LOG(L_ERR, "val2str(): Invalid parameter value\n");
		return -1;
	}
#endif
	if (VAL_NULL(_v)) {
		*_len = snprintf(_s, *_len, "NULL");
		return 0;
	}
	
	switch(VAL_TYPE(_v)) {
	case DB_INT:
		if (int2str(VAL_INT(_v), _s, _len) < 0) {
			LOG(L_ERR, "val2str(): Error while converting string to int\n");
			return -2;
		} else {
			return 0;
		}
		break;

	case DB_BITMAP:
		if (int2str(VAL_BITMAP(_v), _s, _len) < 0) {
			LOG(L_ERR, "val2str: Error while converting string to int\n");
			return -3;
		} else {
			return 0;
		}
		break;

	case DB_DOUBLE:
		if (double2str(VAL_DOUBLE(_v), _s, _len) < 0) {
			LOG(L_ERR, "val2str(): Error while converting string to double\n");
			return -3;
		} else {
			return 0;
		}
		break;

	case DB_STRING:
		l = strlen(VAL_STRING(_v));
		LOG(L_ERR, "val2str(): converting %s, %d\n", VAL_STRING(_v), l);
		if (*_len < (l + 3)) {
			LOG(L_ERR, "val2str(): Destination buffer too short for string\n");
			return -4;
		} else {
			*_s++ = '\'';
			memcpy(_s, VAL_STRING(_v), l);
			*(_s + l) = '\'';
			*(_s + l + 1) = '\0'; /* FIXME */
			*_len = l + 2;
			return 0;
		}
		break;

	case DB_STR:
		l = VAL_STR(_v).len;
		if (*_len < (l + 3)) {
			LOG(L_ERR, "val2str(): Destination buffer too short for str\n");
			return -5;
		} else {
			*_s++ = '\'';
			memcpy(_s, VAL_STR(_v).s, l);
			*(_s + l) = '\'';
			*(_s + l + 1) = '\0';
			*_len = l + 2;
			return 0;
		}
		break;

	case DB_DATETIME:
		if (time2str(VAL_TIME(_v), _s, _len) < 0) {
			LOG(L_ERR, "val2str(): Error while converting string to time_t\n");
			return -6;
		} else {
			return 0;
		}
		break;

	case DB_BLOB:
		l = VAL_BLOB(_v).len;
		if (*_len < (l * 2 + 3)) {
			LOG(L_ERR, "val2str(): Destination buffer too short for blob\n");
			return -7;
		} else {
			*_s++ = '\'';
			tmp_s = (char*)PQescapeBytea((unsigned char*)VAL_STRING(_v),
					(size_t)l, (size_t*)&tmp_len);
			memcpy(_s, tmp_s, tmp_len);
			PQfreemem(tmp_s);
			tmp_len = strlen(_s);
			*(_s + tmp_len) = '\'';
			*(_s + tmp_len + 1) = '\0';
			*_len = tmp_len + 2;
			return 0;
		}
		break;

	default:
		DBG("val2str(): Unknown data type\n");
		return -7;
	}
	return -8;
}