diff options
author | Stanley Sufficool <ssufficool@php.net> | 2014-10-20 21:33:32 -0700 |
---|---|---|
committer | Stanley Sufficool <ssufficool@php.net> | 2014-10-20 21:33:32 -0700 |
commit | 8defcb855ab01d9c8ab4759cb793d80149b55a8c (patch) | |
tree | ed51eb30a2cbc92b102557498fb3e4113da1bb07 /ext/pgsql/pgsql.c | |
parent | 9c7dbb0487f5991fde03873ea8f5e66d6688415f (diff) | |
parent | baddb1c73a170ef1d2c31bd54cddbc6e1ab596b9 (diff) | |
download | php-git-8defcb855ab01d9c8ab4759cb793d80149b55a8c.tar.gz |
Merge branch 'master' of https://git.php.net/push/php-src
* 'master' of https://git.php.net/push/php-src: (6215 commits)
Extra comma
Moved proxy object support in ASSIGN_ADD (and family) from VM to slow paths of corresponding operators
Simplification
zend_get_property_info_quick() cleanup and optimization
initialize lineno before calling compile file file in phar
Use ADDREF instead of DUP, it must be enough.
Removed old irrelevant comment
fixed compilation error
Fix bug #68262: Broken reference across cloned objects
export functions needed for phpdbg
Fixed compilation
Optimized property access handlers. Removed EG(std_property_info).
Fixed bug #68199 (PDO::pgsqlGetNotify doesn't support NOTIFY payloads)
Don't make difference between undefined and unaccessible properies when call __get() and family
Don't make useless CSE
array_pop/array_shift optimization
check for zlib headers as well as lib for mysqlnd
a realpath cache key can be int or float, catching this
News entry for new curl constants
News entry for new curl constants
...
Diffstat (limited to 'ext/pgsql/pgsql.c')
-rw-r--r-- | ext/pgsql/pgsql.c | 2987 |
1 files changed, 1669 insertions, 1318 deletions
diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c index 35eb09e584..207764b165 100644 --- a/ext/pgsql/pgsql.c +++ b/ext/pgsql/pgsql.c @@ -1,8 +1,8 @@ /* +----------------------------------------------------------------------+ - | PHP Version 5 | + | PHP Version 7 | +----------------------------------------------------------------------+ - | Copyright (c) 1997-2013 The PHP Group | + | Copyright (c) 1997-2014 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | @@ -15,11 +15,11 @@ | Authors: Zeev Suraski <zeev@zend.com> | | Jouni Ahto <jouni.ahto@exdec.fi> | | Yasuo Ohgaki <yohgaki@php.net> | - | Youichi Iwakiri <yiwakiri@st.rim.or.jp> (pg_copy_*) | - | Chris Kings-Lynne <chriskl@php.net> (v3 protocol) | + | Youichi Iwakiri <yiwakiri@st.rim.or.jp> (pg_copy_*) | + | Chris Kings-Lynne <chriskl@php.net> (v3 protocol) | +----------------------------------------------------------------------+ */ - + /* $Id$ */ #include <stdlib.h> @@ -35,8 +35,11 @@ #include "php.h" #include "php_ini.h" #include "ext/standard/php_standard.h" -#include "ext/standard/php_smart_str.h" +#include "zend_smart_str.h" #include "ext/ereg/php_regex.h" +#ifdef PHP_WIN32 +# include "win32/time.h" +#endif #undef PACKAGE_BUGREPORT #undef PACKAGE_NAME @@ -63,18 +66,18 @@ #define PGSQL_MAX_LENGTH_OF_LONG 30 #define PGSQL_MAX_LENGTH_OF_DOUBLE 60 -#if LONG_MAX < UINT_MAX +#if ZEND_LONG_MAX < UINT_MAX #define PGSQL_RETURN_OID(oid) do { \ - if (oid > LONG_MAX) { \ + if (oid > ZEND_LONG_MAX) { \ smart_str s = {0}; \ smart_str_append_unsigned(&s, oid); \ smart_str_0(&s); \ - RETURN_STRINGL(s.c, s.len, 0); \ + RETURN_STR(s.s); \ } \ - RETURN_LONG((long)oid); \ + RETURN_LONG((zend_long)oid); \ } while(0) #else -#define PGSQL_RETURN_OID(oid) (RETURN_LONG((long)oid)) +#define PGSQL_RETURN_OID(oid) RETURN_LONG((zend_long)oid) #endif #if HAVE_PQSETNONBLOCKING @@ -112,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) @@ -367,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) @@ -516,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) @@ -564,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) @@ -633,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) @@ -661,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) @@ -743,10 +776,110 @@ ZEND_GET_MODULE(pgsql) static int le_link, le_plink, le_result, le_lofp, le_string; +/* Compatibility definitions */ + +#ifndef HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT +#define pg_encoding_to_char(x) "SQL_ASCII" +#endif + +#if !HAVE_PQESCAPE_CONN +#define PQescapeStringConn(conn, to, from, len, error) PQescapeString(to, from, len) +#endif + +#if HAVE_PQESCAPELITERAL +#define PGSQLescapeLiteral(conn, str, len) PQescapeLiteral(conn, str, len) +#define PGSQLescapeIdentifier(conn, str, len) PQescapeIdentifier(conn, str, len) +#define PGSQLfree(a) PQfreemem(a) +#else +#define PGSQLescapeLiteral(conn, str, len) php_pgsql_PQescapeInternal(conn, str, len, 1, 0) +#define PGSQLescapeLiteral2(conn, str, len) php_pgsql_PQescapeInternal(conn, str, len, 1, 1) +#define PGSQLescapeIdentifier(conn, str, len) php_pgsql_PQescapeInternal(conn, str, len, 0, 0) +#define PGSQLfree(a) efree(a) + +/* emulate libpq's PQescapeInternal() 9.0 or later */ +static char *php_pgsql_PQescapeInternal(PGconn *conn, const char *str, size_t len, int escape_literal, int safe) /* {{{ */ +{ + char *result, *rp, *s; + size_t tmp_len; + + if (!conn) { + return NULL; + } + + /* allocate enough memory */ + rp = result = (char *)safe_emalloc(len, 2, 5); /* leading " E" needs extra 2 bytes + quote_chars on both end for 2 bytes + NULL */ + + if (escape_literal) { + size_t new_len; + + if (safe) { + char *tmp = (char *)safe_emalloc(len, 2, 1); + *rp++ = '\''; + /* PQescapeString does not escape \, but it handles multibyte chars safely. + This escape is incompatible with PQescapeLiteral. */ + new_len = PQescapeStringConn(conn, tmp, str, len, NULL); + strncpy(rp, tmp, new_len); + efree(tmp); + rp += new_len; + } else { + char *encoding; + /* This is compatible with PQescapeLiteral, but it cannot handle multbyte chars + such as SJIS, BIG5. Raise warning and return NULL by checking + client_encoding. */ + encoding = (char *) pg_encoding_to_char(PQclientEncoding(conn)); + if (!strncmp(encoding, "SJIS", sizeof("SJIS")-1) || + !strncmp(encoding, "SHIFT_JIS_2004", sizeof("SHIFT_JIS_2004")-1) || + !strncmp(encoding, "BIG5", sizeof("BIG5")-1) || + !strncmp(encoding, "GB18030", sizeof("GB18030")-1) || + !strncmp(encoding, "GBK", sizeof("GBK")-1) || + !strncmp(encoding, "JOHAB", sizeof("JOHAB")-1) || + !strncmp(encoding, "UHC", sizeof("UHC")-1) ) { + TSRMLS_FETCH(); + + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unsafe encoding is used. Do not use '%s' encoding or use PostgreSQL 9.0 or later libpq.", encoding); + } + /* check backslashes */ + tmp_len = strspn(str, "\\"); + if (tmp_len != len) { + /* add " E" for escaping slashes */ + *rp++ = ' '; + *rp++ = 'E'; + } + *rp++ = '\''; + for (s = (char *)str; s - str < len; ++s) { + if (*s == '\'' || *s == '\\') { + *rp++ = *s; + *rp++ = *s; + } else { + *rp++ = *s; + } + } + } + *rp++ = '\''; + } else { + /* Identifier escape. */ + *rp++ = '"'; + for (s = (char *)str; s - str < len; ++s) { + if (*s == '"') { + *rp++ = '"'; + *rp++ = '"'; + } else { + *rp++ = *s; + } + } + *rp++ = '"'; + } + *rp = '\0'; + + return result; +} +/* }}} */ +#endif + /* {{{ _php_pgsql_trim_message */ -static char * _php_pgsql_trim_message(const char *message, int *len) +static char * _php_pgsql_trim_message(const char *message, size_t *len) { - register int i = strlen(message)-1; + register size_t i = strlen(message)-1; if (i>1 && (message[i-1] == '\r' || message[i-1] == '\n') && message[i] == '.') { --i; @@ -771,29 +904,29 @@ static inline char * _php_pgsql_trim_result(PGconn * pgsql, char **buf) #define PQErrorMessageTrim(pgsql, buf) _php_pgsql_trim_result(pgsql, buf) -#define PHP_PQ_ERROR(text, pgsql) { \ - char *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql), NULL); \ - php_error_docref(NULL TSRMLS_CC, E_WARNING, text, msgbuf); \ - efree(msgbuf); \ +#define PHP_PQ_ERROR(text, pgsql) { \ + char *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql), NULL); \ + php_error_docref(NULL TSRMLS_CC, E_WARNING, text, msgbuf); \ + efree(msgbuf); \ } \ /* {{{ php_pgsql_set_default_link */ -static void php_pgsql_set_default_link(int id TSRMLS_DC) -{ - zend_list_addref(id); +static void php_pgsql_set_default_link(zend_resource *res TSRMLS_DC) +{ + GC_REFCOUNT(res)++; - if (PGG(default_link) != -1) { + if (PGG(default_link) != NULL) { zend_list_delete(PGG(default_link)); } - PGG(default_link) = id; + PGG(default_link) = res; } /* }}} */ /* {{{ _close_pgsql_link */ -static void _close_pgsql_link(zend_rsrc_list_entry *rsrc TSRMLS_DC) +static void _close_pgsql_link(zend_resource *rsrc TSRMLS_DC) { PGconn *link = (PGconn *)rsrc->ptr; PGresult *res; @@ -808,7 +941,7 @@ static void _close_pgsql_link(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ _close_pgsql_plink */ -static void _close_pgsql_plink(zend_rsrc_list_entry *rsrc TSRMLS_DC) +static void _close_pgsql_plink(zend_resource *rsrc TSRMLS_DC) { PGconn *link = (PGconn *)rsrc->ptr; PGresult *res; @@ -831,39 +964,39 @@ static void _php_pgsql_notice_handler(void *resource_id, const char *message) TSRMLS_FETCH(); if (! PGG(ignore_notices)) { notice = (php_pgsql_notice *)emalloc(sizeof(php_pgsql_notice)); - notice->message = _php_pgsql_trim_message(message, (int *)¬ice->len); + notice->message = _php_pgsql_trim_message(message, ¬ice->len); if (PGG(log_notices)) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s", notice->message); } - zend_hash_index_update(&PGG(notices), (ulong)resource_id, (void **)¬ice, sizeof(php_pgsql_notice *), NULL); + zend_hash_index_update_ptr(&PGG(notices), (zend_ulong)resource_id, notice); } } /* }}} */ -#define PHP_PGSQL_NOTICE_PTR_DTOR (void (*)(void *))_php_pgsql_notice_ptr_dtor +#define PHP_PGSQL_NOTICE_PTR_DTOR _php_pgsql_notice_ptr_dtor /* {{{ _php_pgsql_notice_dtor */ -static void _php_pgsql_notice_ptr_dtor(void **ptr) +static void _php_pgsql_notice_ptr_dtor(zval *el) { - php_pgsql_notice *notice = (php_pgsql_notice *)*ptr; + php_pgsql_notice *notice = (php_pgsql_notice *)Z_PTR_P(el); if (notice) { - efree(notice->message); - efree(notice); - notice = NULL; - } + efree(notice->message); + efree(notice); + } } /* }}} */ /* {{{ _rollback_transactions */ -static int _rollback_transactions(zend_rsrc_list_entry *rsrc TSRMLS_DC) +static int _rollback_transactions(zval *el TSRMLS_DC) { PGconn *link; PGresult *res; int orig; + zend_resource *rsrc = Z_RES_P(el); - if (Z_TYPE_P(rsrc) != le_plink) + if (rsrc->type != le_plink) return 0; link = (PGconn *) rsrc->ptr; @@ -899,7 +1032,7 @@ static int _rollback_transactions(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ _free_ptr */ -static void _free_ptr(zend_rsrc_list_entry *rsrc TSRMLS_DC) +static void _free_ptr(zend_resource *rsrc TSRMLS_DC) { pgLofp *lofp = (pgLofp *)rsrc->ptr; efree(lofp); @@ -908,7 +1041,7 @@ static void _free_ptr(zend_rsrc_list_entry *rsrc TSRMLS_DC) /* {{{ _free_result */ -static void _free_result(zend_rsrc_list_entry *rsrc TSRMLS_DC) +static void _free_result(zend_resource *rsrc TSRMLS_DC) { pgsql_result_handle *pg_result = (pgsql_result_handle *)rsrc->ptr; @@ -917,8 +1050,7 @@ static void _free_result(zend_rsrc_list_entry *rsrc TSRMLS_DC) } /* }}} */ - -static int _php_pgsql_detect_identifier_escape(const char *identifier, size_t len) +static int _php_pgsql_detect_identifier_escape(const char *identifier, size_t len) /* {{{ */ { size_t i; @@ -940,57 +1072,6 @@ static int _php_pgsql_detect_identifier_escape(const char *identifier, size_t le /* Escaped properly */ return SUCCESS; } - -#if !HAVE_PQESCAPELITERAL -/* {{{ _php_pgsql_escape_identifier - * Since PQescapeIdentifier() is unavailable (PostgreSQL 9.0 <), idenfifers - * should be escaped by pgsql module. - * Note: this function does not care for encoding. Therefore users should not - * use this with SJIS/BIG5 etc. (i.e. Encoding base injection may possible with - * before PostgreSQL 9.0) - */ -static char *_php_pgsql_escape_identifier(const char *field, size_t field_len) -{ - ulong field_escaped_len = field_len*2 + 3; - ulong i, j = 0; - char *field_escaped; - - field_escaped = (char *)malloc(field_escaped_len); - field_escaped[j++] = '"'; - for (i = 0; i < field_len; i++) { - if (field[i] == '"') { - field_escaped[j++] = '"'; - field_escaped[j++] = '"'; - } else { - field_escaped[j++] = field[i]; - } - } - field_escaped[j++] = '"'; - field_escaped[j] = '\0'; - return field_escaped; -} -/* }}} */ -#endif - -/* {{{ _php_pgsql_strndup, no strndup should be used */ -static char *_php_pgsql_strndup(const char *s, size_t len) -{ - char *new; - - if (NULL == s) { - return (char *)NULL; - } - - new = (char *) malloc(len + 1); - - if (NULL == new) { - return (char *)NULL; - } - - new[len] = '\0'; - - return memmove(new, s, len); -} /* }}} */ /* {{{ PHP_INI @@ -1020,7 +1101,7 @@ static PHP_GINIT_FUNCTION(pgsql) PHP_MINIT_FUNCTION(pgsql) { REGISTER_INI_ENTRIES(); - + le_link = zend_register_list_destructors_ex(_close_pgsql_link, NULL, "pgsql link", module_number); le_plink = zend_register_list_destructors_ex(NULL, _close_pgsql_plink, "pgsql link persistent", module_number); le_result = zend_register_list_destructors_ex(_free_result, NULL, "pgsql result", module_number); @@ -1033,6 +1114,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); @@ -1040,6 +1122,20 @@ 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); +#ifdef CONNECTION_SSL_STARTUP + REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_SSL_STARTUP", CONNECTION_SSL_STARTUP, CONST_CS | CONST_PERSISTENT); +#endif + 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); @@ -1094,6 +1190,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); @@ -1117,7 +1214,7 @@ PHP_MSHUTDOWN_FUNCTION(pgsql) */ PHP_RINIT_FUNCTION(pgsql) { - PGG(default_link)=-1; + PGG(default_link) = NULL; PGG(num_links) = PGG(num_persistent); return SUCCESS; } @@ -1156,10 +1253,10 @@ PHP_MINFO_FUNCTION(pgsql) #else php_info_print_table_row(2, "SSL support", "disabled"); #endif -#endif /* HAVE_PG_CONFIG_H */ - snprintf(buf, sizeof(buf), "%ld", PGG(num_persistent)); +#endif /* HAVE_PG_CONFIG_H */ + snprintf(buf, sizeof(buf), ZEND_LONG_FMT, PGG(num_persistent)); php_info_print_table_row(2, "Active Persistent Links", buf); - snprintf(buf, sizeof(buf), "%ld", PGG(num_links)); + snprintf(buf, sizeof(buf), ZEND_LONG_FMT, PGG(num_links)); php_info_print_table_row(2, "Active Links", buf); php_info_print_table_end(); @@ -1167,7 +1264,6 @@ PHP_MINFO_FUNCTION(pgsql) } /* }}} */ - /* {{{ php_pgsql_do_connect */ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) @@ -1175,12 +1271,14 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) char *host=NULL,*port=NULL,*options=NULL,*tty=NULL,*dbname=NULL,*connstring=NULL; PGconn *pgsql; smart_str str = {0}; - zval **args[5]; + zval *args; int i, connect_type = 0; PGresult *pg_result; + args = (zval *)safe_emalloc(ZEND_NUM_ARGS(), sizeof(zval), 0); if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 5 || zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) { + efree(args); WRONG_PARAM_COUNT; } @@ -1190,66 +1288,67 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) /* make sure that the PGSQL_CONNECT_FORCE_NEW bit is not part of the hash so that subsequent connections * can re-use this connection. Bug #39979 */ - if (i == 1 && ZEND_NUM_ARGS() == 2 && Z_TYPE_PP(args[i]) == IS_LONG) { - if (Z_LVAL_PP(args[1]) == PGSQL_CONNECT_FORCE_NEW) { + if (i == 1 && ZEND_NUM_ARGS() == 2 && Z_TYPE(args[i]) == IS_LONG) { + if (Z_LVAL(args[1]) == PGSQL_CONNECT_FORCE_NEW) { continue; - } else if (Z_LVAL_PP(args[1]) & PGSQL_CONNECT_FORCE_NEW) { - smart_str_append_long(&str, Z_LVAL_PP(args[1]) ^ PGSQL_CONNECT_FORCE_NEW); + } else if (Z_LVAL(args[1]) & PGSQL_CONNECT_FORCE_NEW) { + smart_str_append_long(&str, Z_LVAL(args[1]) ^ PGSQL_CONNECT_FORCE_NEW); } } - convert_to_string_ex(args[i]); + convert_to_string_ex(&args[i]); smart_str_appendc(&str, '_'); - smart_str_appendl(&str, Z_STRVAL_PP(args[i]), Z_STRLEN_PP(args[i])); + smart_str_appendl(&str, Z_STRVAL(args[i]), Z_STRLEN(args[i])); } smart_str_0(&str); if (ZEND_NUM_ARGS() == 1) { /* new style, using connection string */ - connstring = Z_STRVAL_PP(args[0]); + connstring = Z_STRVAL(args[0]); } else if (ZEND_NUM_ARGS() == 2 ) { /* Safe to add conntype_option, since 2 args was illegal */ - connstring = Z_STRVAL_PP(args[0]); - convert_to_long_ex(args[1]); - connect_type = Z_LVAL_PP(args[1]); + connstring = Z_STRVAL(args[0]); + convert_to_long_ex(&args[1]); + connect_type = Z_LVAL(args[1]); } else { - host = Z_STRVAL_PP(args[0]); - port = Z_STRVAL_PP(args[1]); - dbname = Z_STRVAL_PP(args[ZEND_NUM_ARGS()-1]); + host = Z_STRVAL(args[0]); + port = Z_STRVAL(args[1]); + dbname = Z_STRVAL(args[ZEND_NUM_ARGS()-1]); switch (ZEND_NUM_ARGS()) { case 5: - tty = Z_STRVAL_PP(args[3]); + tty = Z_STRVAL(args[3]); /* fall through */ case 4: - options = Z_STRVAL_PP(args[2]); + options = Z_STRVAL(args[2]); break; } } - + efree(args); + if (persistent && PGG(allow_persistent)) { - zend_rsrc_list_entry *le; + zend_resource *le; /* try to find if we already have this link in our persistent list */ - if (zend_hash_find(&EG(persistent_list), str.c, str.len+1, (void **) &le)==FAILURE) { /* we don't */ - zend_rsrc_list_entry new_le; + if ((le = zend_hash_find_ptr(&EG(persistent_list), str.s)) == NULL) { /* we don't */ + zend_resource new_le; - if (PGG(max_links)!=-1 && PGG(num_links)>=PGG(max_links)) { + if (PGG(max_links) != -1 && PGG(num_links) >= PGG(max_links)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, - "Cannot create new link. Too many open links (%ld)", PGG(num_links)); + "Cannot create new link. Too many open links (%pd)", PGG(num_links)); goto err; } - if (PGG(max_persistent)!=-1 && PGG(num_persistent)>=PGG(max_persistent)) { + if (PGG(max_persistent) != -1 && PGG(num_persistent) >= PGG(max_persistent)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, - "Cannot create new link. Too many open persistent links (%ld)", PGG(num_persistent)); + "Cannot create new link. Too many open persistent links (%pd)", PGG(num_persistent)); goto err; } /* create the link */ if (connstring) { - pgsql=PQconnectdb(connstring); + pgsql = PQconnectdb(connstring); } else { - pgsql=PQsetdb(host,port,options,tty,dbname); + pgsql = PQsetdb(host, port, options, tty, dbname); } - if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) { + if (pgsql == NULL || PQstatus(pgsql) == CONNECTION_BAD) { PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql) if (pgsql) { PQfinish(pgsql); @@ -1258,15 +1357,15 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) } /* hash it up */ - Z_TYPE(new_le) = le_plink; + new_le.type = le_plink; new_le.ptr = pgsql; - if (zend_hash_update(&EG(persistent_list), str.c, str.len+1, (void *) &new_le, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) { + if (zend_hash_str_update_mem(&EG(persistent_list), str.s->val, str.s->len, &new_le, sizeof(zend_resource)) == NULL) { goto err; } PGG(num_links)++; PGG(num_persistent)++; } else { /* we do */ - if (Z_TYPE_P(le) != le_plink) { + if (le->type != le_plink) { RETURN_FALSE; } /* ensure that the link did not die */ @@ -1277,20 +1376,20 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) pg_result = PQexec(le->ptr, "select 1"); PQclear(pg_result); } - if (PQstatus(le->ptr)==CONNECTION_BAD) { /* the link died */ + if (PQstatus(le->ptr) == CONNECTION_BAD) { /* the link died */ if (le->ptr == NULL) { if (connstring) { - le->ptr=PQconnectdb(connstring); + le->ptr = PQconnectdb(connstring); } else { - le->ptr=PQsetdb(host,port,options,tty,dbname); + le->ptr = PQsetdb(host,port,options,tty,dbname); } } else { PQreset(le->ptr); } - if (le->ptr==NULL || PQstatus(le->ptr)==CONNECTION_BAD) { + if (le->ptr == NULL || PQstatus(le->ptr) == CONNECTION_BAD) { php_error_docref(NULL TSRMLS_CC, E_WARNING,"PostgreSQL link lost, unable to reconnect"); - zend_hash_del(&EG(persistent_list),str.c,str.len+1); + zend_hash_del(&EG(persistent_list), str.s); goto err; } } @@ -1306,72 +1405,87 @@ static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent) } ZEND_REGISTER_RESOURCE(return_value, pgsql, le_plink); } else { /* Non persistent connection */ - zend_rsrc_list_entry *index_ptr,new_index_ptr; - + zend_resource *index_ptr, new_index_ptr; + /* first we check the hash for the hashed_details key. if it exists, * it should point us to the right offset where the actual pgsql link sits. * if it doesn't, open a new pgsql link, add it to the resource list, * and add a pointer to it with hashed_details as the key. */ if (!(connect_type & PGSQL_CONNECT_FORCE_NEW) - && zend_hash_find(&EG(regular_list),str.c,str.len+1,(void **) &index_ptr)==SUCCESS) { - int type; - ulong link; - void *ptr; + && (index_ptr = zend_hash_find_ptr(&EG(regular_list), str.s)) != NULL) { + zend_resource *link; - if (Z_TYPE_P(index_ptr) != le_index_ptr) { + if (index_ptr->type != le_index_ptr) { RETURN_FALSE; } - link = (ulong) index_ptr->ptr; - ptr = zend_list_find(link,&type); /* check if the link is still there */ - if (ptr && (type==le_link || type==le_plink)) { - Z_LVAL_P(return_value) = link; - zend_list_addref(link); + + link = (zend_resource *)index_ptr->ptr; + if (link->ptr && (link->type == le_link || link->type == le_plink)) { php_pgsql_set_default_link(link TSRMLS_CC); - Z_TYPE_P(return_value) = IS_RESOURCE; + GC_REFCOUNT(link)++; + RETVAL_RES(link); goto cleanup; } else { - zend_hash_del(&EG(regular_list),str.c,str.len+1); + zend_hash_del(&EG(regular_list), str.s); } } - if (PGG(max_links)!=-1 && PGG(num_links)>=PGG(max_links)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create new link. Too many open links (%ld)", PGG(num_links)); + if (PGG(max_links) != -1 && PGG(num_links) >= PGG(max_links)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create new link. Too many open links (%pd)", 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 */ ZEND_REGISTER_RESOURCE(return_value, pgsql, le_link); /* add it to the hash */ - new_index_ptr.ptr = (void *) Z_LVAL_P(return_value); - Z_TYPE(new_index_ptr) = le_index_ptr; - if (zend_hash_update(&EG(regular_list),str.c,str.len+1,(void *) &new_index_ptr, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) { + new_index_ptr.ptr = (void *) Z_RES_P(return_value); + new_index_ptr.type = le_index_ptr; + if (zend_hash_update_mem(&EG(regular_list), str.s, (void *) &new_index_ptr, sizeof(zend_resource)) == NULL) { goto err; } PGG(num_links)++; } /* set notice processer */ if (! PGG(ignore_notices) && Z_TYPE_P(return_value) == IS_RESOURCE) { - PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, (void*)Z_RESVAL_P(return_value)); + PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, (void*)Z_RES_HANDLE_P(return_value)); } - php_pgsql_set_default_link(Z_LVAL_P(return_value) TSRMLS_CC); + php_pgsql_set_default_link(Z_RES_P(return_value) TSRMLS_CC); cleanup: smart_str_free(&str); return; - + err: smart_str_free(&str); RETURN_FALSE; @@ -1400,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) { + 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) @@ -1415,37 +1554,36 @@ PHP_FUNCTION(pg_close) zval *pgsql_link = NULL; int id = -1, argc = ZEND_NUM_ARGS(); PGconn *pgsql; - + if (zend_parse_parameters(argc TSRMLS_CC, "|r", &pgsql_link) == FAILURE) { return; } - + if (argc == 0) { - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } if (pgsql_link == NULL && id == -1) { RETURN_FALSE; - } + } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (id==-1) { /* explicit resource number */ - zend_list_delete(Z_RESVAL_P(pgsql_link)); + zend_list_close(Z_RES_P(pgsql_link)); } - if (id!=-1 - || (pgsql_link && Z_RESVAL_P(pgsql_link)==PGG(default_link))) { - zend_list_delete(PGG(default_link)); - PGG(default_link) = -1; + if (id!=-1 + || (pgsql_link && Z_RES_P(pgsql_link) == PGG(default_link))) { + zend_list_close(PGG(default_link)); + PGG(default_link) = NULL; } RETURN_TRUE; } /* }}} */ - #define PHP_PG_DBNAME 1 #define PHP_PG_ERROR_MESSAGE 2 #define PHP_PG_OPTIONS 3 @@ -1462,49 +1600,74 @@ static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type int id = -1, argc = ZEND_NUM_ARGS(); PGconn *pgsql; char *msgbuf; + char *result; if (zend_parse_parameters(argc TSRMLS_CC, "|r", &pgsql_link) == FAILURE) { return; } if (argc == 0) { - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } if (pgsql_link == NULL && id == -1) { RETURN_FALSE; - } + } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); switch(entry_type) { case PHP_PG_DBNAME: - Z_STRVAL_P(return_value) = PQdb(pgsql); + result = PQdb(pgsql); break; case PHP_PG_ERROR_MESSAGE: - RETURN_STRING(PQErrorMessageTrim(pgsql, &msgbuf), 0); + result = PQErrorMessageTrim(pgsql, &msgbuf); + RETVAL_STRING(result); + efree(result); return; case PHP_PG_OPTIONS: - Z_STRVAL_P(return_value) = PQoptions(pgsql); + result = PQoptions(pgsql); break; case PHP_PG_PORT: - Z_STRVAL_P(return_value) = PQport(pgsql); + result = PQport(pgsql); break; case PHP_PG_TTY: - Z_STRVAL_P(return_value) = PQtty(pgsql); + result = PQtty(pgsql); break; case PHP_PG_HOST: - Z_STRVAL_P(return_value) = PQhost(pgsql); + result = PQhost(pgsql); break; case PHP_PG_VERSION: array_init(return_value); - add_assoc_string(return_value, "client", PG_VERSION, 1); + add_assoc_string(return_value, "client", PG_VERSION); #if HAVE_PQPROTOCOLVERSION add_assoc_long(return_value, "protocol", PQprotocolVersion(pgsql)); #if HAVE_PQPARAMETERSTATUS if (PQprotocolVersion(pgsql) >= 3) { - add_assoc_string(return_value, "server", (char*)PQparameterStatus(pgsql, "server_version"), 1); + /* 8.0 or grater supports protorol version 3 */ + char *tmp; + add_assoc_string(return_value, "server", (char*)PQparameterStatus(pgsql, "server_version")); + tmp = (char*)PQparameterStatus(pgsql, "server_encoding"); + add_assoc_string(return_value, "server_encoding", tmp); + tmp = (char*)PQparameterStatus(pgsql, "client_encoding"); + add_assoc_string(return_value, "client_encoding", tmp); + tmp = (char*)PQparameterStatus(pgsql, "is_superuser"); + add_assoc_string(return_value, "is_superuser", tmp); + tmp = (char*)PQparameterStatus(pgsql, "session_authorization"); + add_assoc_string(return_value, "session_authorization", tmp); + tmp = (char*)PQparameterStatus(pgsql, "DateStyle"); + add_assoc_string(return_value, "DateStyle", tmp); + tmp = (char*)PQparameterStatus(pgsql, "IntervalStyle"); + add_assoc_string(return_value, "IntervalStyle", tmp ? tmp : ""); + tmp = (char*)PQparameterStatus(pgsql, "TimeZone"); + add_assoc_string(return_value, "TimeZone", tmp ? tmp : ""); + tmp = (char*)PQparameterStatus(pgsql, "integer_datetimes"); + add_assoc_string(return_value, "integer_datetimes", tmp ? tmp : ""); + tmp = (char*)PQparameterStatus(pgsql, "standard_conforming_strings"); + add_assoc_string(return_value, "standard_conforming_strings", tmp ? tmp : ""); + tmp = (char*)PQparameterStatus(pgsql, "application_name"); + add_assoc_string(return_value, "application_name", tmp ? tmp : ""); } #endif #endif @@ -1512,14 +1675,11 @@ static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type default: RETURN_FALSE; } - if (Z_STRVAL_P(return_value)) { - Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value)); - Z_STRVAL_P(return_value) = (char *) estrdup(Z_STRVAL_P(return_value)); + if (result) { + RETURN_STRING(result); } else { - Z_STRLEN_P(return_value) = 0; - Z_STRVAL_P(return_value) = (char *) estrdup(""); + RETURN_EMPTY_STRING(); } - Z_TYPE_P(return_value) = IS_STRING; } /* }}} */ @@ -1588,25 +1748,25 @@ PHP_FUNCTION(pg_parameter_status) int id; PGconn *pgsql; char *param; - int len; + size_t len; if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pgsql_link, ¶m, &len) == SUCCESS) { id = -1; } else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", ¶m, &len) == SUCCESS) { pgsql_link = NULL; - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; } else { RETURN_FALSE; } if (pgsql_link == NULL && id == -1) { RETURN_FALSE; - } + } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); param = (char*)PQparameterStatus(pgsql, param); if (param) { - RETURN_STRING(param, 1); + RETURN_STRING(param); } else { RETURN_FALSE; } @@ -1627,13 +1787,13 @@ PHP_FUNCTION(pg_ping) id = -1; } else { pgsql_link = NULL; - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; } if (pgsql_link == NULL && id == -1) { RETURN_FALSE; - } + } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); /* ping connection */ res = PQexec(pgsql, "SELECT 1;"); @@ -1658,7 +1818,8 @@ PHP_FUNCTION(pg_query) { zval *pgsql_link = NULL; char *query; - int id = -1, query_len, argc = ZEND_NUM_ARGS(); + int id = -1, argc = ZEND_NUM_ARGS(); + size_t query_len; int leftover = 0; PGconn *pgsql; PGresult *pgsql_result; @@ -1669,7 +1830,7 @@ PHP_FUNCTION(pg_query) if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &query, &query_len) == FAILURE) { return; } - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } else { if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pgsql_link, &query, &query_len) == FAILURE) { @@ -1681,7 +1842,7 @@ PHP_FUNCTION(pg_query) RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (PQ_SETNONBLOCKING(pgsql, 0)) { php_error_docref(NULL TSRMLS_CC, E_NOTICE,"Cannot set connection to blocking mode"); @@ -1706,7 +1867,7 @@ PHP_FUNCTION(pg_query) } else { status = (ExecStatusType) PQstatus(pgsql); } - + switch (status) { case PGRES_EMPTY_QUERY: case PGRES_BAD_RESPONSE: @@ -1756,9 +1917,10 @@ static void _php_pgsql_free_params(char **params, int num_params) PHP_FUNCTION(pg_query_params) { zval *pgsql_link = NULL; - zval *pv_param_arr, **tmp; + zval *pv_param_arr, *tmp; char *query; - int query_len, id = -1, argc = ZEND_NUM_ARGS(); + size_t query_len; + int id = -1, argc = ZEND_NUM_ARGS(); int leftover = 0; int num_params = 0; char **params = NULL; @@ -1771,7 +1933,7 @@ PHP_FUNCTION(pg_query_params) if (zend_parse_parameters(argc TSRMLS_CC, "sa", &query, &query_len, &pv_param_arr) == FAILURE) { return; } - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } else { if (zend_parse_parameters(argc TSRMLS_CC, "rsa", &pgsql_link, &query, &query_len, &pv_param_arr) == FAILURE) { @@ -1783,7 +1945,7 @@ PHP_FUNCTION(pg_query_params) RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (PQ_SETNONBLOCKING(pgsql, 0)) { php_error_docref(NULL TSRMLS_CC, E_NOTICE,"Cannot set connection to blocking mode"); @@ -1797,37 +1959,31 @@ PHP_FUNCTION(pg_query_params) php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first"); } - zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr)); num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr)); if (num_params > 0) { int i = 0; params = (char **)safe_emalloc(sizeof(char *), num_params, 0); - - 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); - RETURN_FALSE; - } - if (Z_TYPE_PP(tmp) == IS_NULL) { + ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pv_param_arr), tmp) { + + if (Z_TYPE_P(tmp) == IS_NULL) { params[i] = NULL; } else { - zval tmp_val = **tmp; - zval_copy_ctor(&tmp_val); + zval tmp_val; + + ZVAL_COPY(&tmp_val, tmp); convert_to_cstring(&tmp_val); if (Z_TYPE(tmp_val) != IS_STRING) { php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter"); - zval_dtor(&tmp_val); + zval_ptr_dtor(&tmp_val); _php_pgsql_free_params(params, num_params); RETURN_FALSE; } params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val)); - zval_dtor(&tmp_val); + zval_ptr_dtor(&tmp_val); } - - zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr)); - } + i++; + } ZEND_HASH_FOREACH_END(); } pgsql_result = PQexecParams(pgsql, query, num_params, @@ -1881,7 +2037,8 @@ PHP_FUNCTION(pg_prepare) { zval *pgsql_link = NULL; char *query, *stmtname; - int query_len, stmtname_len, id = -1, argc = ZEND_NUM_ARGS(); + size_t query_len, stmtname_len; + int id = -1, argc = ZEND_NUM_ARGS(); int leftover = 0; PGconn *pgsql; PGresult *pgsql_result; @@ -1892,19 +2049,19 @@ PHP_FUNCTION(pg_prepare) if (zend_parse_parameters(argc TSRMLS_CC, "ss", &stmtname, &stmtname_len, &query, &query_len) == FAILURE) { return; } - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } else { if (zend_parse_parameters(argc TSRMLS_CC, "rss", &pgsql_link, &stmtname, &stmtname_len, &query, &query_len) == FAILURE) { return; } } - + if (pgsql_link == NULL && id == -1) { RETURN_FALSE; - } + } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (PQ_SETNONBLOCKING(pgsql, 0)) { php_error_docref(NULL TSRMLS_CC, E_NOTICE,"Cannot set connection to blocking mode"); @@ -1929,7 +2086,7 @@ PHP_FUNCTION(pg_prepare) } else { status = (ExecStatusType) PQstatus(pgsql); } - + switch (status) { case PGRES_EMPTY_QUERY: case PGRES_BAD_RESPONSE: @@ -1963,9 +2120,10 @@ PHP_FUNCTION(pg_prepare) PHP_FUNCTION(pg_execute) { zval *pgsql_link = NULL; - zval *pv_param_arr, **tmp; + zval *pv_param_arr, *tmp; char *stmtname; - int stmtname_len, id = -1, argc = ZEND_NUM_ARGS(); + size_t stmtname_len; + int id = -1, argc = ZEND_NUM_ARGS(); int leftover = 0; int num_params = 0; char **params = NULL; @@ -1978,7 +2136,7 @@ PHP_FUNCTION(pg_execute) if (zend_parse_parameters(argc TSRMLS_CC, "sa/", &stmtname, &stmtname_len, &pv_param_arr)==FAILURE) { return; } - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } else { if (zend_parse_parameters(argc TSRMLS_CC, "rsa/", &pgsql_link, &stmtname, &stmtname_len, &pv_param_arr) == FAILURE) { @@ -1990,7 +2148,7 @@ PHP_FUNCTION(pg_execute) RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (PQ_SETNONBLOCKING(pgsql, 0)) { php_error_docref(NULL TSRMLS_CC, E_NOTICE,"Cannot set connection to blocking mode"); @@ -2004,37 +2162,32 @@ PHP_FUNCTION(pg_execute) php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first"); } - zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr)); num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr)); if (num_params > 0) { int i = 0; params = (char **)safe_emalloc(sizeof(char *), num_params, 0); - - 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); - RETURN_FALSE; - } - if (Z_TYPE_PP(tmp) == IS_NULL) { + ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pv_param_arr), tmp) { + + if (Z_TYPE_P(tmp) == IS_NULL) { params[i] = NULL; } else { - zval tmp_val = **tmp; - zval_copy_ctor(&tmp_val); + zval tmp_val; + + ZVAL_COPY(&tmp_val, tmp); convert_to_string(&tmp_val); if (Z_TYPE(tmp_val) != IS_STRING) { php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter"); - zval_dtor(&tmp_val); + zval_ptr_dtor(&tmp_val); _php_pgsql_free_params(params, num_params); RETURN_FALSE; } params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val)); - zval_dtor(&tmp_val); + zval_ptr_dtor(&tmp_val); } - zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr)); - } + i++; + } ZEND_HASH_FOREACH_END(); } pgsql_result = PQexecPrepared(pgsql, stmtname, num_params, @@ -2051,7 +2204,7 @@ PHP_FUNCTION(pg_execute) } else { status = (ExecStatusType) PQstatus(pgsql); } - + _php_pgsql_free_params(params, num_params); switch (status) { @@ -2097,29 +2250,28 @@ static void php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAMETERS, int entry_ty return; } - ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); pgsql_result = pg_result->result; switch (entry_type) { case PHP_PG_NUM_ROWS: - Z_LVAL_P(return_value) = PQntuples(pgsql_result); + RETVAL_LONG(PQntuples(pgsql_result)); break; case PHP_PG_NUM_FIELDS: - Z_LVAL_P(return_value) = PQnfields(pgsql_result); + RETVAL_LONG(PQnfields(pgsql_result)); break; case PHP_PG_CMD_TUPLES: #if HAVE_PQCMDTUPLES - Z_LVAL_P(return_value) = atoi(PQcmdTuples(pgsql_result)); + RETVAL_LONG(atoi(PQcmdTuples(pgsql_result))); #else php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not supported under this build"); - Z_LVAL_P(return_value) = 0; + RETVAL_LONG(0); #endif break; default: RETURN_FALSE; } - Z_TYPE_P(return_value) = IS_LONG; } /* }}} */ @@ -2153,21 +2305,26 @@ PHP_FUNCTION(pg_affected_rows) Returns the last notice set by the backend */ PHP_FUNCTION(pg_last_notice) { - zval *pgsql_link; + zval *pgsql_link = NULL; PGconn *pg_link; int id = -1; - php_pgsql_notice **notice; + php_pgsql_notice *notice; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_link) == FAILURE) { return; } + + if (pgsql_link == NULL) { + RETURN_FALSE; + } + /* Just to check if user passed valid resoruce */ - ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pg_link, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); - if (zend_hash_index_find(&PGG(notices), Z_RESVAL_P(pgsql_link), (void **)¬ice) == FAILURE) { + if ((notice = zend_hash_index_find_ptr(&PGG(notices), (zend_ulong)Z_RES_HANDLE_P(pgsql_link))) == NULL) { RETURN_FALSE; } - RETURN_STRINGL((*notice)->message, (*notice)->len, 1); + RETURN_STRINGL(notice->message, notice->len); } /* }}} */ @@ -2177,7 +2334,7 @@ static char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list TSRMLS_DC) { PGresult *result; smart_str str = {0}; - zend_rsrc_list_entry *field_type; + zend_resource *field_type; char *ret=NULL; /* try to lookup the type in the resource list */ @@ -2185,41 +2342,41 @@ static char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list TSRMLS_DC) smart_str_append_unsigned(&str, oid); smart_str_0(&str); - if (zend_hash_find(list,str.c,str.len+1,(void **) &field_type)==SUCCESS) { + if ((field_type = zend_hash_find_ptr(list, str.s)) != NULL) { ret = estrdup((char *)field_type->ptr); } else { /* hash all oid's */ - int i,num_rows; + int i, num_rows; int oid_offset,name_offset; char *tmp_oid, *end_ptr, *tmp_name; - zend_rsrc_list_entry new_oid_entry; + zend_resource new_oid_entry; - if ((result = PQexec(pgsql,"select oid,typname from pg_type")) == NULL || PQresultStatus(result) != PGRES_TUPLES_OK) { + if ((result = PQexec(pgsql, "select oid,typname from pg_type")) == NULL || PQresultStatus(result) != PGRES_TUPLES_OK) { if (result) { PQclear(result); } smart_str_free(&str); - return STR_EMPTY_ALLOC(); + return estrndup("", sizeof("") - 1); } num_rows = PQntuples(result); oid_offset = PQfnumber(result,"oid"); name_offset = PQfnumber(result,"typname"); - + for (i=0; i<num_rows; i++) { if ((tmp_oid = PQgetvalue(result,i,oid_offset))==NULL) { continue; } - str.len = 0; + str.s->len = 0; smart_str_appends(&str, "pgsql_oid_"); smart_str_appends(&str, tmp_oid); smart_str_0(&str); - + if ((tmp_name = PQgetvalue(result,i,name_offset))==NULL) { continue; } - Z_TYPE(new_oid_entry) = le_string; + new_oid_entry.type = le_string; new_oid_entry.ptr = estrdup(tmp_name); - zend_hash_update(list,str.c,str.len+1,(void *) &new_oid_entry, sizeof(zend_rsrc_list_entry), NULL); + zend_hash_update_mem(list, str.s, (void *) &new_oid_entry, sizeof(zend_resource)); if (!ret && strtoul(tmp_oid, &end_ptr, 10)==oid) { ret = estrdup(tmp_name); } @@ -2230,7 +2387,7 @@ static char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list TSRMLS_DC) smart_str_free(&str); return ret; } -/* }}} */ +/* }}} */ #ifdef HAVE_PQFTABLE /* {{{ proto mixed pg_field_table(resource result, int field_number[, bool oid_only]) @@ -2239,18 +2396,18 @@ PHP_FUNCTION(pg_field_table) { zval *result; pgsql_result_handle *pg_result; - long fnum = -1; + zend_long fnum = -1; zend_bool return_oid = 0; Oid oid; smart_str hash_key = {0}; char *table_name; - zend_rsrc_list_entry *field_table; + zend_resource *field_table; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|b", &result, &fnum, &return_oid) == FAILURE) { return; } - ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); if (fnum < 0 || fnum >= PQnfields(pg_result->result)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad field offset specified"); @@ -2263,17 +2420,16 @@ PHP_FUNCTION(pg_field_table) RETURN_FALSE; } - if (return_oid) { -#if UINT_MAX > LONG_MAX /* Oid is unsigned int, we don't need this code, where LONG is wider */ - if (oid > LONG_MAX) { +#if UINT_MAX > ZEND_LONG_MAX /* Oid is unsigned int, we don't need this code, where LONG is wider */ + if (oid > ZEND_LONG_MAX) { smart_str oidstr = {0}; smart_str_append_unsigned(&oidstr, oid); smart_str_0(&oidstr); - RETURN_STRINGL(oidstr.c, oidstr.len, 0); + RETURN_STR(oidstr.s); } else #endif - RETURN_LONG((long)oid); + RETURN_LONG((zend_long)oid); } /* try to lookup the table name in the resource list */ @@ -2281,20 +2437,19 @@ PHP_FUNCTION(pg_field_table) smart_str_append_unsigned(&hash_key, oid); smart_str_0(&hash_key); - if (zend_hash_find(&EG(regular_list), hash_key.c, hash_key.len+1, (void **) &field_table) == SUCCESS) { + if ((field_table = zend_hash_find_ptr(&EG(regular_list), hash_key.s)) != NULL) { smart_str_free(&hash_key); - RETURN_STRING((char *)field_table->ptr, 1); + RETURN_STRING((char *)field_table->ptr); } else { /* Not found, lookup by querying PostgreSQL system tables */ PGresult *tmp_res; smart_str querystr = {0}; - zend_rsrc_list_entry new_field_table; + zend_resource new_field_table; smart_str_appends(&querystr, "select relname from pg_class where oid="); smart_str_append_unsigned(&querystr, oid); smart_str_0(&querystr); - - if ((tmp_res = PQexec(pg_result->conn, querystr.c)) == NULL || PQresultStatus(tmp_res) != PGRES_TUPLES_OK) { + if ((tmp_res = PQexec(pg_result->conn, querystr.s->val)) == NULL || PQresultStatus(tmp_res) != PGRES_TUPLES_OK) { if (tmp_res) { PQclear(tmp_res); } @@ -2311,18 +2466,18 @@ PHP_FUNCTION(pg_field_table) RETURN_FALSE; } - Z_TYPE(new_field_table) = le_string; + new_field_table.type = le_string; new_field_table.ptr = estrdup(table_name); - zend_hash_update(&EG(regular_list), hash_key.c, hash_key.len+1, (void *) &new_field_table, sizeof(zend_rsrc_list_entry), NULL); + zend_hash_update_mem(&EG(regular_list), hash_key.s, (void *)&new_field_table, sizeof(zend_resource)); smart_str_free(&hash_key); PQclear(tmp_res); - RETURN_STRING(table_name, 1); + RETURN_STRING(table_name); } } -/* }}} */ -#endif +/* }}} */ +#endif #define PHP_PG_FIELD_NAME 1 #define PHP_PG_FIELD_SIZE 2 @@ -2334,16 +2489,16 @@ PHP_FUNCTION(pg_field_table) static void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type) { zval *result; - long field; + zend_long field; PGresult *pgsql_result; pgsql_result_handle *pg_result; Oid oid; - + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &result, &field) == FAILURE) { return; } - - ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); pgsql_result = pg_result->result; @@ -2351,39 +2506,33 @@ static void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_typ php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad field offset specified"); RETURN_FALSE; } - + switch (entry_type) { case PHP_PG_FIELD_NAME: - Z_STRVAL_P(return_value) = PQfname(pgsql_result, field); - Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value)); - Z_STRVAL_P(return_value) = estrndup(Z_STRVAL_P(return_value),Z_STRLEN_P(return_value)); - Z_TYPE_P(return_value) = IS_STRING; + RETURN_STRING(PQfname(pgsql_result, field)); break; case PHP_PG_FIELD_SIZE: - Z_LVAL_P(return_value) = PQfsize(pgsql_result, field); - Z_TYPE_P(return_value) = IS_LONG; + RETURN_LONG(PQfsize(pgsql_result, field)); break; - case PHP_PG_FIELD_TYPE: - Z_STRVAL_P(return_value) = get_field_name(pg_result->conn, PQftype(pgsql_result, field), &EG(regular_list) TSRMLS_CC); - Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value)); - Z_TYPE_P(return_value) = IS_STRING; + case PHP_PG_FIELD_TYPE: { + char *name = get_field_name(pg_result->conn, PQftype(pgsql_result, field), &EG(regular_list) TSRMLS_CC); + RETVAL_STRING(name); + efree(name); + } break; case PHP_PG_FIELD_TYPE_OID: oid = PQftype(pgsql_result, field); -#if UINT_MAX > LONG_MAX - if (oid > LONG_MAX) { +#if UINT_MAX > ZEND_LONG_MAX + if (oid > ZEND_LONG_MAX) { smart_str s = {0}; smart_str_append_unsigned(&s, oid); smart_str_0(&s); - Z_STRVAL_P(return_value) = s.c; - Z_STRLEN_P(return_value) = s.len; - Z_TYPE_P(return_value) = IS_STRING; + RETURN_STR(s.s); } else #endif { - Z_LVAL_P(return_value) = (long)oid; - Z_TYPE_P(return_value) = IS_LONG; + RETURN_LONG((zend_long)oid); } break; default: @@ -2416,7 +2565,6 @@ PHP_FUNCTION(pg_field_type) } /* }}} */ - /* {{{ proto string pg_field_type_oid(resource result, int field_number) Returns the type oid for the given field */ PHP_FUNCTION(pg_field_type_oid) @@ -2431,20 +2579,19 @@ PHP_FUNCTION(pg_field_num) { zval *result; char *field; - int field_len; + size_t field_len; PGresult *pgsql_result; pgsql_result_handle *pg_result; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &result, &field, &field_len) == FAILURE) { return; } - - ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); pgsql_result = pg_result->result; - - Z_LVAL_P(return_value) = PQfnumber(pgsql_result, field); - Z_TYPE_P(return_value) = IS_LONG; + + RETURN_LONG(PQfnumber(pgsql_result, field)); } /* }}} */ @@ -2452,23 +2599,23 @@ PHP_FUNCTION(pg_field_num) Returns values from a result identifier */ PHP_FUNCTION(pg_fetch_result) { - zval *result, **field=NULL; - long row; + zval *result, *field=NULL; + zend_long row; PGresult *pgsql_result; pgsql_result_handle *pg_result; int field_offset, pgsql_row, argc = ZEND_NUM_ARGS(); - + if (argc == 2) { - if (zend_parse_parameters(argc TSRMLS_CC, "rZ", &result, &field) == FAILURE) { + if (zend_parse_parameters(argc TSRMLS_CC, "rz", &result, &field) == FAILURE) { return; } } else { - if (zend_parse_parameters(argc TSRMLS_CC, "rlZ", &result, &row, &field) == FAILURE) { + if (zend_parse_parameters(argc TSRMLS_CC, "rlz", &result, &row, &field) == FAILURE) { return; } } - ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); pgsql_result = pg_result->result; if (argc == 2) { @@ -2482,61 +2629,59 @@ PHP_FUNCTION(pg_fetch_result) } else { pgsql_row = row; if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %ld on PostgreSQL result index %ld", + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %pd on PostgreSQL result index %pd", row, Z_LVAL_P(result)); RETURN_FALSE; } } - switch(Z_TYPE_PP(field)) { + switch (Z_TYPE_P(field)) { case IS_STRING: - field_offset = PQfnumber(pgsql_result, Z_STRVAL_PP(field)); + field_offset = PQfnumber(pgsql_result, Z_STRVAL_P(field)); break; default: convert_to_long_ex(field); - field_offset = Z_LVAL_PP(field); + field_offset = Z_LVAL_P(field); break; } - if (field_offset<0 || field_offset>=PQnfields(pgsql_result)) { + if (field_offset < 0 || field_offset >= PQnfields(pgsql_result)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad column offset specified"); RETURN_FALSE; } - + if (PQgetisnull(pgsql_result, pgsql_row, field_offset)) { - Z_TYPE_P(return_value) = IS_NULL; + RETVAL_NULL(); } else { - char *value = PQgetvalue(pgsql_result, pgsql_row, field_offset); - int value_len = PQgetlength(pgsql_result, pgsql_row, field_offset); - ZVAL_STRINGL(return_value, value, value_len, 1); + RETVAL_STRINGL(PQgetvalue(pgsql_result, pgsql_row, field_offset), + PQgetlength(pgsql_result, pgsql_row, field_offset)); } } /* }}} */ /* {{{ void php_pgsql_fetch_hash */ -static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, long result_type, int into_object) +static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, zend_long result_type, int into_object) { zval *result, *zrow = NULL; PGresult *pgsql_result; pgsql_result_handle *pg_result; int i, num_fields, pgsql_row, use_row; - long row = -1; + zend_long row = -1; char *field_name; zval *ctor_params = NULL; zend_class_entry *ce = NULL; if (into_object) { - char *class_name = NULL; - int class_name_len; + zend_string *class_name = NULL; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|z!sz", &result, &zrow, &class_name, &class_name_len, &ctor_params) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|z!Sz", &result, &zrow, &class_name, &ctor_params) == FAILURE) { return; } if (!class_name) { ce = zend_standard_class_def; } else { - ce = zend_fetch_class(class_name, class_name_len, ZEND_FETCH_CLASS_AUTO TSRMLS_CC); + ce = zend_fetch_class(class_name, ZEND_FETCH_CLASS_AUTO TSRMLS_CC); } if (!ce) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not find class '%s'", class_name); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not find class '%s'", class_name->val); return; } result_type = PGSQL_ASSOC; @@ -2562,7 +2707,7 @@ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, long result_type, RETURN_FALSE; } - ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); pgsql_result = pg_result->result; @@ -2570,7 +2715,7 @@ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, long result_type, pgsql_row = row; pg_result->row = pgsql_row; if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %ld on PostgreSQL result index %ld", + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %pd on PostgreSQL result index %pd", row, Z_LVAL_P(result)); RETURN_FALSE; } @@ -2596,83 +2741,71 @@ static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, long result_type, } else { char *element = PQgetvalue(pgsql_result, pgsql_row, i); if (element) { - char *data; - int data_len; - int should_copy=0; - const uint element_len = strlen(element); + const size_t element_len = strlen(element); - data = safe_estrndup(element, element_len); - data_len = element_len; - if (result_type & PGSQL_NUM) { - add_index_stringl(return_value, i, data, data_len, should_copy); - should_copy=1; + add_index_stringl(return_value, i, element, element_len); } - + if (result_type & PGSQL_ASSOC) { field_name = PQfname(pgsql_result, i); - add_assoc_stringl(return_value, field_name, data, data_len, should_copy); + add_assoc_stringl(return_value, field_name, element, element_len); } } } } if (into_object) { - zval dataset = *return_value; + zval dataset; zend_fcall_info fci; zend_fcall_info_cache fcc; - zval *retval_ptr; - + zval retval; + + ZVAL_COPY_VALUE(&dataset, return_value); object_and_properties_init(return_value, ce, NULL); - zend_merge_properties(return_value, Z_ARRVAL(dataset), 1 TSRMLS_CC); - + if (!ce->default_properties_count && !ce->__set) { + ALLOC_HASHTABLE(Z_OBJ_P(return_value)->properties); + *Z_OBJ_P(return_value)->properties = *Z_ARRVAL(dataset); + efree(Z_ARR(dataset)); + } else { + zend_merge_properties(return_value, Z_ARRVAL(dataset) TSRMLS_CC); + zval_ptr_dtor(&dataset); + } + if (ce->constructor) { fci.size = sizeof(fci); fci.function_table = &ce->function_table; - fci.function_name = NULL; + ZVAL_UNDEF(&fci.function_name); fci.symbol_table = NULL; - fci.object_ptr = return_value; - fci.retval_ptr_ptr = &retval_ptr; + fci.object = Z_OBJ_P(return_value); + fci.retval = &retval; + 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; fcc.calling_scope = EG(scope); fcc.called_scope = Z_OBJCE_P(return_value); - fcc.object_ptr = return_value; - + fcc.object = Z_OBJ_P(return_value); + if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) { zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Could not execute %s::%s()", ce->name, ce->constructor->common.function_name); } else { - if (retval_ptr) { - zval_ptr_dtor(&retval_ptr); - } + zval_ptr_dtor(&retval); } if (fci.params) { efree(fci.params); @@ -2734,7 +2867,7 @@ PHP_FUNCTION(pg_fetch_all) return; } - ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); pgsql_result = pg_result->result; array_init(return_value); @@ -2752,7 +2885,7 @@ PHP_FUNCTION(pg_fetch_all_columns) zval *result; PGresult *pgsql_result; pgsql_result_handle *pg_result; - unsigned long colno=0; + zend_long colno=0; int pg_numrows, pg_row; size_t num_fields; @@ -2760,19 +2893,19 @@ PHP_FUNCTION(pg_fetch_all_columns) RETURN_FALSE; } - ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); pgsql_result = pg_result->result; num_fields = PQnfields(pgsql_result); if (colno >= num_fields || colno < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid column number '%ld'", colno); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid column number '%pd'", colno); RETURN_FALSE; } array_init(return_value); - if ((pg_numrows = PQntuples(pgsql_result)) <= 0) { + if ((pg_numrows = PQntuples(pgsql_result)) <= 0) { return; } @@ -2780,8 +2913,8 @@ PHP_FUNCTION(pg_fetch_all_columns) if (PQgetisnull(pgsql_result, pg_row, colno)) { add_next_index_null(return_value); } else { - add_next_index_string(return_value, PQgetvalue(pgsql_result, pg_row, colno), 1); - } + add_next_index_string(return_value, PQgetvalue(pgsql_result, pg_row, colno)); + } } } /* }}} */ @@ -2791,26 +2924,25 @@ PHP_FUNCTION(pg_fetch_all_columns) PHP_FUNCTION(pg_result_seek) { zval *result; - long row; + zend_long row; pgsql_result_handle *pg_result; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &result, &row) == FAILURE) { return; } - ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); if (row < 0 || row >= PQntuples(pg_result->result)) { RETURN_FALSE; } - + /* seek to offset */ pg_result->row = row; RETURN_TRUE; } /* }}} */ - #define PHP_PG_DATA_LENGTH 1 #define PHP_PG_DATA_ISNULL 2 @@ -2818,23 +2950,23 @@ PHP_FUNCTION(pg_result_seek) */ static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type) { - zval *result, **field; - long row; + zval *result, *field; + zend_long row; PGresult *pgsql_result; pgsql_result_handle *pg_result; int field_offset, pgsql_row, argc = ZEND_NUM_ARGS(); if (argc == 2) { - if (zend_parse_parameters(argc TSRMLS_CC, "rZ", &result, &field) == FAILURE) { + if (zend_parse_parameters(argc TSRMLS_CC, "rz", &result, &field) == FAILURE) { return; } } else { - if (zend_parse_parameters(argc TSRMLS_CC, "rlZ", &result, &row, &field) == FAILURE) { + if (zend_parse_parameters(argc TSRMLS_CC, "rlz", &result, &row, &field) == FAILURE) { return; } } - - ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); pgsql_result = pg_result->result; if (argc == 2) { @@ -2848,36 +2980,35 @@ static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type) } else { pgsql_row = row; if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %ld on PostgreSQL result index %ld", + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %pd on PostgreSQL result index %pd", row, Z_LVAL_P(result)); RETURN_FALSE; } } - - switch(Z_TYPE_PP(field)) { + + switch (Z_TYPE_P(field)) { case IS_STRING: convert_to_string_ex(field); - field_offset = PQfnumber(pgsql_result, Z_STRVAL_PP(field)); + field_offset = PQfnumber(pgsql_result, Z_STRVAL_P(field)); break; default: convert_to_long_ex(field); - field_offset = Z_LVAL_PP(field); + field_offset = Z_LVAL_P(field); break; } if (field_offset < 0 || field_offset >= PQnfields(pgsql_result)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad column offset specified"); RETURN_FALSE; } - + switch (entry_type) { case PHP_PG_DATA_LENGTH: - Z_LVAL_P(return_value) = PQgetlength(pgsql_result, pgsql_row, field_offset); + RETVAL_LONG(PQgetlength(pgsql_result, pgsql_row, field_offset)); break; case PHP_PG_DATA_ISNULL: - Z_LVAL_P(return_value) = PQgetisnull(pgsql_result, pgsql_row, field_offset); + RETVAL_LONG(PQgetisnull(pgsql_result, pgsql_row, field_offset)) break; } - Z_TYPE_P(return_value) = IS_LONG; } /* }}} */ @@ -2903,16 +3034,16 @@ PHP_FUNCTION(pg_free_result) { zval *result; pgsql_result_handle *pg_result; - + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) { return; } - ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); - if (Z_LVAL_P(result) == 0) { + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); + if (Z_RES_P(result) == NULL) { RETURN_FALSE; } - zend_list_delete(Z_RESVAL_P(result)); + zend_list_close(Z_RES_P(result)); RETURN_TRUE; } /* }}} */ @@ -2931,8 +3062,8 @@ PHP_FUNCTION(pg_last_oid) if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) { return; } - - ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); pgsql_result = pg_result->result; #ifdef HAVE_PQOIDVALUE oid = PQoidValue(pgsql_result); @@ -2943,9 +3074,9 @@ PHP_FUNCTION(pg_last_oid) #else Z_STRVAL_P(return_value) = (char *) PQoidStatus(pgsql_result); if (Z_STRVAL_P(return_value)) { - RETURN_STRING(Z_STRVAL_P(return_value), 1); + RETURN_STRING(Z_STRVAL_P(return_value)); } - RETURN_STRING("", 1); + RETURN_EMPTY_STRING(); #endif } /* }}} */ @@ -2955,27 +3086,27 @@ PHP_FUNCTION(pg_last_oid) PHP_FUNCTION(pg_trace) { char *z_filename, *mode = "w"; - int z_filename_len, mode_len; + size_t z_filename_len, mode_len; zval *pgsql_link = NULL; int id = -1, argc = ZEND_NUM_ARGS(); PGconn *pgsql; FILE *fp = NULL; php_stream *stream; - id = PGG(default_link); - + id = PGG(default_link)? PGG(default_link)->handle : -1; + if (zend_parse_parameters(argc TSRMLS_CC, "s|sr", &z_filename, &z_filename_len, &mode, &mode_len, &pgsql_link) == FAILURE) { return; } if (argc < 3) { CHECK_DEFAULT_LINK(id); - } + } if (pgsql_link == NULL && id == -1) { RETURN_FALSE; - } + } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); stream = php_stream_open_wrapper(z_filename, mode, REPORT_ERRORS, NULL); @@ -3006,15 +3137,15 @@ PHP_FUNCTION(pg_untrace) } if (argc == 0) { - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } if (pgsql_link == NULL && id == -1) { RETURN_FALSE; - } + } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); PQuntrace(pgsql); RETURN_TRUE; } @@ -3024,10 +3155,10 @@ PHP_FUNCTION(pg_untrace) Create a large object */ PHP_FUNCTION(pg_lo_create) { - zval *pgsql_link = NULL, *oid = NULL; - PGconn *pgsql; - Oid pgsql_oid, wanted_oid = InvalidOid; - int id = -1, argc = ZEND_NUM_ARGS(); + zval *pgsql_link = NULL, *oid = NULL; + PGconn *pgsql; + Oid pgsql_oid, wanted_oid = InvalidOid; + int id = -1, argc = ZEND_NUM_ARGS(); if (zend_parse_parameters(argc TSRMLS_CC, "|zz", &pgsql_link, &oid) == FAILURE) { return; @@ -3039,22 +3170,22 @@ PHP_FUNCTION(pg_lo_create) } if (pgsql_link == NULL) { - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); if (id == -1) { RETURN_FALSE; } } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (oid) { -#ifndef HAVE_PG_LO_CREATE - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "OID value passing not supported"); +#ifndef HAVE_PG_LO_CREATE + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Passing OID value is not supported. Upgrade your PostgreSQL"); #else switch (Z_TYPE_P(oid)) { case IS_STRING: - { + { char *end_ptr; wanted_oid = (Oid)strtoul(Z_STRVAL_P(oid), &end_ptr, 10); if ((Z_STRVAL_P(oid)+Z_STRLEN_P(oid)) != end_ptr) { @@ -3065,7 +3196,7 @@ PHP_FUNCTION(pg_lo_create) } break; case IS_LONG: - if (Z_LVAL_P(oid) < (long)InvalidOid) { + if (Z_LVAL_P(oid) < (zend_long)InvalidOid) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "invalid OID value passed"); RETURN_FALSE; } @@ -3080,7 +3211,7 @@ PHP_FUNCTION(pg_lo_create) RETURN_FALSE; } - PGSQL_RETURN_OID(pgsql_oid); + PGSQL_RETURN_OID(pgsql_oid); #endif } @@ -3089,7 +3220,7 @@ PHP_FUNCTION(pg_lo_create) RETURN_FALSE; } - PGSQL_RETURN_OID(pgsql_oid); + PGSQL_RETURN_OID(pgsql_oid); } /* }}} */ @@ -3098,9 +3229,9 @@ PHP_FUNCTION(pg_lo_create) PHP_FUNCTION(pg_lo_unlink) { zval *pgsql_link = NULL; - long oid_long; + zend_long oid_long; char *oid_string, *end_ptr; - int oid_strlen; + size_t oid_strlen; PGconn *pgsql; Oid oid; int id = -1; @@ -3132,7 +3263,7 @@ PHP_FUNCTION(pg_lo_unlink) php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed"); RETURN_FALSE; } - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, @@ -3142,7 +3273,7 @@ PHP_FUNCTION(pg_lo_unlink) RETURN_FALSE; } oid = (Oid)oid_long; - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } else { @@ -3151,9 +3282,9 @@ PHP_FUNCTION(pg_lo_unlink) } if (pgsql_link == NULL && id == -1) { RETURN_FALSE; - } + } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (lo_unlink(pgsql, oid) == -1) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to delete PostgreSQL large object %u", oid); @@ -3168,13 +3299,13 @@ PHP_FUNCTION(pg_lo_unlink) PHP_FUNCTION(pg_lo_open) { zval *pgsql_link = NULL; - long oid_long; + zend_long oid_long; char *oid_string, *end_ptr, *mode_string; - int oid_strlen, mode_strlen; + size_t oid_strlen, mode_strlen; PGconn *pgsql; Oid oid; int id = -1, pgsql_mode=0, pgsql_lofd; - int create=0; + int create = 0; pgLofp *pgsql_lofp; int argc = ZEND_NUM_ARGS(); @@ -3204,7 +3335,7 @@ PHP_FUNCTION(pg_lo_open) php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed"); RETURN_FALSE; } - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, @@ -3214,7 +3345,7 @@ PHP_FUNCTION(pg_lo_open) RETURN_FALSE; } oid = (Oid)oid_long; - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } else { @@ -3223,9 +3354,9 @@ PHP_FUNCTION(pg_lo_open) } if (pgsql_link == NULL && id == -1) { RETURN_FALSE; - } + } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); /* r/w/+ is little bit more PHP-like than INV_READ/INV_WRITE and a lot of faster to type. Unfortunately, doesn't behave the same way as fopen()... @@ -3267,8 +3398,7 @@ PHP_FUNCTION(pg_lo_open) } else { pgsql_lofp->conn = pgsql; pgsql_lofp->lofd = pgsql_lofd; - Z_LVAL_P(return_value) = zend_list_insert(pgsql_lofp, le_lofp TSRMLS_CC); - Z_TYPE_P(return_value) = IS_LONG; + ZEND_REGISTER_RESOURCE(return_value, pgsql_lofp, le_lofp); } } } else { @@ -3295,7 +3425,7 @@ PHP_FUNCTION(pg_lo_close) return; } - ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_lofp, -1, "PostgreSQL large object", le_lofp); + ZEND_FETCH_RESOURCE(pgsql, pgLofp *, pgsql_lofp, -1, "PostgreSQL large object", le_lofp); if (lo_close((PGconn *)pgsql->conn, pgsql->lofd) < 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to close PostgreSQL large object descriptor %d", pgsql->lofd); @@ -3304,7 +3434,7 @@ PHP_FUNCTION(pg_lo_close) RETVAL_TRUE; } - zend_list_delete(Z_RESVAL_P(pgsql_lofp)); + zend_list_close(Z_RES_P(pgsql_lofp)); return; } /* }}} */ @@ -3315,30 +3445,31 @@ PHP_FUNCTION(pg_lo_close) Read a large object */ PHP_FUNCTION(pg_lo_read) { - zval *pgsql_id; - long len; + zval *pgsql_id; + zend_long len; int buf_len = PGSQL_LO_READ_BUF_SIZE, nbytes, argc = ZEND_NUM_ARGS(); - char *buf; + zend_string *buf; pgLofp *pgsql; if (zend_parse_parameters(argc TSRMLS_CC, "r|l", &pgsql_id, &len) == FAILURE) { return; } - ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp); + ZEND_FETCH_RESOURCE(pgsql, pgLofp *, pgsql_id, -1, "PostgreSQL large object", le_lofp); if (argc > 1) { buf_len = len; } - buf = (char *) safe_emalloc(sizeof(char), (buf_len+1), 0); - if ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, buf, buf_len))<0) { - efree(buf); + buf = zend_string_alloc(buf_len, 0); + if ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, buf->val, buf->len))<0) { + zend_string_free(buf); RETURN_FALSE; } - buf[nbytes] = '\0'; - RETURN_STRINGL(buf, nbytes, 0); + buf->len = nbytes; + buf->val[buf->len] = '\0'; + RETURN_STR(buf); } /* }}} */ @@ -3348,9 +3479,9 @@ PHP_FUNCTION(pg_lo_write) { zval *pgsql_id; char *str; - long z_len; - int str_len, nbytes; - int len; + zend_long z_len; + size_t str_len, nbytes; + size_t len; pgLofp *pgsql; int argc = ZEND_NUM_ARGS(); @@ -3360,11 +3491,11 @@ PHP_FUNCTION(pg_lo_write) if (argc > 2) { if (z_len > str_len) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot write more than buffer size %d. Tried to write %ld", str_len, z_len); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot write more than buffer size %d. Tried to write %pd", str_len, z_len); RETURN_FALSE; } if (z_len < 0) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Buffer size must be larger than 0, but %ld was specified", z_len); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Buffer size must be larger than 0, but %pd was specified", z_len); RETURN_FALSE; } len = z_len; @@ -3373,7 +3504,7 @@ PHP_FUNCTION(pg_lo_write) len = str_len; } - ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp); + ZEND_FETCH_RESOURCE(pgsql, pgLofp *, pgsql_id, -1, "PostgreSQL large object", le_lofp); if ((nbytes = lo_write((PGconn *)pgsql->conn, pgsql->lofd, str, len)) == -1) { RETURN_FALSE; @@ -3397,7 +3528,7 @@ PHP_FUNCTION(pg_lo_read_all) return; } - ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp); + ZEND_FETCH_RESOURCE(pgsql, pgLofp *, pgsql_id, -1, "PostgreSQL large object", le_lofp); tbytes = 0; while ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, buf, PGSQL_LO_READ_BUF_SIZE))>0) { @@ -3414,7 +3545,8 @@ PHP_FUNCTION(pg_lo_import) { zval *pgsql_link = NULL, *oid = NULL; char *file_in; - int id = -1, name_len; + int id = -1; + size_t name_len; int argc = ZEND_NUM_ARGS(); PGconn *pgsql; Oid returned_oid; @@ -3425,7 +3557,7 @@ PHP_FUNCTION(pg_lo_import) } else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, "p|z", &file_in, &name_len, &oid) == SUCCESS) { - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } /* old calling convention, deprecated since PHP 4.2 */ @@ -3443,9 +3575,9 @@ PHP_FUNCTION(pg_lo_import) if (pgsql_link == NULL && id == -1) { RETURN_FALSE; - } + } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (oid) { #ifndef HAVE_PG_LO_IMPORT_WITH_OID @@ -3454,7 +3586,7 @@ PHP_FUNCTION(pg_lo_import) Oid wanted_oid; switch (Z_TYPE_P(oid)) { case IS_STRING: - { + { char *end_ptr; wanted_oid = (Oid)strtoul(Z_STRVAL_P(oid), &end_ptr, 10); if ((Z_STRVAL_P(oid)+Z_STRLEN_P(oid)) != end_ptr) { @@ -3465,7 +3597,7 @@ PHP_FUNCTION(pg_lo_import) } break; case IS_LONG: - if (Z_LVAL_P(oid) < (long)InvalidOid) { + if (Z_LVAL_P(oid) < (zend_long)InvalidOid) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "invalid OID value passed"); RETURN_FALSE; } @@ -3501,9 +3633,10 @@ PHP_FUNCTION(pg_lo_export) { zval *pgsql_link = NULL; char *file_out, *oid_string, *end_ptr; - int oid_strlen; - int id = -1, name_len; - long oid_long; + size_t oid_strlen; + int id = -1; + size_t name_len; + zend_long oid_long; Oid oid; PGconn *pgsql; int argc = ZEND_NUM_ARGS(); @@ -3533,7 +3666,7 @@ PHP_FUNCTION(pg_lo_export) RETURN_FALSE; } oid = (Oid)oid_long; - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, @@ -3544,7 +3677,7 @@ PHP_FUNCTION(pg_lo_export) php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed"); RETURN_FALSE; } - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, @@ -3576,13 +3709,13 @@ PHP_FUNCTION(pg_lo_export) if (pgsql_link == NULL && id == -1) { RETURN_FALSE; - } + } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (lo_export(pgsql, oid, file_out)) { RETURN_TRUE; - } + } RETURN_FALSE; } /* }}} */ @@ -3592,7 +3725,7 @@ PHP_FUNCTION(pg_lo_export) PHP_FUNCTION(pg_lo_seek) { zval *pgsql_id = NULL; - long offset = 0, whence = SEEK_CUR; + zend_long result, offset = 0, whence = SEEK_CUR; pgLofp *pgsql; int argc = ZEND_NUM_ARGS(); @@ -3604,9 +3737,18 @@ PHP_FUNCTION(pg_lo_seek) return; } - ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp); + 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; @@ -3619,7 +3761,7 @@ PHP_FUNCTION(pg_lo_seek) PHP_FUNCTION(pg_lo_tell) { zval *pgsql_id = NULL; - int offset = 0; + zend_long offset = 0; pgLofp *pgsql; int argc = ZEND_NUM_ARGS(); @@ -3627,20 +3769,63 @@ PHP_FUNCTION(pg_lo_tell) return; } - ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp); + 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 */ PHP_FUNCTION(pg_set_error_verbosity) { zval *pgsql_link = NULL; - long verbosity; + zend_long verbosity; int id = -1, argc = ZEND_NUM_ARGS(); PGconn *pgsql; @@ -3648,7 +3833,7 @@ PHP_FUNCTION(pg_set_error_verbosity) if (zend_parse_parameters(argc TSRMLS_CC, "l", &verbosity) == FAILURE) { return; } - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } else { if (zend_parse_parameters(argc TSRMLS_CC, "rl", &pgsql_link, &verbosity) == FAILURE) { @@ -3660,11 +3845,10 @@ PHP_FUNCTION(pg_set_error_verbosity) RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (verbosity & (PQERRORS_TERSE|PQERRORS_DEFAULT|PQERRORS_VERBOSE)) { - Z_LVAL_P(return_value) = PQsetErrorVerbosity(pgsql, verbosity); - Z_TYPE_P(return_value) = IS_LONG; + RETURN_LONG(PQsetErrorVerbosity(pgsql, verbosity)); } else { RETURN_FALSE; } @@ -3678,7 +3862,7 @@ PHP_FUNCTION(pg_set_error_verbosity) PHP_FUNCTION(pg_set_client_encoding) { char *encoding; - int encoding_len; + size_t encoding_len; zval *pgsql_link = NULL; int id = -1, argc = ZEND_NUM_ARGS(); PGconn *pgsql; @@ -3687,7 +3871,7 @@ PHP_FUNCTION(pg_set_client_encoding) if (zend_parse_parameters(argc TSRMLS_CC, "s", &encoding, &encoding_len) == FAILURE) { return; } - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } else { if (zend_parse_parameters(argc TSRMLS_CC, "rs", &pgsql_link, &encoding, &encoding_len) == FAILURE) { @@ -3697,12 +3881,11 @@ PHP_FUNCTION(pg_set_client_encoding) if (pgsql_link == NULL && id == -1) { RETURN_FALSE; - } + } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); - Z_LVAL_P(return_value) = PQsetClientEncoding(pgsql, encoding); - Z_TYPE_P(return_value) = IS_LONG; + RETURN_LONG(PQsetClientEncoding(pgsql, encoding)); } /* }}} */ @@ -3719,7 +3902,7 @@ PHP_FUNCTION(pg_client_encoding) } if (argc == 0) { - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } @@ -3727,18 +3910,11 @@ PHP_FUNCTION(pg_client_encoding) RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); /* Just do the same as found in PostgreSQL sources... */ -#ifndef HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT -#define pg_encoding_to_char(x) "SQL_ASCII" -#endif - - Z_STRVAL_P(return_value) = (char *) pg_encoding_to_char(PQclientEncoding(pgsql)); - Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value)); - Z_STRVAL_P(return_value) = (char *) estrdup(Z_STRVAL_P(return_value)); - Z_TYPE_P(return_value) = IS_STRING; + RETURN_STRING((char *) pg_encoding_to_char(PQclientEncoding(pgsql))); } /* }}} */ #endif @@ -3761,15 +3937,15 @@ PHP_FUNCTION(pg_end_copy) } if (argc == 0) { - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } if (pgsql_link == NULL && id == -1) { RETURN_FALSE; - } + } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); result = PQendcopy(pgsql); @@ -3781,14 +3957,14 @@ PHP_FUNCTION(pg_end_copy) } /* }}} */ - /* {{{ proto bool pg_put_line([resource connection,] string query) Send null-terminated string to backend server*/ PHP_FUNCTION(pg_put_line) { char *query; zval *pgsql_link = NULL; - int query_len, id = -1; + size_t query_len; + int id = -1; PGconn *pgsql; int result = 0, argc = ZEND_NUM_ARGS(); @@ -3796,7 +3972,7 @@ PHP_FUNCTION(pg_put_line) if (zend_parse_parameters(argc TSRMLS_CC, "s", &query, &query_len) == FAILURE) { return; } - id = PGG(default_link); + id = PGG(default_link) ? PGG(default_link)->handle : -1; CHECK_DEFAULT_LINK(id); } else { if (zend_parse_parameters(argc TSRMLS_CC, "rs", &pgsql_link, &query, &query_len) == FAILURE) { @@ -3808,7 +3984,7 @@ PHP_FUNCTION(pg_put_line) RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); result = PQputline(pgsql, query); if (result==EOF) { @@ -3825,7 +4001,7 @@ PHP_FUNCTION(pg_copy_to) { zval *pgsql_link; char *table_name, *pg_delim = NULL, *pg_null_as = NULL; - int table_name_len, pg_delim_len, pg_null_as_len, free_pg_null = 0; + size_t table_name_len, pg_delim_len, pg_null_as_len, free_pg_null = 0; char *query; int id = -1; PGconn *pgsql; @@ -3848,10 +4024,10 @@ PHP_FUNCTION(pg_copy_to) pg_delim = "\t"; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (!pg_null_as) { - pg_null_as = safe_estrdup("\\\\N"); + pg_null_as = estrdup("\\\\N"); free_pg_null = 1; } @@ -3891,7 +4067,7 @@ PHP_FUNCTION(pg_copy_to) RETURN_FALSE; break; default: - add_next_index_string(return_value, csv, 1); + add_next_index_string(return_value, csv); PQfreemem(csv); break; } @@ -3903,7 +4079,7 @@ PHP_FUNCTION(pg_copy_to) PHP_PQ_ERROR("getline failed: %s", pgsql); RETURN_FALSE; } - + if (copybuf[0] == '\\' && copybuf[1] == '.' && copybuf[2] == '\0') @@ -3924,7 +4100,7 @@ PHP_FUNCTION(pg_copy_to) case EOF: copydone = 1; case 0: - add_next_index_string(return_value, csv, 1); + add_next_index_string(return_value, csv); efree(csv); csv = (char *)NULL; break; @@ -3960,12 +4136,11 @@ PHP_FUNCTION(pg_copy_to) PHP_FUNCTION(pg_copy_from) { zval *pgsql_link = NULL, *pg_rows; - zval **tmp; + zval *tmp; char *table_name, *pg_delim = NULL, *pg_null_as = NULL; - int table_name_len, pg_delim_len, pg_null_as_len; + size_t table_name_len, pg_delim_len, pg_null_as_len; int pg_null_as_free = 0; char *query; - HashPosition pos; int id = -1; PGconn *pgsql; PGresult *pgsql_result; @@ -3981,11 +4156,11 @@ PHP_FUNCTION(pg_copy_from) pg_delim = "\t"; } if (!pg_null_as) { - pg_null_as = safe_estrdup("\\\\N"); + pg_null_as = estrdup("\\\\N"); pg_null_as_free = 1; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); spprintf(&query, 0, "COPY %s FROM STDIN DELIMITERS E'%c' WITH NULL AS E'%s'", table_name, *pg_delim, pg_null_as); while ((pgsql_result = PQgetResult(pgsql))) { @@ -4009,14 +4184,13 @@ PHP_FUNCTION(pg_copy_from) if (pgsql_result) { int command_failed = 0; PQclear(pgsql_result); - zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(pg_rows), &pos); #if HAVE_PQPUTCOPYDATA - while (zend_hash_get_current_data_ex(Z_ARRVAL_P(pg_rows), (void **) &tmp, &pos) == SUCCESS) { + ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pg_rows), tmp) { convert_to_string_ex(tmp); - query = (char *)emalloc(Z_STRLEN_PP(tmp) + 2); - strlcpy(query, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp) + 2); - if(Z_STRLEN_PP(tmp) > 0 && *(query + Z_STRLEN_PP(tmp) - 1) != '\n') { - strlcat(query, "\n", Z_STRLEN_PP(tmp) + 2); + query = (char *)emalloc(Z_STRLEN_P(tmp) + 2); + strlcpy(query, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp) + 2); + if(Z_STRLEN_P(tmp) > 0 && *(query + Z_STRLEN_P(tmp) - 1) != '\n') { + strlcat(query, "\n", Z_STRLEN_P(tmp) + 2); } if (PQputCopyData(pgsql, query, strlen(query)) != 1) { efree(query); @@ -4024,19 +4198,18 @@ PHP_FUNCTION(pg_copy_from) RETURN_FALSE; } efree(query); - zend_hash_move_forward_ex(Z_ARRVAL_P(pg_rows), &pos); - } + } ZEND_HASH_FOREACH_END(); if (PQputCopyEnd(pgsql, NULL) != 1) { PHP_PQ_ERROR("putcopyend failed: %s", pgsql); RETURN_FALSE; } #else - while (zend_hash_get_current_data_ex(Z_ARRVAL_P(pg_rows), (void **) &tmp, &pos) == SUCCESS) { + ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pg_rows), tmp) { convert_to_string_ex(tmp); - query = (char *)emalloc(Z_STRLEN_PP(tmp) + 2); - strlcpy(query, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp) + 2); - if(Z_STRLEN_PP(tmp) > 0 && *(query + Z_STRLEN_PP(tmp) - 1) != '\n') { - strlcat(query, "\n", Z_STRLEN_PP(tmp) + 2); + query = (char *)emalloc(Z_STRLEN_P(tmp) + 2); + strlcpy(query, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp) + 2); + if(Z_STRLEN_P(tmp) > 0 && *(query + Z_STRLEN_P(tmp) - 1) != '\n') { + strlcat(query, "\n", Z_STRLEN_P(tmp) + 2); } if (PQputline(pgsql, query)==EOF) { efree(query); @@ -4044,8 +4217,7 @@ PHP_FUNCTION(pg_copy_from) RETURN_FALSE; } efree(query); - zend_hash_move_forward_ex(Z_ARRVAL_P(pg_rows), &pos); - } + } ZEND_HASH_FOREACH_END(); if (PQputline(pgsql, "\\.\n") == EOF) { PHP_PQ_ERROR("putline failed: %s", pgsql); RETURN_FALSE; @@ -4085,42 +4257,42 @@ PHP_FUNCTION(pg_copy_from) Escape string for text/char type */ PHP_FUNCTION(pg_escape_string) { - char *from = NULL, *to = NULL; + zend_string *from = NULL, *to = NULL; zval *pgsql_link; #ifdef HAVE_PQESCAPE_CONN PGconn *pgsql; #endif - int to_len; - int from_len; int id = -1; switch (ZEND_NUM_ARGS()) { case 1: - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &from, &from_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &from) == FAILURE) { return; } pgsql_link = NULL; - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; break; default: - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pgsql_link, &from, &from_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rS", &pgsql_link, &from) == FAILURE) { return; } break; } - to = (char *) safe_emalloc(from_len, 2, 1); - + to = zend_string_alloc(from->len * 2, 0); #ifdef HAVE_PQESCAPE_CONN if (pgsql_link != NULL || id != -1) { - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); - to_len = (int) PQescapeStringConn(pgsql, to, from, (size_t)from_len, NULL); - } else + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); + to->len = PQescapeStringConn(pgsql, to->val, from->val, from->len, NULL); + } else #endif - to_len = (int) PQescapeString(to, from, (size_t)from_len); + { + to->len = PQescapeString(to->val, from->val, from->len); + } - RETURN_STRINGL(to, to_len, 0); + to = zend_string_realloc(to, to->len, 0); + RETURN_STR(to); } /* }}} */ @@ -4130,7 +4302,8 @@ PHP_FUNCTION(pg_escape_bytea) { char *from = NULL, *to = NULL; size_t to_len; - int from_len, id = -1; + size_t from_len; + int id = -1; #ifdef HAVE_PQESCAPE_BYTEA_CONN PGconn *pgsql; #endif @@ -4142,7 +4315,7 @@ PHP_FUNCTION(pg_escape_bytea) return; } pgsql_link = NULL; - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : -1; break; default: @@ -4154,14 +4327,13 @@ PHP_FUNCTION(pg_escape_bytea) #ifdef HAVE_PQESCAPE_BYTEA_CONN if (pgsql_link != NULL || id != -1) { - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); to = (char *)PQescapeByteaConn(pgsql, (unsigned char *)from, (size_t)from_len, &to_len); } else #endif to = (char *)PQescapeBytea((unsigned char*)from, from_len, &to_len); - RETVAL_STRINGL(to, to_len-1, 1); /* to_len includes addtional '\0' */ - PQfreemem(to); + RETVAL_STRINGL(to, to_len-1); /* to_len includes addtional '\0' */ } /* }}} */ @@ -4189,9 +4361,9 @@ PHP_FUNCTION(pg_escape_bytea) * 5 \' * 6 \\ */ -static unsigned char * php_pgsql_unescape_bytea(unsigned char *strtext, size_t *retbuflen) +static unsigned char * php_pgsql_unescape_bytea(unsigned char *strtext, size_t *retbuflen) /* {{{ */ { - size_t buflen; + size_t buflen; unsigned char *buffer, *sp, *bp; @@ -4251,7 +4423,7 @@ static unsigned char * php_pgsql_unescape_bytea(unsigned char *strtext, size_t * memcpy(buf, sp-2, 3); buf[3] = '\0'; start = buf; - *bp = (unsigned char)strtoul(start, (char **)&end, 8); + *bp = (unsigned char)strtoul(start, (char **)&end, 8); buflen -= 3; state = 0; } @@ -4269,6 +4441,7 @@ static unsigned char * php_pgsql_unescape_bytea(unsigned char *strtext, size_t * *retbuflen = buflen; return buffer; } +/* }}} */ #endif /* {{{ proto string pg_unescape_bytea(string data) @@ -4277,7 +4450,7 @@ PHP_FUNCTION(pg_unescape_bytea) { char *from = NULL, *to = NULL, *tmp = NULL; size_t to_len; - int from_len; + size_t from_len; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &from, &from_len) == FAILURE) { return; @@ -4294,67 +4467,20 @@ PHP_FUNCTION(pg_unescape_bytea) php_error_docref(NULL TSRMLS_CC, E_WARNING,"Invalid parameter"); RETURN_FALSE; } - RETVAL_STRINGL(to, to_len, 0); + RETVAL_STRINGL(to, to_len); + efree(to); } /* }}} */ #endif #ifdef HAVE_PQESCAPE -#if !HAVE_PQESCAPELITERAL -/* emulate libpq's PQescapeInternal() 9.0 or later */ -static char* php_pgsql_PQescapeInternal(PGconn *conn, const char *str, size_t len, int escape_literal) { - char *result, *rp; - const char *s; - size_t tmp_len; - int input_len = len; - char quote_char = escape_literal ? '\'' : '"'; - - if (!conn) { - return NULL; - } - - /* - * NOTE: multibyte strings that could cointain slashes should be considered. - * (e.g. SJIS, BIG5) However, it cannot be done without valid PGconn and mbstring. - * Therefore, this function does not support such encodings currently. - * FIXME: add encoding check and skip multibyte char bytes if there is vaild PGconn. - */ - - /* allocate enough memory */ - rp = result = (char *)emalloc(len*2 + 5); /* leading " E" needs extra 2 bytes + quote_chars on both end for 2 bytes + NULL */ - - if (escape_literal) { - /* check backslashes */ - tmp_len = strspn(str, "\\"); - if (tmp_len != len) { - /* add " E" for escaping slashes */ - *rp++ = ' '; - *rp++ = 'E'; - } - } - /* open quote */ - *rp++ = quote_char; - for (s = str; s - str < input_len; ++s) { - if (*s == quote_char || (escape_literal && *s == '\\')) { - *rp++ = *s; - *rp++ = *s; - } else { - *rp++ = *s; - } - } - *rp++ = quote_char; - *rp = '\0'; - - return result; -} -#endif - -static void php_pgsql_escape_internal(INTERNAL_FUNCTION_PARAMETERS, int escape_literal) { - char *from = NULL, *to = NULL; +static void php_pgsql_escape_internal(INTERNAL_FUNCTION_PARAMETERS, int escape_literal) /* {{{ */ { + char *from = NULL; zval *pgsql_link = NULL; PGconn *pgsql; - int from_len; + size_t from_len; int id = -1; + char *tmp; switch (ZEND_NUM_ARGS()) { case 1: @@ -4362,7 +4488,7 @@ static void php_pgsql_escape_internal(INTERNAL_FUNCTION_PARAMETERS, int escape_l return; } pgsql_link = NULL; - id = PGG(default_link); + id = PGG(default_link)? PGG(default_link)->handle : - 1; break; default: @@ -4377,38 +4503,26 @@ static void php_pgsql_escape_internal(INTERNAL_FUNCTION_PARAMETERS, int escape_l RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (pgsql == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING,"Cannot get pgsql link"); RETURN_FALSE; } -#ifdef HAVE_PQESCAPELITERAL - /* Use a block with a local var to avoid unused variable warnings */ - { - char *tmp; - if (escape_literal) { - tmp = PQescapeLiteral(pgsql, from, (size_t)from_len); - } else { - tmp = PQescapeIdentifier(pgsql, from, (size_t)from_len); - } - if (!tmp) { - php_error_docref(NULL TSRMLS_CC, E_WARNING,"Failed to escape"); - RETURN_FALSE; - } - to = estrdup(tmp); - PQfreemem(tmp); + if (escape_literal) { + tmp = PGSQLescapeLiteral(pgsql, from, (size_t)from_len); + } else { + tmp = PGSQLescapeIdentifier(pgsql, from, (size_t)from_len); } -#else - to = php_pgsql_PQescapeInternal(pgsql, from, (size_t)from_len, escape_literal); - if (!to) { + if (!tmp) { php_error_docref(NULL TSRMLS_CC, E_WARNING,"Failed to escape"); RETURN_FALSE; } -#endif - RETURN_STRING(to, 0); + RETVAL_STRING(tmp); + PGSQLfree(tmp); } +/* }}} */ /* {{{ proto string pg_escape_literal([resource connection,] string data) Escape parameter as string literal (i.e. parameter) */ @@ -4427,7 +4541,6 @@ PHP_FUNCTION(pg_escape_identifier) /* }}} */ #endif - /* {{{ proto string pg_result_error(resource result) Get error message associated with result */ PHP_FUNCTION(pg_result_error) @@ -4442,14 +4555,14 @@ PHP_FUNCTION(pg_result_error) RETURN_FALSE; } - ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); pgsql_result = pg_result->result; if (!pgsql_result) { RETURN_FALSE; } err = (char *)PQresultErrorMessage(pgsql_result); - RETURN_STRING(err,1); + RETURN_STRING(err); } /* }}} */ @@ -4459,7 +4572,7 @@ PHP_FUNCTION(pg_result_error) PHP_FUNCTION(pg_result_error_field) { zval *result; - long fieldcode; + zend_long fieldcode; PGresult *pgsql_result; pgsql_result_handle *pg_result; char *field = NULL; @@ -4469,7 +4582,7 @@ PHP_FUNCTION(pg_result_error_field) RETURN_FALSE; } - ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); pgsql_result = pg_result->result; if (!pgsql_result) { @@ -4489,7 +4602,7 @@ PHP_FUNCTION(pg_result_error_field) if (field == NULL) { RETURN_NULL(); } else { - RETURN_STRING(field, 1); + RETURN_STRING(field); } } else { RETURN_FALSE; @@ -4511,7 +4624,7 @@ PHP_FUNCTION(pg_connection_status) RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); RETURN_LONG(PQstatus(pgsql)); } @@ -4532,7 +4645,7 @@ PHP_FUNCTION(pg_transaction_status) RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); RETURN_LONG(PQtransactionStatus(pgsql)); } @@ -4553,7 +4666,7 @@ PHP_FUNCTION(pg_connection_reset) RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); PQreset(pgsql); if (PQstatus(pgsql) == CONNECTION_BAD) { @@ -4561,12 +4674,11 @@ PHP_FUNCTION(pg_connection_reset) } RETURN_TRUE; } - /* }}} */ #define PHP_PG_ASYNC_IS_BUSY 1 #define PHP_PG_ASYNC_REQUEST_CANCEL 2 - + /* {{{ php_pgsql_flush_query */ static int php_pgsql_flush_query(PGconn *pgsql TSRMLS_DC) @@ -4586,7 +4698,7 @@ static int php_pgsql_flush_query(PGconn *pgsql TSRMLS_DC) return leftover; } /* }}} */ - + /* {{{ php_pgsql_do_async */ static void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS, int entry_type) @@ -4601,7 +4713,7 @@ static void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS, int entry_type) RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (PQ_SETNONBLOCKING(pgsql, 1)) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode"); @@ -4610,12 +4722,10 @@ static void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS, int entry_type) switch(entry_type) { case PHP_PG_ASYNC_IS_BUSY: PQconsumeInput(pgsql); - Z_LVAL_P(return_value) = PQisBusy(pgsql); - Z_TYPE_P(return_value) = IS_LONG; + RETVAL_LONG(PQisBusy(pgsql)); break; case PHP_PG_ASYNC_REQUEST_CANCEL: - Z_LVAL_P(return_value) = PQrequestCancel(pgsql); - Z_TYPE_P(return_value) = IS_LONG; + RETVAL_LONG(PQrequestCancel(pgsql)); while ((pgsql_result = PQgetResult(pgsql))) { PQclear(pgsql_result); } @@ -4627,7 +4737,7 @@ static void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS, int entry_type) if (PQ_SETNONBLOCKING(pgsql, 0)) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode"); } - convert_to_boolean_ex(&return_value); + convert_to_boolean_ex(return_value); } /* }}} */ @@ -4647,57 +4757,83 @@ 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) { zval *pgsql_link; char *query; - int len; + size_t 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); + 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; } /* }}} */ @@ -4706,72 +4842,71 @@ PHP_FUNCTION(pg_send_query) Send asynchronous parameterized query */ PHP_FUNCTION(pg_send_query_params) { - zval *pgsql_link, *pv_param_arr, **tmp; + zval *pgsql_link, *pv_param_arr, *tmp; int num_params = 0; char **params = NULL; char *query; - int query_len, id = -1; + size_t query_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, "rsa/", &pgsql_link, &query, &query_len, &pv_param_arr) == FAILURE) { return; } - if (pgsql_link == NULL && id == -1) { + if (pgsql_link == NULL) { RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + 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)); num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr)); if (num_params > 0) { int i = 0; params = (char **)safe_emalloc(sizeof(char *), num_params, 0); - 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); - RETURN_FALSE; - } + ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pv_param_arr), tmp) { - if (Z_TYPE_PP(tmp) == IS_NULL) { + if (Z_TYPE_P(tmp) == IS_NULL) { params[i] = NULL; } else { - zval tmp_val = **tmp; - zval_copy_ctor(&tmp_val); + zval tmp_val; + ZVAL_COPY(&tmp_val, tmp); convert_to_string(&tmp_val); if (Z_TYPE(tmp_val) != IS_STRING) { php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter"); - zval_dtor(&tmp_val); + zval_ptr_dtor(&tmp_val); _php_pgsql_free_params(params, num_params); RETURN_FALSE; } params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val)); - zval_dtor(&tmp_val); + zval_ptr_dtor(&tmp_val); } - zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr)); - } + i++; + } ZEND_HASH_FOREACH_END(); } - 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); } @@ -4780,19 +4915,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 @@ -4804,53 +4951,70 @@ PHP_FUNCTION(pg_send_prepare) { zval *pgsql_link; char *query, *stmtname; - int stmtname_len, query_len, id = -1; + size_t stmtname_len, query_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, "rss", &pgsql_link, &stmtname, &stmtname_len, &query, &query_len) == FAILURE) { return; } - if (pgsql_link == NULL && id == -1) { + if (pgsql_link == NULL) { RETURN_FALSE; - } + } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + 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 @@ -4861,72 +5025,71 @@ PHP_FUNCTION(pg_send_prepare) PHP_FUNCTION(pg_send_execute) { zval *pgsql_link; - zval *pv_param_arr, **tmp; + zval *pv_param_arr, *tmp; int num_params = 0; char **params = NULL; char *stmtname; - int stmtname_len, id = -1; + size_t stmtname_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, "rsa", &pgsql_link, &stmtname, &stmtname_len, &pv_param_arr) == FAILURE) { return; } - if (pgsql_link == NULL && id == -1) { + if (pgsql_link == NULL) { RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + 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)); num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr)); if (num_params > 0) { int i = 0; params = (char **)safe_emalloc(sizeof(char *), num_params, 0); - - 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); - RETURN_FALSE; - } - if (Z_TYPE_PP(tmp) == IS_NULL) { + ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pv_param_arr), tmp) { + + if (Z_TYPE_P(tmp) == IS_NULL) { params[i] = NULL; } else { - zval tmp_val = **tmp; - zval_copy_ctor(&tmp_val); + zval tmp_val; + ZVAL_COPY(&tmp_val, tmp); convert_to_string(&tmp_val); if (Z_TYPE(tmp_val) != IS_STRING) { php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter"); - zval_dtor(&tmp_val); + zval_ptr_dtor(&tmp_val); _php_pgsql_free_params(params, num_params); RETURN_FALSE; } params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val)); - zval_dtor(&tmp_val); + zval_ptr_dtor(&tmp_val); } - zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr)); - } + i++; + } ZEND_HASH_FOREACH_END(); } - 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); } @@ -4935,19 +5098,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 @@ -4966,7 +5140,7 @@ PHP_FUNCTION(pg_get_result) RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); pgsql_result = PQgetResult(pgsql); if (!pgsql_result) { @@ -4986,7 +5160,7 @@ PHP_FUNCTION(pg_get_result) PHP_FUNCTION(pg_result_status) { zval *result; - long result_type = PGSQL_STATUS_LONG; + zend_long result_type = PGSQL_STATUS_LONG; ExecStatusType status; PGresult *pgsql_result; pgsql_result_handle *pg_result; @@ -4996,7 +5170,7 @@ PHP_FUNCTION(pg_result_status) RETURN_FALSE; } - ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result); + ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, result, -1, "PostgreSQL result", le_result); pgsql_result = pg_result->result; if (result_type == PGSQL_STATUS_LONG) { @@ -5004,7 +5178,7 @@ PHP_FUNCTION(pg_result_status) RETURN_LONG((int)status); } else if (result_type == PGSQL_STATUS_STRING) { - RETURN_STRING(PQcmdStatus(pgsql_result), 1); + RETURN_STRING(PQcmdStatus(pgsql_result)); } else { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Optional 2nd parameter should be PGSQL_STATUS_LONG or PGSQL_STATUS_STRING"); @@ -5013,14 +5187,13 @@ PHP_FUNCTION(pg_result_status) } /* }}} */ - /* {{{ proto array pg_get_notify([resource connection[, result_type]]) Get asynchronous notification */ PHP_FUNCTION(pg_get_notify) { zval *pgsql_link; int id = -1; - long result_type = PGSQL_ASSOC; + zend_long result_type = PGSQL_ASSOC; PGconn *pgsql; PGnotify *pgsql_notify; @@ -5029,7 +5202,7 @@ PHP_FUNCTION(pg_get_notify) RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (!(result_type & PGSQL_BOTH)) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid result type"); @@ -5044,7 +5217,7 @@ PHP_FUNCTION(pg_get_notify) } array_init(return_value); if (result_type & PGSQL_NUM) { - add_index_string(return_value, 0, pgsql_notify->relname, 1); + add_index_string(return_value, 0, pgsql_notify->relname); add_index_long(return_value, 1, pgsql_notify->be_pid); #if HAVE_PQPROTOCOLVERSION && HAVE_PQPARAMETERSTATUS if (PQprotocolVersion(pgsql) >= 3 && atof(PQparameterStatus(pgsql, "server_version")) >= 9.0) { @@ -5052,12 +5225,12 @@ PHP_FUNCTION(pg_get_notify) if (atof(PG_VERSION) >= 9.0) { #endif #if HAVE_PQPARAMETERSTATUS - add_index_string(return_value, 2, pgsql_notify->extra, 1); + add_index_string(return_value, 2, pgsql_notify->extra); #endif } } if (result_type & PGSQL_ASSOC) { - add_assoc_string(return_value, "message", pgsql_notify->relname, 1); + add_assoc_string(return_value, "message", pgsql_notify->relname); add_assoc_long(return_value, "pid", pgsql_notify->be_pid); #if HAVE_PQPROTOCOLVERSION && HAVE_PQPARAMETERSTATUS if (PQprotocolVersion(pgsql) >= 3 && atof(PQparameterStatus(pgsql, "server_version")) >= 9.0) { @@ -5065,7 +5238,7 @@ PHP_FUNCTION(pg_get_notify) if (atof(PG_VERSION) >= 9.0) { #endif #if HAVE_PQPARAMETERSTATUS - add_assoc_string(return_value, "payload", pgsql_notify->extra, 1); + add_assoc_string(return_value, "payload", pgsql_notify->extra); #endif } } @@ -5086,16 +5259,157 @@ PHP_FUNCTION(pg_get_pid) RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pgsql, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); RETURN_LONG(PQbackendPID(pgsql)); } /* }}} */ +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; @@ -5103,8 +5417,8 @@ PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, z smart_str querystr = {0}; size_t new_len; int i, num_rows; - zval *elem; - + zval elem; + if (!*table_name) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "The table name must be specified"); return FAILURE; @@ -5119,38 +5433,45 @@ 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); -#if HAVE_PQESCAPE_CONN new_len = PQescapeStringConn(pg_link, escaped, tmp_name2, strlen(tmp_name2), NULL); -#else - new_len = PQescapeString(escaped, tmp_name2, strlen(tmp_name2)); -#endif if (new_len) { - smart_str_appends(&querystr, escaped); + smart_str_appendl(&querystr, escaped, new_len); } 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); -#if HAVE_PQESCAPE_CONN new_len = PQescapeStringConn(pg_link, escaped, tmp_name, strlen(tmp_name), NULL); -#else - new_len = PQescapeString(escaped, tmp_name, strlen(tmp_name)); -#endif if (new_len) { - smart_str_appends(&querystr, escaped); + smart_str_appendl(&querystr, escaped, new_len); } 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); - pg_result = PQexec(pg_link, querystr.c); + pg_result = PQexec(pg_link, querystr.s->val); if (PQresultStatus(pg_result) != PGRES_TUPLES_OK || (num_rows = PQntuples(pg_result)) == 0) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Table '%s' doesn't exists", table_name); smart_str_free(&querystr); @@ -5161,32 +5482,32 @@ PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, z for (i = 0; i < num_rows; i++) { char *name; - MAKE_STD_ZVAL(elem); - array_init(elem); - add_assoc_long(elem, "num", atoi(PQgetvalue(pg_result,i,1))); - add_assoc_string(elem, "type", PQgetvalue(pg_result,i,2), 1); - 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); - } - 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); - } + array_init(&elem); + /* pg_attribute.attnum */ + add_assoc_long_ex(&elem, "num", sizeof("num") - 1, atoi(PQgetvalue(pg_result, i, 1))); + /* pg_type.typname */ + add_assoc_string_ex(&elem, "type", sizeof("type") - 1, PQgetvalue(pg_result, i, 2)); + /* pg_attribute.attlen */ + add_assoc_long_ex(&elem, "len", sizeof("len") - 1, atoi(PQgetvalue(pg_result,i,3))); + /* pg_attribute.attnonull */ + add_assoc_bool_ex(&elem, "not null", sizeof("not null") - 1, !strcmp(PQgetvalue(pg_result, i, 4), "t")); + /* pg_attribute.atthasdef */ + add_assoc_bool_ex(&elem, "has default", sizeof("has default") - 1, !strcmp(PQgetvalue(pg_result,i,5), "t")); + /* pg_attribute.attndims */ + add_assoc_long_ex(&elem, "array dims", sizeof("array dims") - 1, atoi(PQgetvalue(pg_result, i, 6))); + /* pg_type.typtype */ + add_assoc_bool_ex(&elem, "is enum", sizeof("is enum") - 1, !strcmp(PQgetvalue(pg_result, i, 7), "e")); + if (extended) { + /* pg_type.typtype */ + add_assoc_bool_ex(&elem, "is base", sizeof("is base") - 1, !strcmp(PQgetvalue(pg_result, i, 7), "b")); + add_assoc_bool_ex(&elem, "is composite", sizeof("is composite") - 1, !strcmp(PQgetvalue(pg_result, i, 7), "c")); + add_assoc_bool_ex(&elem, "is pesudo", sizeof("is pesudo") - 1, !strcmp(PQgetvalue(pg_result, i, 7), "p")); + /* pg_description.description */ + add_assoc_string_ex(&elem, "description", sizeof("description") - 1, PQgetvalue(pg_result, i, 8)); + } + /* pg_attribute.attname */ name = PQgetvalue(pg_result,i,0); - add_assoc_zval(meta, name, elem); + add_assoc_zval(meta, name, &elem); } PQclear(pg_result); @@ -5195,40 +5516,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; + size_t 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); - + 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); - } - } } /* }}} */ @@ -5236,9 +5546,9 @@ PHP_FUNCTION(pg_meta_data) */ static php_pgsql_data_type php_pgsql_get_data_type(const char *type_name, size_t len) { - /* This is stupid way to do. I'll fix it when I decied how to support + /* This is stupid way to do. I'll fix it when I decied how to support user defined types. (Yasuo) */ - + /* boolean */ if (!strcmp(type_name, "bool")|| !strcmp(type_name, "boolean")) return PG_BOOL; @@ -5317,25 +5627,35 @@ static php_pgsql_data_type php_pgsql_get_data_type(const char *type_name, size_t return PG_POLYGON; if (!strcmp(type_name, "circle")) return PG_CIRCLE; - + return PG_UNKNOWN; } /* }}} */ /* {{{ php_pgsql_convert_match - * test field value with regular expression specified. + * test field value with regular expression specified. */ -static int php_pgsql_convert_match(const char *str, const char *regex , int icase TSRMLS_DC) +static int php_pgsql_convert_match(const char *str, size_t str_len, const char *regex , int icase TSRMLS_DC) { - regex_t re; + regex_t re; regmatch_t *subs; int regopt = REG_EXTENDED; int regerr, ret = SUCCESS; + size_t i; + + /* Check invalid chars for POSIX regex */ + for (i = 0; i < str_len; i++) { + if (str[i] == '\n' || + str[i] == '\r' || + str[i] == '\0' ) { + return FAILURE; + } + } if (icase) { regopt |= REG_ICASE; } - + regerr = regcomp(&re, regex, regopt); if (regerr) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot compile regex"); @@ -5346,7 +5666,7 @@ static int php_pgsql_convert_match(const char *str, const char *regex , int icas regerr = regexec(&re, str, re.re_nsub+1, subs, 0); if (regerr == REG_NOMATCH) { -#ifdef PHP_DEBUG +#ifdef PHP_DEBUG php_error_docref(NULL TSRMLS_CC, E_NOTICE, "'%s' does not match with '%s'", str, regex); #endif ret = FAILURE; @@ -5368,7 +5688,7 @@ static int php_pgsql_convert_match(const char *str, const char *regex , int icas static int php_pgsql_add_quotes(zval *src, zend_bool should_free TSRMLS_DC) { smart_str str = {0}; - + assert(Z_TYPE_P(src) == IS_STRING); assert(should_free == 1 || should_free == 0); @@ -5377,45 +5697,41 @@ static int php_pgsql_add_quotes(zval *src, zend_bool should_free TSRMLS_DC) smart_str_appendl(&str, Z_STRVAL_P(src), Z_STRLEN_P(src)); smart_str_appendc(&str, '\''); smart_str_0(&str); - + if (should_free) { - efree(Z_STRVAL_P(src)); + zval_ptr_dtor(src); } - Z_STRVAL_P(src) = str.c; - Z_STRLEN_P(src) = str.len; + ZVAL_NEW_STR(src, str.s); return SUCCESS; } /* }}} */ #define PGSQL_CONV_CHECK_IGNORE() \ - if (!err && Z_TYPE_P(new_val) == IS_STRING && !strcmp(Z_STRVAL_P(new_val), "NULL")) { \ - /* if new_value is string "NULL" and field has default value, remove element to use default value */ \ - if (!(opt & PGSQL_CONV_IGNORE_DEFAULT) && Z_BVAL_PP(has_default)) { \ - zval_dtor(new_val); \ - FREE_ZVAL(new_val); \ - skip_field = 1; \ - } \ - /* raise error if it's not null and cannot be ignored */ \ - else if (!(opt & PGSQL_CONV_IGNORE_NOT_NULL) && Z_BVAL_PP(not_null)) { \ - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected NULL for 'NOT NULL' field '%s'", field ); \ - err = 1; \ - } \ - } + if (!err && Z_TYPE(new_val) == IS_STRING && !strcmp(Z_STRVAL(new_val), "NULL")) { \ + /* if new_value is string "NULL" and field has default value, remove element to use default value */ \ + if (!(opt & PGSQL_CONV_IGNORE_DEFAULT) && Z_TYPE_P(has_default) == IS_TRUE) { \ + zval_ptr_dtor(&new_val); \ + skip_field = 1; \ + } \ + /* raise error if it's not null and cannot be ignored */ \ + else if (!(opt & PGSQL_CONV_IGNORE_NOT_NULL) && Z_TYPE_P(not_null) == IS_TRUE) { \ + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected NULL for 'NOT NULL' field '%s'", field->val); \ + err = 1; \ + } \ + } /* {{{ php_pgsql_convert * check and convert array values (fieldname=>vlaue pair) for sql */ -PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, const zval *values, zval *result, ulong opt TSRMLS_DC) +PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, const zval *values, zval *result, zend_ulong opt TSRMLS_DC) { - HashPosition pos; - char *field = NULL; - uint field_len = -1; - ulong num_idx = -1; - zval *meta, **def, **type, **not_null, **has_default, **is_enum, **val, *new_val; - int key_type, err = 0, skip_field; + zend_string *field = NULL; + zend_ulong num_idx = -1; + zval meta, *def, *type, *not_null, *has_default, *is_enum, *val, new_val; + int err = 0, skip_field; php_pgsql_data_type data_type; - + assert(pg_link != NULL); assert(Z_TYPE_P(values) == IS_ARRAY); assert(Z_TYPE_P(result) == IS_ARRAY); @@ -5424,114 +5740,108 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con if (!table_name) { return FAILURE; } - MAKE_STD_ZVAL(meta); - array_init(meta); + 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) { - zval_dtor(meta); - FREE_ZVAL(meta); + if (php_pgsql_meta_data(pg_link, table_name, &meta, 0 TSRMLS_CC) == FAILURE) { + zval_ptr_dtor(&meta); return FAILURE; } - for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(values), &pos); - zend_hash_get_current_data_ex(Z_ARRVAL_P(values), (void **)&val, &pos) == SUCCESS; - zend_hash_move_forward_ex(Z_ARRVAL_P(values), &pos)) { + + ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(values), num_idx, field, val) { skip_field = 0; - new_val = NULL; - - if ((key_type = zend_hash_get_current_key_ex(Z_ARRVAL_P(values), &field, &field_len, &num_idx, 0, &pos)) == HASH_KEY_NON_EXISTENT) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to get array key type"); - err = 1; - } - if (!err && key_type == HASH_KEY_IS_LONG) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Accepts only string key for values"); - err = 1; - } - if (!err && key_type == HASH_KEY_NON_EXISTENT) { + ZVAL_NULL(&new_val); + + if (!err && field == NULL) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Accepts only string key for values"); err = 1; } - if (!err && zend_hash_find(Z_ARRVAL_P(meta), field, field_len, (void **)&def) == FAILURE) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid field name (%s) in values", field); + + if (!err && (def = zend_hash_find(Z_ARRVAL(meta), field)) == NULL) { + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid field name (%s) in values", field->val); err = 1; } - if (!err && zend_hash_find(Z_ARRVAL_PP(def), "type", sizeof("type"), (void **)&type) == FAILURE) { + if (!err && (type = zend_hash_str_find(Z_ARRVAL_P(def), "type", sizeof("type") - 1)) == NULL) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected broken meta data. Missing 'type'"); err = 1; } - if (!err && zend_hash_find(Z_ARRVAL_PP(def), "not null", sizeof("not null"), (void **)¬_null) == FAILURE) { + if (!err && (not_null = zend_hash_str_find(Z_ARRVAL_P(def), "not null", sizeof("not null") - 1)) == NULL) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected broken meta data. Missing 'not null'"); err = 1; } - if (!err && zend_hash_find(Z_ARRVAL_PP(def), "has default", sizeof("has default"), (void **)&has_default) == FAILURE) { + if (!err && (has_default = zend_hash_str_find(Z_ARRVAL_P(def), "has default", sizeof("has default") - 1)) == NULL) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected broken meta data. Missing 'has default'"); err = 1; } - if (!err && zend_hash_find(Z_ARRVAL_PP(def), "is enum", sizeof("is enum"), (void **)&is_enum) == FAILURE) { + if (!err && (is_enum = zend_hash_str_find(Z_ARRVAL_P(def), "is enum", sizeof("is enum") - 1)) == NULL) { 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_P(val) == IS_ARRAY || Z_TYPE_P(val) == IS_OBJECT)) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects scalar values as field values"); err = 1; } if (err) { break; /* break out for() */ } - ALLOC_INIT_ZVAL(new_val); - if (Z_BVAL_PP(is_enum)) { + convert_to_boolean(is_enum); + if (Z_TYPE_P(is_enum) == IS_TRUE) { /* enums need to be treated like strings */ data_type = PG_TEXT; - } - else { - data_type = php_pgsql_get_data_type(Z_STRVAL_PP(type), Z_STRLEN_PP(type)); + } else { + data_type = php_pgsql_get_data_type(Z_STRVAL_P(type), Z_STRLEN_P(type)); } switch(data_type) { case PG_BOOL: - switch (Z_TYPE_PP(val)) { + switch (Z_TYPE_P(val)) { case IS_STRING: - if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + if (Z_STRLEN_P(val) == 0) { + ZVAL_STRING(&new_val, "NULL"); } else { - if (!strcmp(Z_STRVAL_PP(val), "t") || !strcmp(Z_STRVAL_PP(val), "T") || - !strcmp(Z_STRVAL_PP(val), "y") || !strcmp(Z_STRVAL_PP(val), "Y") || - !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); + if (!strcmp(Z_STRVAL_P(val), "t") || !strcmp(Z_STRVAL_P(val), "T") || + !strcmp(Z_STRVAL_P(val), "y") || !strcmp(Z_STRVAL_P(val), "Y") || + !strcmp(Z_STRVAL_P(val), "true") || !strcmp(Z_STRVAL_P(val), "True") || + !strcmp(Z_STRVAL_P(val), "yes") || !strcmp(Z_STRVAL_P(val), "Yes") || + !strcmp(Z_STRVAL_P(val), "1")) { + ZVAL_STRINGL(&new_val, "'t'", sizeof("'t'")-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); + else if (!strcmp(Z_STRVAL_P(val), "f") || !strcmp(Z_STRVAL_P(val), "F") || + !strcmp(Z_STRVAL_P(val), "n") || !strcmp(Z_STRVAL_P(val), "N") || + !strcmp(Z_STRVAL_P(val), "false") || !strcmp(Z_STRVAL_P(val), "False") || + !strcmp(Z_STRVAL_P(val), "no") || !strcmp(Z_STRVAL_P(val), "No") || + !strcmp(Z_STRVAL_P(val), "0")) { + ZVAL_STRINGL(&new_val, "'f'", sizeof("'f'")-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); + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected invalid value (%s) for PostgreSQL %s field (%s)", Z_STRVAL_P(val), Z_STRVAL_P(type), field->val); err = 1; } } break; - + case IS_LONG: - case IS_BOOL: - if (Z_LVAL_PP(val)) { - ZVAL_STRING(new_val, "'t'", 1); + if (Z_LVAL_P(val)) { + ZVAL_STRINGL(&new_val, "'t'", sizeof("'t'")-1); } else { - ZVAL_STRING(new_val, "'f'", 1); + ZVAL_STRINGL(&new_val, "'f'", sizeof("'f'")-1); } break; + case IS_TRUE: + ZVAL_STRINGL(&new_val, "'t'", sizeof("'t'")-1); + break; + + case IS_FALSE: + ZVAL_STRINGL(&new_val, "'f'", sizeof("'f'")-1); + break; + case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1); break; default: @@ -5539,41 +5849,41 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con } PGSQL_CONV_CHECK_IGNORE(); if (err) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects string, null, long or boolelan value for PostgreSQL '%s' (%s)", Z_STRVAL_PP(type), field); + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects string, null, long or boolelan value for PostgreSQL '%s' (%s)", Z_STRVAL_P(type), field->val); } break; - + case PG_OID: case PG_INT2: case PG_INT4: case PG_INT8: - switch (Z_TYPE_PP(val)) { + switch (Z_TYPE_P(val)) { case IS_STRING: - if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + if (Z_STRLEN_P(val) == 0) { + ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1); } else { /* FIXME: better regex must be used */ - if (php_pgsql_convert_match(Z_STRVAL_PP(val), "^([+-]{0,1}[0-9]+)$", 0 TSRMLS_CC) == FAILURE) { + if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^([+-]{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_P(val), Z_STRLEN_P(val)); } } break; - + case IS_DOUBLE: - ZVAL_DOUBLE(new_val, Z_DVAL_PP(val)); + ZVAL_DOUBLE(&new_val, Z_DVAL_P(val)); convert_to_long_ex(&new_val); break; case IS_LONG: - ZVAL_LONG(new_val, Z_LVAL_PP(val)); + ZVAL_LONG(&new_val, Z_LVAL_P(val)); break; case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1); break; default: @@ -5581,7 +5891,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con } PGSQL_CONV_CHECK_IGNORE(); if (err) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for pgsql '%s' (%s)", Z_STRVAL_PP(type), field); + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for pgsql '%s' (%s)", Z_STRVAL_P(type), field->val); } break; @@ -5589,32 +5899,32 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con case PG_MONEY: case PG_FLOAT4: case PG_FLOAT8: - switch (Z_TYPE_PP(val)) { + switch (Z_TYPE_P(val)) { case IS_STRING: - if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + if (Z_STRLEN_P(val) == 0) { + ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1); } else { - /* FIXME: better regex must be used */ - if (php_pgsql_convert_match(Z_STRVAL_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_P(val), Z_STRLEN_P(val), "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$", 0 TSRMLS_CC) == FAILURE) { err = 1; } else { - ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1); + ZVAL_STRING(&new_val, Z_STRVAL_P(val)); } } break; - + case IS_LONG: - ZVAL_LONG(new_val, Z_LVAL_PP(val)); + ZVAL_LONG(&new_val, Z_LVAL_P(val)); break; - + case IS_DOUBLE: - ZVAL_DOUBLE(new_val, Z_DVAL_PP(val)); + ZVAL_DOUBLE(&new_val, Z_DVAL_P(val)); break; - + case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1); break; default: @@ -5622,50 +5932,62 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con } PGSQL_CONV_CHECK_IGNORE(); if (err) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for PostgreSQL '%s' (%s)", Z_STRVAL_PP(type), field); + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for PostgreSQL '%s' (%s)", Z_STRVAL_P(type), field->val); } 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: - switch (Z_TYPE_PP(val)) { + /* 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_P(val)) { case IS_STRING: - if (Z_STRLEN_PP(val) == 0) { + if (Z_STRLEN_P(val) == 0) { if (opt & PGSQL_CONV_FORCE_NULL) { - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1); } else { - ZVAL_STRING(new_val, "''", 1); + ZVAL_STRINGL(&new_val, "''", sizeof("''")-1); } } else { - Z_TYPE_P(new_val) = IS_STRING; -#if HAVE_PQESCAPE_CONN - { - char *tmp; - tmp = (char *)safe_emalloc(Z_STRLEN_PP(val), 2, 1); - 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; - } -#else - Z_STRVAL_P(new_val) = (int)PQescapeString(Z_STRVAL_PP(val), Z_STRLEN_PP(val), &Z_STRLEN_P(new_val), 0 TSRMLS_CC); -#endif - php_pgsql_add_quotes(new_val, 1 TSRMLS_CC); + zend_string *str; + /* PostgreSQL ignores \0 */ + str = zend_string_alloc(Z_STRLEN_P(val) * 2, 0); + /* better to use PGSQLescapeLiteral since PGescapeStringConn does not handle special \ */ + str->len = PQescapeStringConn(pg_link, str->val, Z_STRVAL_P(val), Z_STRLEN_P(val), NULL); + str = zend_string_realloc(str, str->len, 0); + ZVAL_NEW_STR(&new_val, str); + php_pgsql_add_quotes(&new_val, 1 TSRMLS_CC); } break; - + case IS_LONG: - ZVAL_LONG(new_val, Z_LVAL_PP(val)); + ZVAL_LONG(&new_val, Z_LVAL_P(val)); convert_to_string_ex(&new_val); break; - + case IS_DOUBLE: - ZVAL_DOUBLE(new_val, Z_DVAL_PP(val)); + ZVAL_DOUBLE(&new_val, Z_DVAL_P(val)); convert_to_string_ex(&new_val); break; case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1); break; default: @@ -5673,41 +5995,41 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con } PGSQL_CONV_CHECK_IGNORE(); if (err) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for PostgreSQL '%s' (%s)", Z_STRVAL_PP(type), field); + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for PostgreSQL '%s' (%s)", Z_STRVAL_P(type), field->val); } break; - + case PG_UNIX_TIME: case PG_UNIX_TIME_INTERVAL: /* these are the actallay a integer */ - switch (Z_TYPE_PP(val)) { + switch (Z_TYPE_P(val)) { case IS_STRING: - if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + if (Z_STRLEN_P(val) == 0) { + ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1); } else { - /* FIXME: Better regex must be used */ - if (php_pgsql_convert_match(Z_STRVAL_PP(val), "^[0-9]+$", 0 TSRMLS_CC) == FAILURE) { + /* better regex? */ + if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(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_P(val), Z_STRLEN_P(val)); convert_to_long_ex(&new_val); } } break; - + case IS_DOUBLE: - ZVAL_DOUBLE(new_val, Z_DVAL_PP(val)); + ZVAL_DOUBLE(&new_val, Z_DVAL_P(val)); convert_to_long_ex(&new_val); break; - + case IS_LONG: - ZVAL_LONG(new_val, Z_LVAL_PP(val)); + ZVAL_LONG(&new_val, Z_LVAL_P(val)); break; - + case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1); break; default: @@ -5715,64 +6037,64 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con } PGSQL_CONV_CHECK_IGNORE(); if (err) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for '%s' (%s)", Z_STRVAL_PP(type), field); + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for '%s' (%s)", Z_STRVAL_P(type), field->val); } break; - + case PG_CIDR: case PG_INET: - switch (Z_TYPE_PP(val)) { + switch (Z_TYPE_P(val)) { case IS_STRING: - if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + if (Z_STRLEN_P(val) == 0) { + ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1); } else { - /* FIXME: Better regex must be used */ - if (php_pgsql_convert_match(Z_STRVAL_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_P(val), Z_STRLEN_P(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); - php_pgsql_add_quotes(new_val, 1 TSRMLS_CC); + ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val)); + 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); break; default: err = 1; - } + } PGSQL_CONV_CHECK_IGNORE(); if (err) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for '%s' (%s)", Z_STRVAL_PP(type), field); + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for '%s' (%s)", Z_STRVAL_P(type), field->val); } break; - + case PG_TIME_WITH_TIMEZONE: case PG_TIMESTAMP: case PG_TIMESTAMP_WITH_TIMEZONE: - switch(Z_TYPE_PP(val)) { + switch(Z_TYPE_P(val)) { case IS_STRING: - if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); - } else if (!strcasecmp(Z_STRVAL_PP(val), "now()")) { - ZVAL_STRINGL(new_val, "NOW()", sizeof("NOW()")-1, 1); + if (Z_STRLEN_P(val) == 0) { + ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1); + } else if (!strcasecmp(Z_STRVAL_P(val), "now()")) { + ZVAL_STRINGL(&new_val, "NOW()", sizeof("NOW()")-1); } else { - /* FIXME: better regex must be used */ - if (php_pgsql_convert_match(Z_STRVAL_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) { + /* better regex? */ + if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(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 { - ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1); - php_pgsql_add_quotes(new_val, 1 TSRMLS_CC); + ZVAL_STRING(&new_val, Z_STRVAL_P(val)); + php_pgsql_add_quotes(&new_val, 1 TSRMLS_CC); } } break; - + case IS_NULL: - ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1); + ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1); break; default: @@ -5780,30 +6102,30 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con } PGSQL_CONV_CHECK_IGNORE(); if (err) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_PP(type), field); + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_P(type), field->val); } break; - + case PG_DATE: - switch(Z_TYPE_PP(val)) { + switch(Z_TYPE_P(val)) { case IS_STRING: - if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + if (Z_STRLEN_P(val) == 0) { + ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1); } else { /* FIXME: better regex must be used */ - if (php_pgsql_convert_match(Z_STRVAL_PP(val), "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})$", 1 TSRMLS_CC) == FAILURE) { + if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})$", 1 TSRMLS_CC) == FAILURE) { err = 1; } else { - ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1); - php_pgsql_add_quotes(new_val, 1 TSRMLS_CC); + ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val)); + 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); break; default: @@ -5811,30 +6133,30 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con } PGSQL_CONV_CHECK_IGNORE(); if (err) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_PP(type), field); + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_P(type), field->val); } break; case PG_TIME: - switch(Z_TYPE_PP(val)) { + switch(Z_TYPE_P(val)) { case IS_STRING: - if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + if (Z_STRLEN_P(val) == 0) { + ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1); } else { /* FIXME: better regex must be used */ - if (php_pgsql_convert_match(Z_STRVAL_PP(val), "^(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1})){0,1}$", 1 TSRMLS_CC) == FAILURE) { + if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1})){0,1}$", 1 TSRMLS_CC) == FAILURE) { err = 1; } else { - ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1); - php_pgsql_add_quotes(new_val, 1 TSRMLS_CC); + ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val)); + 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); break; default: @@ -5842,15 +6164,15 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con } PGSQL_CONV_CHECK_IGNORE(); if (err) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_PP(type), field); + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_P(type), field->val); } break; case PG_INTERVAL: - switch(Z_TYPE_PP(val)) { + switch(Z_TYPE_P(val)) { case IS_STRING: - if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + if (Z_STRLEN_P(val) == 0) { + ZVAL_STRING(&new_val, "NULL"); } else { @@ -5869,10 +6191,10 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con Quantities of days, hours, minutes, and seconds can be specified without explicit unit markings. For example, '1 12:59:10' is read the same as '1 day 12 hours 59 min 10 sec'. - */ - if (php_pgsql_convert_match(Z_STRVAL_PP(val), + */ + if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(val), "^(@?[ \\t]+)?(" - + /* Textual time units and their abbreviations: */ "(([-+]?[ \\t]+)?" "[0-9]+(\\.[0-9]*)?[ \\t]*" @@ -5906,14 +6228,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); - php_pgsql_add_quotes(new_val, 1 TSRMLS_CC); + ZVAL_STRING(&new_val, Z_STRVAL_P(val)); + php_pgsql_add_quotes(&new_val, 1 TSRMLS_CC); } - } + } break; - + case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRING(&new_val, "NULL"); break; default: @@ -5921,51 +6243,47 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con } PGSQL_CONV_CHECK_IGNORE(); if (err) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_PP(type), field); + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_P(type), field->val); } break; #ifdef HAVE_PQESCAPE case PG_BYTEA: - switch (Z_TYPE_PP(val)) { + switch (Z_TYPE_P(val)) { case IS_STRING: - if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + if (Z_STRLEN_P(val) == 0) { + ZVAL_STRING(&new_val, "NULL"); } else { unsigned char *tmp; size_t to_len; smart_str s = {0}; #ifdef HAVE_PQESCAPE_BYTEA_CONN - tmp = PQescapeByteaConn(pg_link, (unsigned char *)Z_STRVAL_PP(val), Z_STRLEN_PP(val), &to_len); + tmp = PQescapeByteaConn(pg_link, (unsigned char *)Z_STRVAL_P(val), Z_STRLEN_P(val), &to_len); #else - tmp = PQescapeBytea(Z_STRVAL_PP(val), (unsigned char *)Z_STRLEN_PP(val), &to_len); + tmp = PQescapeBytea(Z_STRVAL_P(val), (unsigned char *)Z_STRLEN_P(val), &to_len); #endif - Z_TYPE_P(new_val) = IS_STRING; - Z_STRLEN_P(new_val) = to_len-1; /* PQescapeBytea's to_len includes additional '\0' */ - Z_STRVAL_P(new_val) = emalloc(to_len); - memcpy(Z_STRVAL_P(new_val), tmp, to_len); + ZVAL_STRINGL(&new_val, (char *)tmp, to_len - 1); /* PQescapeBytea's to_len includes additional '\0' */ PQfreemem(tmp); - php_pgsql_add_quotes(new_val, 1 TSRMLS_CC); - smart_str_appendl(&s, Z_STRVAL_P(new_val), Z_STRLEN_P(new_val)); + php_pgsql_add_quotes(&new_val, 1 TSRMLS_CC); + smart_str_appendl(&s, Z_STRVAL(new_val), Z_STRLEN(new_val)); smart_str_0(&s); - efree(Z_STRVAL_P(new_val)); - Z_STRVAL_P(new_val) = s.c; - Z_STRLEN_P(new_val) = s.len; + zval_ptr_dtor(&new_val); + ZVAL_NEW_STR(&new_val, s.s); } break; - + case IS_LONG: - ZVAL_LONG(new_val, Z_LVAL_PP(val)); + ZVAL_LONG(&new_val, Z_LVAL_P(val)); convert_to_string_ex(&new_val); break; - + case IS_DOUBLE: - ZVAL_DOUBLE(new_val, Z_DVAL_PP(val)); + ZVAL_DOUBLE(&new_val, Z_DVAL_P(val)); convert_to_string_ex(&new_val); break; case IS_NULL: - ZVAL_STRING(new_val, "NULL", 1); + ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1); break; default: @@ -5973,30 +6291,30 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con } PGSQL_CONV_CHECK_IGNORE(); if (err) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for PostgreSQL '%s' (%s)", Z_STRVAL_PP(type), field); + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for PostgreSQL '%s' (%s)", Z_STRVAL_P(type), field->val); } break; #endif case PG_MACADDR: - switch(Z_TYPE_PP(val)) { + switch(Z_TYPE_P(val)) { case IS_STRING: - if (Z_STRLEN_PP(val) == 0) { - ZVAL_STRING(new_val, "NULL", 1); + if (Z_STRLEN_P(val) == 0) { + ZVAL_STRINGL(&new_val, "NULL", sizeof("NULL")-1); } else { - if (php_pgsql_convert_match(Z_STRVAL_PP(val), "^([0-9a-f]{2,2}:){5,5}[0-9a-f]{2,2}$", 1 TSRMLS_CC) == FAILURE) { + if (php_pgsql_convert_match(Z_STRVAL_P(val), Z_STRLEN_P(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); - php_pgsql_add_quotes(new_val, 1 TSRMLS_CC); + ZVAL_STRINGL(&new_val, Z_STRVAL_P(val), Z_STRLEN_P(val)); + 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); break; default: @@ -6004,57 +6322,36 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con } PGSQL_CONV_CHECK_IGNORE(); if (err) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_PP(type), field); + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_P(type), field->val); } 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_P(type), field->val); err = 1; break; } /* switch */ - + if (err) { - zval_dtor(new_val); - FREE_ZVAL(new_val); + zval_ptr_dtor(&new_val); break; /* break out for() */ } /* If field is NULL and HAS DEFAULT, should be skipped */ if (!skip_field) { char *escaped; - size_t field_len = strlen(field); - if (_php_pgsql_detect_identifier_escape(field, field_len) == SUCCESS) { - escaped = _php_pgsql_strndup(field, field_len); + if (_php_pgsql_detect_identifier_escape(field->val, field->len) == SUCCESS) { + zend_hash_update(Z_ARRVAL_P(result), field, &new_val); } else { -#if HAVE_PQESCAPELITERAL - escaped = PQescapeIdentifier(pg_link, field, field_len); -#else - escaped = _php_pgsql_escape_identifier(field, field_len); -#endif + escaped = PGSQLescapeIdentifier(pg_link, field->val, field->len); + add_assoc_zval(result, escaped, &new_val); + PGSQLfree(escaped); } - add_assoc_zval(result, escaped, new_val); - free(escaped); } - } /* for */ - zval_dtor(meta); - FREE_ZVAL(meta); + } ZEND_HASH_FOREACH_END(); /* for */ + + zval_ptr_dtor(&meta); if (err) { /* shouldn't destroy & free zval here */ @@ -6064,18 +6361,17 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con } /* }}} */ - /* {{{ proto array pg_convert(resource db, string table, array values[, int options]) Check and convert values for PostgreSQL SQL statement */ PHP_FUNCTION(pg_convert) { zval *pgsql_link, *values; char *table_name; - int table_name_len; - ulong option = 0; + size_t table_name_len; + zend_ulong option = 0; PGconn *pg_link; int id = -1; - + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsa|l", &pgsql_link, &table_name, &table_name_len, &values, &option) == FAILURE) { return; @@ -6089,7 +6385,7 @@ PHP_FUNCTION(pg_convert) RETURN_FALSE; } - ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pg_link, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (php_pgsql_flush_query(pg_link TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected unhandled result(s) in connection"); @@ -6102,17 +6398,17 @@ PHP_FUNCTION(pg_convert) } /* }}} */ -static int do_exec(smart_str *querystr, int expect, PGconn *pg_link, ulong opt TSRMLS_DC) +static int do_exec(smart_str *querystr, int expect, PGconn *pg_link, zend_ulong opt TSRMLS_DC) /* {{{ */ { if (opt & PGSQL_DML_ASYNC) { - if (PQsendQuery(pg_link, querystr->c)) { + if (PQsendQuery(pg_link, querystr->s->val)) { return 0; } } else { PGresult *pg_result; - pg_result = PQexec(pg_link, querystr->c); + pg_result = PQexec(pg_link, querystr->s->val); if (PQresultStatus(pg_result) == expect) { PQclear(pg_result); return 0; @@ -6124,8 +6420,9 @@ static int do_exec(smart_str *querystr, int expect, PGconn *pg_link, ulong opt T return -1; } +/* }}} */ -static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const char *table) +static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const char *table) /* {{{ */ { char *table_copy, *escaped, *token, *tmp; size_t len; @@ -6135,52 +6432,46 @@ static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const c token = php_strtok_r(table_copy, ".", &tmp); len = strlen(token); if (_php_pgsql_detect_identifier_escape(token, len) == SUCCESS) { - escaped = _php_pgsql_strndup(token, len); + smart_str_appendl(querystr, token, len); } else { -#if HAVE_PQESCAPELITERAL - escaped = PQescapeIdentifier(pg_link, token, len); -#else - escaped = _php_pgsql_escape_identifier(token, len); -#endif + escaped = PGSQLescapeIdentifier(pg_link, token, len); + smart_str_appends(querystr, escaped); + PGSQLfree(escaped); } - smart_str_appends(querystr, escaped); - free(escaped); if (tmp && *tmp) { len = strlen(tmp); /* "schema"."table" format */ if (_php_pgsql_detect_identifier_escape(tmp, len) == SUCCESS) { - escaped = _php_pgsql_strndup(tmp, len); + smart_str_appendc(querystr, '.'); + smart_str_appendl(querystr, tmp, len); } else { -#if HAVE_PQESCAPELITERAL - escaped = PQescapeIdentifier(pg_link, tmp, len); -#else - escaped = _php_pgsql_escape_identifier(tmp, len); -#endif + escaped = PGSQLescapeIdentifier(pg_link, tmp, len); + smart_str_appendc(querystr, '.'); + smart_str_appends(querystr, escaped); + PGSQLfree(escaped); } - smart_str_appendc(querystr, '.'); - smart_str_appends(querystr, escaped); - free(escaped); } efree(table_copy); } +/* }}} */ /* {{{ php_pgsql_insert */ -PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *var_array, ulong opt, char **sql TSRMLS_DC) +PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *var_array, zend_ulong opt, zend_string **sql TSRMLS_DC) { - zval **val, *converted = NULL; + zval *val, converted; char buf[256]; - char *fld; + char *tmp; smart_str querystr = {0}; - int key_type, ret = FAILURE; - uint fld_len; - ulong num_idx; - HashPosition pos; + int ret = FAILURE; + zend_ulong num_idx; + zend_string *fld; assert(pg_link != NULL); assert(table != NULL); assert(Z_TYPE_P(var_array) == IS_ARRAY); + ZVAL_UNDEF(&converted); if (zend_hash_num_elements(Z_ARRVAL_P(var_array)) == 0) { smart_str_appends(&querystr, "INSERT INTO "); build_tablename(&querystr, pg_link, table); @@ -6190,59 +6481,71 @@ 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)) { - MAKE_STD_ZVAL(converted); - array_init(converted); - if (php_pgsql_convert(pg_link, table, var_array, converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) { + if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) { + array_init(&converted); + if (php_pgsql_convert(pg_link, table, var_array, &converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) { goto cleanup; } - var_array = converted; + var_array = &converted; } smart_str_appends(&querystr, "INSERT INTO "); build_tablename(&querystr, pg_link, table); smart_str_appends(&querystr, " ("); - zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(var_array), &pos); - while ((key_type = zend_hash_get_current_key_ex(Z_ARRVAL_P(var_array), &fld, - &fld_len, &num_idx, 0, &pos)) != HASH_KEY_NON_EXISTENT) { - if (key_type == HASH_KEY_IS_LONG) { + ZEND_HASH_FOREACH_KEY(Z_ARRVAL_P(var_array), num_idx, fld) { + if (fld == NULL) { 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->val, fld->len + 1); + smart_str_appends(&querystr, tmp); + PGSQLfree(tmp); + } else { + smart_str_appendl(&querystr, fld->val, fld->len); + } smart_str_appendc(&querystr, ','); - zend_hash_move_forward_ex(Z_ARRVAL_P(var_array), &pos); - } - querystr.len--; + } ZEND_HASH_FOREACH_END(); + querystr.s->len--; smart_str_appends(&querystr, ") VALUES ("); /* make values string */ - for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(var_array), &pos); - zend_hash_get_current_data_ex(Z_ARRVAL_P(var_array), (void **)&val, &pos) == SUCCESS; - zend_hash_move_forward_ex(Z_ARRVAL_P(var_array), &pos)) { - + ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(var_array), val) { /* we can avoid the key_type check here, because we tested it in the other loop */ - switch(Z_TYPE_PP(val)) { + switch (Z_TYPE_P(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_P(val), 2, 1); + new_len = PQescapeStringConn(pg_link, tmp, Z_STRVAL_P(val), Z_STRLEN_P(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_P(val), Z_STRLEN_P(val)); + } break; case IS_LONG: - smart_str_append_long(&querystr, Z_LVAL_PP(val)); + smart_str_append_long(&querystr, Z_LVAL_P(val)); break; case IS_DOUBLE: - smart_str_appendl(&querystr, buf, snprintf(buf, sizeof(buf), "%F", Z_DVAL_PP(val))); + smart_str_appendl(&querystr, buf, snprintf(buf, sizeof(buf), "%F", Z_DVAL_P(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_P(val)); goto cleanup; break; } smart_str_appendc(&querystr, ','); - } + } ZEND_HASH_FOREACH_END(); /* Remove the trailing "," */ - querystr.len--; + querystr.s->len--; smart_str_appends(&querystr, ");"); no_values: @@ -6258,12 +6561,9 @@ no_values: } cleanup: - if (!(opt & PGSQL_DML_NO_CONV) && converted) { - zval_dtor(converted); - FREE_ZVAL(converted); - } + zval_ptr_dtor(&converted); if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) { - *sql = querystr.c; + *sql = querystr.s; } else { smart_str_free(&querystr); @@ -6277,88 +6577,154 @@ cleanup: PHP_FUNCTION(pg_insert) { zval *pgsql_link, *values; - char *table, *sql = NULL; - int table_len; - ulong option = PGSQL_DML_EXEC; + char *table; + size_t table_len; + zend_ulong option = PGSQL_DML_EXEC, return_sql; PGconn *pg_link; + PGresult *pg_result; + ExecStatusType status; + pgsql_result_handle *pgsql_handle; + zend_string *sql = NULL; 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; } - ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pg_link, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); 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->val); + if ((PGG(auto_reset_persistent) & 2) && PQstatus(pg_link) != CONNECTION_OK) { + PQclear(pg_result); + PQreset(pg_link); + pg_result = PQexec(pg_link, sql->val); + } + 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) { - RETURN_STRING(sql, 0); + if (return_sql) { + RETURN_STR(sql); + return; } 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, zend_ulong opt TSRMLS_DC) /* {{{ */ { - HashPosition pos; - uint fld_len; - int key_type; - ulong num_idx; - char *fld; + char *tmp; char buf[256]; - zval **val; + zend_ulong num_idx; + zend_string *fld; + zval *val; - for (zend_hash_internal_pointer_reset_ex(ht, &pos); - zend_hash_get_current_data_ex(ht, (void **)&val, &pos) == SUCCESS; - zend_hash_move_forward_ex(ht, &pos)) { - key_type = zend_hash_get_current_key_ex(ht, &fld, &fld_len, &num_idx, 0, &pos); - if (key_type == HASH_KEY_IS_LONG) { + ZEND_HASH_FOREACH_KEY_VAL(ht, num_idx, fld, val) { + if (fld == NULL) { 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->val, fld->len + 1); + smart_str_appends(querystr, tmp); + PGSQLfree(tmp); + } else { + smart_str_appendl(querystr, fld->val, fld->len); + } + if (where_cond && (Z_TYPE_P(val) == IS_TRUE || Z_TYPE_P(val) == IS_FALSE || (Z_TYPE_P(val) == IS_STRING && !strcmp(Z_STRVAL_P(val), "NULL")))) { smart_str_appends(querystr, " IS "); } else { smart_str_appendc(querystr, '='); } - - switch(Z_TYPE_PP(val)) { + + switch (Z_TYPE_P(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_P(val), 2, 1); + new_len = PQescapeStringConn(pg_link, tmp, Z_STRVAL_P(val), Z_STRLEN_P(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_P(val), Z_STRLEN_P(val)); + } break; case IS_LONG: - smart_str_append_long(querystr, Z_LVAL_PP(val)); + smart_str_append_long(querystr, Z_LVAL_P(val)); break; case IS_DOUBLE: - smart_str_appendl(querystr, buf, MIN(snprintf(buf, sizeof(buf), "%F", Z_DVAL_PP(val)), sizeof(buf)-1)); + smart_str_appendl(querystr, buf, MIN(snprintf(buf, sizeof(buf), "%F", Z_DVAL_P(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_P(val)); return -1; } smart_str_appendl(querystr, pad, pad_len); + } ZEND_HASH_FOREACH_END(); + if (querystr->s) { + querystr->s->len -= pad_len; } - querystr->len -= pad_len; return 0; -} +} +/* }}} */ /* {{{ php_pgsql_update */ -PHP_PGSQL_API int php_pgsql_update(PGconn *pg_link, const char *table, zval *var_array, zval *ids_array, ulong opt, char **sql TSRMLS_DC) +PHP_PGSQL_API int php_pgsql_update(PGconn *pg_link, const char *table, zval *var_array, zval *ids_array, zend_ulong opt, zend_string **sql TSRMLS_DC) { - zval *var_converted = NULL, *ids_converted = NULL; + zval var_converted, ids_converted; smart_str querystr = {0}; int ret = FAILURE; @@ -6366,41 +6732,41 @@ 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)) { - 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) { + ZVAL_UNDEF(&var_converted); + ZVAL_UNDEF(&ids_converted); + if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) { + array_init(&var_converted); + if (php_pgsql_convert(pg_link, table, var_array, &var_converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) { goto cleanup; } - var_array = var_converted; - 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) { + var_array = &var_converted; + array_init(&ids_converted); + if (php_pgsql_convert(pg_link, table, ids_array, &ids_converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) { goto cleanup; } - ids_array = ids_converted; + ids_array = &ids_converted; } smart_str_appends(&querystr, "UPDATE "); 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) { @@ -6410,16 +6776,10 @@ PHP_PGSQL_API int php_pgsql_update(PGconn *pg_link, const char *table, zval *var } cleanup: - if (var_converted) { - zval_dtor(var_converted); - FREE_ZVAL(var_converted); - } - if (ids_converted) { - zval_dtor(ids_converted); - FREE_ZVAL(ids_converted); - } + zval_ptr_dtor(&var_converted); + zval_ptr_dtor(&ids_converted); if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) { - *sql = querystr.c; + *sql = querystr.s; } else { smart_str_free(&querystr); @@ -6433,22 +6793,23 @@ cleanup: PHP_FUNCTION(pg_update) { zval *pgsql_link, *values, *ids; - char *table, *sql = NULL; - int table_len; - ulong option = PGSQL_DML_EXEC; + char *table; + size_t table_len; + zend_ulong option = PGSQL_DML_EXEC; PGconn *pg_link; + zend_string *sql = NULL; int id = -1, argc = ZEND_NUM_ARGS(); if (zend_parse_parameters(argc TSRMLS_CC, "rsaa|l", &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; } - ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pg_link, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (php_pgsql_flush_query(pg_link TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected unhandled result(s) in connection"); @@ -6457,43 +6818,43 @@ PHP_FUNCTION(pg_update) RETURN_FALSE; } if (option & PGSQL_DML_STRING) { - RETURN_STRING(sql, 0); + RETURN_STR(sql); } RETURN_TRUE; -} +} /* }}} */ /* {{{ php_pgsql_delete */ -PHP_PGSQL_API int php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids_array, ulong opt, char **sql TSRMLS_DC) +PHP_PGSQL_API int php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids_array, zend_ulong opt, zend_string **sql TSRMLS_DC) { - zval *ids_converted = NULL; + zval ids_converted; smart_str querystr = {0}; int ret = FAILURE; 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)) { - 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) { + ZVAL_UNDEF(&ids_converted); + if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) { + array_init(&ids_converted); + if (php_pgsql_convert(pg_link, table, ids_array, &ids_converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) { goto cleanup; } - ids_array = ids_converted; + ids_array = &ids_converted; } smart_str_appends(&querystr, "DELETE FROM "); 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, ';'); @@ -6506,12 +6867,9 @@ PHP_PGSQL_API int php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids } cleanup: - if (!(opt & PGSQL_DML_NO_CONV)) { - zval_dtor(ids_converted); - FREE_ZVAL(ids_converted); - } + zval_ptr_dtor(&ids_converted); if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) { - *sql = querystr.c; + *sql = querystr.s; } else { smart_str_free(&querystr); @@ -6525,22 +6883,23 @@ cleanup: PHP_FUNCTION(pg_delete) { zval *pgsql_link, *ids; - char *table, *sql = NULL; - int table_len; - ulong option = PGSQL_DML_EXEC; + char *table; + size_t table_len; + zend_ulong option = PGSQL_DML_EXEC; PGconn *pg_link; + zend_string *sql; int id = -1, argc = ZEND_NUM_ARGS(); if (zend_parse_parameters(argc TSRMLS_CC, "rsa|l", &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; } - ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pg_link, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (php_pgsql_flush_query(pg_link TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected unhandled result(s) in connection"); @@ -6549,7 +6908,7 @@ PHP_FUNCTION(pg_delete) RETURN_FALSE; } if (option & PGSQL_DML_STRING) { - RETURN_STRING(sql, 0); + RETURN_STR(sql); } RETURN_TRUE; } @@ -6559,7 +6918,7 @@ PHP_FUNCTION(pg_delete) */ PHP_PGSQL_API int php_pgsql_result2array(PGresult *pg_result, zval *ret_array TSRMLS_DC) { - zval *row; + zval row; char *field_name; size_t num_fields; int pg_numrows, pg_row; @@ -6570,28 +6929,22 @@ PHP_PGSQL_API int php_pgsql_result2array(PGresult *pg_result, zval *ret_array TS return FAILURE; } for (pg_row = 0; pg_row < pg_numrows; pg_row++) { - MAKE_STD_ZVAL(row); - array_init(row); - add_index_zval(ret_array, pg_row, row); + array_init(&row); for (i = 0, num_fields = PQnfields(pg_result); i < num_fields; i++) { if (PQgetisnull(pg_result, pg_row, i)) { field_name = PQfname(pg_result, i); - add_assoc_null(row, field_name); + add_assoc_null(&row, field_name); } else { char *element = PQgetvalue(pg_result, pg_row, i); if (element) { - char *data; - size_t data_len; const size_t element_len = strlen(element); - data = safe_estrndup(element, element_len); - data_len = element_len; - field_name = PQfname(pg_result, i); - add_assoc_stringl(row, field_name, data, data_len, 0); + add_assoc_stringl(&row, field_name, element, element_len); } } } + add_index_zval(ret_array, pg_row, &row); } return SUCCESS; } @@ -6599,9 +6952,9 @@ 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, zend_ulong opt, zend_string **sql TSRMLS_DC) { - zval *ids_converted = NULL; + zval ids_converted; smart_str querystr = {0}; int ret = FAILURE; PGresult *pg_result; @@ -6610,46 +6963,43 @@ 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)) { - 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) { + ZVAL_UNDEF(&ids_converted); + if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) { + array_init(&ids_converted); + if (php_pgsql_convert(pg_link, table, ids_array, &ids_converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) { goto cleanup; } - ids_array = ids_converted; + ids_array = &ids_converted; } smart_str_appends(&querystr, "SELECT * FROM "); 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, ';'); smart_str_0(&querystr); - pg_result = PQexec(pg_link, querystr.c); + pg_result = PQexec(pg_link, querystr.s->val); if (PQresultStatus(pg_result) == PGRES_TUPLES_OK) { ret = php_pgsql_result2array(pg_result, ret_array TSRMLS_CC); } else { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Failed to execute '%s'", querystr.c); + php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Failed to execute '%s'", querystr.s->val); } PQclear(pg_result); cleanup: - if (!(opt & PGSQL_DML_NO_CONV)) { - zval_dtor(ids_converted); - FREE_ZVAL(ids_converted); - } + zval_ptr_dtor(&ids_converted); if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) { - *sql = querystr.c; + *sql = querystr.s; } else { smart_str_free(&querystr); @@ -6663,37 +7013,38 @@ cleanup: PHP_FUNCTION(pg_select) { zval *pgsql_link, *ids; - char *table, *sql = NULL; - int table_len; - ulong option = PGSQL_DML_EXEC; + char *table; + size_t table_len; + zend_ulong option = PGSQL_DML_EXEC; PGconn *pg_link; + zend_string *sql = NULL; int id = -1, argc = ZEND_NUM_ARGS(); if (zend_parse_parameters(argc TSRMLS_CC, "rsa|l", &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; } - ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink); + ZEND_FETCH_RESOURCE2(pg_link, PGconn *, pgsql_link, id, "PostgreSQL link", le_link, le_plink); if (php_pgsql_flush_query(pg_link TSRMLS_CC)) { php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected unhandled result(s) in connection"); } array_init(return_value); if (php_pgsql_select(pg_link, table, ids, return_value, option, &sql TSRMLS_CC) == FAILURE) { - zval_dtor(return_value); + zval_ptr_dtor(return_value); RETURN_FALSE; } if (option & PGSQL_DML_STRING) { - zval_dtor(return_value); - RETURN_STRING(sql, 0); + zval_ptr_dtor(return_value); + RETURN_STR(sql); } return; -} +} /* }}} */ #endif |