1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Authors: Edin Kadribasic <edink@emini.dk> |
16 : | Ilia Alshanestsky <ilia@prohost.org> |
17 : | Wez Furlong <wez@php.net> |
18 : +----------------------------------------------------------------------+
19 : */
20 :
21 : /* $Id: pgsql_statement.c 290214 2009-11-04 19:32:27Z mbeccati $ */
22 :
23 : #ifdef HAVE_CONFIG_H
24 : #include "config.h"
25 : #endif
26 :
27 : #include "php.h"
28 : #include "php_ini.h"
29 : #include "ext/standard/info.h"
30 : #include "pdo/php_pdo.h"
31 : #include "pdo/php_pdo_driver.h"
32 : #include "php_pdo_pgsql.h"
33 : #include "php_pdo_pgsql_int.h"
34 : #if HAVE_NETINET_IN_H
35 : #include <netinet/in.h>
36 : #endif
37 :
38 : /* from postgresql/src/include/catalog/pg_type.h */
39 : #define BOOLOID 16
40 : #define BYTEAOID 17
41 : #define INT8OID 20
42 : #define INT2OID 21
43 : #define INT4OID 23
44 : #define TEXTOID 25
45 : #define OIDOID 26
46 :
47 : static int pgsql_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC)
48 143 : {
49 143 : pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
50 :
51 143 : if (S->result) {
52 : /* free the resource */
53 142 : PQclear(S->result);
54 142 : S->result = NULL;
55 : }
56 :
57 : #if HAVE_PQPREPARE
58 143 : if (S->stmt_name) {
59 130 : pdo_pgsql_db_handle *H = S->H;
60 130 : char *q = NULL;
61 : PGresult *res;
62 :
63 130 : if (S->is_prepared) {
64 120 : spprintf(&q, 0, "DEALLOCATE %s", S->stmt_name);
65 120 : res = PQexec(H->server, q);
66 120 : efree(q);
67 120 : if (res) {
68 120 : PQclear(res);
69 : }
70 : }
71 130 : efree(S->stmt_name);
72 130 : S->stmt_name = NULL;
73 : }
74 143 : if (S->param_lengths) {
75 38 : efree(S->param_lengths);
76 38 : S->param_lengths = NULL;
77 : }
78 143 : if (S->param_values) {
79 38 : efree(S->param_values);
80 38 : S->param_values = NULL;
81 : }
82 143 : if (S->param_formats) {
83 38 : efree(S->param_formats);
84 38 : S->param_formats = NULL;
85 : }
86 143 : if (S->param_types) {
87 38 : efree(S->param_types);
88 38 : S->param_types = NULL;
89 : }
90 143 : if (S->query) {
91 130 : efree(S->query);
92 130 : S->query = NULL;
93 : }
94 : #endif
95 :
96 143 : if (S->cursor_name) {
97 0 : pdo_pgsql_db_handle *H = S->H;
98 0 : char *q = NULL;
99 : PGresult *res;
100 :
101 0 : spprintf(&q, 0, "CLOSE %s", S->cursor_name);
102 0 : res = PQexec(H->server, q);
103 0 : efree(q);
104 0 : if (res) PQclear(res);
105 0 : efree(S->cursor_name);
106 0 : S->cursor_name = NULL;
107 : }
108 :
109 143 : if(S->cols) {
110 130 : efree(S->cols);
111 130 : S->cols = NULL;
112 : }
113 143 : efree(S);
114 143 : stmt->driver_data = NULL;
115 143 : return 1;
116 : }
117 :
118 : static int pgsql_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
119 231 : {
120 231 : pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
121 231 : pdo_pgsql_db_handle *H = S->H;
122 : ExecStatusType status;
123 :
124 : /* ensure that we free any previous unfetched results */
125 231 : if(S->result) {
126 88 : PQclear(S->result);
127 88 : S->result = NULL;
128 : }
129 :
130 231 : S->current_row = 0;
131 :
132 : #if HAVE_PQPREPARE
133 231 : if (S->stmt_name) {
134 : /* using a prepared statement */
135 :
136 215 : if (!S->is_prepared) {
137 130 : stmt_retry:
138 : /* we deferred the prepare until now, because we didn't
139 : * know anything about the parameter types; now we do */
140 130 : S->result = PQprepare(H->server, S->stmt_name, S->query,
141 : stmt->bound_params ? zend_hash_num_elements(stmt->bound_params) : 0,
142 : S->param_types);
143 130 : status = PQresultStatus(S->result);
144 130 : switch (status) {
145 : case PGRES_COMMAND_OK:
146 : case PGRES_TUPLES_OK:
147 : /* it worked */
148 121 : S->is_prepared = 1;
149 121 : PQclear(S->result);
150 121 : break;
151 : default: {
152 9 : char *sqlstate = pdo_pgsql_sqlstate(S->result);
153 : /* 42P05 means that the prepared statement already existed. this can happen if you use
154 : * a connection pooling software line pgpool which doesn't close the db-connection once
155 : * php disconnects. if php dies (no chance to run RSHUTDOWN) during execution it has no
156 : * chance to DEALLOCATE the prepared statements it has created. so, if we hit a 42P05 we
157 : * deallocate it and retry ONCE (thies 2005.12.15)
158 : */
159 9 : if (!strcmp(sqlstate, "42P05")) {
160 : char buf[100]; /* stmt_name == "pdo_crsr_%08x" */
161 : PGresult *res;
162 0 : snprintf(buf, sizeof(buf), "DEALLOCATE %s", S->stmt_name);
163 0 : res = PQexec(H->server, buf);
164 0 : if (res) {
165 0 : PQclear(res);
166 : }
167 0 : goto stmt_retry;
168 : } else {
169 9 : pdo_pgsql_error_stmt(stmt, status, sqlstate);
170 9 : return 0;
171 : }
172 : }
173 : }
174 : }
175 206 : S->result = PQexecPrepared(H->server, S->stmt_name,
176 : stmt->bound_params ?
177 : zend_hash_num_elements(stmt->bound_params) :
178 : 0,
179 : (const char**)S->param_values,
180 : S->param_lengths,
181 : S->param_formats,
182 : 0);
183 : } else
184 : #endif
185 16 : if (S->cursor_name) {
186 0 : char *q = NULL;
187 0 : spprintf(&q, 0, "DECLARE %s CURSOR FOR %s", S->cursor_name, stmt->active_query_string);
188 0 : S->result = PQexec(H->server, q);
189 0 : efree(q);
190 : } else {
191 16 : S->result = PQexec(H->server, stmt->active_query_string);
192 : }
193 222 : status = PQresultStatus(S->result);
194 :
195 222 : if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
196 5 : pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
197 5 : return 0;
198 : }
199 :
200 217 : if (!stmt->executed && !stmt->column_count) {
201 131 : stmt->column_count = (int) PQnfields(S->result);
202 131 : S->cols = ecalloc(stmt->column_count, sizeof(pdo_pgsql_column));
203 : }
204 :
205 217 : if (status == PGRES_COMMAND_OK) {
206 68 : stmt->row_count = (long)atoi(PQcmdTuples(S->result));
207 68 : H->pgoid = PQoidValue(S->result);
208 : } else {
209 149 : stmt->row_count = (long)PQntuples(S->result);
210 : }
211 :
212 217 : return 1;
213 : }
214 :
215 : static int pgsql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
216 : enum pdo_param_event event_type TSRMLS_DC)
217 1230 : {
218 : #if HAVE_PQPREPARE
219 1230 : pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
220 :
221 1230 : if (S->stmt_name && param->is_param) {
222 887 : switch (event_type) {
223 : case PDO_PARAM_EVT_FREE:
224 148 : if (param->driver_data) {
225 1 : efree(param->driver_data);
226 : }
227 148 : break;
228 :
229 : case PDO_PARAM_EVT_NORMALIZE:
230 : /* decode name from $1, $2 into 0, 1 etc. */
231 150 : if (param->name) {
232 56 : if (param->name[0] == '$') {
233 0 : param->paramno = atoi(param->name + 1);
234 : } else {
235 : /* resolve parameter name to rewritten name */
236 : char *nameptr;
237 110 : if (stmt->bound_param_map && SUCCESS == zend_hash_find(stmt->bound_param_map,
238 : param->name, param->namelen + 1, (void**)&nameptr)) {
239 54 : param->paramno = atoi(nameptr + 1) - 1;
240 : } else {
241 2 : pdo_raise_impl_error(stmt->dbh, stmt, "HY093", param->name TSRMLS_CC);
242 2 : return 0;
243 : }
244 : }
245 : }
246 148 : break;
247 :
248 : case PDO_PARAM_EVT_ALLOC:
249 : case PDO_PARAM_EVT_EXEC_POST:
250 : case PDO_PARAM_EVT_FETCH_PRE:
251 : case PDO_PARAM_EVT_FETCH_POST:
252 : /* work is handled by EVT_NORMALIZE */
253 411 : return 1;
254 :
255 : case PDO_PARAM_EVT_EXEC_PRE:
256 178 : if (!stmt->bound_param_map) {
257 0 : return 0;
258 : }
259 178 : if (!S->param_values) {
260 38 : S->param_values = ecalloc(
261 : zend_hash_num_elements(stmt->bound_param_map),
262 : sizeof(char*));
263 38 : S->param_lengths = ecalloc(
264 : zend_hash_num_elements(stmt->bound_param_map),
265 : sizeof(int));
266 38 : S->param_formats = ecalloc(
267 : zend_hash_num_elements(stmt->bound_param_map),
268 : sizeof(int));
269 38 : S->param_types = ecalloc(
270 : zend_hash_num_elements(stmt->bound_param_map),
271 : sizeof(Oid));
272 : }
273 178 : if (param->paramno >= 0) {
274 178 : if (param->paramno > zend_hash_num_elements(stmt->bound_param_map)) {
275 0 : pdo_pgsql_error_stmt(stmt, PGRES_FATAL_ERROR, "HY105");
276 0 : return 0;
277 : }
278 :
279 178 : if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB &&
280 : Z_TYPE_P(param->parameter) == IS_RESOURCE) {
281 : php_stream *stm;
282 6 : php_stream_from_zval_no_verify(stm, ¶m->parameter);
283 6 : if (stm) {
284 6 : if (php_stream_is(stm, &pdo_pgsql_lob_stream_ops)) {
285 1 : struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stm->abstract;
286 1 : pdo_pgsql_bound_param *P = param->driver_data;
287 :
288 1 : if (P == NULL) {
289 1 : P = ecalloc(1, sizeof(*P));
290 1 : param->driver_data = P;
291 : }
292 1 : P->oid = htonl(self->oid);
293 1 : S->param_values[param->paramno] = (char*)&P->oid;
294 1 : S->param_lengths[param->paramno] = sizeof(P->oid);
295 1 : S->param_formats[param->paramno] = 1;
296 1 : S->param_types[param->paramno] = OIDOID;
297 1 : return 1;
298 : } else {
299 : int len;
300 :
301 5 : SEPARATE_ZVAL_IF_NOT_REF(¶m->parameter);
302 5 : Z_TYPE_P(param->parameter) = IS_STRING;
303 :
304 5 : if ((len = php_stream_copy_to_mem(stm, &Z_STRVAL_P(param->parameter), PHP_STREAM_COPY_ALL, 0)) > 0) {
305 3 : Z_STRLEN_P(param->parameter) = len;
306 : } else {
307 2 : ZVAL_EMPTY_STRING(param->parameter);
308 : }
309 : }
310 : } else {
311 : /* expected a stream resource */
312 0 : pdo_pgsql_error_stmt(stmt, PGRES_FATAL_ERROR, "HY105");
313 0 : return 0;
314 : }
315 : }
316 :
317 181 : if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL ||
318 : Z_TYPE_P(param->parameter) == IS_NULL) {
319 4 : S->param_values[param->paramno] = NULL;
320 4 : S->param_lengths[param->paramno] = 0;
321 173 : } else if (Z_TYPE_P(param->parameter) == IS_BOOL) {
322 2 : S->param_values[param->paramno] = Z_BVAL_P(param->parameter) ? "t" : "f";
323 2 : S->param_lengths[param->paramno] = 1;
324 2 : S->param_formats[param->paramno] = 0;
325 : } else {
326 171 : SEPARATE_ZVAL_IF_NOT_REF(¶m->parameter);
327 171 : convert_to_string(param->parameter);
328 171 : S->param_values[param->paramno] = Z_STRVAL_P(param->parameter);
329 171 : S->param_lengths[param->paramno] = Z_STRLEN_P(param->parameter);
330 171 : S->param_formats[param->paramno] = 0;
331 : }
332 :
333 177 : if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
334 9 : S->param_types[param->paramno] = 0;
335 9 : S->param_formats[param->paramno] = 1;
336 : } else {
337 168 : S->param_types[param->paramno] = 0;
338 : }
339 : }
340 : break;
341 : }
342 : }
343 : #endif
344 816 : return 1;
345 : }
346 :
347 : static int pgsql_stmt_fetch(pdo_stmt_t *stmt,
348 : enum pdo_fetch_orientation ori, long offset TSRMLS_DC)
349 356 : {
350 356 : pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
351 :
352 356 : if (S->cursor_name) {
353 : char *ori_str;
354 0 : char *q = NULL;
355 : ExecStatusType status;
356 :
357 0 : switch (ori) {
358 0 : case PDO_FETCH_ORI_NEXT: ori_str = "FORWARD"; break;
359 0 : case PDO_FETCH_ORI_PRIOR: ori_str = "BACKWARD"; break;
360 0 : case PDO_FETCH_ORI_REL: ori_str = "RELATIVE"; break;
361 : default:
362 0 : return 0;
363 : }
364 :
365 0 : spprintf(&q, 0, "FETCH %s %ld FROM %s", ori_str, offset, S->cursor_name);
366 0 : S->result = PQexec(S->H->server, q);
367 0 : efree(q);
368 0 : status = PQresultStatus(S->result);
369 :
370 0 : if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
371 0 : pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
372 0 : return 0;
373 : }
374 :
375 0 : S->current_row = 1;
376 0 : return 1;
377 :
378 : } else {
379 356 : if (S->current_row < stmt->row_count) {
380 272 : S->current_row++;
381 272 : return 1;
382 : } else {
383 84 : return 0;
384 : }
385 : }
386 : }
387 :
388 : static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
389 173 : {
390 173 : pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
391 173 : struct pdo_column_data *cols = stmt->columns;
392 : struct pdo_bound_param_data *param;
393 :
394 173 : if (!S->result) {
395 0 : return 0;
396 : }
397 :
398 173 : cols[colno].name = estrdup(PQfname(S->result, colno));
399 173 : cols[colno].namelen = strlen(cols[colno].name);
400 173 : cols[colno].maxlen = PQfsize(S->result, colno);
401 173 : cols[colno].precision = PQfmod(S->result, colno);
402 173 : S->cols[colno].pgsql_type = PQftype(S->result, colno);
403 :
404 173 : switch(S->cols[colno].pgsql_type) {
405 :
406 : case BOOLOID:
407 0 : cols[colno].param_type = PDO_PARAM_BOOL;
408 0 : break;
409 :
410 : case OIDOID:
411 : /* did the user bind the column as a LOB ? */
412 3 : if (stmt->bound_columns && (
413 : SUCCESS == zend_hash_index_find(stmt->bound_columns,
414 : colno, (void**)¶m) ||
415 : SUCCESS == zend_hash_find(stmt->bound_columns,
416 : cols[colno].name, cols[colno].namelen,
417 : (void**)¶m))) {
418 1 : if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
419 1 : cols[colno].param_type = PDO_PARAM_LOB;
420 1 : break;
421 : }
422 : }
423 2 : cols[colno].param_type = PDO_PARAM_INT;
424 2 : break;
425 :
426 : case INT2OID:
427 : case INT4OID:
428 51 : cols[colno].param_type = PDO_PARAM_INT;
429 51 : break;
430 :
431 : case INT8OID:
432 : if (sizeof(long)>=8) {
433 : cols[colno].param_type = PDO_PARAM_INT;
434 : } else {
435 12 : cols[colno].param_type = PDO_PARAM_STR;
436 : }
437 12 : break;
438 :
439 : case BYTEAOID:
440 2 : cols[colno].param_type = PDO_PARAM_LOB;
441 2 : break;
442 :
443 : default:
444 105 : cols[colno].param_type = PDO_PARAM_STR;
445 : }
446 :
447 173 : return 1;
448 : }
449 :
450 : /* PQunescapeBytea() from PostgreSQL 7.3 to provide bytea unescape feature to 7.2 users.
451 : Renamed to php_pdo_pgsql_unescape_bytea() */
452 : /*
453 : * PQunescapeBytea - converts the null terminated string representation
454 : * of a bytea, strtext, into binary, filling a buffer. It returns a
455 : * pointer to the buffer which is NULL on error, and the size of the
456 : * buffer in retbuflen. The pointer may subsequently be used as an
457 : * argument to the function free(3). It is the reverse of PQescapeBytea.
458 : *
459 : * The following transformations are reversed:
460 : * '\0' == ASCII 0 == \000
461 : * '\'' == ASCII 39 == \'
462 : * '\\' == ASCII 92 == \\
463 : *
464 : * States:
465 : * 0 normal 0->1->2->3->4
466 : * 1 \ 1->5
467 : * 2 \0 1->6
468 : * 3 \00
469 : * 4 \000
470 : * 5 \'
471 : * 6 \\
472 : */
473 : static unsigned char *php_pdo_pgsql_unescape_bytea(unsigned char *strtext, size_t *retbuflen)
474 6 : {
475 : size_t buflen;
476 : unsigned char *buffer,
477 : *sp,
478 : *bp;
479 6 : unsigned int state = 0;
480 :
481 6 : if (strtext == NULL)
482 0 : return NULL;
483 6 : buflen = strlen(strtext); /* will shrink, also we discover if
484 : * strtext */
485 6 : buffer = (unsigned char *) emalloc(buflen); /* isn't NULL terminated */
486 12 : for (bp = buffer, sp = strtext; *sp != '\0'; bp++, sp++)
487 : {
488 6 : switch (state)
489 : {
490 : case 0:
491 6 : if (*sp == '\\')
492 0 : state = 1;
493 6 : *bp = *sp;
494 6 : break;
495 : case 1:
496 0 : if (*sp == '\'') /* state=5 */
497 : { /* replace \' with 39 */
498 0 : bp--;
499 0 : *bp = '\'';
500 0 : buflen--;
501 0 : state = 0;
502 : }
503 0 : else if (*sp == '\\') /* state=6 */
504 : { /* replace \\ with 92 */
505 0 : bp--;
506 0 : *bp = '\\';
507 0 : buflen--;
508 0 : state = 0;
509 : }
510 : else
511 : {
512 0 : if (isdigit(*sp))
513 0 : state = 2;
514 : else
515 0 : state = 0;
516 0 : *bp = *sp;
517 : }
518 0 : break;
519 : case 2:
520 0 : if (isdigit(*sp))
521 0 : state = 3;
522 : else
523 0 : state = 0;
524 0 : *bp = *sp;
525 0 : break;
526 : case 3:
527 0 : if (isdigit(*sp)) /* state=4 */
528 : {
529 : unsigned char *start, *end, buf[4]; /* 000 + '\0' */
530 :
531 0 : bp -= 3;
532 0 : memcpy(buf, sp-2, 3);
533 0 : buf[3] = '\0';
534 0 : start = buf;
535 0 : *bp = (unsigned char)strtoul(start, (char **)&end, 8);
536 0 : buflen -= 3;
537 0 : state = 0;
538 : }
539 : else
540 : {
541 0 : *bp = *sp;
542 0 : state = 0;
543 : }
544 : break;
545 : }
546 : }
547 6 : buffer = erealloc(buffer, buflen+1);
548 6 : buffer[buflen] = '\0';
549 :
550 6 : *retbuflen = buflen;
551 6 : return buffer;
552 : }
553 :
554 : static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees TSRMLS_DC)
555 536 : {
556 536 : pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
557 536 : struct pdo_column_data *cols = stmt->columns;
558 : size_t tmp_len;
559 :
560 536 : if (!S->result) {
561 0 : return 0;
562 : }
563 :
564 : /* We have already increased count by 1 in pgsql_stmt_fetch() */
565 536 : if (PQgetisnull(S->result, S->current_row - 1, colno)) { /* Check if we got NULL */
566 9 : *ptr = NULL;
567 9 : *len = 0;
568 : } else {
569 527 : *ptr = PQgetvalue(S->result, S->current_row - 1, colno);
570 527 : *len = PQgetlength(S->result, S->current_row - 1, colno);
571 :
572 527 : switch(cols[colno].param_type) {
573 :
574 : case PDO_PARAM_INT:
575 191 : S->cols[colno].intval = atol(*ptr);
576 191 : *ptr = (char *) &(S->cols[colno].intval);
577 191 : *len = sizeof(long);
578 191 : break;
579 :
580 : case PDO_PARAM_BOOL:
581 0 : S->cols[colno].boolval = **ptr == 't' ? 1: 0;
582 0 : *ptr = (char *) &(S->cols[colno].boolval);
583 0 : *len = sizeof(zend_bool);
584 0 : break;
585 :
586 : case PDO_PARAM_LOB:
587 8 : if (S->cols[colno].pgsql_type == OIDOID) {
588 : /* ooo, a real large object */
589 : char *end_ptr;
590 2 : Oid oid = (Oid)strtoul(*ptr, &end_ptr, 10);
591 2 : int loid = lo_open(S->H->server, oid, INV_READ);
592 2 : if (loid >= 0) {
593 2 : *ptr = (char*)pdo_pgsql_create_lob_stream(stmt->dbh, loid, oid TSRMLS_CC);
594 2 : *len = 0;
595 2 : return *ptr ? 1 : 0;
596 : }
597 0 : *ptr = NULL;
598 0 : *len = 0;
599 0 : return 0;
600 : } else {
601 6 : *ptr = php_pdo_pgsql_unescape_bytea(*ptr, &tmp_len);
602 6 : if (!tmp_len) {
603 : /* Empty string, return as empty stream */
604 4 : *ptr = (char *)php_stream_memory_open(TEMP_STREAM_READONLY, "", 0);
605 4 : *len = 0;
606 : } else {
607 2 : *len = tmp_len;
608 2 : *caller_frees = 1;
609 : }
610 : }
611 : break;
612 : case PDO_PARAM_NULL:
613 : case PDO_PARAM_STR:
614 : case PDO_PARAM_STMT:
615 : case PDO_PARAM_INPUT_OUTPUT:
616 : default:
617 : break;
618 : }
619 : }
620 :
621 534 : return 1;
622 : }
623 :
624 : static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC)
625 0 : {
626 0 : pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
627 : PGresult *res;
628 0 : char *q=NULL;
629 : ExecStatusType status;
630 :
631 0 : if (!S->result) {
632 0 : return FAILURE;
633 : }
634 :
635 0 : if (colno >= stmt->column_count) {
636 0 : return FAILURE;
637 : }
638 :
639 0 : array_init(return_value);
640 0 : add_assoc_long(return_value, "pgsql:oid", S->cols[colno].pgsql_type);
641 :
642 : /* Fetch metadata from Postgres system catalogue */
643 0 : spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%d", S->cols[colno].pgsql_type);
644 0 : res = PQexec(S->H->server, q);
645 0 : efree(q);
646 :
647 0 : status = PQresultStatus(res);
648 :
649 0 : if (status != PGRES_TUPLES_OK) {
650 : /* Failed to get system catalogue, but return success
651 : * with the data we have collected so far
652 : */
653 0 : PQclear(res);
654 0 : return 1;
655 : }
656 :
657 : /* We want exactly one row returned */
658 0 : if (1 != PQntuples(res)) {
659 0 : PQclear(res);
660 0 : return 1;
661 : }
662 :
663 0 : add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0), 1);
664 :
665 0 : PQclear(res);
666 0 : return 1;
667 : }
668 :
669 : static int pdo_pgsql_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC)
670 28 : {
671 28 : return 1;
672 : }
673 :
674 : struct pdo_stmt_methods pgsql_stmt_methods = {
675 : pgsql_stmt_dtor,
676 : pgsql_stmt_execute,
677 : pgsql_stmt_fetch,
678 : pgsql_stmt_describe,
679 : pgsql_stmt_get_col,
680 : pgsql_stmt_param_hook,
681 : NULL, /* set_attr */
682 : NULL, /* get_attr */
683 : pgsql_stmt_get_column_meta,
684 : NULL, /* next_rowset */
685 : pdo_pgsql_stmt_cursor_closer
686 : };
687 :
688 : /*
689 : * Local variables:
690 : * tab-width: 4
691 : * c-basic-offset: 4
692 : * End:
693 : * vim600: noet sw=4 ts=4 fdm=marker
694 : * vim<600: noet sw=4 ts=4
695 : */
|