1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Edin Kadribasic <edink@emini.dk> |
14 | Ilia Alshanestsky <ilia@prohost.org> |
15 | Wez Furlong <wez@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "php.h"
24 #include "php_ini.h"
25 #include "ext/standard/info.h"
26 #include "pdo/php_pdo.h"
27 #include "pdo/php_pdo_driver.h"
28 #include "php_pdo_pgsql.h"
29 #include "php_pdo_pgsql_int.h"
30 #ifdef HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33
34 /* from postgresql/src/include/catalog/pg_type.h */
35 #define BOOLLABEL "bool"
36 #define BOOLOID 16
37 #define BYTEALABEL "bytea"
38 #define BYTEAOID 17
39 #define DATELABEL "date"
40 #define DATEOID 1082
41 #define INT2LABEL "int2"
42 #define INT2OID 21
43 #define INT4LABEL "int4"
44 #define INT4OID 23
45 #define INT8LABEL "int8"
46 #define INT8OID 20
47 #define OIDOID 26
48 #define TEXTLABEL "text"
49 #define TEXTOID 25
50 #define TIMESTAMPLABEL "timestamp"
51 #define TIMESTAMPOID 1114
52 #define VARCHARLABEL "varchar"
53 #define VARCHAROID 1043
54 #define FLOAT4LABEL "float4"
55 #define FLOAT4OID 700
56 #define FLOAT8LABEL "float8"
57 #define FLOAT8OID 701
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 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 && !(OBJ_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 zend_string_release(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 bool in_trans = stmt->dbh->methods->in_transaction(stmt->dbh);
142
143 /* ensure that we free any previous unfetched results */
144 if(S->result) {
145 PQclear(S->result);
146 S->result = NULL;
147 }
148
149 S->current_row = 0;
150
151 if (S->cursor_name) {
152 char *q = NULL;
153
154 if (S->is_prepared) {
155 spprintf(&q, 0, "CLOSE %s", S->cursor_name);
156 PQclear(PQexec(H->server, q));
157 efree(q);
158 }
159
160 spprintf(&q, 0, "DECLARE %s SCROLL CURSOR WITH HOLD FOR %s", S->cursor_name, ZSTR_VAL(stmt->active_query_string));
161 S->result = PQexec(H->server, q);
162 efree(q);
163
164 /* check if declare failed */
165 status = PQresultStatus(S->result);
166 if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
167 pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
168 return 0;
169 }
170 PQclear(S->result);
171
172 /* the cursor was declared correctly */
173 S->is_prepared = 1;
174
175 /* fetch to be able to get the number of tuples later, but don't advance the cursor pointer */
176 spprintf(&q, 0, "FETCH FORWARD 0 FROM %s", S->cursor_name);
177 S->result = PQexec(H->server, q);
178 efree(q);
179 } else if (S->stmt_name) {
180 /* using a prepared statement */
181
182 if (!S->is_prepared) {
183 stmt_retry:
184 /* we deferred the prepare until now, because we didn't
185 * know anything about the parameter types; now we do */
186 S->result = PQprepare(H->server, S->stmt_name, ZSTR_VAL(S->query),
187 stmt->bound_params ? zend_hash_num_elements(stmt->bound_params) : 0,
188 S->param_types);
189 status = PQresultStatus(S->result);
190 switch (status) {
191 case PGRES_COMMAND_OK:
192 case PGRES_TUPLES_OK:
193 /* it worked */
194 S->is_prepared = 1;
195 PQclear(S->result);
196 break;
197 default: {
198 char *sqlstate = pdo_pgsql_sqlstate(S->result);
199 /* 42P05 means that the prepared statement already existed. this can happen if you use
200 * a connection pooling software line pgpool which doesn't close the db-connection once
201 * php disconnects. if php dies (no chance to run RSHUTDOWN) during execution it has no
202 * chance to DEALLOCATE the prepared statements it has created. so, if we hit a 42P05 we
203 * deallocate it and retry ONCE (thies 2005.12.15)
204 */
205 if (sqlstate && !strcmp(sqlstate, "42P05")) {
206 char buf[100]; /* stmt_name == "pdo_crsr_%08x" */
207 PGresult *res;
208 snprintf(buf, sizeof(buf), "DEALLOCATE %s", S->stmt_name);
209 res = PQexec(H->server, buf);
210 if (res) {
211 PQclear(res);
212 }
213 goto stmt_retry;
214 } else {
215 pdo_pgsql_error_stmt(stmt, status, sqlstate);
216 return 0;
217 }
218 }
219 }
220 }
221 S->result = PQexecPrepared(H->server, S->stmt_name,
222 stmt->bound_params ?
223 zend_hash_num_elements(stmt->bound_params) :
224 0,
225 (const char**)S->param_values,
226 S->param_lengths,
227 S->param_formats,
228 0);
229 } else if (stmt->supports_placeholders == PDO_PLACEHOLDER_NAMED) {
230 /* execute query with parameters */
231 S->result = PQexecParams(H->server, ZSTR_VAL(S->query),
232 stmt->bound_params ? zend_hash_num_elements(stmt->bound_params) : 0,
233 S->param_types,
234 (const char**)S->param_values,
235 S->param_lengths,
236 S->param_formats,
237 0);
238 } else {
239 /* execute plain query (with embedded parameters) */
240 S->result = PQexec(H->server, ZSTR_VAL(stmt->active_query_string));
241 }
242 status = PQresultStatus(S->result);
243
244 if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
245 pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
246 return 0;
247 }
248
249 stmt->column_count = (int) PQnfields(S->result);
250 if (S->cols == NULL) {
251 S->cols = ecalloc(stmt->column_count, sizeof(pdo_pgsql_column));
252 }
253
254 if (status == PGRES_COMMAND_OK) {
255 stmt->row_count = ZEND_ATOL(PQcmdTuples(S->result));
256 H->pgoid = PQoidValue(S->result);
257 } else {
258 stmt->row_count = (zend_long)PQntuples(S->result);
259 }
260
261 if (in_trans && !stmt->dbh->methods->in_transaction(stmt->dbh)) {
262 pdo_pgsql_close_lob_streams(stmt->dbh);
263 }
264
265 return 1;
266 }
267
pgsql_stmt_param_hook(pdo_stmt_t * stmt,struct pdo_bound_param_data * param,enum pdo_param_event event_type)268 static int pgsql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
269 enum pdo_param_event event_type)
270 {
271 pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
272
273 if (stmt->supports_placeholders == PDO_PLACEHOLDER_NAMED && param->is_param) {
274 switch (event_type) {
275 case PDO_PARAM_EVT_FREE:
276 if (param->driver_data) {
277 efree(param->driver_data);
278 }
279 break;
280
281 case PDO_PARAM_EVT_NORMALIZE:
282 /* decode name from $1, $2 into 0, 1 etc. */
283 if (param->name) {
284 if (ZSTR_VAL(param->name)[0] == '$') {
285 param->paramno = ZEND_ATOL(ZSTR_VAL(param->name) + 1);
286 } else {
287 /* resolve parameter name to rewritten name */
288 zend_string *namevar;
289
290 if (stmt->bound_param_map && (namevar = zend_hash_find_ptr(stmt->bound_param_map,
291 param->name)) != NULL) {
292 param->paramno = ZEND_ATOL(ZSTR_VAL(namevar) + 1);
293 param->paramno--;
294 } else {
295 pdo_pgsql_error_stmt_msg(stmt, 0, "HY093", ZSTR_VAL(param->name));
296 return 0;
297 }
298 }
299 }
300 break;
301
302 case PDO_PARAM_EVT_ALLOC:
303 if (!stmt->bound_param_map) {
304 return 1;
305 }
306 if (!zend_hash_index_exists(stmt->bound_param_map, param->paramno)) {
307 pdo_pgsql_error_stmt_msg(stmt, 0, "HY093", "parameter was not defined");
308 return 0;
309 }
310 ZEND_FALLTHROUGH;
311 case PDO_PARAM_EVT_EXEC_POST:
312 case PDO_PARAM_EVT_FETCH_PRE:
313 case PDO_PARAM_EVT_FETCH_POST:
314 /* work is handled by EVT_NORMALIZE */
315 return 1;
316
317 case PDO_PARAM_EVT_EXEC_PRE:
318 if (!stmt->bound_param_map) {
319 return 1;
320 }
321 if (!S->param_values) {
322 S->param_values = ecalloc(
323 zend_hash_num_elements(stmt->bound_param_map),
324 sizeof(char*));
325 S->param_lengths = ecalloc(
326 zend_hash_num_elements(stmt->bound_param_map),
327 sizeof(int));
328 S->param_formats = ecalloc(
329 zend_hash_num_elements(stmt->bound_param_map),
330 sizeof(int));
331 S->param_types = ecalloc(
332 zend_hash_num_elements(stmt->bound_param_map),
333 sizeof(Oid));
334 }
335 if (param->paramno >= 0) {
336 zval *parameter;
337
338 /*
339 if (param->paramno >= zend_hash_num_elements(stmt->bound_params)) {
340 pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");
341 return 0;
342 }
343 */
344
345 if (Z_ISREF(param->parameter)) {
346 parameter = Z_REFVAL(param->parameter);
347 } else {
348 parameter = ¶m->parameter;
349 }
350
351 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB &&
352 Z_TYPE_P(parameter) == IS_RESOURCE) {
353 php_stream *stm;
354 php_stream_from_zval_no_verify(stm, parameter);
355 if (stm) {
356 if (php_stream_is(stm, &pdo_pgsql_lob_stream_ops)) {
357 struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stm->abstract;
358 pdo_pgsql_bound_param *P = param->driver_data;
359
360 if (P == NULL) {
361 P = ecalloc(1, sizeof(*P));
362 param->driver_data = P;
363 }
364 P->oid = htonl(self->oid);
365 S->param_values[param->paramno] = (char*)&P->oid;
366 S->param_lengths[param->paramno] = sizeof(P->oid);
367 S->param_formats[param->paramno] = 1;
368 S->param_types[param->paramno] = OIDOID;
369 return 1;
370 } else {
371 zend_string *str = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
372 if (str != NULL) {
373 ZVAL_STR(parameter, str);
374 } else {
375 ZVAL_EMPTY_STRING(parameter);
376 }
377 }
378 } else {
379 /* expected a stream resource */
380 pdo_pgsql_error_stmt(stmt, PGRES_FATAL_ERROR, "HY105");
381 return 0;
382 }
383 }
384
385 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL ||
386 Z_TYPE_P(parameter) == IS_NULL) {
387 S->param_values[param->paramno] = NULL;
388 S->param_lengths[param->paramno] = 0;
389 } else if (Z_TYPE_P(parameter) == IS_FALSE || Z_TYPE_P(parameter) == IS_TRUE) {
390 S->param_values[param->paramno] = Z_TYPE_P(parameter) == IS_TRUE ? "t" : "f";
391 S->param_lengths[param->paramno] = 1;
392 S->param_formats[param->paramno] = 0;
393 } else {
394 convert_to_string(parameter);
395 S->param_values[param->paramno] = Z_STRVAL_P(parameter);
396 S->param_lengths[param->paramno] = Z_STRLEN_P(parameter);
397 S->param_formats[param->paramno] = 0;
398 }
399
400 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
401 S->param_types[param->paramno] = 0;
402 S->param_formats[param->paramno] = 1;
403 } else {
404 S->param_types[param->paramno] = 0;
405 }
406 }
407 break;
408 }
409 } else if (param->is_param && event_type == PDO_PARAM_EVT_NORMALIZE) {
410 /* We need to manually convert to a pg native boolean value */
411 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_BOOL &&
412 ((param->param_type & PDO_PARAM_INPUT_OUTPUT) != PDO_PARAM_INPUT_OUTPUT)) {
413 const char *s = zend_is_true(¶m->parameter) ? "t" : "f";
414 param->param_type = PDO_PARAM_STR;
415 zval_ptr_dtor(¶m->parameter);
416 ZVAL_STRINGL(¶m->parameter, s, 1);
417 }
418 }
419 return 1;
420 }
421
pgsql_stmt_fetch(pdo_stmt_t * stmt,enum pdo_fetch_orientation ori,zend_long offset)422 static int pgsql_stmt_fetch(pdo_stmt_t *stmt,
423 enum pdo_fetch_orientation ori, zend_long offset)
424 {
425 pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
426
427 if (S->cursor_name) {
428 char *ori_str = NULL;
429 char *q = NULL;
430 ExecStatusType status;
431
432 switch (ori) {
433 case PDO_FETCH_ORI_NEXT: spprintf(&ori_str, 0, "NEXT"); break;
434 case PDO_FETCH_ORI_PRIOR: spprintf(&ori_str, 0, "BACKWARD"); break;
435 case PDO_FETCH_ORI_FIRST: spprintf(&ori_str, 0, "FIRST"); break;
436 case PDO_FETCH_ORI_LAST: spprintf(&ori_str, 0, "LAST"); break;
437 case PDO_FETCH_ORI_ABS: spprintf(&ori_str, 0, "ABSOLUTE " ZEND_LONG_FMT, offset); break;
438 case PDO_FETCH_ORI_REL: spprintf(&ori_str, 0, "RELATIVE " ZEND_LONG_FMT, offset); break;
439 default:
440 return 0;
441 }
442
443 if(S->result) {
444 PQclear(S->result);
445 S->result = NULL;
446 }
447
448 spprintf(&q, 0, "FETCH %s FROM %s", ori_str, S->cursor_name);
449 efree(ori_str);
450 S->result = PQexec(S->H->server, q);
451 efree(q);
452 status = PQresultStatus(S->result);
453
454 if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
455 pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
456 return 0;
457 }
458
459 if (PQntuples(S->result)) {
460 S->current_row = 1;
461 return 1;
462 } else {
463 return 0;
464 }
465 } else {
466 if (S->current_row < stmt->row_count) {
467 S->current_row++;
468 return 1;
469 } else {
470 return 0;
471 }
472 }
473 }
474
pgsql_stmt_describe(pdo_stmt_t * stmt,int colno)475 static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno)
476 {
477 pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
478 struct pdo_column_data *cols = stmt->columns;
479 char *str;
480
481 if (!S->result) {
482 return 0;
483 }
484
485 str = PQfname(S->result, colno);
486 cols[colno].name = zend_string_init(str, strlen(str), 0);
487 cols[colno].maxlen = PQfsize(S->result, colno);
488 cols[colno].precision = PQfmod(S->result, colno);
489 S->cols[colno].pgsql_type = PQftype(S->result, colno);
490
491 return 1;
492 }
493
pgsql_stmt_get_col(pdo_stmt_t * stmt,int colno,zval * result,enum pdo_param_type * type)494 static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, zval *result, enum pdo_param_type *type)
495 {
496 pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
497 if (!S->result) {
498 return 0;
499 }
500
501 /* We have already increased count by 1 in pgsql_stmt_fetch() */
502 if (PQgetisnull(S->result, S->current_row - 1, colno)) { /* Check if we got NULL */
503 ZVAL_NULL(result);
504 } else {
505 char *ptr = PQgetvalue(S->result, S->current_row - 1, colno);
506 size_t len = PQgetlength(S->result, S->current_row - 1, colno);
507
508 switch (S->cols[colno].pgsql_type) {
509 case BOOLOID:
510 ZVAL_BOOL(result, *ptr == 't');
511 break;
512
513 case INT2OID:
514 case INT4OID:
515 #if SIZEOF_ZEND_LONG >= 8
516 case INT8OID:
517 #endif
518 ZVAL_LONG(result, ZEND_ATOL(ptr));
519 break;
520 case FLOAT4OID:
521 case FLOAT8OID:
522 if (strncmp(ptr, "Infinity", len) == 0) {
523 ZVAL_DOUBLE(result, ZEND_INFINITY);
524 } else if (strncmp(ptr, "-Infinity", len) == 0) {
525 ZVAL_DOUBLE(result, -ZEND_INFINITY);
526 } else if (strncmp(ptr, "NaN", len) == 0) {
527 ZVAL_DOUBLE(result, ZEND_NAN);
528 } else {
529 ZVAL_DOUBLE(result, zend_strtod(ptr, NULL));
530 }
531 break;
532
533 case OIDOID: {
534 char *end_ptr;
535 Oid oid = (Oid)strtoul(ptr, &end_ptr, 10);
536 if (type && *type == PDO_PARAM_LOB) {
537 /* If column was bound as LOB, return a stream. */
538 int loid = lo_open(S->H->server, oid, INV_READ);
539 if (loid >= 0) {
540 php_stream *stream = pdo_pgsql_create_lob_stream(&stmt->database_object_handle, loid, oid);
541 if (stream) {
542 php_stream_to_zval(stream, result);
543 return 1;
544 }
545 }
546 return 0;
547 } else {
548 /* Otherwise return OID as integer. */
549 ZVAL_LONG(result, oid);
550 }
551 break;
552 }
553
554 case BYTEAOID: {
555 size_t tmp_len;
556 char *tmp_ptr = (char *)PQunescapeBytea((unsigned char *) ptr, &tmp_len);
557 if (!tmp_ptr) {
558 /* PQunescapeBytea returned an error */
559 return 0;
560 }
561
562 zend_string *str = zend_string_init(tmp_ptr, tmp_len, 0);
563 php_stream *stream = php_stream_memory_open(TEMP_STREAM_READONLY, str);
564 php_stream_to_zval(stream, result);
565 zend_string_release(str);
566 PQfreemem(tmp_ptr);
567 break;
568 }
569
570 default:
571 ZVAL_STRINGL_FAST(result, ptr, len);
572 break;
573 }
574 }
575
576 return 1;
577 }
578
pdo_pgsql_translate_oid_to_table(Oid oid,PGconn * conn)579 static zend_always_inline char * pdo_pgsql_translate_oid_to_table(Oid oid, PGconn *conn)
580 {
581 char *table_name = NULL;
582 PGresult *tmp_res;
583 char *querystr = NULL;
584
585 spprintf(&querystr, 0, "SELECT RELNAME FROM PG_CLASS WHERE OID=%d", oid);
586
587 if ((tmp_res = PQexec(conn, querystr)) == NULL || PQresultStatus(tmp_res) != PGRES_TUPLES_OK) {
588 if (tmp_res) {
589 PQclear(tmp_res);
590 }
591 efree(querystr);
592 return 0;
593 }
594 efree(querystr);
595
596 if (1 == PQgetisnull(tmp_res, 0, 0) || (table_name = PQgetvalue(tmp_res, 0, 0)) == NULL) {
597 PQclear(tmp_res);
598 return 0;
599 }
600
601 table_name = estrdup(table_name);
602
603 PQclear(tmp_res);
604 return table_name;
605 }
606
pgsql_stmt_get_column_meta(pdo_stmt_t * stmt,zend_long colno,zval * return_value)607 static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value)
608 {
609 pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
610 PGresult *res;
611 char *q=NULL;
612 ExecStatusType status;
613 Oid table_oid;
614 char *table_name=NULL;
615
616 if (!S->result) {
617 return FAILURE;
618 }
619
620 if (colno >= stmt->column_count) {
621 return FAILURE;
622 }
623
624 array_init(return_value);
625 add_assoc_long(return_value, "pgsql:oid", S->cols[colno].pgsql_type);
626
627 table_oid = PQftable(S->result, colno);
628 add_assoc_long(return_value, "pgsql:table_oid", table_oid);
629 table_name = pdo_pgsql_translate_oid_to_table(table_oid, S->H->server);
630 if (table_name) {
631 add_assoc_string(return_value, "table", table_name);
632 efree(table_name);
633 }
634
635 switch (S->cols[colno].pgsql_type) {
636 case BOOLOID:
637 add_assoc_string(return_value, "native_type", BOOLLABEL);
638 break;
639 case BYTEAOID:
640 add_assoc_string(return_value, "native_type", BYTEALABEL);
641 break;
642 case INT8OID:
643 add_assoc_string(return_value, "native_type", INT8LABEL);
644 break;
645 case INT2OID:
646 add_assoc_string(return_value, "native_type", INT2LABEL);
647 break;
648 case INT4OID:
649 add_assoc_string(return_value, "native_type", INT4LABEL);
650 break;
651 case FLOAT4OID:
652 add_assoc_string(return_value, "native_type", FLOAT4LABEL);
653 break;
654 case FLOAT8OID:
655 add_assoc_string(return_value, "native_type", FLOAT8LABEL);
656 break;
657 case TEXTOID:
658 add_assoc_string(return_value, "native_type", TEXTLABEL);
659 break;
660 case VARCHAROID:
661 add_assoc_string(return_value, "native_type", VARCHARLABEL);
662 break;
663 case DATEOID:
664 add_assoc_string(return_value, "native_type", DATELABEL);
665 break;
666 case TIMESTAMPOID:
667 add_assoc_string(return_value, "native_type", TIMESTAMPLABEL);
668 break;
669 default:
670 /* Fetch metadata from Postgres system catalogue */
671 spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%u", S->cols[colno].pgsql_type);
672 res = PQexec(S->H->server, q);
673 efree(q);
674 status = PQresultStatus(res);
675 if (status == PGRES_TUPLES_OK && 1 == PQntuples(res)) {
676 add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0));
677 }
678 PQclear(res);
679 }
680
681 enum pdo_param_type param_type;
682 switch (S->cols[colno].pgsql_type) {
683 case BOOLOID:
684 param_type = PDO_PARAM_BOOL;
685 break;
686 case INT2OID:
687 case INT4OID:
688 case INT8OID:
689 param_type = PDO_PARAM_INT;
690 break;
691 case OIDOID:
692 case BYTEAOID:
693 param_type = PDO_PARAM_LOB;
694 break;
695 default:
696 param_type = PDO_PARAM_STR;
697 }
698 add_assoc_long(return_value, "pdo_type", param_type);
699
700 return 1;
701 }
702
pdo_pgsql_stmt_cursor_closer(pdo_stmt_t * stmt)703 static int pdo_pgsql_stmt_cursor_closer(pdo_stmt_t *stmt)
704 {
705 return 1;
706 }
707
708 const struct pdo_stmt_methods pgsql_stmt_methods = {
709 pgsql_stmt_dtor,
710 pgsql_stmt_execute,
711 pgsql_stmt_fetch,
712 pgsql_stmt_describe,
713 pgsql_stmt_get_col,
714 pgsql_stmt_param_hook,
715 NULL, /* set_attr */
716 NULL, /* get_attr */
717 pgsql_stmt_get_column_meta,
718 NULL, /* next_rowset */
719 pdo_pgsql_stmt_cursor_closer
720 };
721