File: db_res.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 (254 lines) | stat: -rw-r--r-- 5,822 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
/*
 * $Id: db_res.c,v 1.4 2006/05/25 15:25:25 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)
 *
 */

#include <stdlib.h>
#include <string.h>
#include "../../db/db_res.h"
#include "../../db/db_con.h"
#include "../../dprint.h"
#include "../../mem/mem.h"
#include "defs.h"
#include "con_postgres.h"
#include "pg_type.h"
#include "aug_std.h"

int str2valp(db_type_t _t, db_val_t* _v, const char* _s, int _l, void *_p);

/*
 * Create a new result structure and initialize it
 */
db_res_t* new_result_pg(char *parent)
{
	db_res_t* r;

	r = (db_res_t*)aug_alloc(sizeof(db_res_t), parent);

	RES_NAMES(r) = 0;
	RES_TYPES(r) = 0;
	RES_COL_N(r) = 0;
	RES_ROWS(r) = 0;
	RES_ROW_N(r) = 0;

	return r;
}

/*
 * Get and convert columns from a result
 */
static inline int get_columns(db_con_t* _h, db_res_t* _r)
{
	int n, i;

	n = PQnfields(CON_RESULT(_h));

	if (!n) {
		LOG(L_ERR, "get_columns(): No columns\n");
		return -2;
	}

        RES_NAMES(_r) = (db_key_t*)aug_alloc(sizeof(db_key_t) * n, _r);
	RES_TYPES(_r) = (db_type_t*)aug_alloc(sizeof(db_type_t) * n, _r);
	RES_COL_N(_r) = n;

	for(i = 0; i < n; i++) {
		int ft;
		RES_NAMES(_r)[i] = aug_strdup(PQfname(CON_RESULT(_h),i),
			RES_NAMES(_r));
		switch(ft = PQftype(CON_RESULT(_h),i))
		{
			case INT2OID:
			case INT4OID:
			case INT8OID:
				RES_TYPES(_r)[i] = DB_INT;
			break;

			case FLOAT4OID:
			case FLOAT8OID:
			case NUMERICOID:
				RES_TYPES(_r)[i] = DB_DOUBLE;
			break;

			case DATEOID:
			case TIMESTAMPOID:
			case TIMESTAMPTZOID:
				RES_TYPES(_r)[i] = DB_DATETIME;
			break;

			case VARCHAROID:
				RES_TYPES(_r)[i] = DB_STRING;
			break;
			
			default:
				LOG(L_ERR, "unknown type %d\n", ft);
				RES_TYPES(_r)[i] = DB_STRING;
			break;
		}		
	}
	return 0;
}

/*
 * Convert a row from result into db API representation
 */
int convert_row_pg(db_con_t* _h, db_res_t* _res, db_row_t* _r, char **row_buf,
	char *parent)
{
	int i;

    ROW_VALUES(_r) = (db_val_t*)aug_alloc(
		sizeof(db_val_t) * RES_COL_N(_res), parent);
	if(ROW_VALUES(_r)==NULL)
	{
		LOG(L_ERR, "convert_row_pg(): no more memory\n");
		return -1;
	}
	memset(ROW_VALUES(_r),0,sizeof(db_val_t) * RES_COL_N(_res));
	ROW_N(_r) = RES_COL_N(_res);

	/* I'm not sure about this */
	/* PQfsize() gives us the native length in the database, so */
	/* an int would be 4, not the strlen of the value */
	/* however, strlen of the value would be easy to strlen() */

	for(i = 0; i < RES_COL_N(_res); i++) {
		if (str2valp(RES_TYPES(_res)[i], &(ROW_VALUES(_r)[i]), 
			    row_buf[i],
			    PQfsize(CON_RESULT(_h),i),
			    (void *) ROW_VALUES(_r)) < 0) {
		LOG(L_ERR, "convert_row_pg(): Error while converting value\n");
			return -3;
		}
	}
	return 0;
}
/*
 * Convert rows from postgres to db API representation
 */
static inline int convert_rows(db_con_t* _h, db_res_t* _r)
{
	int n, i, j, k;
	char **row_buf, *s;
	int value;

	n = PQntuples(CON_RESULT(_h));
	RES_ROW_N(_r) = n;
	if (!n) {
		RES_ROWS(_r) = 0;
		return 0;
	}
	j = RES_COL_N(_r);
	value = 0;

	/* row instance but column(s) value ? */
	for(i = 0; i < n; i++) {
		for(k = 0; k < j; k++) {
			if(PQgetlength(CON_RESULT(_h), i, k))
				value = 1;
		}
	}
	/* Looking for the row instance with no values */
	if(!value){
		LOG(L_ERR, "convert_rows(): Row instance, does not have "
			"a column value!\n");
		RES_ROW_N(_r) = 0;
		return 0;
	}

	RES_ROWS(_r) = (struct db_row*)aug_alloc(sizeof(db_row_t) * n,_r);
	if(RES_ROWS(_r)==NULL)
	{
		LOG(L_ERR, "convert_rows(): no more memory\n");
		return -1;
	}
	memset(RES_ROWS(_r),0,sizeof(db_row_t) * n);
	row_buf = (char **) aug_alloc(sizeof(char *) * (j + 1), CON_SQLURL(_h));

	/* j is the number of columns in the answer set */
	/* n is the number of rows in the answer set */
	for(i = 0; i < n; i++) {
		for(k = 0; k < j; k++) {
			if(PQgetisnull(CON_RESULT(_h), i, k)) {
				/*
				** I don't believe there is a NULL
				** representation, so, I'll cheat
				*/
				s = "";
			} else {
				s = PQgetvalue(CON_RESULT(_h), i, k);
			}
			*(row_buf + k) = aug_strdup(s, row_buf);
		}
		*(row_buf + k) = (char *) 0;

		/*
		** ASSERT: row_buf contains an entire row in strings
		*/
		if (convert_row_pg(_h, _r, &(RES_ROWS(_r)[i]), row_buf,
			(char *) RES_ROWS(_r)) < 0) {
			LOG(L_ERR, "convert_rows(): Error converting row #%d\n",
				i);
			RES_ROW_N(_r) = i;
			aug_free(row_buf);
			return -4;
		}
	}
	aug_free(row_buf);
	return 0;
}

/*
 * Fill the structure with data from database
 */
int convert_result(db_con_t* _h, db_res_t* _r)
{
	if (get_columns(_h, _r) < 0) {
		LOG(L_ERR, "convert_result(): Error getting column names\n");
		return -2;
	}

	if (convert_rows(_h, _r) < 0) {
		LOG(L_ERR, "convert_result(): Error while converting rows\n");
		return -3;
	}
	return 0;
}


/*
 * Release memory used by a result structure
 */
int free_result(db_res_t* _r)
{
	aug_free(_r);

	return 0;
}