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