xref: /PHP-8.0/ext/pdo_pgsql/pgsql_statement.c (revision 6ac3f7c8)
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   | http://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 	zend_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 		efree(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, 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, 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, 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, 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 	if (!stmt->executed && (!stmt->column_count || S->cols == NULL)) {
246 		stmt->column_count = (int) PQnfields(S->result);
247 		S->cols = ecalloc(stmt->column_count, sizeof(pdo_pgsql_column));
248 	}
249 
250 	if (status == PGRES_COMMAND_OK) {
251 		ZEND_ATOL(stmt->row_count, 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 						ZEND_ATOL(param->paramno, ZSTR_VAL(param->name) + 1);
282 					} else {
283 						/* resolve parameter name to rewritten name */
284 						char *namevar;
285 
286 						if (stmt->bound_param_map && (namevar = zend_hash_find_ptr(stmt->bound_param_map,
287 								param->name)) != NULL) {
288 							ZEND_ATOL(param->paramno, 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 			case PDO_PARAM_EVT_EXEC_POST:
307 			case PDO_PARAM_EVT_FETCH_PRE:
308 			case PDO_PARAM_EVT_FETCH_POST:
309 				/* work is handled by EVT_NORMALIZE */
310 				return 1;
311 
312 			case PDO_PARAM_EVT_EXEC_PRE:
313 				if (!stmt->bound_param_map) {
314 					return 1;
315 				}
316 				if (!S->param_values) {
317 					S->param_values = ecalloc(
318 							zend_hash_num_elements(stmt->bound_param_map),
319 							sizeof(char*));
320 					S->param_lengths = ecalloc(
321 							zend_hash_num_elements(stmt->bound_param_map),
322 							sizeof(int));
323 					S->param_formats = ecalloc(
324 							zend_hash_num_elements(stmt->bound_param_map),
325 							sizeof(int));
326 					S->param_types = ecalloc(
327 							zend_hash_num_elements(stmt->bound_param_map),
328 							sizeof(Oid));
329 				}
330 				if (param->paramno >= 0) {
331 					zval *parameter;
332 
333 					/*
334 					if (param->paramno >= zend_hash_num_elements(stmt->bound_params)) {
335 						pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");
336 						return 0;
337 					}
338 					*/
339 
340 					if (Z_ISREF(param->parameter)) {
341 						parameter = Z_REFVAL(param->parameter);
342 					} else {
343 						parameter = &param->parameter;
344 					}
345 
346 					if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB &&
347 							Z_TYPE_P(parameter) == IS_RESOURCE) {
348 						php_stream *stm;
349 						php_stream_from_zval_no_verify(stm, parameter);
350 						if (stm) {
351 							if (php_stream_is(stm, &pdo_pgsql_lob_stream_ops)) {
352 								struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stm->abstract;
353 								pdo_pgsql_bound_param *P = param->driver_data;
354 
355 								if (P == NULL) {
356 									P = ecalloc(1, sizeof(*P));
357 									param->driver_data = P;
358 								}
359 								P->oid = htonl(self->oid);
360 								S->param_values[param->paramno] = (char*)&P->oid;
361 								S->param_lengths[param->paramno] = sizeof(P->oid);
362 								S->param_formats[param->paramno] = 1;
363 								S->param_types[param->paramno] = OIDOID;
364 								return 1;
365 							} else {
366 								zend_string *str = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
367 								if (str != NULL) {
368 									//??SEPARATE_ZVAL_IF_NOT_REF(&param->parameter);
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 						//SEPARATE_ZVAL_IF_NOT_REF(&param->parameter);
391 						convert_to_string_ex(parameter);
392 						S->param_values[param->paramno] = Z_STRVAL_P(parameter);
393 						S->param_lengths[param->paramno] = Z_STRLEN_P(parameter);
394 						S->param_formats[param->paramno] = 0;
395 					}
396 
397 					if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
398 						S->param_types[param->paramno] = 0;
399 						S->param_formats[param->paramno] = 1;
400 					} else {
401 						S->param_types[param->paramno] = 0;
402 					}
403 				}
404 				break;
405 		}
406 	} else if (param->is_param && event_type == PDO_PARAM_EVT_NORMALIZE) {
407 		/* We need to manually convert to a pg native boolean value */
408 		if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_BOOL &&
409 			((param->param_type & PDO_PARAM_INPUT_OUTPUT) != PDO_PARAM_INPUT_OUTPUT)) {
410 			const char *s = zend_is_true(&param->parameter) ? "t" : "f";
411 			param->param_type = PDO_PARAM_STR;
412 			zval_ptr_dtor(&param->parameter);
413 			ZVAL_STRINGL(&param->parameter, s, 1);
414 		}
415 	}
416 	return 1;
417 }
418 
pgsql_stmt_fetch(pdo_stmt_t * stmt,enum pdo_fetch_orientation ori,zend_long offset)419 static int pgsql_stmt_fetch(pdo_stmt_t *stmt,
420 	enum pdo_fetch_orientation ori, zend_long offset)
421 {
422 	pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
423 
424 	if (S->cursor_name) {
425 		char *ori_str = NULL;
426 		char *q = NULL;
427 		ExecStatusType status;
428 
429 		switch (ori) {
430 			case PDO_FETCH_ORI_NEXT: 	spprintf(&ori_str, 0, "NEXT"); break;
431 			case PDO_FETCH_ORI_PRIOR:	spprintf(&ori_str, 0, "BACKWARD"); break;
432 			case PDO_FETCH_ORI_FIRST:	spprintf(&ori_str, 0, "FIRST"); break;
433 			case PDO_FETCH_ORI_LAST:	spprintf(&ori_str, 0, "LAST"); break;
434 			case PDO_FETCH_ORI_ABS:		spprintf(&ori_str, 0, "ABSOLUTE " ZEND_LONG_FMT, offset); break;
435 			case PDO_FETCH_ORI_REL:		spprintf(&ori_str, 0, "RELATIVE " ZEND_LONG_FMT, offset); break;
436 			default:
437 				return 0;
438 		}
439 
440 		if(S->result) {
441 			PQclear(S->result);
442 			S->result = NULL;
443 		}
444 
445 		spprintf(&q, 0, "FETCH %s FROM %s", ori_str, S->cursor_name);
446 		efree(ori_str);
447 		S->result = PQexec(S->H->server, q);
448 		efree(q);
449 		status = PQresultStatus(S->result);
450 
451 		if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
452 			pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
453 			return 0;
454 		}
455 
456 		if (PQntuples(S->result)) {
457 			S->current_row = 1;
458 			return 1;
459 		} else {
460 			return 0;
461 		}
462 	} else {
463 		if (S->current_row < stmt->row_count) {
464 			S->current_row++;
465 			return 1;
466 		} else {
467 			return 0;
468 		}
469 	}
470 }
471 
pgsql_stmt_describe(pdo_stmt_t * stmt,int colno)472 static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno)
473 {
474 	pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
475 	struct pdo_column_data *cols = stmt->columns;
476 	struct pdo_bound_param_data *param;
477 	char *str;
478 
479 	if (!S->result) {
480 		return 0;
481 	}
482 
483 	str = PQfname(S->result, colno);
484 	cols[colno].name = zend_string_init(str, strlen(str), 0);
485 	cols[colno].maxlen = PQfsize(S->result, colno);
486 	cols[colno].precision = PQfmod(S->result, colno);
487 	S->cols[colno].pgsql_type = PQftype(S->result, colno);
488 
489 	switch (S->cols[colno].pgsql_type) {
490 
491 		case BOOLOID:
492 			cols[colno].param_type = PDO_PARAM_BOOL;
493 			break;
494 
495 		case OIDOID:
496 			/* did the user bind the column as a LOB ? */
497 			if (stmt->bound_columns && (
498 					(param = zend_hash_index_find_ptr(stmt->bound_columns, colno)) != NULL ||
499 					(param = zend_hash_find_ptr(stmt->bound_columns, cols[colno].name)) != NULL)) {
500 
501 				if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
502 					cols[colno].param_type = PDO_PARAM_LOB;
503 					break;
504 				}
505 			}
506 			cols[colno].param_type = PDO_PARAM_INT;
507 			break;
508 
509 		case INT2OID:
510 		case INT4OID:
511 			cols[colno].param_type = PDO_PARAM_INT;
512 			break;
513 
514 		case INT8OID:
515 			if (sizeof(zend_long)>=8) {
516 				cols[colno].param_type = PDO_PARAM_INT;
517 			} else {
518 				cols[colno].param_type = PDO_PARAM_STR;
519 			}
520 			break;
521 
522 		case BYTEAOID:
523 			cols[colno].param_type = PDO_PARAM_LOB;
524 			break;
525 
526 		default:
527 			cols[colno].param_type = PDO_PARAM_STR;
528 	}
529 
530 	return 1;
531 }
532 
pgsql_stmt_get_col(pdo_stmt_t * stmt,int colno,char ** ptr,size_t * len,int * caller_frees)533 static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, size_t *len, int *caller_frees )
534 {
535 	pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
536 	struct pdo_column_data *cols = stmt->columns;
537 	size_t tmp_len;
538 
539 	if (!S->result) {
540 		return 0;
541 	}
542 
543 	/* We have already increased count by 1 in pgsql_stmt_fetch() */
544 	if (PQgetisnull(S->result, S->current_row - 1, colno)) { /* Check if we got NULL */
545 		*ptr = NULL;
546 		*len = 0;
547 	} else {
548 		*ptr = PQgetvalue(S->result, S->current_row - 1, colno);
549 		*len = PQgetlength(S->result, S->current_row - 1, colno);
550 
551 		switch (cols[colno].param_type) {
552 
553 			case PDO_PARAM_INT:
554 				ZEND_ATOL(S->cols[colno].intval, *ptr);
555 				*ptr = (char *) &(S->cols[colno].intval);
556 				*len = sizeof(zend_long);
557 				break;
558 
559 			case PDO_PARAM_BOOL:
560 				S->cols[colno].boolval = **ptr == 't';
561 				*ptr = (char *) &(S->cols[colno].boolval);
562 				*len = sizeof(zend_bool);
563 				break;
564 
565 			case PDO_PARAM_LOB:
566 				if (S->cols[colno].pgsql_type == OIDOID) {
567 					/* ooo, a real large object */
568 					char *end_ptr;
569 					Oid oid = (Oid)strtoul(*ptr, &end_ptr, 10);
570 					int loid = lo_open(S->H->server, oid, INV_READ);
571 					if (loid >= 0) {
572 						*ptr = (char*)pdo_pgsql_create_lob_stream(&stmt->database_object_handle, loid, oid);
573 						*len = 0;
574 						return *ptr ? 1 : 0;
575 					}
576 					*ptr = NULL;
577 					*len = 0;
578 					return 0;
579 				} else {
580 					char *tmp_ptr = (char *)PQunescapeBytea((unsigned char *)*ptr, &tmp_len);
581 					if (!tmp_ptr) {
582 						/* PQunescapeBytea returned an error */
583 						*len = 0;
584 						return 0;
585 					}
586 					if (!tmp_len) {
587 						/* Empty string, return as empty stream */
588 						*ptr = (char *)php_stream_memory_open(TEMP_STREAM_READONLY, "", 0);
589 						PQfreemem(tmp_ptr);
590 						*len = 0;
591 					} else {
592 						*ptr = estrndup(tmp_ptr, tmp_len);
593 						PQfreemem(tmp_ptr);
594 						*len = tmp_len;
595 						*caller_frees = 1;
596 					}
597 				}
598 				break;
599 			case PDO_PARAM_NULL:
600 			case PDO_PARAM_STR:
601 			case PDO_PARAM_STMT:
602 			case PDO_PARAM_INPUT_OUTPUT:
603 			case PDO_PARAM_ZVAL:
604 			default:
605 				break;
606 		}
607 	}
608 
609 	return 1;
610 }
611 
pdo_pgsql_translate_oid_to_table(Oid oid,PGconn * conn)612 static zend_always_inline char * pdo_pgsql_translate_oid_to_table(Oid oid, PGconn *conn)
613 {
614 	char *table_name = NULL;
615 	PGresult *tmp_res;
616 	char *querystr = NULL;
617 
618 	spprintf(&querystr, 0, "SELECT RELNAME FROM PG_CLASS WHERE OID=%d", oid);
619 
620 	if ((tmp_res = PQexec(conn, querystr)) == NULL || PQresultStatus(tmp_res) != PGRES_TUPLES_OK) {
621 		if (tmp_res) {
622 			PQclear(tmp_res);
623 		}
624 		efree(querystr);
625 		return 0;
626 	}
627 	efree(querystr);
628 
629 	if (1 == PQgetisnull(tmp_res, 0, 0) || (table_name = PQgetvalue(tmp_res, 0, 0)) == NULL) {
630 		PQclear(tmp_res);
631 		return 0;
632 	}
633 
634 	table_name = estrdup(table_name);
635 
636 	PQclear(tmp_res);
637 	return table_name;
638 }
639 
pgsql_stmt_get_column_meta(pdo_stmt_t * stmt,zend_long colno,zval * return_value)640 static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value)
641 {
642 	pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
643 	PGresult *res;
644 	char *q=NULL;
645 	ExecStatusType status;
646 	Oid table_oid;
647 	char *table_name=NULL;
648 
649 	if (!S->result) {
650 		return FAILURE;
651 	}
652 
653 	if (colno >= stmt->column_count) {
654 		return FAILURE;
655 	}
656 
657 	array_init(return_value);
658 	add_assoc_long(return_value, "pgsql:oid", S->cols[colno].pgsql_type);
659 
660 	table_oid = PQftable(S->result, colno);
661 	add_assoc_long(return_value, "pgsql:table_oid", table_oid);
662 	table_name = pdo_pgsql_translate_oid_to_table(table_oid, S->H->server);
663 	if (table_name) {
664 		add_assoc_string(return_value, "table", table_name);
665 		efree(table_name);
666 	}
667 
668 	switch (S->cols[colno].pgsql_type) {
669 		case BOOLOID:
670 			add_assoc_string(return_value, "native_type", BOOLLABEL);
671 			break;
672 		case BYTEAOID:
673 			add_assoc_string(return_value, "native_type", BYTEALABEL);
674 			break;
675 		case INT8OID:
676 			add_assoc_string(return_value, "native_type", INT8LABEL);
677 			break;
678 		case INT2OID:
679 			add_assoc_string(return_value, "native_type", INT2LABEL);
680 			break;
681 		case INT4OID:
682 			add_assoc_string(return_value, "native_type", INT4LABEL);
683 			break;
684 		case TEXTOID:
685 			add_assoc_string(return_value, "native_type", TEXTLABEL);
686 			break;
687 		case VARCHAROID:
688 			add_assoc_string(return_value, "native_type", VARCHARLABEL);
689 			break;
690 		case DATEOID:
691 			add_assoc_string(return_value, "native_type", DATELABEL);
692 			break;
693 		case TIMESTAMPOID:
694 			add_assoc_string(return_value, "native_type", TIMESTAMPLABEL);
695 			break;
696 		default:
697 			/* Fetch metadata from Postgres system catalogue */
698 			spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%u", S->cols[colno].pgsql_type);
699 			res = PQexec(S->H->server, q);
700 			efree(q);
701 			status = PQresultStatus(res);
702 			if (status == PGRES_TUPLES_OK && 1 == PQntuples(res)) {
703 				add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0));
704 			}
705 			PQclear(res);
706 	}
707 	return 1;
708 }
709 
pdo_pgsql_stmt_cursor_closer(pdo_stmt_t * stmt)710 static int pdo_pgsql_stmt_cursor_closer(pdo_stmt_t *stmt)
711 {
712 	pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
713 
714 	if (S->cols != NULL){
715 		efree(S->cols);
716 		S->cols = NULL;
717 	}
718 	return 1;
719 }
720 
721 const struct pdo_stmt_methods pgsql_stmt_methods = {
722 	pgsql_stmt_dtor,
723 	pgsql_stmt_execute,
724 	pgsql_stmt_fetch,
725 	pgsql_stmt_describe,
726 	pgsql_stmt_get_col,
727 	pgsql_stmt_param_hook,
728 	NULL, /* set_attr */
729 	NULL, /* get_attr */
730 	pgsql_stmt_get_column_meta,
731 	NULL,  /* next_rowset */
732 	pdo_pgsql_stmt_cursor_closer
733 };
734