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