2004-05-20 10:27:49 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| PHP Version 5 |
|
|
|
|
+----------------------------------------------------------------------+
|
2014-01-03 11:08:10 +08:00
|
|
|
| Copyright (c) 1997-2014 The PHP Group |
|
2004-05-20 10:27:49 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:51:34 +08:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
2004-05-20 10:27:49 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 20:51:34 +08:00
|
|
|
| http://www.php.net/license/3_01.txt |
|
2004-05-20 10:27:49 +08:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
2005-11-29 10:11:39 +08:00
|
|
|
| Authors: Edin Kadribasic <edink@emini.dk> |
|
|
|
|
| Ilia Alshanestsky <ilia@prohost.org> |
|
|
|
|
| Wez Furlong <wez@php.net> |
|
2004-05-20 10:27:49 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "php.h"
|
|
|
|
#include "php_ini.h"
|
|
|
|
#include "ext/standard/info.h"
|
|
|
|
#include "pdo/php_pdo.h"
|
|
|
|
#include "pdo/php_pdo_driver.h"
|
|
|
|
#include "php_pdo_pgsql.h"
|
|
|
|
#include "php_pdo_pgsql_int.h"
|
2005-12-18 02:26:15 +08:00
|
|
|
#if HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
2004-05-20 10:27:49 +08:00
|
|
|
|
2005-01-23 07:31:19 +08:00
|
|
|
/* from postgresql/src/include/catalog/pg_type.h */
|
2005-02-06 04:38:47 +08:00
|
|
|
#define BOOLOID 16
|
|
|
|
#define BYTEAOID 17
|
|
|
|
#define INT8OID 20
|
|
|
|
#define INT2OID 21
|
|
|
|
#define INT4OID 23
|
2005-11-25 11:35:04 +08:00
|
|
|
#define TEXTOID 25
|
2005-02-06 04:38:47 +08:00
|
|
|
#define OIDOID 26
|
2005-01-23 07:31:19 +08:00
|
|
|
|
2004-05-20 10:27:49 +08:00
|
|
|
static int pgsql_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC)
|
|
|
|
{
|
|
|
|
pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
|
|
|
|
|
|
|
|
if (S->result) {
|
|
|
|
/* free the resource */
|
|
|
|
PQclear(S->result);
|
|
|
|
S->result = NULL;
|
|
|
|
}
|
2005-01-13 09:59:39 +08:00
|
|
|
|
2005-07-08 23:27:34 +08:00
|
|
|
if (S->stmt_name) {
|
|
|
|
pdo_pgsql_db_handle *H = S->H;
|
|
|
|
char *q = NULL;
|
|
|
|
PGresult *res;
|
|
|
|
|
2006-09-19 23:45:22 +08:00
|
|
|
if (S->is_prepared) {
|
|
|
|
spprintf(&q, 0, "DEALLOCATE %s", S->stmt_name);
|
|
|
|
res = PQexec(H->server, q);
|
|
|
|
efree(q);
|
|
|
|
if (res) {
|
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
}
|
2005-07-08 23:27:34 +08:00
|
|
|
efree(S->stmt_name);
|
|
|
|
S->stmt_name = NULL;
|
|
|
|
}
|
|
|
|
if (S->param_lengths) {
|
|
|
|
efree(S->param_lengths);
|
|
|
|
S->param_lengths = NULL;
|
|
|
|
}
|
|
|
|
if (S->param_values) {
|
|
|
|
efree(S->param_values);
|
|
|
|
S->param_values = NULL;
|
|
|
|
}
|
2005-07-09 04:45:19 +08:00
|
|
|
if (S->param_formats) {
|
|
|
|
efree(S->param_formats);
|
|
|
|
S->param_formats = NULL;
|
|
|
|
}
|
2005-12-02 00:33:01 +08:00
|
|
|
if (S->param_types) {
|
|
|
|
efree(S->param_types);
|
|
|
|
S->param_types = NULL;
|
|
|
|
}
|
2005-12-01 07:27:15 +08:00
|
|
|
if (S->query) {
|
|
|
|
efree(S->query);
|
|
|
|
S->query = NULL;
|
|
|
|
}
|
2005-07-08 23:27:34 +08:00
|
|
|
|
2005-01-13 09:59:39 +08:00
|
|
|
if (S->cursor_name) {
|
|
|
|
pdo_pgsql_db_handle *H = S->H;
|
|
|
|
char *q = NULL;
|
2005-01-21 08:39:03 +08:00
|
|
|
PGresult *res;
|
|
|
|
|
2005-01-13 09:59:39 +08:00
|
|
|
spprintf(&q, 0, "CLOSE %s", S->cursor_name);
|
2005-01-21 08:39:03 +08:00
|
|
|
res = PQexec(H->server, q);
|
2005-01-13 09:59:39 +08:00
|
|
|
efree(q);
|
|
|
|
if (res) PQclear(res);
|
|
|
|
efree(S->cursor_name);
|
|
|
|
S->cursor_name = NULL;
|
|
|
|
}
|
2014-03-09 18:43:13 +08:00
|
|
|
|
2004-05-20 10:27:49 +08:00
|
|
|
if(S->cols) {
|
|
|
|
efree(S->cols);
|
|
|
|
S->cols = NULL;
|
|
|
|
}
|
|
|
|
efree(S);
|
2005-07-08 23:27:34 +08:00
|
|
|
stmt->driver_data = NULL;
|
2004-05-20 10:27:49 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pgsql_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
|
|
|
|
{
|
|
|
|
pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
|
|
|
|
pdo_pgsql_db_handle *H = S->H;
|
|
|
|
ExecStatusType status;
|
|
|
|
|
2005-11-25 11:35:04 +08:00
|
|
|
/* ensure that we free any previous unfetched results */
|
|
|
|
if(S->result) {
|
|
|
|
PQclear(S->result);
|
|
|
|
S->result = NULL;
|
2004-05-20 10:27:49 +08:00
|
|
|
}
|
2014-03-09 18:43:13 +08:00
|
|
|
|
2005-03-11 02:48:01 +08:00
|
|
|
S->current_row = 0;
|
2004-05-20 10:27:49 +08:00
|
|
|
|
2009-03-28 10:58:04 +08:00
|
|
|
if (S->cursor_name) {
|
|
|
|
char *q = NULL;
|
2009-05-13 06:18:15 +08:00
|
|
|
|
|
|
|
if (S->is_prepared) {
|
|
|
|
spprintf(&q, 0, "CLOSE %s", S->cursor_name);
|
|
|
|
S->result = PQexec(H->server, q);
|
|
|
|
efree(q);
|
|
|
|
}
|
|
|
|
|
2009-03-28 10:58:04 +08:00
|
|
|
spprintf(&q, 0, "DECLARE %s SCROLL CURSOR WITH HOLD FOR %s", S->cursor_name, stmt->active_query_string);
|
|
|
|
S->result = PQexec(H->server, q);
|
|
|
|
efree(q);
|
|
|
|
|
|
|
|
/* check if declare failed */
|
|
|
|
status = PQresultStatus(S->result);
|
|
|
|
if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
|
|
|
|
pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-05-13 06:18:15 +08:00
|
|
|
/* the cursor was declared correctly */
|
|
|
|
S->is_prepared = 1;
|
|
|
|
|
2009-03-28 10:58:04 +08:00
|
|
|
/* fetch to be able to get the number of tuples later, but don't advance the cursor pointer */
|
|
|
|
spprintf(&q, 0, "FETCH FORWARD 0 FROM %s", S->cursor_name);
|
|
|
|
S->result = PQexec(H->server, q);
|
|
|
|
efree(q);
|
2014-03-09 18:43:13 +08:00
|
|
|
} else if (S->stmt_name) {
|
2005-07-08 23:27:34 +08:00
|
|
|
/* using a prepared statement */
|
|
|
|
|
2005-12-18 01:59:05 +08:00
|
|
|
if (!S->is_prepared) {
|
|
|
|
stmt_retry:
|
2005-11-25 11:35:04 +08:00
|
|
|
/* we deferred the prepare until now, because we didn't
|
|
|
|
* know anything about the parameter types; now we do */
|
2014-03-09 18:43:13 +08:00
|
|
|
S->result = PQprepare(H->server, S->stmt_name, S->query,
|
2005-11-25 11:35:04 +08:00
|
|
|
stmt->bound_params ? zend_hash_num_elements(stmt->bound_params) : 0,
|
|
|
|
S->param_types);
|
|
|
|
status = PQresultStatus(S->result);
|
|
|
|
switch (status) {
|
|
|
|
case PGRES_COMMAND_OK:
|
|
|
|
case PGRES_TUPLES_OK:
|
|
|
|
/* it worked */
|
2005-12-18 01:59:05 +08:00
|
|
|
S->is_prepared = 1;
|
2005-11-25 11:35:04 +08:00
|
|
|
PQclear(S->result);
|
|
|
|
break;
|
2005-12-18 01:59:05 +08:00
|
|
|
default: {
|
|
|
|
char *sqlstate = pdo_pgsql_sqlstate(S->result);
|
2014-03-09 18:43:13 +08:00
|
|
|
/* 42P05 means that the prepared statement already existed. this can happen if you use
|
|
|
|
* a connection pooling software line pgpool which doesn't close the db-connection once
|
|
|
|
* php disconnects. if php dies (no chance to run RSHUTDOWN) during execution it has no
|
|
|
|
* chance to DEALLOCATE the prepared statements it has created. so, if we hit a 42P05 we
|
2009-11-05 03:16:39 +08:00
|
|
|
* deallocate it and retry ONCE (thies 2005.12.15)
|
2005-12-18 01:59:05 +08:00
|
|
|
*/
|
2010-12-11 01:50:26 +08:00
|
|
|
if (sqlstate && !strcmp(sqlstate, "42P05")) {
|
2009-11-05 03:32:27 +08:00
|
|
|
char buf[100]; /* stmt_name == "pdo_crsr_%08x" */
|
2005-12-18 01:59:05 +08:00
|
|
|
PGresult *res;
|
|
|
|
snprintf(buf, sizeof(buf), "DEALLOCATE %s", S->stmt_name);
|
|
|
|
res = PQexec(H->server, buf);
|
|
|
|
if (res) {
|
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
goto stmt_retry;
|
|
|
|
} else {
|
|
|
|
pdo_pgsql_error_stmt(stmt, status, sqlstate);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2005-11-25 11:35:04 +08:00
|
|
|
}
|
|
|
|
}
|
2005-07-08 23:27:34 +08:00
|
|
|
S->result = PQexecPrepared(H->server, S->stmt_name,
|
|
|
|
stmt->bound_params ?
|
|
|
|
zend_hash_num_elements(stmt->bound_params) :
|
|
|
|
0,
|
|
|
|
(const char**)S->param_values,
|
|
|
|
S->param_lengths,
|
2005-11-25 11:35:04 +08:00
|
|
|
S->param_formats,
|
2005-07-08 23:27:34 +08:00
|
|
|
0);
|
2014-03-09 21:08:17 +08:00
|
|
|
} else if (stmt->supports_placeholders == PDO_PLACEHOLDER_NAMED) {
|
|
|
|
/* execute query with parameters */
|
|
|
|
S->result = PQexecParams(H->server, S->query,
|
|
|
|
stmt->bound_params ? zend_hash_num_elements(stmt->bound_params) : 0,
|
|
|
|
S->param_types,
|
|
|
|
(const char**)S->param_values,
|
|
|
|
S->param_lengths,
|
|
|
|
S->param_formats,
|
|
|
|
0);
|
2014-03-09 18:43:13 +08:00
|
|
|
} else {
|
2014-03-09 21:08:17 +08:00
|
|
|
/* execute plain query (with embedded parameters) */
|
2005-01-13 09:59:39 +08:00
|
|
|
S->result = PQexec(H->server, stmt->active_query_string);
|
|
|
|
}
|
2004-05-20 10:27:49 +08:00
|
|
|
status = PQresultStatus(S->result);
|
|
|
|
|
|
|
|
if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
|
2005-07-02 06:30:55 +08:00
|
|
|
pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
|
2004-05-20 10:27:49 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-11-29 00:27:53 +08:00
|
|
|
if (!stmt->executed && !stmt->column_count) {
|
2004-05-20 10:27:49 +08:00
|
|
|
stmt->column_count = (int) PQnfields(S->result);
|
|
|
|
S->cols = ecalloc(stmt->column_count, sizeof(pdo_pgsql_column));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == PGRES_COMMAND_OK) {
|
2014-08-26 02:22:49 +08:00
|
|
|
ZEND_ATOL(stmt->row_count, PQcmdTuples(S->result));
|
2005-02-04 09:48:01 +08:00
|
|
|
H->pgoid = PQoidValue(S->result);
|
2004-05-20 10:27:49 +08:00
|
|
|
} else {
|
2014-08-26 01:24:55 +08:00
|
|
|
stmt->row_count = (zend_long)PQntuples(S->result);
|
2004-05-20 10:27:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pgsql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
|
|
|
|
enum pdo_param_event event_type TSRMLS_DC)
|
|
|
|
{
|
2006-11-17 01:33:39 +08:00
|
|
|
pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
|
|
|
|
|
2014-03-09 21:08:17 +08:00
|
|
|
if (stmt->supports_placeholders == PDO_PLACEHOLDER_NAMED && param->is_param) {
|
2005-07-08 23:27:34 +08:00
|
|
|
switch (event_type) {
|
2005-11-29 10:11:39 +08:00
|
|
|
case PDO_PARAM_EVT_FREE:
|
|
|
|
if (param->driver_data) {
|
|
|
|
efree(param->driver_data);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2006-03-28 04:51:01 +08:00
|
|
|
case PDO_PARAM_EVT_NORMALIZE:
|
2005-07-08 23:27:34 +08:00
|
|
|
/* decode name from $1, $2 into 0, 1 etc. */
|
|
|
|
if (param->name) {
|
2014-05-15 20:15:06 +08:00
|
|
|
if (param->name->val[0] == '$') {
|
2014-08-26 02:22:49 +08:00
|
|
|
ZEND_ATOL(param->paramno, param->name->val + 1);
|
2005-07-09 01:01:20 +08:00
|
|
|
} else {
|
|
|
|
/* resolve parameter name to rewritten name */
|
2014-05-16 14:18:11 +08:00
|
|
|
char *namevar;
|
2014-05-15 20:15:06 +08:00
|
|
|
|
2014-05-16 14:18:11 +08:00
|
|
|
if (stmt->bound_param_map && (namevar = zend_hash_find_ptr(stmt->bound_param_map,
|
2014-05-15 20:15:06 +08:00
|
|
|
param->name)) != NULL) {
|
2014-08-26 02:22:49 +08:00
|
|
|
ZEND_ATOL(param->paramno, namevar + 1);
|
2014-08-20 01:02:07 +08:00
|
|
|
param->paramno--;
|
2005-07-09 01:01:20 +08:00
|
|
|
} else {
|
2014-05-15 20:15:06 +08:00
|
|
|
pdo_raise_impl_error(stmt->dbh, stmt, "HY093", param->name->val TSRMLS_CC);
|
2005-07-09 01:01:20 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2005-07-08 23:27:34 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2006-03-28 04:51:01 +08:00
|
|
|
case PDO_PARAM_EVT_ALLOC:
|
2008-10-12 23:01:12 +08:00
|
|
|
case PDO_PARAM_EVT_EXEC_POST:
|
|
|
|
case PDO_PARAM_EVT_FETCH_PRE:
|
|
|
|
case PDO_PARAM_EVT_FETCH_POST:
|
2006-03-28 04:51:01 +08:00
|
|
|
/* work is handled by EVT_NORMALIZE */
|
|
|
|
return 1;
|
|
|
|
|
2005-07-08 23:27:34 +08:00
|
|
|
case PDO_PARAM_EVT_EXEC_PRE:
|
2008-02-26 08:13:57 +08:00
|
|
|
if (!stmt->bound_param_map) {
|
|
|
|
return 0;
|
|
|
|
}
|
2005-07-08 23:27:34 +08:00
|
|
|
if (!S->param_values) {
|
|
|
|
S->param_values = ecalloc(
|
2006-08-02 00:31:29 +08:00
|
|
|
zend_hash_num_elements(stmt->bound_param_map),
|
2005-07-08 23:27:34 +08:00
|
|
|
sizeof(char*));
|
|
|
|
S->param_lengths = ecalloc(
|
2006-08-02 00:31:29 +08:00
|
|
|
zend_hash_num_elements(stmt->bound_param_map),
|
2005-07-08 23:27:34 +08:00
|
|
|
sizeof(int));
|
|
|
|
S->param_formats = ecalloc(
|
2006-08-02 00:31:29 +08:00
|
|
|
zend_hash_num_elements(stmt->bound_param_map),
|
2005-07-08 23:27:34 +08:00
|
|
|
sizeof(int));
|
2005-11-25 11:35:04 +08:00
|
|
|
S->param_types = ecalloc(
|
2006-08-02 00:31:29 +08:00
|
|
|
zend_hash_num_elements(stmt->bound_param_map),
|
2005-11-25 11:35:04 +08:00
|
|
|
sizeof(Oid));
|
2005-07-08 23:27:34 +08:00
|
|
|
}
|
2005-09-04 07:50:25 +08:00
|
|
|
if (param->paramno >= 0) {
|
2014-05-16 14:18:11 +08:00
|
|
|
zval *parameter;
|
|
|
|
|
2007-11-21 07:12:17 +08:00
|
|
|
if (param->paramno > zend_hash_num_elements(stmt->bound_param_map)) {
|
|
|
|
pdo_pgsql_error_stmt(stmt, PGRES_FATAL_ERROR, "HY105");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-16 14:18:11 +08:00
|
|
|
if (Z_ISREF(param->parameter)) {
|
|
|
|
parameter = Z_REFVAL(param->parameter);
|
|
|
|
} else {
|
|
|
|
parameter = ¶m->parameter;
|
|
|
|
}
|
|
|
|
|
2005-09-25 02:47:50 +08:00
|
|
|
if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB &&
|
2014-05-16 14:18:11 +08:00
|
|
|
Z_TYPE_P(parameter) == IS_RESOURCE) {
|
2005-09-25 02:47:50 +08:00
|
|
|
php_stream *stm;
|
2014-05-16 14:18:11 +08:00
|
|
|
php_stream_from_zval_no_verify(stm, parameter);
|
2005-09-25 02:47:50 +08:00
|
|
|
if (stm) {
|
2005-11-29 10:11:39 +08:00
|
|
|
if (php_stream_is(stm, &pdo_pgsql_lob_stream_ops)) {
|
|
|
|
struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stm->abstract;
|
|
|
|
pdo_pgsql_bound_param *P = param->driver_data;
|
|
|
|
|
|
|
|
if (P == NULL) {
|
|
|
|
P = ecalloc(1, sizeof(*P));
|
|
|
|
param->driver_data = P;
|
|
|
|
}
|
|
|
|
P->oid = htonl(self->oid);
|
|
|
|
S->param_values[param->paramno] = (char*)&P->oid;
|
|
|
|
S->param_lengths[param->paramno] = sizeof(P->oid);
|
|
|
|
S->param_formats[param->paramno] = 1;
|
|
|
|
S->param_types[param->paramno] = OIDOID;
|
|
|
|
return 1;
|
|
|
|
} else {
|
2014-05-15 22:33:13 +08:00
|
|
|
zend_string *str = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
|
|
|
|
if (str != NULL) {
|
2014-05-16 14:18:11 +08:00
|
|
|
//??SEPARATE_ZVAL_IF_NOT_REF(¶m->parameter);
|
|
|
|
ZVAL_STR(parameter, str);
|
2014-05-15 22:33:13 +08:00
|
|
|
} else {
|
2014-05-16 14:18:11 +08:00
|
|
|
ZVAL_EMPTY_STRING(parameter);
|
2008-10-12 03:03:23 +08:00
|
|
|
}
|
2005-11-29 10:11:39 +08:00
|
|
|
}
|
2005-09-25 02:47:50 +08:00
|
|
|
} else {
|
|
|
|
/* expected a stream resource */
|
|
|
|
pdo_pgsql_error_stmt(stmt, PGRES_FATAL_ERROR, "HY105");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-04 07:50:25 +08:00
|
|
|
if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL ||
|
2014-05-16 14:18:11 +08:00
|
|
|
Z_TYPE_P(parameter) == IS_NULL) {
|
2005-09-04 07:50:25 +08:00
|
|
|
S->param_values[param->paramno] = NULL;
|
|
|
|
S->param_lengths[param->paramno] = 0;
|
2014-05-16 14:18:11 +08:00
|
|
|
} else if (Z_TYPE_P(parameter) == IS_FALSE || Z_TYPE_P(parameter) == IS_TRUE) {
|
|
|
|
S->param_values[param->paramno] = Z_TYPE_P(parameter) == IS_TRUE ? "t" : "f";
|
2005-09-11 05:00:52 +08:00
|
|
|
S->param_lengths[param->paramno] = 1;
|
2005-11-25 11:35:04 +08:00
|
|
|
S->param_formats[param->paramno] = 0;
|
2005-09-04 07:50:25 +08:00
|
|
|
} else {
|
2014-05-16 14:38:24 +08:00
|
|
|
//SEPARATE_ZVAL_IF_NOT_REF(¶m->parameter);
|
2014-05-16 14:18:11 +08:00
|
|
|
convert_to_string_ex(parameter);
|
2014-05-16 14:38:24 +08:00
|
|
|
S->param_values[param->paramno] = Z_STRVAL_P(parameter);
|
2014-08-26 01:24:55 +08:00
|
|
|
S->param_lengths[param->paramno] = Z_STRLEN_P(parameter);
|
2005-11-25 11:35:04 +08:00
|
|
|
S->param_formats[param->paramno] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
|
|
|
|
S->param_types[param->paramno] = 0;
|
2005-09-04 07:50:25 +08:00
|
|
|
S->param_formats[param->paramno] = 1;
|
2005-11-25 11:35:04 +08:00
|
|
|
} else {
|
|
|
|
S->param_types[param->paramno] = 0;
|
2005-09-04 07:50:25 +08:00
|
|
|
}
|
2005-07-08 23:27:34 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-03-09 18:43:13 +08:00
|
|
|
} else if (param->is_param) {
|
|
|
|
/* We need to manually convert to a pg native boolean value */
|
|
|
|
if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_BOOL &&
|
|
|
|
((param->param_type & PDO_PARAM_INPUT_OUTPUT) != PDO_PARAM_INPUT_OUTPUT)) {
|
2014-05-16 14:18:11 +08:00
|
|
|
zval *parameter;
|
|
|
|
if (Z_ISREF(param->parameter)) {
|
|
|
|
parameter = Z_REFVAL(param->parameter);
|
|
|
|
} else {
|
|
|
|
parameter = ¶m->parameter;
|
|
|
|
}
|
2014-05-16 14:38:24 +08:00
|
|
|
SEPARATE_ZVAL(¶m->parameter);
|
2014-03-09 18:43:13 +08:00
|
|
|
param->param_type = PDO_PARAM_STR;
|
2014-05-16 14:18:11 +08:00
|
|
|
ZVAL_STRINGL(parameter, Z_TYPE_P(parameter) == IS_TRUE ? "t" : "f", 1);
|
2014-03-09 18:43:13 +08:00
|
|
|
}
|
2005-07-08 23:27:34 +08:00
|
|
|
}
|
2004-05-20 10:27:49 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-01-12 14:11:33 +08:00
|
|
|
static int pgsql_stmt_fetch(pdo_stmt_t *stmt,
|
2014-08-26 01:24:55 +08:00
|
|
|
enum pdo_fetch_orientation ori, zend_long offset TSRMLS_DC)
|
2004-05-20 10:27:49 +08:00
|
|
|
{
|
|
|
|
pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
|
|
|
|
|
2005-01-13 09:59:39 +08:00
|
|
|
if (S->cursor_name) {
|
2009-03-28 10:58:04 +08:00
|
|
|
char *ori_str = NULL;
|
2005-01-13 09:59:39 +08:00
|
|
|
char *q = NULL;
|
|
|
|
ExecStatusType status;
|
|
|
|
|
|
|
|
switch (ori) {
|
2009-03-28 10:58:04 +08:00
|
|
|
case PDO_FETCH_ORI_NEXT: spprintf(&ori_str, 0, "NEXT"); break;
|
|
|
|
case PDO_FETCH_ORI_PRIOR: spprintf(&ori_str, 0, "BACKWARD"); break;
|
|
|
|
case PDO_FETCH_ORI_FIRST: spprintf(&ori_str, 0, "FIRST"); break;
|
|
|
|
case PDO_FETCH_ORI_LAST: spprintf(&ori_str, 0, "LAST"); break;
|
2014-08-20 01:02:07 +08:00
|
|
|
case PDO_FETCH_ORI_ABS: spprintf(&ori_str, 0, "ABSOLUTE %pd", offset); break;
|
|
|
|
case PDO_FETCH_ORI_REL: spprintf(&ori_str, 0, "RELATIVE %pd", offset); break;
|
2005-03-11 02:48:01 +08:00
|
|
|
default:
|
|
|
|
return 0;
|
2005-01-13 09:59:39 +08:00
|
|
|
}
|
2014-03-09 18:43:13 +08:00
|
|
|
|
2009-03-28 10:58:04 +08:00
|
|
|
spprintf(&q, 0, "FETCH %s FROM %s", ori_str, S->cursor_name);
|
|
|
|
efree(ori_str);
|
2005-01-13 09:59:39 +08:00
|
|
|
S->result = PQexec(S->H->server, q);
|
2006-05-08 22:33:00 +08:00
|
|
|
efree(q);
|
2005-01-13 09:59:39 +08:00
|
|
|
status = PQresultStatus(S->result);
|
|
|
|
|
|
|
|
if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
|
2005-07-02 06:30:55 +08:00
|
|
|
pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
|
2005-01-13 09:59:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-28 10:58:04 +08:00
|
|
|
if (PQntuples(S->result)) {
|
|
|
|
S->current_row = 1;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-20 10:27:49 +08:00
|
|
|
} else {
|
2005-01-13 09:59:39 +08:00
|
|
|
if (S->current_row < stmt->row_count) {
|
|
|
|
S->current_row++;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2004-05-20 10:27:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
|
|
|
|
{
|
|
|
|
pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
|
|
|
|
struct pdo_column_data *cols = stmt->columns;
|
2005-11-29 10:11:39 +08:00
|
|
|
struct pdo_bound_param_data *param;
|
2014-03-09 18:43:13 +08:00
|
|
|
|
2004-05-21 02:10:16 +08:00
|
|
|
if (!S->result) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-20 10:27:49 +08:00
|
|
|
cols[colno].name = estrdup(PQfname(S->result, colno));
|
|
|
|
cols[colno].namelen = strlen(cols[colno].name);
|
|
|
|
cols[colno].maxlen = PQfsize(S->result, colno);
|
|
|
|
cols[colno].precision = PQfmod(S->result, colno);
|
2005-01-23 18:57:11 +08:00
|
|
|
S->cols[colno].pgsql_type = PQftype(S->result, colno);
|
2014-03-09 18:43:13 +08:00
|
|
|
|
2014-05-16 14:18:11 +08:00
|
|
|
switch (S->cols[colno].pgsql_type) {
|
2005-01-23 07:31:19 +08:00
|
|
|
|
|
|
|
case BOOLOID:
|
|
|
|
cols[colno].param_type = PDO_PARAM_BOOL;
|
|
|
|
break;
|
2014-03-09 18:43:13 +08:00
|
|
|
|
2005-11-29 10:11:39 +08:00
|
|
|
case OIDOID:
|
|
|
|
/* did the user bind the column as a LOB ? */
|
|
|
|
if (stmt->bound_columns && (
|
2014-05-15 22:33:13 +08:00
|
|
|
(param = zend_hash_index_find_ptr(stmt->bound_columns, colno)) != NULL ||
|
|
|
|
(param = zend_hash_str_find_ptr(stmt->bound_columns, cols[colno].name, cols[colno].namelen)) != NULL)) {
|
2014-05-15 20:15:06 +08:00
|
|
|
|
2005-11-29 10:11:39 +08:00
|
|
|
if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
|
|
|
|
cols[colno].param_type = PDO_PARAM_LOB;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cols[colno].param_type = PDO_PARAM_INT;
|
|
|
|
break;
|
2005-01-23 07:31:19 +08:00
|
|
|
|
|
|
|
case INT2OID:
|
|
|
|
case INT4OID:
|
|
|
|
cols[colno].param_type = PDO_PARAM_INT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case INT8OID:
|
2014-08-26 01:24:55 +08:00
|
|
|
if (sizeof(zend_long)>=8) {
|
2005-01-23 07:31:19 +08:00
|
|
|
cols[colno].param_type = PDO_PARAM_INT;
|
2005-02-15 20:41:24 +08:00
|
|
|
} else {
|
|
|
|
cols[colno].param_type = PDO_PARAM_STR;
|
2005-01-23 07:31:19 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-02-06 06:55:23 +08:00
|
|
|
case BYTEAOID:
|
|
|
|
cols[colno].param_type = PDO_PARAM_LOB;
|
|
|
|
break;
|
|
|
|
|
2005-01-23 07:31:19 +08:00
|
|
|
default:
|
|
|
|
cols[colno].param_type = PDO_PARAM_STR;
|
|
|
|
}
|
2004-05-20 10:27:49 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, zend_ulong *len, int *caller_frees TSRMLS_DC)
|
2004-05-20 10:27:49 +08:00
|
|
|
{
|
|
|
|
pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
|
2005-01-23 07:31:19 +08:00
|
|
|
struct pdo_column_data *cols = stmt->columns;
|
2005-02-07 06:48:35 +08:00
|
|
|
size_t tmp_len;
|
2004-05-20 10:27:49 +08:00
|
|
|
|
2004-05-21 02:10:16 +08:00
|
|
|
if (!S->result) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-20 10:27:49 +08:00
|
|
|
/* We have already increased count by 1 in pgsql_stmt_fetch() */
|
2005-01-23 07:31:19 +08:00
|
|
|
if (PQgetisnull(S->result, S->current_row - 1, colno)) { /* Check if we got NULL */
|
|
|
|
*ptr = NULL;
|
|
|
|
*len = 0;
|
|
|
|
} else {
|
|
|
|
*ptr = PQgetvalue(S->result, S->current_row - 1, colno);
|
|
|
|
*len = PQgetlength(S->result, S->current_row - 1, colno);
|
2014-03-09 18:43:13 +08:00
|
|
|
|
2014-05-16 14:18:11 +08:00
|
|
|
switch (cols[colno].param_type) {
|
2005-01-23 07:31:19 +08:00
|
|
|
|
|
|
|
case PDO_PARAM_INT:
|
2014-08-26 02:22:49 +08:00
|
|
|
ZEND_ATOL(S->cols[colno].intval, *ptr);
|
2005-01-23 18:57:11 +08:00
|
|
|
*ptr = (char *) &(S->cols[colno].intval);
|
2014-08-26 01:24:55 +08:00
|
|
|
*len = sizeof(zend_long);
|
2005-01-23 07:31:19 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PDO_PARAM_BOOL:
|
2005-01-23 18:57:11 +08:00
|
|
|
S->cols[colno].boolval = **ptr == 't' ? 1: 0;
|
|
|
|
*ptr = (char *) &(S->cols[colno].boolval);
|
2005-01-23 07:31:19 +08:00
|
|
|
*len = sizeof(zend_bool);
|
|
|
|
break;
|
2014-03-09 18:43:13 +08:00
|
|
|
|
2005-02-06 06:55:23 +08:00
|
|
|
case PDO_PARAM_LOB:
|
2005-11-29 10:11:39 +08:00
|
|
|
if (S->cols[colno].pgsql_type == OIDOID) {
|
|
|
|
/* ooo, a real large object */
|
|
|
|
char *end_ptr;
|
|
|
|
Oid oid = (Oid)strtoul(*ptr, &end_ptr, 10);
|
|
|
|
int loid = lo_open(S->H->server, oid, INV_READ);
|
|
|
|
if (loid >= 0) {
|
2014-05-15 22:33:13 +08:00
|
|
|
*ptr = (char*)pdo_pgsql_create_lob_stream(&stmt->database_object_handle, loid, oid TSRMLS_CC);
|
2005-11-29 10:11:39 +08:00
|
|
|
*len = 0;
|
|
|
|
return *ptr ? 1 : 0;
|
|
|
|
}
|
|
|
|
*ptr = NULL;
|
|
|
|
*len = 0;
|
|
|
|
return 0;
|
|
|
|
} else {
|
2013-08-21 17:08:16 +08:00
|
|
|
char *tmp_ptr = (char *)PQunescapeBytea((unsigned char *)*ptr, &tmp_len);
|
2009-04-30 20:38:43 +08:00
|
|
|
if (!tmp_ptr) {
|
|
|
|
/* PQunescapeBytea returned an error */
|
|
|
|
*len = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2009-04-23 21:26:10 +08:00
|
|
|
if (!tmp_len) {
|
|
|
|
/* Empty string, return as empty stream */
|
|
|
|
*ptr = (char *)php_stream_memory_open(TEMP_STREAM_READONLY, "", 0);
|
2009-04-30 20:38:43 +08:00
|
|
|
PQfreemem(tmp_ptr);
|
2009-04-23 21:26:10 +08:00
|
|
|
*len = 0;
|
|
|
|
} else {
|
2009-04-30 20:38:43 +08:00
|
|
|
*ptr = estrndup(tmp_ptr, tmp_len);
|
|
|
|
PQfreemem(tmp_ptr);
|
2009-04-23 21:26:10 +08:00
|
|
|
*len = tmp_len;
|
|
|
|
*caller_frees = 1;
|
|
|
|
}
|
2005-11-29 10:11:39 +08:00
|
|
|
}
|
2005-05-14 02:09:03 +08:00
|
|
|
break;
|
|
|
|
case PDO_PARAM_NULL:
|
|
|
|
case PDO_PARAM_STR:
|
|
|
|
case PDO_PARAM_STMT:
|
|
|
|
case PDO_PARAM_INPUT_OUTPUT:
|
2008-10-12 23:01:12 +08:00
|
|
|
case PDO_PARAM_ZVAL:
|
|
|
|
default:
|
2005-02-06 06:55:23 +08:00
|
|
|
break;
|
2005-01-23 07:31:19 +08:00
|
|
|
}
|
|
|
|
}
|
2004-05-20 10:27:49 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-26 01:24:55 +08:00
|
|
|
static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value TSRMLS_DC)
|
2005-01-23 22:50:06 +08:00
|
|
|
{
|
|
|
|
pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
|
|
|
|
PGresult *res;
|
|
|
|
char *q=NULL;
|
|
|
|
ExecStatusType status;
|
2014-03-09 18:43:13 +08:00
|
|
|
|
2005-01-23 22:50:06 +08:00
|
|
|
if (!S->result) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2014-03-09 18:43:13 +08:00
|
|
|
|
2005-01-23 22:50:06 +08:00
|
|
|
if (colno >= stmt->column_count) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2014-03-09 18:43:13 +08:00
|
|
|
|
2005-01-23 22:50:06 +08:00
|
|
|
array_init(return_value);
|
2014-08-26 01:24:55 +08:00
|
|
|
add_assoc_long(return_value, "pgsql:oid", S->cols[colno].pgsql_type);
|
2005-01-23 22:50:06 +08:00
|
|
|
|
|
|
|
/* Fetch metadata from Postgres system catalogue */
|
2013-07-11 11:23:11 +08:00
|
|
|
spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%u", S->cols[colno].pgsql_type);
|
2005-01-23 22:50:06 +08:00
|
|
|
res = PQexec(S->H->server, q);
|
|
|
|
efree(q);
|
2014-03-09 18:43:13 +08:00
|
|
|
|
2005-01-23 22:50:06 +08:00
|
|
|
status = PQresultStatus(res);
|
2014-03-09 18:43:13 +08:00
|
|
|
|
2005-01-23 22:50:06 +08:00
|
|
|
if (status != PGRES_TUPLES_OK) {
|
|
|
|
/* Failed to get system catalogue, but return success
|
|
|
|
* with the data we have collected so far
|
|
|
|
*/
|
2008-01-29 09:17:30 +08:00
|
|
|
goto done;
|
2005-01-23 22:50:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We want exactly one row returned */
|
|
|
|
if (1 != PQntuples(res)) {
|
2008-01-29 09:17:30 +08:00
|
|
|
goto done;
|
2005-01-23 22:50:06 +08:00
|
|
|
}
|
|
|
|
|
2014-04-15 19:40:40 +08:00
|
|
|
add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0));
|
2008-01-29 09:17:30 +08:00
|
|
|
done:
|
2014-03-09 18:43:13 +08:00
|
|
|
PQclear(res);
|
2005-01-23 22:50:06 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-07-09 12:54:04 +08:00
|
|
|
static int pdo_pgsql_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-05-20 10:27:49 +08:00
|
|
|
struct pdo_stmt_methods pgsql_stmt_methods = {
|
|
|
|
pgsql_stmt_dtor,
|
|
|
|
pgsql_stmt_execute,
|
|
|
|
pgsql_stmt_fetch,
|
|
|
|
pgsql_stmt_describe,
|
|
|
|
pgsql_stmt_get_col,
|
2005-01-23 22:50:06 +08:00
|
|
|
pgsql_stmt_param_hook,
|
|
|
|
NULL, /* set_attr */
|
|
|
|
NULL, /* get_attr */
|
|
|
|
pgsql_stmt_get_column_meta,
|
2005-07-09 12:54:04 +08:00
|
|
|
NULL, /* next_rowset */
|
|
|
|
pdo_pgsql_stmt_cursor_closer
|
2004-05-20 10:27:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
* vim600: noet sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: noet sw=4 ts=4
|
|
|
|
*/
|