1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 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$ */
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 BOOLLABEL "bool"
40 #define BOOLOID 16
41 #define BYTEALABEL "bytea"
42 #define BYTEAOID 17
43 #define DATELABEL "date"
44 #define DATEOID 1082
45 #define INT2LABEL "int2"
46 #define INT2OID 21
47 #define INT4LABEL "int4"
48 #define INT4OID 23
49 #define INT8LABEL "int8"
50 #define INT8OID 20
51 #define OIDOID 26
52 #define TEXTLABEL "text"
53 #define TEXTOID 25
54 #define TIMESTAMPLABEL "timestamp"
55 #define TIMESTAMPOID 1114
56 #define VARCHARLABEL "varchar"
57 #define VARCHAROID 1043
58
59
60
pgsql_stmt_dtor(pdo_stmt_t * stmt)61 static int pgsql_stmt_dtor(pdo_stmt_t *stmt)
62 {
63 pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
64 zend_bool server_obj_usable = !Z_ISUNDEF(stmt->database_object_handle)
65 && IS_OBJ_VALID(EG(objects_store).object_buckets[Z_OBJ_HANDLE(stmt->database_object_handle)])
66 && !(GC_FLAGS(Z_OBJ(stmt->database_object_handle)) & IS_OBJ_FREE_CALLED);
67
68 if (S->result) {
69 /* free the resource */
70 PQclear(S->result);
71 S->result = NULL;
72 }
73
74 if (S->stmt_name) {
75 if (S->is_prepared && server_obj_usable) {
76 pdo_pgsql_db_handle *H = S->H;
77 char *q = NULL;
78 PGresult *res;
79
80 spprintf(&q, 0, "DEALLOCATE %s", S->stmt_name);
81 res = PQexec(H->server, q);
82 efree(q);
83 if (res) {
84 PQclear(res);
85 }
86 }
87 efree(S->stmt_name);
88 S->stmt_name = NULL;
89 }
90 if (S->param_lengths) {
91 efree(S->param_lengths);
92 S->param_lengths = NULL;
93 }
94 if (S->param_values) {
95 efree(S->param_values);
96 S->param_values = NULL;
97 }
98 if (S->param_formats) {
99 efree(S->param_formats);
100 S->param_formats = NULL;
101 }
102 if (S->param_types) {
103 efree(S->param_types);
104 S->param_types = NULL;
105 }
106 if (S->query) {
107 efree(S->query);
108 S->query = NULL;
109 }
110
111 if (S->cursor_name) {
112 if (server_obj_usable) {
113 pdo_pgsql_db_handle *H = S->H;
114 char *q = NULL;
115 PGresult *res;
116
117 spprintf(&q, 0, "CLOSE %s", S->cursor_name);
118 res = PQexec(H->server, q);
119 efree(q);
120 if (res) PQclear(res);
121 }
122 efree(S->cursor_name);
123 S->cursor_name = NULL;
124 }
125
126 if(S->cols) {
127 efree(S->cols);
128 S->cols = NULL;
129 }
130 efree(S);
131 stmt->driver_data = NULL;
132 return 1;
133 }
134
pgsql_stmt_execute(pdo_stmt_t * stmt)135 static int pgsql_stmt_execute(pdo_stmt_t *stmt)
136 {
137 pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
138 pdo_pgsql_db_handle *H = S->H;
139 ExecStatusType status;
140
141 /* ensure that we free any previous unfetched results */
142 if(S->result) {
143 PQclear(S->result);
144 S->result = NULL;
145 }
146
147 S->current_row = 0;
148
149 if (S->cursor_name) {
150 char *q = NULL;
151
152 if (S->is_prepared) {
153 spprintf(&q, 0, "CLOSE %s", S->cursor_name);
154 S->result = PQexec(H->server, q);
155 efree(q);
156 }
157
158 spprintf(&q, 0, "DECLARE %s SCROLL CURSOR WITH HOLD FOR %s", S->cursor_name, stmt->active_query_string);
159 S->result = PQexec(H->server, q);
160 efree(q);
161
162 /* check if declare failed */
163 status = PQresultStatus(S->result);
164 if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
165 pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
166 return 0;
167 }
168 PQclear(S->result);
169
170 /* the cursor was declared correctly */
171 S->is_prepared = 1;
172
173 /* fetch to be able to get the number of tuples later, but don't advance the cursor pointer */
174 spprintf(&q, 0, "FETCH FORWARD 0 FROM %s", S->cursor_name);
175 S->result = PQexec(H->server, q);
176 efree(q);
177 } else if (S->stmt_name) {
178 /* using a prepared statement */
179
180 if (!S->is_prepared) {
181 stmt_retry:
182 /* we deferred the prepare until now, because we didn't
183 * know anything about the parameter types; now we do */
184 S->result = PQprepare(H->server, S->stmt_name, S->query,
185 stmt->bound_params ? zend_hash_num_elements(stmt->bound_params) : 0,
186 S->param_types);
187 status = PQresultStatus(S->result);
188 switch (status) {
189 case PGRES_COMMAND_OK:
190 case PGRES_TUPLES_OK:
191 /* it worked */
192 S->is_prepared = 1;
193 PQclear(S->result);
194 break;
195 default: {
196 char *sqlstate = pdo_pgsql_sqlstate(S->result);
197 /* 42P05 means that the prepared statement already existed. this can happen if you use
198 * a connection pooling software line pgpool which doesn't close the db-connection once
199 * php disconnects. if php dies (no chance to run RSHUTDOWN) during execution it has no
200 * chance to DEALLOCATE the prepared statements it has created. so, if we hit a 42P05 we
201 * deallocate it and retry ONCE (thies 2005.12.15)
202 */
203 if (sqlstate && !strcmp(sqlstate, "42P05")) {
204 char buf[100]; /* stmt_name == "pdo_crsr_%08x" */
205 PGresult *res;
206 snprintf(buf, sizeof(buf), "DEALLOCATE %s", S->stmt_name);
207 res = PQexec(H->server, buf);
208 if (res) {
209 PQclear(res);
210 }
211 goto stmt_retry;
212 } else {
213 pdo_pgsql_error_stmt(stmt, status, sqlstate);
214 return 0;
215 }
216 }
217 }
218 }
219 S->result = PQexecPrepared(H->server, S->stmt_name,
220 stmt->bound_params ?
221 zend_hash_num_elements(stmt->bound_params) :
222 0,
223 (const char**)S->param_values,
224 S->param_lengths,
225 S->param_formats,
226 0);
227 } else if (stmt->supports_placeholders == PDO_PLACEHOLDER_NAMED) {
228 /* execute query with parameters */
229 S->result = PQexecParams(H->server, S->query,
230 stmt->bound_params ? zend_hash_num_elements(stmt->bound_params) : 0,
231 S->param_types,
232 (const char**)S->param_values,
233 S->param_lengths,
234 S->param_formats,
235 0);
236 } else {
237 /* execute plain query (with embedded parameters) */
238 S->result = PQexec(H->server, stmt->active_query_string);
239 }
240 status = PQresultStatus(S->result);
241
242 if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
243 pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
244 return 0;
245 }
246
247 if (!stmt->executed && (!stmt->column_count || S->cols == NULL)) {
248 stmt->column_count = (int) PQnfields(S->result);
249 S->cols = ecalloc(stmt->column_count, sizeof(pdo_pgsql_column));
250 }
251
252 if (status == PGRES_COMMAND_OK) {
253 ZEND_ATOL(stmt->row_count, PQcmdTuples(S->result));
254 H->pgoid = PQoidValue(S->result);
255 } else {
256 stmt->row_count = (zend_long)PQntuples(S->result);
257 }
258
259 return 1;
260 }
261
pgsql_stmt_param_hook(pdo_stmt_t * stmt,struct pdo_bound_param_data * param,enum pdo_param_event event_type)262 static int pgsql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
263 enum pdo_param_event event_type)
264 {
265 pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
266
267 if (stmt->supports_placeholders == PDO_PLACEHOLDER_NAMED && param->is_param) {
268 switch (event_type) {
269 case PDO_PARAM_EVT_FREE:
270 if (param->driver_data) {
271 efree(param->driver_data);
272 }
273 break;
274
275 case PDO_PARAM_EVT_NORMALIZE:
276 /* decode name from $1, $2 into 0, 1 etc. */
277 if (param->name) {
278 if (ZSTR_VAL(param->name)[0] == '$') {
279 ZEND_ATOL(param->paramno, ZSTR_VAL(param->name) + 1);
280 } else {
281 /* resolve parameter name to rewritten name */
282 char *namevar;
283
284 if (stmt->bound_param_map && (namevar = zend_hash_find_ptr(stmt->bound_param_map,
285 param->name)) != NULL) {
286 ZEND_ATOL(param->paramno, namevar + 1);
287 param->paramno--;
288 } else {
289 pdo_raise_impl_error(stmt->dbh, stmt, "HY093", ZSTR_VAL(param->name));
290 return 0;
291 }
292 }
293 }
294 break;
295
296 case PDO_PARAM_EVT_ALLOC:
297 if (!stmt->bound_param_map) {
298 return 1;
299 }
300 if (!zend_hash_index_exists(stmt->bound_param_map, param->paramno)) {
301 pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");
302 return 0;
303 }
304 case PDO_PARAM_EVT_EXEC_POST:
305 case PDO_PARAM_EVT_FETCH_PRE:
306 case PDO_PARAM_EVT_FETCH_POST:
307 /* work is handled by EVT_NORMALIZE */
308 return 1;
309
310 case PDO_PARAM_EVT_EXEC_PRE:
311 if (!stmt->bound_param_map) {
312 return 1;
313 }
314 if (!S->param_values) {
315 S->param_values = ecalloc(
316 zend_hash_num_elements(stmt->bound_param_map),
317 sizeof(char*));
318 S->param_lengths = ecalloc(
319 zend_hash_num_elements(stmt->bound_param_map),
320 sizeof(int));
321 S->param_formats = ecalloc(
322 zend_hash_num_elements(stmt->bound_param_map),
323 sizeof(int));
324 S->param_types = ecalloc(
325 zend_hash_num_elements(stmt->bound_param_map),
326 sizeof(Oid));
327 }
328 if (param->paramno >= 0) {
329 zval *parameter;
330
331 /*
332 if (param->paramno >= zend_hash_num_elements(stmt->bound_params)) {
333 pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");
334 return 0;
335 }
336 */
337
338 if (Z_ISREF(param->parameter)) {
339 parameter = Z_REFVAL(param->parameter);
340 } else {
341 parameter = ¶m->parameter;
342 }
343
344 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB &&
345 Z_TYPE_P(parameter) == IS_RESOURCE) {
346 php_stream *stm;
347 php_stream_from_zval_no_verify(stm, parameter);
348 if (stm) {
349 if (php_stream_is(stm, &pdo_pgsql_lob_stream_ops)) {
350 struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stm->abstract;
351 pdo_pgsql_bound_param *P = param->driver_data;
352
353 if (P == NULL) {
354 P = ecalloc(1, sizeof(*P));
355 param->driver_data = P;
356 }
357 P->oid = htonl(self->oid);
358 S->param_values[param->paramno] = (char*)&P->oid;
359 S->param_lengths[param->paramno] = sizeof(P->oid);
360 S->param_formats[param->paramno] = 1;
361 S->param_types[param->paramno] = OIDOID;
362 return 1;
363 } else {
364 zend_string *str = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
365 if (str != NULL) {
366 //??SEPARATE_ZVAL_IF_NOT_REF(¶m->parameter);
367 ZVAL_STR(parameter, str);
368 } else {
369 ZVAL_EMPTY_STRING(parameter);
370 }
371 }
372 } else {
373 /* expected a stream resource */
374 pdo_pgsql_error_stmt(stmt, PGRES_FATAL_ERROR, "HY105");
375 return 0;
376 }
377 }
378
379 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL ||
380 Z_TYPE_P(parameter) == IS_NULL) {
381 S->param_values[param->paramno] = NULL;
382 S->param_lengths[param->paramno] = 0;
383 } else if (Z_TYPE_P(parameter) == IS_FALSE || Z_TYPE_P(parameter) == IS_TRUE) {
384 S->param_values[param->paramno] = Z_TYPE_P(parameter) == IS_TRUE ? "t" : "f";
385 S->param_lengths[param->paramno] = 1;
386 S->param_formats[param->paramno] = 0;
387 } else {
388 //SEPARATE_ZVAL_IF_NOT_REF(¶m->parameter);
389 convert_to_string_ex(parameter);
390 S->param_values[param->paramno] = Z_STRVAL_P(parameter);
391 S->param_lengths[param->paramno] = Z_STRLEN_P(parameter);
392 S->param_formats[param->paramno] = 0;
393 }
394
395 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
396 S->param_types[param->paramno] = 0;
397 S->param_formats[param->paramno] = 1;
398 } else {
399 S->param_types[param->paramno] = 0;
400 }
401 }
402 break;
403 }
404 } else if (param->is_param) {
405 /* We need to manually convert to a pg native boolean value */
406 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_BOOL &&
407 ((param->param_type & PDO_PARAM_INPUT_OUTPUT) != PDO_PARAM_INPUT_OUTPUT)) {
408 const char *s = zend_is_true(¶m->parameter) ? "t" : "f";
409 param->param_type = PDO_PARAM_STR;
410 zval_ptr_dtor(¶m->parameter);
411 ZVAL_STRINGL(¶m->parameter, s, 1);
412 }
413 }
414 return 1;
415 }
416
pgsql_stmt_fetch(pdo_stmt_t * stmt,enum pdo_fetch_orientation ori,zend_long offset)417 static int pgsql_stmt_fetch(pdo_stmt_t *stmt,
418 enum pdo_fetch_orientation ori, zend_long offset)
419 {
420 pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
421
422 if (S->cursor_name) {
423 char *ori_str = NULL;
424 char *q = NULL;
425 ExecStatusType status;
426
427 switch (ori) {
428 case PDO_FETCH_ORI_NEXT: spprintf(&ori_str, 0, "NEXT"); break;
429 case PDO_FETCH_ORI_PRIOR: spprintf(&ori_str, 0, "BACKWARD"); break;
430 case PDO_FETCH_ORI_FIRST: spprintf(&ori_str, 0, "FIRST"); break;
431 case PDO_FETCH_ORI_LAST: spprintf(&ori_str, 0, "LAST"); break;
432 case PDO_FETCH_ORI_ABS: spprintf(&ori_str, 0, "ABSOLUTE " ZEND_LONG_FMT, offset); break;
433 case PDO_FETCH_ORI_REL: spprintf(&ori_str, 0, "RELATIVE " ZEND_LONG_FMT, offset); break;
434 default:
435 return 0;
436 }
437
438 if(S->result) {
439 PQclear(S->result);
440 S->result = NULL;
441 }
442
443 spprintf(&q, 0, "FETCH %s FROM %s", ori_str, S->cursor_name);
444 efree(ori_str);
445 S->result = PQexec(S->H->server, q);
446 efree(q);
447 status = PQresultStatus(S->result);
448
449 if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
450 pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
451 return 0;
452 }
453
454 if (PQntuples(S->result)) {
455 S->current_row = 1;
456 return 1;
457 } else {
458 return 0;
459 }
460 } else {
461 if (S->current_row < stmt->row_count) {
462 S->current_row++;
463 return 1;
464 } else {
465 return 0;
466 }
467 }
468 }
469
pgsql_stmt_describe(pdo_stmt_t * stmt,int colno)470 static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno)
471 {
472 pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
473 struct pdo_column_data *cols = stmt->columns;
474 struct pdo_bound_param_data *param;
475 char *str;
476
477 if (!S->result) {
478 return 0;
479 }
480
481 str = PQfname(S->result, colno);
482 cols[colno].name = zend_string_init(str, strlen(str), 0);
483 cols[colno].maxlen = PQfsize(S->result, colno);
484 cols[colno].precision = PQfmod(S->result, colno);
485 S->cols[colno].pgsql_type = PQftype(S->result, colno);
486
487 switch (S->cols[colno].pgsql_type) {
488
489 case BOOLOID:
490 cols[colno].param_type = PDO_PARAM_BOOL;
491 break;
492
493 case OIDOID:
494 /* did the user bind the column as a LOB ? */
495 if (stmt->bound_columns && (
496 (param = zend_hash_index_find_ptr(stmt->bound_columns, colno)) != NULL ||
497 (param = zend_hash_find_ptr(stmt->bound_columns, cols[colno].name)) != NULL)) {
498
499 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
500 cols[colno].param_type = PDO_PARAM_LOB;
501 break;
502 }
503 }
504 cols[colno].param_type = PDO_PARAM_INT;
505 break;
506
507 case INT2OID:
508 case INT4OID:
509 cols[colno].param_type = PDO_PARAM_INT;
510 break;
511
512 case INT8OID:
513 if (sizeof(zend_long)>=8) {
514 cols[colno].param_type = PDO_PARAM_INT;
515 } else {
516 cols[colno].param_type = PDO_PARAM_STR;
517 }
518 break;
519
520 case BYTEAOID:
521 cols[colno].param_type = PDO_PARAM_LOB;
522 break;
523
524 default:
525 cols[colno].param_type = PDO_PARAM_STR;
526 }
527
528 return 1;
529 }
530
pgsql_stmt_get_col(pdo_stmt_t * stmt,int colno,char ** ptr,zend_ulong * len,int * caller_frees)531 static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, zend_ulong *len, int *caller_frees )
532 {
533 pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
534 struct pdo_column_data *cols = stmt->columns;
535 size_t tmp_len;
536
537 if (!S->result) {
538 return 0;
539 }
540
541 /* We have already increased count by 1 in pgsql_stmt_fetch() */
542 if (PQgetisnull(S->result, S->current_row - 1, colno)) { /* Check if we got NULL */
543 *ptr = NULL;
544 *len = 0;
545 } else {
546 *ptr = PQgetvalue(S->result, S->current_row - 1, colno);
547 *len = PQgetlength(S->result, S->current_row - 1, colno);
548
549 switch (cols[colno].param_type) {
550
551 case PDO_PARAM_INT:
552 ZEND_ATOL(S->cols[colno].intval, *ptr);
553 *ptr = (char *) &(S->cols[colno].intval);
554 *len = sizeof(zend_long);
555 break;
556
557 case PDO_PARAM_BOOL:
558 S->cols[colno].boolval = **ptr == 't' ? 1: 0;
559 *ptr = (char *) &(S->cols[colno].boolval);
560 *len = sizeof(zend_bool);
561 break;
562
563 case PDO_PARAM_LOB:
564 if (S->cols[colno].pgsql_type == OIDOID) {
565 /* ooo, a real large object */
566 char *end_ptr;
567 Oid oid = (Oid)strtoul(*ptr, &end_ptr, 10);
568 int loid = lo_open(S->H->server, oid, INV_READ);
569 if (loid >= 0) {
570 *ptr = (char*)pdo_pgsql_create_lob_stream(&stmt->database_object_handle, loid, oid);
571 *len = 0;
572 return *ptr ? 1 : 0;
573 }
574 *ptr = NULL;
575 *len = 0;
576 return 0;
577 } else {
578 char *tmp_ptr = (char *)PQunescapeBytea((unsigned char *)*ptr, &tmp_len);
579 if (!tmp_ptr) {
580 /* PQunescapeBytea returned an error */
581 *len = 0;
582 return 0;
583 }
584 if (!tmp_len) {
585 /* Empty string, return as empty stream */
586 *ptr = (char *)php_stream_memory_open(TEMP_STREAM_READONLY, "", 0);
587 PQfreemem(tmp_ptr);
588 *len = 0;
589 } else {
590 *ptr = estrndup(tmp_ptr, tmp_len);
591 PQfreemem(tmp_ptr);
592 *len = tmp_len;
593 *caller_frees = 1;
594 }
595 }
596 break;
597 case PDO_PARAM_NULL:
598 case PDO_PARAM_STR:
599 case PDO_PARAM_STMT:
600 case PDO_PARAM_INPUT_OUTPUT:
601 case PDO_PARAM_ZVAL:
602 default:
603 break;
604 }
605 }
606
607 return 1;
608 }
609
pdo_pgsql_translate_oid_to_table(Oid oid,PGconn * conn)610 static zend_always_inline char * pdo_pgsql_translate_oid_to_table(Oid oid, PGconn *conn)
611 {
612 char *table_name = NULL;
613 PGresult *tmp_res;
614 char *querystr = NULL;
615
616 spprintf(&querystr, 0, "SELECT RELNAME FROM PG_CLASS WHERE OID=%d", oid);
617
618 if ((tmp_res = PQexec(conn, querystr)) == NULL || PQresultStatus(tmp_res) != PGRES_TUPLES_OK) {
619 if (tmp_res) {
620 PQclear(tmp_res);
621 }
622 efree(querystr);
623 return 0;
624 }
625 efree(querystr);
626
627 if (1 == PQgetisnull(tmp_res, 0, 0) || (table_name = PQgetvalue(tmp_res, 0, 0)) == NULL) {
628 PQclear(tmp_res);
629 return 0;
630 }
631
632 table_name = estrdup(table_name);
633
634 PQclear(tmp_res);
635 return table_name;
636 }
637
pgsql_stmt_get_column_meta(pdo_stmt_t * stmt,zend_long colno,zval * return_value)638 static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value)
639 {
640 pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
641 PGresult *res;
642 char *q=NULL;
643 ExecStatusType status;
644 Oid table_oid;
645 char *table_name=NULL;
646
647 if (!S->result) {
648 return FAILURE;
649 }
650
651 if (colno >= stmt->column_count) {
652 return FAILURE;
653 }
654
655 array_init(return_value);
656 add_assoc_long(return_value, "pgsql:oid", S->cols[colno].pgsql_type);
657
658 table_oid = PQftable(S->result, colno);
659 add_assoc_long(return_value, "pgsql:table_oid", table_oid);
660 table_name = pdo_pgsql_translate_oid_to_table(table_oid, S->H->server);
661 if (table_name) {
662 add_assoc_string(return_value, "table", table_name);
663 efree(table_name);
664 }
665
666 switch (S->cols[colno].pgsql_type) {
667 case BOOLOID:
668 add_assoc_string(return_value, "native_type", BOOLLABEL);
669 break;
670 case BYTEAOID:
671 add_assoc_string(return_value, "native_type", BYTEALABEL);
672 break;
673 case INT8OID:
674 add_assoc_string(return_value, "native_type", INT8LABEL);
675 break;
676 case INT2OID:
677 add_assoc_string(return_value, "native_type", INT2LABEL);
678 break;
679 case INT4OID:
680 add_assoc_string(return_value, "native_type", INT4LABEL);
681 break;
682 case TEXTOID:
683 add_assoc_string(return_value, "native_type", TEXTLABEL);
684 break;
685 case VARCHAROID:
686 add_assoc_string(return_value, "native_type", VARCHARLABEL);
687 break;
688 case DATEOID:
689 add_assoc_string(return_value, "native_type", DATELABEL);
690 break;
691 case TIMESTAMPOID:
692 add_assoc_string(return_value, "native_type", TIMESTAMPLABEL);
693 break;
694 default:
695 /* Fetch metadata from Postgres system catalogue */
696 spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%u", S->cols[colno].pgsql_type);
697 res = PQexec(S->H->server, q);
698 efree(q);
699 status = PQresultStatus(res);
700 if (status == PGRES_TUPLES_OK && 1 == PQntuples(res)) {
701 add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0));
702 }
703 PQclear(res);
704 }
705 return 1;
706 }
707
pdo_pgsql_stmt_cursor_closer(pdo_stmt_t * stmt)708 static int pdo_pgsql_stmt_cursor_closer(pdo_stmt_t *stmt)
709 {
710 pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
711
712 if (S->cols != NULL){
713 efree(S->cols);
714 S->cols = NULL;
715 }
716 return 1;
717 }
718
719 struct pdo_stmt_methods pgsql_stmt_methods = {
720 pgsql_stmt_dtor,
721 pgsql_stmt_execute,
722 pgsql_stmt_fetch,
723 pgsql_stmt_describe,
724 pgsql_stmt_get_col,
725 pgsql_stmt_param_hook,
726 NULL, /* set_attr */
727 NULL, /* get_attr */
728 pgsql_stmt_get_column_meta,
729 NULL, /* next_rowset */
730 pdo_pgsql_stmt_cursor_closer
731 };
732
733 /*
734 * Local variables:
735 * tab-width: 4
736 * c-basic-offset: 4
737 * End:
738 * vim600: noet sw=4 ts=4 fdm=marker
739 * vim<600: noet sw=4 ts=4
740 */
741