diff options
Diffstat (limited to 'ext/pgsql/pgsql.c')
-rw-r--r-- | ext/pgsql/pgsql.c | 951 |
1 files changed, 700 insertions, 251 deletions
diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c index be915ae5ae..d867f433b2 100644 --- a/ext/pgsql/pgsql.c +++ b/ext/pgsql/pgsql.c @@ -19,7 +19,7 @@ | Chris Kings-Lynne <chriskl@php.net> (v3 protocol) | +----------------------------------------------------------------------+ */ - + /* $Id$ */ #include <stdlib.h> @@ -115,6 +115,10 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_pconnect, 0, 0, 1) ZEND_ARG_INFO(0, database) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_connect_poll, 0, 0, 0) + ZEND_ARG_INFO(0, connection) +ZEND_END_ARG_INFO() + #if HAVE_PQPARAMETERSTATUS ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_parameter_status, 0, 0, 1) ZEND_ARG_INFO(0, connection) @@ -370,6 +374,13 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_tell, 0, 0, 1) ZEND_ARG_INFO(0, large_object) ZEND_END_ARG_INFO() +#if HAVE_PG_LO_TRUNCATE +ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_truncate, 0, 0, 1) + ZEND_ARG_INFO(0, large_object) + ZEND_ARG_INFO(0, size) +ZEND_END_ARG_INFO() +#endif + #if HAVE_PQSETERRORVERBOSITY ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_set_error_verbosity, 0, 0, 0) ZEND_ARG_INFO(0, connection) @@ -519,6 +530,18 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_get_pid, 0, 0, 0) ZEND_ARG_INFO(0, connection) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_socket, 0, 0, 1) + ZEND_ARG_INFO(0, connection) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_consume_input, 0, 0, 1) + ZEND_ARG_INFO(0, connection) +ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_flush, 0, 0, 1) + ZEND_ARG_INFO(0, connection) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_meta_data, 0, 0, 2) ZEND_ARG_INFO(0, db) ZEND_ARG_INFO(0, table) @@ -567,6 +590,7 @@ const zend_function_entry pgsql_functions[] = { /* connection functions */ PHP_FE(pg_connect, arginfo_pg_connect) PHP_FE(pg_pconnect, arginfo_pg_pconnect) + PHP_FE(pg_connect_poll, arginfo_pg_connect_poll) PHP_FE(pg_close, arginfo_pg_close) PHP_FE(pg_connection_status, arginfo_pg_connection_status) PHP_FE(pg_connection_busy, arginfo_pg_connection_busy) @@ -636,6 +660,9 @@ const zend_function_entry pgsql_functions[] = { #endif /* async message function */ PHP_FE(pg_get_notify, arginfo_pg_get_notify) + PHP_FE(pg_socket, arginfo_pg_socket) + PHP_FE(pg_consume_input,arginfo_pg_consume_input) + PHP_FE(pg_flush, arginfo_pg_flush) PHP_FE(pg_get_pid, arginfo_pg_get_pid) /* error message functions */ PHP_FE(pg_result_error, arginfo_pg_result_error) @@ -664,6 +691,9 @@ const zend_function_entry pgsql_functions[] = { PHP_FE(pg_lo_export, arginfo_pg_lo_export) PHP_FE(pg_lo_seek, arginfo_pg_lo_seek) PHP_FE(pg_lo_tell, arginfo_pg_lo_tell) +#if HAVE_PG_LO_TRUNCATE + PHP_FE(pg_lo_truncate, arginfo_pg_lo_truncate) +#endif /* utility functions */ #if HAVE_PQESCAPE PHP_FE(pg_escape_string, arginfo_pg_escape_string) @@ -1085,6 +1115,7 @@ PHP_MINIT_FUNCTION(pgsql) #endif /* For connection option */ REGISTER_LONG_CONSTANT("PGSQL_CONNECT_FORCE_NEW", PGSQL_CONNECT_FORCE_NEW, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_CONNECT_ASYNC", PGSQL_CONNECT_ASYNC, CONST_CS | CONST_PERSISTENT); /* For pg_fetch_array() */ REGISTER_LONG_CONSTANT("PGSQL_ASSOC", PGSQL_ASSOC, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PGSQL_NUM", PGSQL_NUM, CONST_CS | CONST_PERSISTENT); @@ -1092,6 +1123,18 @@ PHP_MINIT_FUNCTION(pgsql) /* For pg_connection_status() */ REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_BAD", CONNECTION_BAD, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_OK", CONNECTION_OK, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_STARTED", CONNECTION_STARTED, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_MADE", CONNECTION_MADE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_AWAITING_RESPONSE", CONNECTION_AWAITING_RESPONSE, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_AUTH_OK", CONNECTION_AUTH_OK, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_SSL_STARTUP", CONNECTION_SSL_STARTUP, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_SETENV", CONNECTION_SETENV, CONST_CS | CONST_PERSISTENT); + /* For pg_connect_poll() */ + REGISTER_LONG_CONSTANT("PGSQL_POLLING_FAILED", PGRES_POLLING_FAILED, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_POLLING_READING", PGRES_POLLING_READING, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_POLLING_WRITING", PGRES_POLLING_WRITING, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_POLLING_OK", PGRES_POLLING_OK, CONST_CS | CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("PGSQL_POLLING_ACTIVE", PGRES_POLLING_ACTIVE, CONST_CS | CONST_PERSISTENT); #if HAVE_PGTRANSACTIONSTATUS /* For pg_transaction_status() */ REGISTER_LONG_CONSTANT("PGSQL_TRANSACTION_IDLE", PQTRANS_IDLE, CONST_CS | CONST_PERSISTENT); @@ -1146,6 +1189,7 @@ PHP_MINIT_FUNCTION(pgsql) REGISTER_LONG_CONSTANT("PGSQL_CONV_FORCE_NULL", PGSQL_CONV_FORCE_NULL, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PGSQL_CONV_IGNORE_NOT_NULL", PGSQL_CONV_IGNORE_NOT_NULL, CONST_CS | CONST_PERSISTENT); /* pg_insert/update/delete/select options */ + REGISTER_LONG_CONSTANT("PGSQL_DML_ESCAPE", PGSQL_DML_ESCAPE, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PGSQL_DML_NO_CONV", PGSQL_DML_NO_CONV, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PGSQL_DML_EXEC", PGSQL_DML_EXEC, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("PGSQL_DML_ASYNC", PGSQL_DML_ASYNC, CONST_CS | CONST_PERSISTENT); @@ -1390,17 +1434,35 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create new link. Too many open links (%ld)", PGG(num_links)); goto err; } - if (connstring) { - pgsql = PQconnectdb(connstring); + + /* Non-blocking connect */ + if (connect_type & PGSQL_CONNECT_ASYNC) { + if (connstring) { + pgsql = PQconnectStart(connstring); + if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) { + PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql); + if (pgsql) { + PQfinish(pgsql); + } + goto err; + } + } else { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connection string required for async connections"); + goto err; + } } else { - pgsql = PQsetdb(host,port,options,tty,dbname); - } - if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) { - PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql); - if (pgsql) { - PQfinish(pgsql); + if (connstring) { + pgsql = PQconnectdb(connstring); + } else { + pgsql = PQsetdb(host,port,options,tty,dbname); + } + if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) { + PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql); + if (pgsql) { + PQfinish(pgsql); + } + goto err; } - goto err; } /* add it to the list */ @@ -1452,6 +1514,31 @@ PHP_FUNCTION(pg_connect) } /* }}} */ +/* {{{ proto resource pg_connect_poll(resource connection) + Poll the status of an in-progress async PostgreSQL connection attempt*/ +PHP_FUNCTION(pg_connect_poll) +{ + zval *pgsql_link; + int id = -1; + PGconn *pgsql; + int ret; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_link) == FAILURE) { + return; + } + + if (pgsql_link == NULL && id == -1) { + RETURN_FALSE; + } + + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + + ret = PQconnectPoll(pgsql); + + RETURN_LONG(ret); +} +/* }}} */ + /* {{{ proto resource pg_pconnect(string connection_string | [string host, string port [, string options [, string tty,]]] string database) Open a persistent PostgreSQL connection */ PHP_FUNCTION(pg_pconnect) @@ -1557,7 +1644,29 @@ static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type add_assoc_long(return_value, "protocol", PQprotocolVersion(pgsql)); #if HAVE_PQPARAMETERSTATUS if (PQprotocolVersion(pgsql) >= 3) { + /* 8.0 or grater supports protorol version 3 */ + char *tmp; add_assoc_string(return_value, "server", (char*)PQparameterStatus(pgsql, "server_version"), 1); + tmp = (char*)PQparameterStatus(pgsql, "server_encoding"); + add_assoc_string(return_value, "server_encoding", tmp, 1); + tmp = (char*)PQparameterStatus(pgsql, "client_encoding"); + add_assoc_string(return_value, "client_encoding", tmp, 1); + tmp = (char*)PQparameterStatus(pgsql, "is_superuser"); + add_assoc_string(return_value, "is_superuser", tmp, 1); + tmp = (char*)PQparameterStatus(pgsql, "session_authorization"); + add_assoc_string(return_value, "session_authorization", tmp, 1); + tmp = (char*)PQparameterStatus(pgsql, "DateStyle"); + add_assoc_string(return_value, "DateStyle", tmp, 1); + tmp = (char*)PQparameterStatus(pgsql, "IntervalStyle"); + add_assoc_string(return_value, "IntervalStyle", tmp ? tmp : "", 1); + tmp = (char*)PQparameterStatus(pgsql, "TimeZone"); + add_assoc_string(return_value, "TimeZone", tmp ? tmp : "", 1); + tmp = (char*)PQparameterStatus(pgsql, "integer_datetimes"); + add_assoc_string(return_value, "integer_datetimes", tmp ? tmp : "", 1); + tmp = (char*)PQparameterStatus(pgsql, "standard_conforming_strings"); + add_assoc_string(return_value, "standard_conforming_strings", tmp ? tmp : "", 1); + tmp = (char*)PQparameterStatus(pgsql, "application_name"); + add_assoc_string(return_value, "application_name", tmp ? tmp : "", 1); } #endif #endif @@ -2684,33 +2793,22 @@ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, long result_type, fci.symbol_table = NULL; fci.object_ptr = return_value; fci.retval_ptr_ptr = &retval_ptr; + fci.params = NULL; + fci.param_count = 0; + fci.no_separation = 1; + if (ctor_params && Z_TYPE_P(ctor_params) != IS_NULL) { - if (Z_TYPE_P(ctor_params) == IS_ARRAY) { - HashTable *ht = Z_ARRVAL_P(ctor_params); - Bucket *p; - - fci.param_count = 0; - fci.params = safe_emalloc(sizeof(zval***), ht->nNumOfElements, 0); - p = ht->pListHead; - while (p != NULL) { - fci.params[fci.param_count++] = (zval**)p->pData; - p = p->pListNext; - } - } else { + if (zend_fcall_info_args(&fci, ctor_params TSRMLS_CC) == FAILURE) { /* Two problems why we throw exceptions here: PHP is typeless * and hence passing one argument that's not an array could be - * by mistake and the other way round is possible, too. The + * by mistake and the other way round is possible, too. The * single value is an array. Also we'd have to make that one * argument passed by reference. */ zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Parameter ctor_params must be an array", 0 TSRMLS_CC); return; } - } else { - fci.param_count = 0; - fci.params = NULL; } - fci.no_separation = 1; fcc.initialized = 1; fcc.function_handler = ce->constructor; @@ -3643,7 +3741,7 @@ PHP_FUNCTION(pg_lo_export) PHP_FUNCTION(pg_lo_seek) { zval *pgsql_id = NULL; - long offset = 0, whence = SEEK_CUR; + long result, offset = 0, whence = SEEK_CUR; pgLofp *pgsql; int argc = ZEND_NUM_ARGS(); @@ -3657,7 +3755,16 @@ PHP_FUNCTION(pg_lo_seek) ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp); - if (lo_lseek((PGconn *)pgsql->conn, pgsql->lofd, offset, whence) > -1) { +#if HAVE_PG_LO64 + if (PQserverVersion((PGconn *)pgsql->conn) >= 90300) { + result = lo_lseek64((PGconn *)pgsql->conn, pgsql->lofd, offset, whence); + } else { + result = lo_lseek((PGconn *)pgsql->conn, pgsql->lofd, offset, whence); + } +#else + result = lo_lseek((PGconn *)pgsql->conn, pgsql->lofd, offset, whence); +#endif + if (result > -1) { RETURN_TRUE; } else { RETURN_FALSE; @@ -3670,7 +3777,7 @@ PHP_FUNCTION(pg_lo_seek) PHP_FUNCTION(pg_lo_tell) { zval *pgsql_id = NULL; - int offset = 0; + long offset = 0; pgLofp *pgsql; int argc = ZEND_NUM_ARGS(); @@ -3680,11 +3787,54 @@ PHP_FUNCTION(pg_lo_tell) ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp); +#if HAVE_PG_LO64 + if (PQserverVersion((PGconn *)pgsql->conn) >= 90300) { + offset = lo_tell64((PGconn *)pgsql->conn, pgsql->lofd); + } else { + offset = lo_tell((PGconn *)pgsql->conn, pgsql->lofd); + } +#else offset = lo_tell((PGconn *)pgsql->conn, pgsql->lofd); +#endif RETURN_LONG(offset); } /* }}} */ +#if HAVE_PG_LO_TRUNCATE +/* {{{ proto bool pg_lo_truncate(resource large_object, int size) + Truncate large object to size */ +PHP_FUNCTION(pg_lo_truncate) +{ + zval *pgsql_id = NULL; + size_t size; + pgLofp *pgsql; + int argc = ZEND_NUM_ARGS(); + int result; + + if (zend_parse_parameters(argc TSRMLS_CC, "rl", &pgsql_id, &size) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp); + +#if HAVE_PG_LO64 + if (PQserverVersion((PGconn *)pgsql->conn) >= 90300) { + result = lo_truncate64((PGconn *)pgsql->conn, pgsql->lofd, size); + } else { + result = lo_truncate((PGconn *)pgsql->conn, pgsql->lofd, size); + } +#else + result = lo_truncate((PGconn *)pgsql->conn, pgsql->lofd, size); +#endif + if (!result) { + RETURN_TRUE; + } else { + RETURN_FALSE; + } +} +/* }}} */ +#endif + #if HAVE_PQSETERRORVERBOSITY /* {{{ proto int pg_set_error_verbosity([resource connection,] int verbosity) Set error verbosity */ @@ -4639,6 +4789,16 @@ PHP_FUNCTION(pg_connection_busy) } /* }}} */ +static int _php_pgsql_link_has_results(PGconn *pgsql) +{ + PGresult *result; + while ((result = PQgetResult(pgsql))) { + PQclear(result); + return 1; + } + return 0; +} + /* {{{ proto bool pg_send_query(resource connection, string query) Send asynchronous query */ PHP_FUNCTION(pg_send_query) @@ -4648,48 +4808,63 @@ PHP_FUNCTION(pg_send_query) int len; int id = -1; PGconn *pgsql; - PGresult *res; - int leftover = 0; + int is_non_blocking; int ret; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", - &pgsql_link, &query, &len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pgsql_link, &query, &len) == FAILURE) { return; } ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); - if (PQ_SETNONBLOCKING(pgsql, 1)) { + is_non_blocking = PQisnonblocking(pgsql); + + if (is_non_blocking == 0 && PQ_SETNONBLOCKING(pgsql, 1) == -1) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode"); RETURN_FALSE; } - while ((res = PQgetResult(pgsql))) { - PQclear(res); - leftover = 1; - } - if (leftover) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "There are results on this connection. Call pg_get_result() until it returns FALSE"); + + if (_php_pgsql_link_has_results(pgsql)) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, + "There are results on this connection. Call pg_get_result() until it returns FALSE"); } - if (!PQsendQuery(pgsql, query)) { - if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) { - PQreset(pgsql); - } + + if (is_non_blocking) { if (!PQsendQuery(pgsql, query)) { RETURN_FALSE; } - } - /* Wait to finish sending buffer */ - while ((ret = PQflush(pgsql))) { - if (ret == -1) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not empty PostgreSQL send buffer"); - break; + ret = PQflush(pgsql); + } else { + if (!PQsendQuery(pgsql, query)) { + if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) { + PQreset(pgsql); + } + if (!PQsendQuery(pgsql, query)) { + RETURN_FALSE; + } + } + + /* Wait to finish sending buffer */ + while ((ret = PQflush(pgsql))) { + if (ret == -1) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not empty PostgreSQL send buffer"); + break; + } + usleep(10000); + } + + if (PQ_SETNONBLOCKING(pgsql, 0)) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode"); } - usleep(10000); } - if (PQ_SETNONBLOCKING(pgsql, 0)) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode"); + + if (ret == 0) { + RETURN_TRUE; + } else if (ret == -1) { + RETURN_FALSE; + } else { + RETURN_LONG(0); } - RETURN_TRUE; } /* }}} */ @@ -4704,8 +4879,7 @@ PHP_FUNCTION(pg_send_query_params) char *query; int query_len, id = -1; PGconn *pgsql; - PGresult *res; - int leftover = 0; + int is_non_blocking; int ret; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsa/", &pgsql_link, &query, &query_len, &pv_param_arr) == FAILURE) { @@ -4718,16 +4892,16 @@ PHP_FUNCTION(pg_send_query_params) ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); - if (PQ_SETNONBLOCKING(pgsql, 1)) { + is_non_blocking = PQisnonblocking(pgsql); + + if (is_non_blocking == 0 && PQ_SETNONBLOCKING(pgsql, 1) == -1) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode"); RETURN_FALSE; } - while ((res = PQgetResult(pgsql))) { - PQclear(res); - leftover = 1; - } - if (leftover) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "There are results on this connection. Call pg_get_result() until it returns FALSE"); + + if (_php_pgsql_link_has_results(pgsql)) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, + "There are results on this connection. Call pg_get_result() until it returns FALSE"); } zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr)); @@ -4763,7 +4937,12 @@ PHP_FUNCTION(pg_send_query_params) } } - if (!PQsendQueryParams(pgsql, query, num_params, NULL, (const char * const *)params, NULL, NULL, 0)) { + if (PQsendQueryParams(pgsql, query, num_params, NULL, (const char * const *)params, NULL, NULL, 0)) { + _php_pgsql_free_params(params, num_params); + } else if (is_non_blocking) { + _php_pgsql_free_params(params, num_params); + RETURN_FALSE; + } else { if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) { PQreset(pgsql); } @@ -4772,19 +4951,31 @@ PHP_FUNCTION(pg_send_query_params) RETURN_FALSE; } } - _php_pgsql_free_params(params, num_params); - /* Wait to finish sending buffer */ - while ((ret = PQflush(pgsql))) { - if (ret == -1) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not empty PostgreSQL send buffer"); - break; + + if (is_non_blocking) { + ret = PQflush(pgsql); + } else { + /* Wait to finish sending buffer */ + while ((ret = PQflush(pgsql))) { + if (ret == -1) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not empty PostgreSQL send buffer"); + break; + } + usleep(10000); + } + + if (PQ_SETNONBLOCKING(pgsql, 0) != 0) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode"); } - usleep(10000); } - if (PQ_SETNONBLOCKING(pgsql, 0)) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode"); + + if (ret == 0) { + RETURN_TRUE; + } else if (ret == -1) { + RETURN_FALSE; + } else { + RETURN_LONG(0); } - RETURN_TRUE; } /* }}} */ #endif @@ -4798,8 +4989,7 @@ PHP_FUNCTION(pg_send_prepare) char *query, *stmtname; int stmtname_len, query_len, id = -1; PGconn *pgsql; - PGresult *res; - int leftover = 0; + int is_non_blocking; int ret; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &pgsql_link, &stmtname, &stmtname_len, &query, &query_len) == FAILURE) { @@ -4812,37 +5002,54 @@ PHP_FUNCTION(pg_send_prepare) ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); - if (PQ_SETNONBLOCKING(pgsql, 1)) { + is_non_blocking = PQisnonblocking(pgsql); + + if (is_non_blocking == 0 && PQ_SETNONBLOCKING(pgsql, 1) == -1) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode"); RETURN_FALSE; } - while ((res = PQgetResult(pgsql))) { - PQclear(res); - leftover = 1; - } - if (leftover) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "There are results on this connection. Call pg_get_result() until it returns FALSE"); + + if (_php_pgsql_link_has_results(pgsql)) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, + "There are results on this connection. Call pg_get_result() until it returns FALSE"); } + if (!PQsendPrepare(pgsql, stmtname, query, 0, NULL)) { - if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) { - PQreset(pgsql); - } - if (!PQsendPrepare(pgsql, stmtname, query, 0, NULL)) { + if (is_non_blocking) { RETURN_FALSE; + } else { + if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) { + PQreset(pgsql); + } + if (!PQsendPrepare(pgsql, stmtname, query, 0, NULL)) { + RETURN_FALSE; + } } } - /* Wait to finish sending buffer */ - while ((ret = PQflush(pgsql))) { - if (ret == -1) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not empty postgres send buffer"); - break; + + if (is_non_blocking) { + ret = PQflush(pgsql); + } else { + /* Wait to finish sending buffer */ + while ((ret = PQflush(pgsql))) { + if (ret == -1) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not empty PostgreSQL send buffer"); + break; + } + usleep(10000); + } + if (PQ_SETNONBLOCKING(pgsql, 0) != 0) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode"); } - usleep(10000); } - if (PQ_SETNONBLOCKING(pgsql, 0)) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode"); + + if (ret == 0) { + RETURN_TRUE; + } else if (ret == -1) { + RETURN_FALSE; + } else { + RETURN_LONG(0); } - RETURN_TRUE; } /* }}} */ #endif @@ -4859,8 +5066,7 @@ PHP_FUNCTION(pg_send_execute) char *stmtname; int stmtname_len, id = -1; PGconn *pgsql; - PGresult *res; - int leftover = 0; + int is_non_blocking; int ret; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsa", &pgsql_link, &stmtname, &stmtname_len, &pv_param_arr) == FAILURE) { @@ -4873,16 +5079,16 @@ PHP_FUNCTION(pg_send_execute) ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); - if (PQ_SETNONBLOCKING(pgsql, 1)) { + is_non_blocking = PQisnonblocking(pgsql); + + if (is_non_blocking == 0 && PQ_SETNONBLOCKING(pgsql, 1) == -1) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode"); RETURN_FALSE; } - while ((res = PQgetResult(pgsql))) { - PQclear(res); - leftover = 1; - } - if (leftover) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "There are results on this connection. Call pg_get_result() until it returns FALSE"); + + if (_php_pgsql_link_has_results(pgsql)) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, + "There are results on this connection. Call pg_get_result() until it returns FALSE"); } zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr)); @@ -4890,8 +5096,8 @@ PHP_FUNCTION(pg_send_execute) if (num_params > 0) { int i = 0; params = (char **)safe_emalloc(sizeof(char *), num_params, 0); - - for(i = 0; i < num_params; i++) { + + for (i = 0; i < num_params; i++) { if (zend_hash_get_current_data(Z_ARRVAL_P(pv_param_arr), (void **) &tmp) == FAILURE) { php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error getting parameter"); _php_pgsql_free_params(params, num_params); @@ -4918,7 +5124,12 @@ PHP_FUNCTION(pg_send_execute) } } - if (!PQsendQueryPrepared(pgsql, stmtname, num_params, (const char * const *)params, NULL, NULL, 0)) { + if (PQsendQueryPrepared(pgsql, stmtname, num_params, (const char * const *)params, NULL, NULL, 0)) { + _php_pgsql_free_params(params, num_params); + } else if (is_non_blocking) { + _php_pgsql_free_params(params, num_params); + RETURN_FALSE; + } else { if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) { PQreset(pgsql); } @@ -4927,19 +5138,30 @@ PHP_FUNCTION(pg_send_execute) RETURN_FALSE; } } - _php_pgsql_free_params(params, num_params); - /* Wait to finish sending buffer */ - while ((ret = PQflush(pgsql))) { - if (ret == -1) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not empty postgres send buffer"); - break; + + if (is_non_blocking) { + ret = PQflush(pgsql); + } else { + /* Wait to finish sending buffer */ + while ((ret = PQflush(pgsql))) { + if (ret == -1) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not empty PostgreSQL send buffer"); + break; + } + usleep(10000); + } + if (PQ_SETNONBLOCKING(pgsql, 0) != 0) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode"); } - usleep(10000); } - if (PQ_SETNONBLOCKING(pgsql, 0)) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode"); + + if (ret == 0) { + RETURN_TRUE; + } else if (ret == -1) { + RETURN_FALSE; + } else { + RETURN_LONG(0); } - RETURN_TRUE; } /* }}} */ #endif @@ -5084,10 +5306,145 @@ PHP_FUNCTION(pg_get_pid) } /* }}} */ +static size_t php_pgsql_fd_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC) +{ + return 0; +} + +static size_t php_pgsql_fd_read(php_stream *stream, char *buf, size_t count TSRMLS_DC) +{ + return 0; +} + +static int php_pgsql_fd_close(php_stream *stream, int close_handle TSRMLS_DC) +{ + return EOF; +} + +static int php_pgsql_fd_flush(php_stream *stream TSRMLS_DC) +{ + return FAILURE; +} + +static int php_pgsql_fd_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC) +{ + PGconn *pgsql = (PGconn *) stream->abstract; + switch (option) { + case PHP_STREAM_OPTION_BLOCKING: + return PQ_SETNONBLOCKING(pgsql, value); + default: + return FAILURE; + } +} + +static int php_pgsql_fd_cast(php_stream *stream, int cast_as, void **ret TSRMLS_DC) +{ + PGconn *pgsql = (PGconn *) stream->abstract; + int fd_number; + + switch (cast_as) { + case PHP_STREAM_AS_FD_FOR_SELECT: + case PHP_STREAM_AS_FD: + case PHP_STREAM_AS_SOCKETD: + if (ret) { + fd_number = PQsocket(pgsql); + if (fd_number == -1) { + return FAILURE; + } + + *(php_socket_t *)ret = fd_number; + return SUCCESS; + } + default: + return FAILURE; + } +} + +/* {{{ proto resource pg_socket(resource) + Get a read-only handle to the socket underlying the pgsql connection */ +PHP_FUNCTION(pg_socket) +{ + zval *pgsql_link; + php_stream *stream; + PGconn *pgsql; + int id = -1; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_link) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + + stream = php_stream_alloc(&php_stream_pgsql_fd_ops, pgsql, NULL, "r"); + + if (stream) { + php_stream_to_zval(stream, return_value); + return; + } + + RETURN_FALSE; +} +/* }}} */ + +/* {{{ proto bool pg_consume_input(resource) + Reads input on the connection */ +PHP_FUNCTION(pg_consume_input) +{ + zval *pgsql_link; + int id = -1; + PGconn *pgsql; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_link) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + + RETURN_BOOL(PQconsumeInput(pgsql)); +} +/* }}} */ + +/* {{{ proto mixed pg_flush(resource) + Flush outbound query data on the connection */ +PHP_FUNCTION(pg_flush) +{ + zval *pgsql_link; + int id = -1; + PGconn *pgsql; + int ret; + int is_non_blocking; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_link) == FAILURE) { + return; + } + + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + + is_non_blocking = PQisnonblocking(pgsql); + + if (is_non_blocking == 0 && PQ_SETNONBLOCKING(pgsql, 1) == -1) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode"); + RETURN_FALSE; + } + + ret = PQflush(pgsql); + + if (is_non_blocking == 0 && PQ_SETNONBLOCKING(pgsql, 0) == -1) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Failed resetting connection to blocking mode"); + } + + switch (ret) { + case 0: RETURN_TRUE; break; + case 1: RETURN_LONG(0); break; + default: RETURN_FALSE; + } +} +/* }}} */ + /* {{{ php_pgsql_meta_data * TODO: Add meta_data cache for better performance */ -PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, zval *meta TSRMLS_DC) +PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, zval *meta, zend_bool extended TSRMLS_DC) { PGresult *pg_result; char *src, *tmp_name, *tmp_name2 = NULL; @@ -5111,10 +5468,25 @@ PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, z tmp_name = "public"; } - smart_str_appends(&querystr, - "SELECT a.attname, a.attnum, t.typname, a.attlen, a.attnotnull, a.atthasdef, a.attndims, t.typtype = 'e' " - "FROM pg_class as c, pg_attribute a, pg_type t, pg_namespace n " - "WHERE a.attnum > 0 AND a.attrelid = c.oid AND c.relname = '"); + if (extended) { + smart_str_appends(&querystr, + "SELECT a.attname, a.attnum, t.typname, a.attlen, a.attnotNULL, a.atthasdef, a.attndims, t.typtype, " + "d.description " + "FROM pg_class as c " + " JOIN pg_attribute a ON (a.attrelid = c.oid) " + " JOIN pg_type t ON (a.atttypid = t.oid) " + " JOIN pg_namespace n ON (c.relnamespace = n.oid) " + " LEFT JOIN pg_description d ON (d.objoid=a.attrelid AND d.objsubid=a.attnum AND c.oid=d.objoid) " + "WHERE a.attnum > 0 AND c.relname = '"); + } else { + smart_str_appends(&querystr, + "SELECT a.attname, a.attnum, t.typname, a.attlen, a.attnotnull, a.atthasdef, a.attndims, t.typtype " + "FROM pg_class as c " + " JOIN pg_attribute a ON (a.attrelid = c.oid) " + " JOIN pg_type t ON (a.atttypid = t.oid) " + " JOIN pg_namespace n ON (c.relnamespace = n.oid) " + "WHERE a.attnum > 0 AND c.relname = '"); + } escaped = (char *)safe_emalloc(strlen(tmp_name2), 2, 1); new_len = PQescapeStringConn(pg_link, escaped, tmp_name2, strlen(tmp_name2), NULL); if (new_len) { @@ -5122,7 +5494,7 @@ PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, z } efree(escaped); - smart_str_appends(&querystr, "' AND c.relnamespace = n.oid AND n.nspname = '"); + smart_str_appends(&querystr, "' AND n.nspname = '"); escaped = (char *)safe_emalloc(strlen(tmp_name), 2, 1); new_len = PQescapeStringConn(pg_link, escaped, tmp_name, strlen(tmp_name), NULL); if (new_len) { @@ -5130,7 +5502,7 @@ PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, z } efree(escaped); - smart_str_appends(&querystr, "' AND a.atttypid = t.oid ORDER BY a.attnum;"); + smart_str_appends(&querystr, "' ORDER BY a.attnum;"); smart_str_0(&querystr); efree(src); @@ -5147,28 +5519,29 @@ PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, z char *name; MAKE_STD_ZVAL(elem); array_init(elem); + /* pg_attribute.attnum */ add_assoc_long(elem, "num", atoi(PQgetvalue(pg_result,i,1))); + /* pg_type.typname */ add_assoc_string(elem, "type", PQgetvalue(pg_result,i,2), 1); + /* pg_attribute.attlen */ add_assoc_long(elem, "len", atoi(PQgetvalue(pg_result,i,3))); - if (!strcmp(PQgetvalue(pg_result,i,4), "t")) { - add_assoc_bool(elem, "not null", 1); - } - else { - add_assoc_bool(elem, "not null", 0); - } - if (!strcmp(PQgetvalue(pg_result,i,5), "t")) { - add_assoc_bool(elem, "has default", 1); - } - else { - add_assoc_bool(elem, "has default", 0); - } + /* pg_attribute.attnonull */ + add_assoc_bool(elem, "not null", !strcmp(PQgetvalue(pg_result,i,4), "t")); + /* pg_attribute.atthasdef */ + add_assoc_bool(elem, "has default", !strcmp(PQgetvalue(pg_result,i,5), "t")); + /* pg_attribute.attndims */ add_assoc_long(elem, "array dims", atoi(PQgetvalue(pg_result,i,6))); - if (!strcmp(PQgetvalue(pg_result,i,7), "t")) { - add_assoc_bool(elem, "is enum", 1); - } - else { - add_assoc_bool(elem, "is enum", 0); - } + /* pg_type.typtype */ + add_assoc_bool(elem, "is enum", !strcmp(PQgetvalue(pg_result,i,7), "e")); + if (extended) { + /* pg_type.typtype */ + add_assoc_bool(elem, "is base", !strcmp(PQgetvalue(pg_result,i,7), "b")); + add_assoc_bool(elem, "is composite", !strcmp(PQgetvalue(pg_result,i,7), "c")); + add_assoc_bool(elem, "is pesudo", !strcmp(PQgetvalue(pg_result,i,7), "p")); + /* pg_description.description */ + add_assoc_string(elem, "description", PQgetvalue(pg_result,i,8), 1); + } + /* pg_attribute.attname */ name = PQgetvalue(pg_result,i,0); add_assoc_zval(meta, name, elem); } @@ -5180,39 +5553,29 @@ PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, z /* }}} */ -/* {{{ proto array pg_meta_data(resource db, string table) +/* {{{ proto array pg_meta_data(resource db, string table [, bool extended]) Get meta_data */ PHP_FUNCTION(pg_meta_data) { zval *pgsql_link; char *table_name; uint table_name_len; + zend_bool extended=0; PGconn *pgsql; int id = -1; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", - &pgsql_link, &table_name, &table_name_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|b", + &pgsql_link, &table_name, &table_name_len, &extended) == FAILURE) { return; } ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); - + array_init(return_value); - if (php_pgsql_meta_data(pgsql, table_name, return_value TSRMLS_CC) == FAILURE) { + if (php_pgsql_meta_data(pgsql, table_name, return_value, extended TSRMLS_CC) == FAILURE) { zval_dtor(return_value); /* destroy array */ RETURN_FALSE; } - else { - HashPosition pos; - zval **val; - - for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(return_value), &pos); - zend_hash_get_current_data_ex(Z_ARRVAL_P(return_value), (void **)&val, &pos) == SUCCESS; - zend_hash_move_forward_ex(Z_ARRVAL_P(return_value), &pos)) { - /* delete newly added entry, in order to keep BC */ - zend_hash_del_key_or_index(Z_ARRVAL_PP(val), "is enum", sizeof("is enum"), 0, HASH_DEL_KEY); - } - } } /* }}} */ @@ -5423,7 +5786,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con array_init(meta); /* table_name is escaped by php_pgsql_meta_data */ - if (php_pgsql_meta_data(pg_link, table_name, meta TSRMLS_CC) == FAILURE) { + if (php_pgsql_meta_data(pg_link, table_name, meta, 0 TSRMLS_CC) == FAILURE) { zval_dtor(meta); FREE_ZVAL(meta); return FAILURE; @@ -5466,9 +5829,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected broken meta data. Missing 'is enum'"); err = 1; } - if (!err && (Z_TYPE_PP(val) == IS_ARRAY || - Z_TYPE_PP(val) == IS_OBJECT || - Z_TYPE_PP(val) == IS_CONSTANT_ARRAY)) { + if (!err && (Z_TYPE_PP(val) == IS_ARRAY || Z_TYPE_PP(val) == IS_OBJECT)) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects scalar values as field values"); err = 1; } @@ -5499,14 +5860,14 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con !strcmp(Z_STRVAL_PP(val), "true") || !strcmp(Z_STRVAL_PP(val), "True") || !strcmp(Z_STRVAL_PP(val), "yes") || !strcmp(Z_STRVAL_PP(val), "Yes") || !strcmp(Z_STRVAL_PP(val), "1")) { - ZVAL_STRING(new_val, "'t'", 1); + ZVAL_STRINGL(new_val, "'t'", sizeof("'t'")-1, 1); } else if (!strcmp(Z_STRVAL_PP(val), "f") || !strcmp(Z_STRVAL_PP(val), "F") || !strcmp(Z_STRVAL_PP(val), "n") || !strcmp(Z_STRVAL_PP(val), "N") || !strcmp(Z_STRVAL_PP(val), "false") || !strcmp(Z_STRVAL_PP(val), "False") || !strcmp(Z_STRVAL_PP(val), "no") || !strcmp(Z_STRVAL_PP(val), "No") || !strcmp(Z_STRVAL_PP(val), "0")) { - ZVAL_STRING(new_val, "'f'", 1); + ZVAL_STRINGL(new_val, "'f'", sizeof("'f'")-1, 1); } else { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected invalid value (%s) for PostgreSQL %s field (%s)", Z_STRVAL_PP(val), Z_STRVAL_PP(type), field); @@ -5518,15 +5879,15 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con case IS_LONG: case IS_BOOL: if (Z_LVAL_PP(val)) { - ZVAL_STRING(new_val, "'t'", 1); + ZVAL_STRINGL(new_val, "'t'", sizeof("'t'")-1, 1); } else { - ZVAL_STRING(new_val, "'f'", 1); + ZVAL_STRINGL(new_val, "'f'", sizeof("'f'")-1, 1); } break; case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); break; default: @@ -5545,7 +5906,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con switch (Z_TYPE_PP(val)) { case IS_STRING: if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); } else { /* FIXME: better regex must be used */ @@ -5553,7 +5914,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con err = 1; } else { - ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1); + ZVAL_STRINGL(new_val, Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1); } } break; @@ -5568,7 +5929,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con break; case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); break; default: @@ -5587,11 +5948,11 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con switch (Z_TYPE_PP(val)) { case IS_STRING: if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); } else { - /* FIXME: better regex must be used */ - if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val), "^([+-]{0,1}[0-9]+)|([+-]{0,1}[0-9]*[\\.][0-9]+)|([+-]{0,1}[0-9]+[\\.][0-9]*)$", 0 TSRMLS_CC) == FAILURE) { + /* better regex? */ + if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val), "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$", 0 TSRMLS_CC) == FAILURE) { err = 1; } else { @@ -5609,7 +5970,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con break; case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); break; default: @@ -5621,22 +5982,40 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con } break; + /* Exotic types are handled as string also. + Please feel free to add more valitions. Invalid query fails + at execution anyway. */ case PG_TEXT: case PG_CHAR: case PG_VARCHAR: + /* bit */ + case PG_BIT: + case PG_VARBIT: + /* geometric */ + case PG_LINE: + case PG_LSEG: + case PG_POINT: + case PG_BOX: + case PG_PATH: + case PG_POLYGON: + case PG_CIRCLE: + /* unknown. JSON, Array etc */ + case PG_UNKNOWN: switch (Z_TYPE_PP(val)) { case IS_STRING: if (Z_STRLEN_PP(val) == 0) { if (opt & PGSQL_CONV_FORCE_NULL) { - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); } else { - ZVAL_STRING(new_val, "''", 1); + ZVAL_STRINGL(new_val, "''", sizeof("''")-1, 1); } } else { char *tmp; + /* PostgreSQL ignores \0 */ Z_TYPE_P(new_val) = IS_STRING; tmp = (char *)safe_emalloc(Z_STRLEN_PP(val), 2, 1); + /* better to use PGSQLescapeLiteral since PGescapeStringConn does not handle special \ */ Z_STRLEN_P(new_val) = (int)PQescapeStringConn(pg_link, tmp, Z_STRVAL_PP(val), Z_STRLEN_PP(val), NULL); Z_STRVAL_P(new_val) = tmp; php_pgsql_add_quotes(new_val, 1 TSRMLS_CC); @@ -5654,7 +6033,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con break; case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); break; default: @@ -5672,15 +6051,15 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con switch (Z_TYPE_PP(val)) { case IS_STRING: if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); } else { - /* FIXME: Better regex must be used */ + /* better regex? */ if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val), "^[0-9]+$", 0 TSRMLS_CC) == FAILURE) { err = 1; } else { - ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1); + ZVAL_STRINGL(new_val, Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1); convert_to_long_ex(&new_val); } } @@ -5696,7 +6075,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con break; case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); break; default: @@ -5713,22 +6092,22 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con switch (Z_TYPE_PP(val)) { case IS_STRING: if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); } else { - /* FIXME: Better regex must be used */ - if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val), "^([0-9]{1,3}\\.){3}[0-9]{1,3}(/[0-9]{1,2}){0,1}$", 0 TSRMLS_CC) == FAILURE) { + /* better regex? IPV6 and IPV4 */ + if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val), "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$", 0 TSRMLS_CC) == FAILURE) { err = 1; } else { - ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1); + ZVAL_STRINGL(new_val, Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1); php_pgsql_add_quotes(new_val, 1 TSRMLS_CC); } } break; case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); break; default: @@ -5750,7 +6129,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con } else if (!strcasecmp(Z_STRVAL_PP(val), "now()")) { ZVAL_STRINGL(new_val, "NOW()", sizeof("NOW()")-1, 1); } else { - /* FIXME: better regex must be used */ + /* better regex? */ if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val), "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})([ \\t]+(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1}(\\.[0-9]+){0,1}([ \\t]*([+-][0-9]{1,4}(:[0-9]{1,2}){0,1}|[-a-zA-Z_/+]{1,50})){0,1})){0,1}$", 1 TSRMLS_CC) == FAILURE) { err = 1; } else { @@ -5777,7 +6156,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con switch(Z_TYPE_PP(val)) { case IS_STRING: if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); } else { /* FIXME: better regex must be used */ @@ -5785,14 +6164,14 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con err = 1; } else { - ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1); + ZVAL_STRINGL(new_val, Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1); php_pgsql_add_quotes(new_val, 1 TSRMLS_CC); } } break; case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); break; default: @@ -5808,7 +6187,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con switch(Z_TYPE_PP(val)) { case IS_STRING: if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); } else { /* FIXME: better regex must be used */ @@ -5816,14 +6195,14 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con err = 1; } else { - ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1); + ZVAL_STRINGL(new_val, Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1); php_pgsql_add_quotes(new_val, 1 TSRMLS_CC); } } break; case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); break; default: @@ -5954,7 +6333,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con break; case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); break; default: @@ -5971,21 +6350,21 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con switch(Z_TYPE_PP(val)) { case IS_STRING: if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); } else { if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val), "^([0-9a-f]{2,2}:){5,5}[0-9a-f]{2,2}$", 1 TSRMLS_CC) == FAILURE) { err = 1; } else { - ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1); + ZVAL_STRINGL(new_val, Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1); php_pgsql_add_quotes(new_val, 1 TSRMLS_CC); } } break; case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); break; default: @@ -5997,24 +6376,9 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con } break; - /* bit */ - case PG_BIT: - case PG_VARBIT: - /* geometric */ - case PG_LINE: - case PG_LSEG: - case PG_POINT: - case PG_BOX: - case PG_PATH: - case PG_POLYGON: - case PG_CIRCLE: - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "PostgreSQL '%s' type (%s) is not supported", Z_STRVAL_PP(type), field); - err = 1; - break; - - case PG_UNKNOWN: default: - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unknown or system data type '%s' for '%s'", Z_STRVAL_PP(type), field); + /* should not happen */ + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unknown or system data type '%s' for '%s'. Report error", Z_STRVAL_PP(type), field); err = 1; break; } /* switch */ @@ -6148,7 +6512,7 @@ PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *var { zval **val, *converted = NULL; char buf[256]; - char *fld; + char *fld, *tmp; smart_str querystr = {0}; int key_type, ret = FAILURE; uint fld_len; @@ -6168,7 +6532,7 @@ PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *var } /* convert input array if needed */ - if (!(opt & PGSQL_DML_NO_CONV)) { + if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) { MAKE_STD_ZVAL(converted); array_init(converted); if (php_pgsql_convert(pg_link, table, var_array, converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) { @@ -6188,7 +6552,13 @@ PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *var php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects associative array for values to be inserted"); goto cleanup; } - smart_str_appendl(&querystr, fld, fld_len - 1); + if (opt & PGSQL_DML_ESCAPE) { + tmp = PGSQLescapeIdentifier(pg_link, fld, fld_len); + smart_str_appends(&querystr, tmp); + PGSQLfree(tmp); + } else { + smart_str_appendl(&querystr, fld, fld_len - 1); + } smart_str_appendc(&querystr, ','); zend_hash_move_forward_ex(Z_ARRVAL_P(var_array), &pos); } @@ -6203,7 +6573,18 @@ PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *var /* we can avoid the key_type check here, because we tested it in the other loop */ switch(Z_TYPE_PP(val)) { case IS_STRING: - smart_str_appendl(&querystr, Z_STRVAL_PP(val), Z_STRLEN_PP(val)); + if (opt & PGSQL_DML_ESCAPE) { + size_t new_len; + char *tmp; + tmp = (char *)safe_emalloc(Z_STRLEN_PP(val), 2, 1); + new_len = PQescapeStringConn(pg_link, tmp, Z_STRVAL_PP(val), Z_STRLEN_PP(val), NULL); + smart_str_appendc(&querystr, '\''); + smart_str_appendl(&querystr, tmp, new_len); + smart_str_appendc(&querystr, '\''); + efree(tmp); + } else { + smart_str_appendl(&querystr, Z_STRVAL_PP(val), Z_STRLEN_PP(val)); + } break; case IS_LONG: smart_str_append_long(&querystr, Z_LVAL_PP(val)); @@ -6211,9 +6592,11 @@ PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *var case IS_DOUBLE: smart_str_appendl(&querystr, buf, snprintf(buf, sizeof(buf), "%F", Z_DVAL_PP(val))); break; + case IS_NULL: + smart_str_appendl(&querystr, "NULL", sizeof("NULL")-1); + break; default: - /* should not happen */ - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Report this error to php-dev@lists.php.net, type = %d", Z_TYPE_PP(val)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expects scaler values. type = %d", Z_TYPE_PP(val)); goto cleanup; break; } @@ -6257,15 +6640,18 @@ PHP_FUNCTION(pg_insert) zval *pgsql_link, *values; char *table, *sql = NULL; int table_len; - ulong option = PGSQL_DML_EXEC; + ulong option = PGSQL_DML_EXEC, return_sql; PGconn *pg_link; + PGresult *pg_result; + ExecStatusType status; + pgsql_result_handle *pgsql_handle; int id = -1, argc = ZEND_NUM_ARGS(); if (zend_parse_parameters(argc TSRMLS_CC, "rsa|l", &pgsql_link, &table, &table_len, &values, &option) == FAILURE) { return; } - if (option & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING)) { + if (option & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid option is specified"); RETURN_FALSE; } @@ -6275,23 +6661,68 @@ PHP_FUNCTION(pg_insert) if (php_pgsql_flush_query(pg_link TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected unhandled result(s) in connection"); } - if (php_pgsql_insert(pg_link, table, values, option, &sql TSRMLS_CC) == FAILURE) { + return_sql = option & PGSQL_DML_STRING; + if (option & PGSQL_DML_EXEC) { + /* return resource when executed */ + option = option & ~PGSQL_DML_EXEC; + if (php_pgsql_insert(pg_link, table, values, option|PGSQL_DML_STRING, &sql TSRMLS_CC) == FAILURE) { + RETURN_FALSE; + } + pg_result = PQexec(pg_link, sql); + if ((PGG(auto_reset_persistent) & 2) && PQstatus(pg_link) != CONNECTION_OK) { + PQclear(pg_result); + PQreset(pg_link); + pg_result = PQexec(pg_link, sql); + } + efree(sql); + + if (pg_result) { + status = PQresultStatus(pg_result); + } else { + status = (ExecStatusType) PQstatus(pg_link); + } + + switch (status) { + case PGRES_EMPTY_QUERY: + case PGRES_BAD_RESPONSE: + case PGRES_NONFATAL_ERROR: + case PGRES_FATAL_ERROR: + PHP_PQ_ERROR("Query failed: %s", pg_link); + PQclear(pg_result); + RETURN_FALSE; + break; + case PGRES_COMMAND_OK: /* successful command that did not return rows */ + default: + if (pg_result) { + pgsql_handle = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle)); + pgsql_handle->conn = pg_link; + pgsql_handle->result = pg_result; + pgsql_handle->row = 0; + ZEND_REGISTER_RESOURCE(return_value, pgsql_handle, le_result); + return; + } else { + PQclear(pg_result); + RETURN_FALSE; + } + break; + } + } else if (php_pgsql_insert(pg_link, table, values, option, &sql TSRMLS_CC) == FAILURE) { RETURN_FALSE; } - if (option & PGSQL_DML_STRING) { + if (return_sql) { RETURN_STRING(sql, 0); } RETURN_TRUE; } /* }}} */ -static inline int build_assignment_string(smart_str *querystr, HashTable *ht, int where_cond, const char *pad, int pad_len TSRMLS_DC) +static inline int build_assignment_string(PGconn *pg_link, smart_str *querystr, HashTable *ht, int where_cond, const char *pad, int pad_len, ulong opt TSRMLS_DC) { HashPosition pos; uint fld_len; int key_type; ulong num_idx; - char *fld; + char *fld, *tmp; char buf[256]; zval **val; @@ -6303,8 +6734,14 @@ static inline int build_assignment_string(smart_str *querystr, HashTable *ht, in php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects associative array for values to be inserted"); return -1; } - smart_str_appendl(querystr, fld, fld_len - 1); - if (where_cond && Z_TYPE_PP(val) == IS_STRING && !strcmp(Z_STRVAL_PP(val), "NULL")) { + if (opt & PGSQL_DML_ESCAPE) { + tmp = PGSQLescapeIdentifier(pg_link, fld, fld_len); + smart_str_appends(querystr, tmp); + PGSQLfree(tmp); + } else { + smart_str_appendl(querystr, fld, fld_len - 1); + } + if (where_cond && (Z_TYPE_PP(val) == IS_BOOL || (Z_TYPE_PP(val) == IS_STRING && !strcmp(Z_STRVAL_PP(val), "NULL")))) { smart_str_appends(querystr, " IS "); } else { smart_str_appendc(querystr, '='); @@ -6312,7 +6749,17 @@ static inline int build_assignment_string(smart_str *querystr, HashTable *ht, in switch(Z_TYPE_PP(val)) { case IS_STRING: - smart_str_appendl(querystr, Z_STRVAL_PP(val), Z_STRLEN_PP(val)); + if (opt & PGSQL_DML_ESCAPE) { + size_t new_len; + tmp = (char *)safe_emalloc(Z_STRLEN_PP(val), 2, 1); + new_len = PQescapeStringConn(pg_link, tmp, Z_STRVAL_PP(val), Z_STRLEN_PP(val), NULL); + smart_str_appendc(querystr, '\''); + smart_str_appendl(querystr, tmp, new_len); + smart_str_appendc(querystr, '\''); + efree(tmp); + } else { + smart_str_appendl(querystr, Z_STRVAL_PP(val), Z_STRLEN_PP(val)); + } break; case IS_LONG: smart_str_append_long(querystr, Z_LVAL_PP(val)); @@ -6320,9 +6767,11 @@ static inline int build_assignment_string(smart_str *querystr, HashTable *ht, in case IS_DOUBLE: smart_str_appendl(querystr, buf, MIN(snprintf(buf, sizeof(buf), "%F", Z_DVAL_PP(val)), sizeof(buf)-1)); break; + case IS_NULL: + smart_str_appendl(querystr, "NULL", sizeof("NULL")-1); + break; default: - /* should not happen */ - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects scaler values other than NULL. Need to convert?"); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expects scaler values. type=%d", Z_TYPE_PP(val)); return -1; } smart_str_appendl(querystr, pad, pad_len); @@ -6344,14 +6793,14 @@ PHP_PGSQL_API int php_pgsql_update(PGconn *pg_link, const char *table, zval *var assert(table != NULL); assert(Z_TYPE_P(var_array) == IS_ARRAY); assert(Z_TYPE_P(ids_array) == IS_ARRAY); - assert(!(opt & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING))); + assert(!(opt & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE))); if (zend_hash_num_elements(Z_ARRVAL_P(var_array)) == 0 || zend_hash_num_elements(Z_ARRVAL_P(ids_array)) == 0) { return FAILURE; } - if (!(opt & PGSQL_DML_NO_CONV)) { + if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) { MAKE_STD_ZVAL(var_converted); array_init(var_converted); if (php_pgsql_convert(pg_link, table, var_array, var_converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) { @@ -6370,15 +6819,15 @@ PHP_PGSQL_API int php_pgsql_update(PGconn *pg_link, const char *table, zval *var build_tablename(&querystr, pg_link, table); smart_str_appends(&querystr, " SET "); - if (build_assignment_string(&querystr, Z_ARRVAL_P(var_array), 0, ",", 1 TSRMLS_CC)) + if (build_assignment_string(pg_link, &querystr, Z_ARRVAL_P(var_array), 0, ",", 1, opt TSRMLS_CC)) goto cleanup; smart_str_appends(&querystr, " WHERE "); - if (build_assignment_string(&querystr, Z_ARRVAL_P(ids_array), 1, " AND ", sizeof(" AND ")-1 TSRMLS_CC)) + if (build_assignment_string(pg_link, &querystr, Z_ARRVAL_P(ids_array), 1, " AND ", sizeof(" AND ")-1, opt TSRMLS_CC)) goto cleanup; - smart_str_appendc(&querystr, ';'); + smart_str_appendc(&querystr, ';'); smart_str_0(&querystr); if ((opt & PGSQL_DML_EXEC) && do_exec(&querystr, PGRES_COMMAND_OK, pg_link, opt TSRMLS_CC) == 0) { @@ -6421,7 +6870,7 @@ PHP_FUNCTION(pg_update) &pgsql_link, &table, &table_len, &values, &ids, &option) == FAILURE) { return; } - if (option & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING)) { + if (option & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid option is specified"); RETURN_FALSE; } @@ -6452,13 +6901,13 @@ PHP_PGSQL_API int php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids assert(pg_link != NULL); assert(table != NULL); assert(Z_TYPE_P(ids_array) == IS_ARRAY); - assert(!(opt & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_EXEC|PGSQL_DML_STRING))); - + assert(!(opt & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_EXEC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE))); + if (zend_hash_num_elements(Z_ARRVAL_P(ids_array)) == 0) { return FAILURE; } - if (!(opt & PGSQL_DML_NO_CONV)) { + if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) { MAKE_STD_ZVAL(ids_converted); array_init(ids_converted); if (php_pgsql_convert(pg_link, table, ids_array, ids_converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) { @@ -6471,7 +6920,7 @@ PHP_PGSQL_API int php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids build_tablename(&querystr, pg_link, table); smart_str_appends(&querystr, " WHERE "); - if (build_assignment_string(&querystr, Z_ARRVAL_P(ids_array), 1, " AND ", sizeof(" AND ")-1 TSRMLS_CC)) + if (build_assignment_string(pg_link, &querystr, Z_ARRVAL_P(ids_array), 1, " AND ", sizeof(" AND ")-1, opt TSRMLS_CC)) goto cleanup; smart_str_appendc(&querystr, ';'); @@ -6484,7 +6933,7 @@ PHP_PGSQL_API int php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids } cleanup: - if (!(opt & PGSQL_DML_NO_CONV)) { + if (ids_converted) { zval_dtor(ids_converted); FREE_ZVAL(ids_converted); } @@ -6513,7 +6962,7 @@ PHP_FUNCTION(pg_delete) &pgsql_link, &table, &table_len, &ids, &option) == FAILURE) { return; } - if (option & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING)) { + if (option & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid option is specified"); RETURN_FALSE; } @@ -6577,7 +7026,7 @@ PHP_PGSQL_API int php_pgsql_result2array(PGresult *pg_result, zval *ret_array TS /* {{{ php_pgsql_select */ -PHP_PGSQL_API int php_pgsql_select(PGconn *pg_link, const char *table, zval *ids_array, zval *ret_array, ulong opt, char **sql TSRMLS_DC) +PHP_PGSQL_API int php_pgsql_select(PGconn *pg_link, const char *table, zval *ids_array, zval *ret_array, ulong opt, char **sql TSRMLS_DC) { zval *ids_converted = NULL; smart_str querystr = {0}; @@ -6588,13 +7037,13 @@ PHP_PGSQL_API int php_pgsql_select(PGconn *pg_link, const char *table, zval *ids assert(table != NULL); assert(Z_TYPE_P(ids_array) == IS_ARRAY); assert(Z_TYPE_P(ret_array) == IS_ARRAY); - assert(!(opt & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING))); + assert(!(opt & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE))); if (zend_hash_num_elements(Z_ARRVAL_P(ids_array)) == 0) { return FAILURE; } - if (!(opt & PGSQL_DML_NO_CONV)) { + if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) { MAKE_STD_ZVAL(ids_converted); array_init(ids_converted); if (php_pgsql_convert(pg_link, table, ids_array, ids_converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) { @@ -6607,7 +7056,7 @@ PHP_PGSQL_API int php_pgsql_select(PGconn *pg_link, const char *table, zval *ids build_tablename(&querystr, pg_link, table); smart_str_appends(&querystr, " WHERE "); - if (build_assignment_string(&querystr, Z_ARRVAL_P(ids_array), 1, " AND ", sizeof(" AND ")-1 TSRMLS_CC)) + if (build_assignment_string(pg_link, &querystr, Z_ARRVAL_P(ids_array), 1, " AND ", sizeof(" AND ")-1, opt TSRMLS_CC)) goto cleanup; smart_str_appendc(&querystr, ';'); @@ -6622,7 +7071,7 @@ PHP_PGSQL_API int php_pgsql_select(PGconn *pg_link, const char *table, zval *ids PQclear(pg_result); cleanup: - if (!(opt & PGSQL_DML_NO_CONV)) { + if (ids_converted) { zval_dtor(ids_converted); FREE_ZVAL(ids_converted); } @@ -6651,7 +7100,7 @@ PHP_FUNCTION(pg_select) &pgsql_link, &table, &table_len, &ids, &option) == FAILURE) { return; } - if (option & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING)) { + if (option & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid option is specified"); RETURN_FALSE; } |