PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/pgsql - pgsql.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 1804 2917 61.8 %
Date: 2014-04-16 Functions: 126 138 91.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Zeev Suraski <zeev@zend.com>                                |
      16             :    |          Jouni Ahto <jouni.ahto@exdec.fi>                            |
      17             :    |          Yasuo Ohgaki <yohgaki@php.net>                              |
      18             :    |          Youichi Iwakiri <yiwakiri@st.rim.or.jp> (pg_copy_*)         |
      19             :    |          Chris Kings-Lynne <chriskl@php.net> (v3 protocol)           |
      20             :    +----------------------------------------------------------------------+
      21             :  */
      22             : 
      23             : /* $Id$ */
      24             : 
      25             : #include <stdlib.h>
      26             : 
      27             : #define PHP_PGSQL_PRIVATE 1
      28             : 
      29             : #ifdef HAVE_CONFIG_H
      30             : #include "config.h"
      31             : #endif
      32             : 
      33             : #define SMART_STR_PREALLOC 512
      34             : 
      35             : #include "php.h"
      36             : #include "php_ini.h"
      37             : #include "ext/standard/php_standard.h"
      38             : #include "ext/standard/php_smart_str.h"
      39             : #include "ext/ereg/php_regex.h"
      40             : #ifdef PHP_WIN32
      41             : # include "win32/time.h"
      42             : #endif
      43             : 
      44             : #undef PACKAGE_BUGREPORT
      45             : #undef PACKAGE_NAME
      46             : #undef PACKAGE_STRING
      47             : #undef PACKAGE_TARNAME
      48             : #undef PACKAGE_VERSION
      49             : #include "php_pgsql.h"
      50             : #include "php_globals.h"
      51             : #include "zend_exceptions.h"
      52             : 
      53             : #if HAVE_PGSQL
      54             : 
      55             : #ifndef InvalidOid
      56             : #define InvalidOid ((Oid) 0)
      57             : #endif
      58             : 
      59             : #define PGSQL_ASSOC             1<<0
      60             : #define PGSQL_NUM               1<<1
      61             : #define PGSQL_BOTH              (PGSQL_ASSOC|PGSQL_NUM)
      62             : 
      63             : #define PGSQL_STATUS_LONG     1
      64             : #define PGSQL_STATUS_STRING   2
      65             : 
      66             : #define PGSQL_MAX_LENGTH_OF_LONG   30
      67             : #define PGSQL_MAX_LENGTH_OF_DOUBLE 60
      68             : 
      69             : #if LONG_MAX < UINT_MAX
      70             : #define PGSQL_RETURN_OID(oid) do { \
      71             :         if (oid > LONG_MAX) { \
      72             :                 smart_str s = {0}; \
      73             :                 smart_str_append_unsigned(&s, oid); \
      74             :                 smart_str_0(&s); \
      75             :                 RETURN_STRINGL(s.c, s.len, 0); \
      76             :         } \
      77             :         RETURN_LONG((long)oid); \
      78             : } while(0)
      79             : #else
      80             : #define PGSQL_RETURN_OID(oid) (RETURN_LONG((long)oid))
      81             : #endif
      82             : 
      83             : #if HAVE_PQSETNONBLOCKING
      84             : #define PQ_SETNONBLOCKING(pg_link, flag) PQsetnonblocking(pg_link, flag)
      85             : #else
      86             : #define PQ_SETNONBLOCKING(pg_link, flag) 0
      87             : #endif
      88             : 
      89             : #define CHECK_DEFAULT_LINK(x) if ((x) == -1) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "No PostgreSQL link opened yet"); }
      90             : 
      91             : #ifndef HAVE_PQFREEMEM
      92             : #define PQfreemem free
      93             : #endif
      94             : 
      95             : ZEND_DECLARE_MODULE_GLOBALS(pgsql)
      96             : static PHP_GINIT_FUNCTION(pgsql);
      97             : 
      98             : /* {{{ arginfo */
      99             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_connect, 0, 0, 1)
     100             :         ZEND_ARG_INFO(0, connection_string)
     101             :         ZEND_ARG_INFO(0, connect_type)
     102             :         ZEND_ARG_INFO(0, host)
     103             :         ZEND_ARG_INFO(0, port)
     104             :         ZEND_ARG_INFO(0, options)
     105             :         ZEND_ARG_INFO(0, tty)
     106             :         ZEND_ARG_INFO(0, database)
     107             : ZEND_END_ARG_INFO()
     108             : 
     109             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_pconnect, 0, 0, 1)
     110             :         ZEND_ARG_INFO(0, connection_string)
     111             :         ZEND_ARG_INFO(0, host)
     112             :         ZEND_ARG_INFO(0, port)
     113             :         ZEND_ARG_INFO(0, options)
     114             :         ZEND_ARG_INFO(0, tty)
     115             :         ZEND_ARG_INFO(0, database)
     116             : ZEND_END_ARG_INFO()
     117             : 
     118             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_connect_poll, 0, 0, 0)
     119             :         ZEND_ARG_INFO(0, connection)
     120             : ZEND_END_ARG_INFO()
     121             : 
     122             : #if HAVE_PQPARAMETERSTATUS
     123             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_parameter_status, 0, 0, 1)
     124             :         ZEND_ARG_INFO(0, connection)
     125             :         ZEND_ARG_INFO(0, param_name)
     126             : ZEND_END_ARG_INFO()
     127             : #endif
     128             : 
     129             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_close, 0, 0, 0)
     130             :         ZEND_ARG_INFO(0, connection)
     131             : ZEND_END_ARG_INFO()
     132             : 
     133             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_dbname, 0, 0, 0)
     134             :         ZEND_ARG_INFO(0, connection)
     135             : ZEND_END_ARG_INFO()
     136             : 
     137             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_last_error, 0, 0, 0)
     138             :         ZEND_ARG_INFO(0, connection)
     139             : ZEND_END_ARG_INFO()
     140             : 
     141             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_options, 0, 0, 0)
     142             :         ZEND_ARG_INFO(0, connection)
     143             : ZEND_END_ARG_INFO()
     144             : 
     145             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_port, 0, 0, 0)
     146             :         ZEND_ARG_INFO(0, connection)
     147             : ZEND_END_ARG_INFO()
     148             : 
     149             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_tty, 0, 0, 0)
     150             :         ZEND_ARG_INFO(0, connection)
     151             : ZEND_END_ARG_INFO()
     152             : 
     153             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_host, 0, 0, 0)
     154             :         ZEND_ARG_INFO(0, connection)
     155             : ZEND_END_ARG_INFO()
     156             : 
     157             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_version, 0, 0, 0)
     158             :         ZEND_ARG_INFO(0, connection)
     159             : ZEND_END_ARG_INFO()
     160             : 
     161             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_ping, 0, 0, 0)
     162             :         ZEND_ARG_INFO(0, connection)
     163             : ZEND_END_ARG_INFO()
     164             : 
     165             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_query, 0, 0, 0)
     166             :         ZEND_ARG_INFO(0, connection)
     167             :         ZEND_ARG_INFO(0, query)
     168             : ZEND_END_ARG_INFO()
     169             : 
     170             : #if HAVE_PQEXECPARAMS
     171             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_query_params, 0, 0, 0)
     172             :         ZEND_ARG_INFO(0, connection)
     173             :         ZEND_ARG_INFO(0, query)
     174             :         ZEND_ARG_INFO(0, params)
     175             : ZEND_END_ARG_INFO()
     176             : #endif
     177             : 
     178             : #if HAVE_PQPREPARE
     179             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_prepare, 0, 0, 0)
     180             :         ZEND_ARG_INFO(0, connection)
     181             :         ZEND_ARG_INFO(0, stmtname)
     182             :         ZEND_ARG_INFO(0, query)
     183             : ZEND_END_ARG_INFO()
     184             : #endif
     185             : 
     186             : #if HAVE_PQEXECPREPARED
     187             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_execute, 0, 0, 0)
     188             :         ZEND_ARG_INFO(0, connection)
     189             :         ZEND_ARG_INFO(0, stmtname)
     190             :         ZEND_ARG_INFO(0, params)
     191             : ZEND_END_ARG_INFO()
     192             : #endif
     193             : 
     194             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_num_rows, 0, 0, 1)
     195             :         ZEND_ARG_INFO(0, result)
     196             : ZEND_END_ARG_INFO()
     197             : 
     198             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_num_fields, 0, 0, 1)
     199             :         ZEND_ARG_INFO(0, result)
     200             : ZEND_END_ARG_INFO()
     201             : 
     202             : #if HAVE_PQCMDTUPLES
     203             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_affected_rows, 0, 0, 1)
     204             :         ZEND_ARG_INFO(0, result)
     205             : ZEND_END_ARG_INFO()
     206             : #endif
     207             : 
     208             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_last_notice, 0, 0, 1)
     209             :         ZEND_ARG_INFO(0, connection)
     210             : ZEND_END_ARG_INFO()
     211             : 
     212             : #ifdef HAVE_PQFTABLE
     213             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_field_table, 0, 0, 2)
     214             :         ZEND_ARG_INFO(0, result)
     215             :         ZEND_ARG_INFO(0, field_number)
     216             :         ZEND_ARG_INFO(0, oid_only)
     217             : ZEND_END_ARG_INFO()
     218             : #endif
     219             : 
     220             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_field_name, 0, 0, 2)
     221             :         ZEND_ARG_INFO(0, result)
     222             :         ZEND_ARG_INFO(0, field_number)
     223             : ZEND_END_ARG_INFO()
     224             : 
     225             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_field_size, 0, 0, 2)
     226             :         ZEND_ARG_INFO(0, result)
     227             :         ZEND_ARG_INFO(0, field_number)
     228             : ZEND_END_ARG_INFO()
     229             : 
     230             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_field_type, 0, 0, 2)
     231             :         ZEND_ARG_INFO(0, result)
     232             :         ZEND_ARG_INFO(0, field_number)
     233             : ZEND_END_ARG_INFO()
     234             : 
     235             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_field_type_oid, 0, 0, 2)
     236             :         ZEND_ARG_INFO(0, result)
     237             :         ZEND_ARG_INFO(0, field_number)
     238             : ZEND_END_ARG_INFO()
     239             : 
     240             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_field_num, 0, 0, 2)
     241             :         ZEND_ARG_INFO(0, result)
     242             :         ZEND_ARG_INFO(0, field_name)
     243             : ZEND_END_ARG_INFO()
     244             : 
     245             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_fetch_result, 0, 0, 1)
     246             :         ZEND_ARG_INFO(0, result)
     247             :         ZEND_ARG_INFO(0, row_number)
     248             :         ZEND_ARG_INFO(0, field_name)
     249             : ZEND_END_ARG_INFO()
     250             : 
     251             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_fetch_row, 0, 0, 1)
     252             :         ZEND_ARG_INFO(0, result)
     253             :         ZEND_ARG_INFO(0, row)
     254             :         ZEND_ARG_INFO(0, result_type)
     255             : ZEND_END_ARG_INFO()
     256             : 
     257             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_fetch_assoc, 0, 0, 1)
     258             :         ZEND_ARG_INFO(0, result)
     259             :         ZEND_ARG_INFO(0, row)
     260             : ZEND_END_ARG_INFO()
     261             : 
     262             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_fetch_array, 0, 0, 1)
     263             :         ZEND_ARG_INFO(0, result)
     264             :         ZEND_ARG_INFO(0, row)
     265             :         ZEND_ARG_INFO(0, result_type)
     266             : ZEND_END_ARG_INFO()
     267             : 
     268             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_fetch_object, 0, 0, 1)
     269             :         ZEND_ARG_INFO(0, result)
     270             :         ZEND_ARG_INFO(0, row)
     271             :         ZEND_ARG_INFO(0, class_name)
     272             :         ZEND_ARG_INFO(0, l)
     273             :         ZEND_ARG_INFO(0, ctor_params)
     274             : ZEND_END_ARG_INFO()
     275             : 
     276             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_fetch_all, 0, 0, 1)
     277             :         ZEND_ARG_INFO(0, result)
     278             : ZEND_END_ARG_INFO()
     279             : 
     280             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_fetch_all_columns, 0, 0, 1)
     281             :         ZEND_ARG_INFO(0, result)
     282             :         ZEND_ARG_INFO(0, column_number)
     283             : ZEND_END_ARG_INFO()
     284             : 
     285             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_result_seek, 0, 0, 2)
     286             :         ZEND_ARG_INFO(0, result)
     287             :         ZEND_ARG_INFO(0, offset)
     288             : ZEND_END_ARG_INFO()
     289             : 
     290             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_field_prtlen, 0, 0, 1)
     291             :         ZEND_ARG_INFO(0, result)
     292             :         ZEND_ARG_INFO(0, row)
     293             :         ZEND_ARG_INFO(0, field_name_or_number)
     294             : ZEND_END_ARG_INFO()
     295             : 
     296             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_field_is_null, 0, 0, 1)
     297             :         ZEND_ARG_INFO(0, result)
     298             :         ZEND_ARG_INFO(0, row)
     299             :         ZEND_ARG_INFO(0, field_name_or_number)
     300             : ZEND_END_ARG_INFO()
     301             : 
     302             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_free_result, 0, 0, 1)
     303             :         ZEND_ARG_INFO(0, result)
     304             : ZEND_END_ARG_INFO()
     305             : 
     306             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_last_oid, 0, 0, 1)
     307             :         ZEND_ARG_INFO(0, result)
     308             : ZEND_END_ARG_INFO()
     309             : 
     310             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_trace, 0, 0, 1)
     311             :         ZEND_ARG_INFO(0, filename)
     312             :         ZEND_ARG_INFO(0, mode)
     313             :         ZEND_ARG_INFO(0, connection)
     314             : ZEND_END_ARG_INFO()
     315             : 
     316             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_untrace, 0, 0, 0)
     317             :         ZEND_ARG_INFO(0, connection)
     318             : ZEND_END_ARG_INFO()
     319             : 
     320             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_create, 0, 0, 0)
     321             :         ZEND_ARG_INFO(0, connection)
     322             :         ZEND_ARG_INFO(0, large_object_id)
     323             : ZEND_END_ARG_INFO()
     324             : 
     325             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_unlink, 0, 0, 0)
     326             :         ZEND_ARG_INFO(0, connection)
     327             :         ZEND_ARG_INFO(0, large_object_oid)
     328             : ZEND_END_ARG_INFO()
     329             : 
     330             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_open, 0, 0, 0)
     331             :         ZEND_ARG_INFO(0, connection)
     332             :         ZEND_ARG_INFO(0, large_object_oid)
     333             :         ZEND_ARG_INFO(0, mode)
     334             : ZEND_END_ARG_INFO()
     335             : 
     336             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_close, 0, 0, 1)
     337             :         ZEND_ARG_INFO(0, large_object)
     338             : ZEND_END_ARG_INFO()
     339             : 
     340             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_read, 0, 0, 1)
     341             :         ZEND_ARG_INFO(0, large_object)
     342             :         ZEND_ARG_INFO(0, len)
     343             : ZEND_END_ARG_INFO()
     344             : 
     345             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_write, 0, 0, 2)
     346             :         ZEND_ARG_INFO(0, large_object)
     347             :         ZEND_ARG_INFO(0, buf)
     348             :         ZEND_ARG_INFO(0, len)
     349             : ZEND_END_ARG_INFO()
     350             : 
     351             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_read_all, 0, 0, 1)
     352             :         ZEND_ARG_INFO(0, large_object)
     353             : ZEND_END_ARG_INFO()
     354             : 
     355             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_import, 0, 0, 0)
     356             :         ZEND_ARG_INFO(0, connection)
     357             :         ZEND_ARG_INFO(0, filename)
     358             :         ZEND_ARG_INFO(0, large_object_oid)
     359             : ZEND_END_ARG_INFO()
     360             : 
     361             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_export, 0, 0, 0)
     362             :         ZEND_ARG_INFO(0, connection)
     363             :         ZEND_ARG_INFO(0, objoid)
     364             :         ZEND_ARG_INFO(0, filename)
     365             : ZEND_END_ARG_INFO()
     366             : 
     367             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_seek, 0, 0, 2)
     368             :         ZEND_ARG_INFO(0, large_object)
     369             :         ZEND_ARG_INFO(0, offset)
     370             :         ZEND_ARG_INFO(0, whence)
     371             : ZEND_END_ARG_INFO()
     372             : 
     373             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_tell, 0, 0, 1)
     374             :         ZEND_ARG_INFO(0, large_object)
     375             : ZEND_END_ARG_INFO()
     376             : 
     377             : #if HAVE_PG_LO_TRUNCATE
     378             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_lo_truncate, 0, 0, 1)
     379             :         ZEND_ARG_INFO(0, large_object)
     380             :         ZEND_ARG_INFO(0, size)
     381             : ZEND_END_ARG_INFO()
     382             : #endif
     383             : 
     384             : #if HAVE_PQSETERRORVERBOSITY
     385             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_set_error_verbosity, 0, 0, 0)
     386             :         ZEND_ARG_INFO(0, connection)
     387             :         ZEND_ARG_INFO(0, verbosity)
     388             : ZEND_END_ARG_INFO()
     389             : #endif
     390             : 
     391             : #if HAVE_PQCLIENTENCODING
     392             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_set_client_encoding, 0, 0, 0)
     393             :         ZEND_ARG_INFO(0, connection)
     394             :         ZEND_ARG_INFO(0, encoding)
     395             : ZEND_END_ARG_INFO()
     396             : 
     397             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_client_encoding, 0, 0, 0)
     398             :         ZEND_ARG_INFO(0, connection)
     399             : ZEND_END_ARG_INFO()
     400             : #endif
     401             : 
     402             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_end_copy, 0, 0, 0)
     403             :         ZEND_ARG_INFO(0, connection)
     404             : ZEND_END_ARG_INFO()
     405             : 
     406             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_put_line, 0, 0, 0)
     407             :         ZEND_ARG_INFO(0, connection)
     408             :         ZEND_ARG_INFO(0, query)
     409             : ZEND_END_ARG_INFO()
     410             : 
     411             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_copy_to, 0, 0, 2)
     412             :         ZEND_ARG_INFO(0, connection)
     413             :         ZEND_ARG_INFO(0, table_name)
     414             :         ZEND_ARG_INFO(0, delimiter)
     415             :         ZEND_ARG_INFO(0, null_as)
     416             : ZEND_END_ARG_INFO()
     417             : 
     418             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_copy_from, 0, 0, 3)
     419             :         ZEND_ARG_INFO(0, connection)
     420             :         ZEND_ARG_INFO(0, table_name)
     421             :         ZEND_ARG_INFO(0, rows)
     422             :         ZEND_ARG_INFO(0, delimiter)
     423             :         ZEND_ARG_INFO(0, null_as)
     424             : ZEND_END_ARG_INFO()
     425             : 
     426             : #if HAVE_PQESCAPE
     427             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_escape_string, 0, 0, 0)
     428             :         ZEND_ARG_INFO(0, connection)
     429             :         ZEND_ARG_INFO(0, data)
     430             : ZEND_END_ARG_INFO()
     431             : 
     432             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_escape_bytea, 0, 0, 0)
     433             :         ZEND_ARG_INFO(0, connection)
     434             :         ZEND_ARG_INFO(0, data)
     435             : ZEND_END_ARG_INFO()
     436             : 
     437             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_unescape_bytea, 0, 0, 1)
     438             :         ZEND_ARG_INFO(0, data)
     439             : ZEND_END_ARG_INFO()
     440             : #endif
     441             : 
     442             : #if HAVE_PQESCAPE
     443             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_escape_literal, 0, 0, 0)
     444             :         ZEND_ARG_INFO(0, connection)
     445             :         ZEND_ARG_INFO(0, data)
     446             : ZEND_END_ARG_INFO()
     447             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_escape_identifier, 0, 0, 0)
     448             :         ZEND_ARG_INFO(0, connection)
     449             :         ZEND_ARG_INFO(0, data)
     450             : ZEND_END_ARG_INFO()
     451             : #endif
     452             : 
     453             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_result_error, 0, 0, 1)
     454             :         ZEND_ARG_INFO(0, result)
     455             : ZEND_END_ARG_INFO()
     456             : 
     457             : #if HAVE_PQRESULTERRORFIELD
     458             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_result_error_field, 0, 0, 2)
     459             :         ZEND_ARG_INFO(0, result)
     460             :         ZEND_ARG_INFO(0, fieldcode)
     461             : ZEND_END_ARG_INFO()
     462             : #endif
     463             : 
     464             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_connection_status, 0, 0, 1)
     465             :         ZEND_ARG_INFO(0, connection)
     466             : ZEND_END_ARG_INFO()
     467             : 
     468             : #if HAVE_PGTRANSACTIONSTATUS
     469             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_transaction_status, 0, 0, 1)
     470             :         ZEND_ARG_INFO(0, connection)
     471             : ZEND_END_ARG_INFO()
     472             : #endif
     473             : 
     474             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_connection_reset, 0, 0, 1)
     475             :         ZEND_ARG_INFO(0, connection)
     476             : ZEND_END_ARG_INFO()
     477             : 
     478             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_cancel_query, 0, 0, 1)
     479             :         ZEND_ARG_INFO(0, connection)
     480             : ZEND_END_ARG_INFO()
     481             : 
     482             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_connection_busy, 0, 0, 1)
     483             :         ZEND_ARG_INFO(0, connection)
     484             : ZEND_END_ARG_INFO()
     485             : 
     486             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_send_query, 0, 0, 2)
     487             :         ZEND_ARG_INFO(0, connection)
     488             :         ZEND_ARG_INFO(0, query)
     489             : ZEND_END_ARG_INFO()
     490             : 
     491             : #if HAVE_PQSENDQUERYPARAMS
     492             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_send_query_params, 0, 0, 3)
     493             :         ZEND_ARG_INFO(0, connection)
     494             :         ZEND_ARG_INFO(0, query)
     495             :         ZEND_ARG_INFO(0, params)
     496             : ZEND_END_ARG_INFO()
     497             : #endif
     498             : 
     499             : #if HAVE_PQSENDPREPARE
     500             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_send_prepare, 0, 0, 3)
     501             :         ZEND_ARG_INFO(0, connection)
     502             :         ZEND_ARG_INFO(0, stmtname)
     503             :         ZEND_ARG_INFO(0, query)
     504             : ZEND_END_ARG_INFO()
     505             : #endif
     506             : 
     507             : #if HAVE_PQSENDQUERYPREPARED
     508             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_send_execute, 0, 0, 3)
     509             :         ZEND_ARG_INFO(0, connection)
     510             :         ZEND_ARG_INFO(0, stmtname)
     511             :         ZEND_ARG_INFO(0, params)
     512             : ZEND_END_ARG_INFO()
     513             : #endif
     514             : 
     515             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_get_result, 0, 0, 1)
     516             :         ZEND_ARG_INFO(0, connection)
     517             : ZEND_END_ARG_INFO()
     518             : 
     519             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_result_status, 0, 0, 1)
     520             :         ZEND_ARG_INFO(0, result)
     521             :         ZEND_ARG_INFO(0, result_type)
     522             : ZEND_END_ARG_INFO()
     523             : 
     524             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_get_notify, 0, 0, 0)
     525             :         ZEND_ARG_INFO(0, connection)
     526             :         ZEND_ARG_INFO(0, e)
     527             : ZEND_END_ARG_INFO()
     528             : 
     529             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_get_pid, 0, 0, 0)
     530             :         ZEND_ARG_INFO(0, connection)
     531             : ZEND_END_ARG_INFO()
     532             : 
     533             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_socket, 0, 0, 1)
     534             :         ZEND_ARG_INFO(0, connection)
     535             : ZEND_END_ARG_INFO()
     536             : 
     537             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_consume_input, 0, 0, 1)
     538             :         ZEND_ARG_INFO(0, connection)
     539             : ZEND_END_ARG_INFO()
     540             : 
     541             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_flush, 0, 0, 1)
     542             :         ZEND_ARG_INFO(0, connection)
     543             : ZEND_END_ARG_INFO()
     544             : 
     545             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_meta_data, 0, 0, 2)
     546             :         ZEND_ARG_INFO(0, db)
     547             :         ZEND_ARG_INFO(0, table)
     548             : ZEND_END_ARG_INFO()
     549             : 
     550             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_convert, 0, 0, 3)
     551             :         ZEND_ARG_INFO(0, db)
     552             :         ZEND_ARG_INFO(0, table)
     553             :         ZEND_ARG_INFO(0, values)
     554             :         ZEND_ARG_INFO(0, options)
     555             : ZEND_END_ARG_INFO()
     556             : 
     557             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_insert, 0, 0, 3)
     558             :         ZEND_ARG_INFO(0, db)
     559             :         ZEND_ARG_INFO(0, table)
     560             :         ZEND_ARG_INFO(0, values)
     561             :         ZEND_ARG_INFO(0, options)
     562             : ZEND_END_ARG_INFO()
     563             : 
     564             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_update, 0, 0, 4)
     565             :         ZEND_ARG_INFO(0, db)
     566             :         ZEND_ARG_INFO(0, table)
     567             :         ZEND_ARG_INFO(0, fields)
     568             :         ZEND_ARG_INFO(0, ids)
     569             :         ZEND_ARG_INFO(0, options)
     570             : ZEND_END_ARG_INFO()
     571             : 
     572             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_delete, 0, 0, 3)
     573             :         ZEND_ARG_INFO(0, db)
     574             :         ZEND_ARG_INFO(0, table)
     575             :         ZEND_ARG_INFO(0, ids)
     576             :         ZEND_ARG_INFO(0, options)
     577             : ZEND_END_ARG_INFO()
     578             : 
     579             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pg_select, 0, 0, 3)
     580             :         ZEND_ARG_INFO(0, db)
     581             :         ZEND_ARG_INFO(0, table)
     582             :         ZEND_ARG_INFO(0, ids)
     583             :         ZEND_ARG_INFO(0, options)
     584             : ZEND_END_ARG_INFO()
     585             : /* }}} */
     586             : 
     587             : /* {{{ pgsql_functions[]
     588             :  */
     589             : const zend_function_entry pgsql_functions[] = {
     590             :         /* connection functions */
     591             :         PHP_FE(pg_connect,              arginfo_pg_connect)
     592             :         PHP_FE(pg_pconnect,             arginfo_pg_pconnect)
     593             :         PHP_FE(pg_connect_poll, arginfo_pg_connect_poll)
     594             :         PHP_FE(pg_close,                arginfo_pg_close)
     595             :         PHP_FE(pg_connection_status,    arginfo_pg_connection_status)
     596             :         PHP_FE(pg_connection_busy,              arginfo_pg_connection_busy)
     597             :         PHP_FE(pg_connection_reset,             arginfo_pg_connection_reset)
     598             :         PHP_FE(pg_host,                 arginfo_pg_host)
     599             :         PHP_FE(pg_dbname,               arginfo_pg_dbname)
     600             :         PHP_FE(pg_port,                 arginfo_pg_port)
     601             :         PHP_FE(pg_tty,                  arginfo_pg_tty)
     602             :         PHP_FE(pg_options,              arginfo_pg_options)
     603             :         PHP_FE(pg_version,              arginfo_pg_version)
     604             :         PHP_FE(pg_ping,                 arginfo_pg_ping)
     605             : #if HAVE_PQPARAMETERSTATUS
     606             :         PHP_FE(pg_parameter_status, arginfo_pg_parameter_status)
     607             : #endif
     608             : #if HAVE_PGTRANSACTIONSTATUS
     609             :         PHP_FE(pg_transaction_status, arginfo_pg_transaction_status)
     610             : #endif
     611             :         /* query functions */
     612             :         PHP_FE(pg_query,                arginfo_pg_query)
     613             : #if HAVE_PQEXECPARAMS
     614             :         PHP_FE(pg_query_params,         arginfo_pg_query_params)
     615             : #endif
     616             : #if HAVE_PQPREPARE
     617             :         PHP_FE(pg_prepare,              arginfo_pg_prepare)
     618             : #endif
     619             : #if HAVE_PQEXECPREPARED
     620             :         PHP_FE(pg_execute,              arginfo_pg_execute)
     621             : #endif
     622             :         PHP_FE(pg_send_query,   arginfo_pg_send_query)
     623             : #if HAVE_PQSENDQUERYPARAMS
     624             :         PHP_FE(pg_send_query_params,    arginfo_pg_send_query_params)
     625             : #endif
     626             : #if HAVE_PQSENDPREPARE
     627             :         PHP_FE(pg_send_prepare, arginfo_pg_send_prepare)
     628             : #endif
     629             : #if HAVE_PQSENDQUERYPREPARED
     630             :         PHP_FE(pg_send_execute, arginfo_pg_send_execute)
     631             : #endif
     632             :         PHP_FE(pg_cancel_query, arginfo_pg_cancel_query)
     633             :         /* result functions */
     634             :         PHP_FE(pg_fetch_result, arginfo_pg_fetch_result)
     635             :         PHP_FE(pg_fetch_row,    arginfo_pg_fetch_row)
     636             :         PHP_FE(pg_fetch_assoc,  arginfo_pg_fetch_assoc)
     637             :         PHP_FE(pg_fetch_array,  arginfo_pg_fetch_array)
     638             :         PHP_FE(pg_fetch_object, arginfo_pg_fetch_object)
     639             :         PHP_FE(pg_fetch_all,    arginfo_pg_fetch_all)
     640             :         PHP_FE(pg_fetch_all_columns,    arginfo_pg_fetch_all_columns)
     641             : #if HAVE_PQCMDTUPLES
     642             :         PHP_FE(pg_affected_rows,arginfo_pg_affected_rows)
     643             : #endif
     644             :         PHP_FE(pg_get_result,   arginfo_pg_get_result)
     645             :         PHP_FE(pg_result_seek,  arginfo_pg_result_seek)
     646             :         PHP_FE(pg_result_status,arginfo_pg_result_status)
     647             :         PHP_FE(pg_free_result,  arginfo_pg_free_result)
     648             :         PHP_FE(pg_last_oid,         arginfo_pg_last_oid)
     649             :         PHP_FE(pg_num_rows,             arginfo_pg_num_rows)
     650             :         PHP_FE(pg_num_fields,   arginfo_pg_num_fields)
     651             :         PHP_FE(pg_field_name,   arginfo_pg_field_name)
     652             :         PHP_FE(pg_field_num,    arginfo_pg_field_num)
     653             :         PHP_FE(pg_field_size,   arginfo_pg_field_size)
     654             :         PHP_FE(pg_field_type,   arginfo_pg_field_type)
     655             :         PHP_FE(pg_field_type_oid, arginfo_pg_field_type_oid)
     656             :         PHP_FE(pg_field_prtlen, arginfo_pg_field_prtlen)
     657             :         PHP_FE(pg_field_is_null,arginfo_pg_field_is_null)
     658             : #ifdef HAVE_PQFTABLE
     659             :         PHP_FE(pg_field_table,  arginfo_pg_field_table)
     660             : #endif
     661             :         /* async message function */
     662             :         PHP_FE(pg_get_notify,   arginfo_pg_get_notify)
     663             :         PHP_FE(pg_socket,               arginfo_pg_socket)
     664             :         PHP_FE(pg_consume_input,arginfo_pg_consume_input)
     665             :         PHP_FE(pg_flush,                arginfo_pg_flush)
     666             :         PHP_FE(pg_get_pid,      arginfo_pg_get_pid)
     667             :         /* error message functions */
     668             :         PHP_FE(pg_result_error, arginfo_pg_result_error)
     669             : #if HAVE_PQRESULTERRORFIELD
     670             :         PHP_FE(pg_result_error_field, arginfo_pg_result_error_field)
     671             : #endif
     672             :         PHP_FE(pg_last_error,   arginfo_pg_last_error)
     673             :         PHP_FE(pg_last_notice,  arginfo_pg_last_notice)
     674             :         /* copy functions */
     675             :         PHP_FE(pg_put_line,             arginfo_pg_put_line)
     676             :         PHP_FE(pg_end_copy,             arginfo_pg_end_copy)
     677             :         PHP_FE(pg_copy_to,      arginfo_pg_copy_to)
     678             :         PHP_FE(pg_copy_from,    arginfo_pg_copy_from)
     679             :         /* debug functions */
     680             :         PHP_FE(pg_trace,                arginfo_pg_trace)
     681             :         PHP_FE(pg_untrace,              arginfo_pg_untrace)
     682             :         /* large object functions */
     683             :         PHP_FE(pg_lo_create,    arginfo_pg_lo_create)
     684             :         PHP_FE(pg_lo_unlink,    arginfo_pg_lo_unlink)
     685             :         PHP_FE(pg_lo_open,              arginfo_pg_lo_open)
     686             :         PHP_FE(pg_lo_close,             arginfo_pg_lo_close)
     687             :         PHP_FE(pg_lo_read,              arginfo_pg_lo_read)
     688             :         PHP_FE(pg_lo_write,             arginfo_pg_lo_write)
     689             :         PHP_FE(pg_lo_read_all,  arginfo_pg_lo_read_all)
     690             :         PHP_FE(pg_lo_import,    arginfo_pg_lo_import)
     691             :         PHP_FE(pg_lo_export,    arginfo_pg_lo_export)
     692             :         PHP_FE(pg_lo_seek,              arginfo_pg_lo_seek)
     693             :         PHP_FE(pg_lo_tell,              arginfo_pg_lo_tell)
     694             : #if HAVE_PG_LO_TRUNCATE
     695             :         PHP_FE(pg_lo_truncate,  arginfo_pg_lo_truncate)
     696             : #endif
     697             :         /* utility functions */
     698             : #if HAVE_PQESCAPE
     699             :         PHP_FE(pg_escape_string,        arginfo_pg_escape_string)
     700             :         PHP_FE(pg_escape_bytea,         arginfo_pg_escape_bytea)
     701             :         PHP_FE(pg_unescape_bytea,       arginfo_pg_unescape_bytea)
     702             :         PHP_FE(pg_escape_literal,       arginfo_pg_escape_literal)
     703             :         PHP_FE(pg_escape_identifier,    arginfo_pg_escape_identifier)
     704             : #endif
     705             : #if HAVE_PQSETERRORVERBOSITY
     706             :         PHP_FE(pg_set_error_verbosity,  arginfo_pg_set_error_verbosity)
     707             : #endif
     708             : #if HAVE_PQCLIENTENCODING
     709             :         PHP_FE(pg_client_encoding,              arginfo_pg_client_encoding)
     710             :         PHP_FE(pg_set_client_encoding,  arginfo_pg_set_client_encoding)
     711             : #endif
     712             :         /* misc function */
     713             :         PHP_FE(pg_meta_data,    arginfo_pg_meta_data)
     714             :         PHP_FE(pg_convert,      arginfo_pg_convert)
     715             :         PHP_FE(pg_insert,       arginfo_pg_insert)
     716             :         PHP_FE(pg_update,       arginfo_pg_update)
     717             :         PHP_FE(pg_delete,       arginfo_pg_delete)
     718             :         PHP_FE(pg_select,       arginfo_pg_select)
     719             :         /* aliases for downwards compatibility */
     720             :         PHP_FALIAS(pg_exec,          pg_query,          arginfo_pg_query)
     721             :         PHP_FALIAS(pg_getlastoid,    pg_last_oid,       arginfo_pg_last_oid)
     722             : #if HAVE_PQCMDTUPLES
     723             :         PHP_FALIAS(pg_cmdtuples,         pg_affected_rows,  arginfo_pg_affected_rows)
     724             : #endif
     725             :         PHP_FALIAS(pg_errormessage,      pg_last_error,     arginfo_pg_last_error)
     726             :         PHP_FALIAS(pg_numrows,           pg_num_rows,       arginfo_pg_num_rows)
     727             :         PHP_FALIAS(pg_numfields,         pg_num_fields,     arginfo_pg_num_fields)
     728             :         PHP_FALIAS(pg_fieldname,         pg_field_name,     arginfo_pg_field_name)
     729             :         PHP_FALIAS(pg_fieldsize,     pg_field_size,     arginfo_pg_field_size)
     730             :         PHP_FALIAS(pg_fieldtype,         pg_field_type,     arginfo_pg_field_type)
     731             :         PHP_FALIAS(pg_fieldnum,      pg_field_num,      arginfo_pg_field_num)
     732             :         PHP_FALIAS(pg_fieldprtlen,       pg_field_prtlen,   arginfo_pg_field_prtlen)
     733             :         PHP_FALIAS(pg_fieldisnull,       pg_field_is_null,  arginfo_pg_field_is_null)
     734             :         PHP_FALIAS(pg_freeresult,    pg_free_result,    arginfo_pg_free_result)
     735             :         PHP_FALIAS(pg_result,        pg_fetch_result,   arginfo_pg_get_result)
     736             :         PHP_FALIAS(pg_loreadall,         pg_lo_read_all,    arginfo_pg_lo_read_all)
     737             :         PHP_FALIAS(pg_locreate,      pg_lo_create,      arginfo_pg_lo_create)
     738             :         PHP_FALIAS(pg_lounlink,      pg_lo_unlink,      arginfo_pg_lo_unlink)
     739             :         PHP_FALIAS(pg_loopen,        pg_lo_open,        arginfo_pg_lo_open)
     740             :         PHP_FALIAS(pg_loclose,       pg_lo_close,       arginfo_pg_lo_close)
     741             :         PHP_FALIAS(pg_loread,        pg_lo_read,        arginfo_pg_lo_read)
     742             :         PHP_FALIAS(pg_lowrite,       pg_lo_write,       arginfo_pg_lo_write)
     743             :         PHP_FALIAS(pg_loimport,      pg_lo_import,      arginfo_pg_lo_import)
     744             :         PHP_FALIAS(pg_loexport,      pg_lo_export,      arginfo_pg_lo_export)
     745             : #if HAVE_PQCLIENTENCODING
     746             :         PHP_FALIAS(pg_clientencoding,           pg_client_encoding,             arginfo_pg_client_encoding)
     747             :         PHP_FALIAS(pg_setclientencoding,        pg_set_client_encoding, arginfo_pg_set_client_encoding)
     748             : #endif
     749             :         PHP_FE_END
     750             : };
     751             : /* }}} */
     752             : 
     753             : /* {{{ pgsql_module_entry
     754             :  */
     755             : zend_module_entry pgsql_module_entry = {
     756             :         STANDARD_MODULE_HEADER,
     757             :         "pgsql",
     758             :         pgsql_functions,
     759             :         PHP_MINIT(pgsql),
     760             :         PHP_MSHUTDOWN(pgsql),
     761             :         PHP_RINIT(pgsql),
     762             :         PHP_RSHUTDOWN(pgsql),
     763             :         PHP_MINFO(pgsql),
     764             :         NO_VERSION_YET,
     765             :         PHP_MODULE_GLOBALS(pgsql),
     766             :         PHP_GINIT(pgsql),
     767             :         NULL,
     768             :         NULL,
     769             :         STANDARD_MODULE_PROPERTIES_EX
     770             : };
     771             : /* }}} */
     772             : 
     773             : #ifdef COMPILE_DL_PGSQL
     774             : ZEND_GET_MODULE(pgsql)
     775             : #endif
     776             : 
     777             : static int le_link, le_plink, le_result, le_lofp, le_string;
     778             : 
     779             : /* Compatibility definitions */
     780             : 
     781             : #ifndef HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT
     782             : #define pg_encoding_to_char(x) "SQL_ASCII"
     783             : #endif
     784             : 
     785             : #if !HAVE_PQESCAPE_CONN
     786             : #define PQescapeStringConn(conn, to, form, len, error) PQescapeString(to, from, len)
     787             : #endif
     788             : 
     789             : #if HAVE_PQESCAPELITERAL
     790             : #define PGSQLescapeLiteral(conn, str, len) PQescapeLiteral(conn, str, len)
     791             : #define PGSQLescapeIdentifier(conn, str, len) PQescapeIdentifier(conn, str, len)
     792             : #define PGSQLfree(a) PQfreemem(a)
     793             : #else
     794             : #define PGSQLescapeLiteral(conn, str, len) php_pgsql_PQescapeInternal(conn, str, len, 1, 0)
     795             : #define PGSQLescapeLiteral2(conn, str, len) php_pgsql_PQescapeInternal(conn, str, len, 1, 1)
     796             : #define PGSQLescapeIdentifier(conn, str, len) php_pgsql_PQescapeInternal(conn, str, len, 0, 0)
     797             : #define PGSQLfree(a) efree(a)
     798             : 
     799             : /* emulate libpq's PQescapeInternal() 9.0 or later */
     800         146 : static char* php_pgsql_PQescapeInternal(PGconn *conn, const char *str, size_t len, int escape_literal, int safe) {
     801             :         char *result, *rp, *s;
     802             :         size_t tmp_len;
     803             : 
     804         146 :         if (!conn) {
     805           0 :                 return NULL;
     806             :         }
     807             : 
     808             :         /* allocate enough memory */
     809         146 :         rp = result = (char *)safe_emalloc(len, 2, 5); /* leading " E" needs extra 2 bytes + quote_chars on both end for 2 bytes + NULL */
     810             : 
     811         146 :         if (escape_literal) {
     812             :                 size_t new_len;
     813             : 
     814           1 :                 if (safe) {
     815           0 :                         char *tmp = (char *)safe_emalloc(len, 2, 1);
     816           0 :                         *rp++ = '\'';
     817             :                         /* PQescapeString does not escape \, but it handles multibyte chars safely.
     818             :                            This escape is incompatible with PQescapeLiteral. */
     819           0 :                         new_len = PQescapeStringConn(conn, tmp, str, len, NULL);
     820           0 :                         strncpy(rp, tmp, new_len);
     821           0 :                         efree(tmp);
     822           0 :                         rp += new_len;
     823             :                 } else {
     824             :                         char *encoding;
     825             :                         /* This is compatible with PQescapeLiteral, but it cannot handle multbyte chars
     826             :                            such as SJIS, BIG5. Raise warning and return NULL by checking
     827             :                            client_encoding. */
     828           1 :                         encoding = (char *) pg_encoding_to_char(PQclientEncoding(conn));
     829           7 :                         if (!strncmp(encoding, "SJIS", sizeof("SJIS")-1) ||
     830           1 :                                 !strncmp(encoding, "SHIFT_JIS_2004", sizeof("SHIFT_JIS_2004")-1) ||
     831           1 :                                 !strncmp(encoding, "BIG5", sizeof("BIG5")-1) ||
     832           1 :                                 !strncmp(encoding, "GB18030", sizeof("GB18030")-1) ||
     833           1 :                                 !strncmp(encoding, "GBK", sizeof("GBK")-1) ||
     834           1 :                                 !strncmp(encoding, "JOHAB", sizeof("JOHAB")-1) ||
     835           1 :                                 !strncmp(encoding, "UHC", sizeof("UHC")-1) ) {
     836             :                                 TSRMLS_FETCH();
     837             : 
     838           0 :                                 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);
     839             :                         }
     840             :                         /* check backslashes */
     841           1 :                         tmp_len = strspn(str, "\\");
     842           1 :                         if (tmp_len != len) {
     843             :                                 /* add " E" for escaping slashes */
     844           1 :                                 *rp++ = ' ';
     845           1 :                                 *rp++ = 'E';
     846             :                         }
     847           1 :                         *rp++ = '\'';
     848          10 :                         for (s = (char *)str; s - str < len; ++s) {
     849          12 :                                 if (*s == '\'' || *s == '\\') {
     850           3 :                                         *rp++ = *s;
     851           3 :                                         *rp++ = *s;
     852             :                                 } else {
     853           6 :                                         *rp++ = *s;
     854             :                                 }
     855             :                         }
     856             :                 }
     857           1 :                 *rp++ = '\'';
     858             :         } else {
     859             :                 /* Identifier escape. */
     860         145 :                 *rp++ = '"';
     861         878 :                 for (s = (char *)str; s - str < len; ++s) {
     862         733 :                         if (*s == '"') {
     863           0 :                                 *rp++ = '"';
     864           0 :                                 *rp++ = '"';
     865             :                         } else {
     866         733 :                                 *rp++ = *s;
     867             :                         }
     868             :                 }
     869         145 :                 *rp++ = '"';
     870             :         }
     871         146 :         *rp = '\0';
     872             : 
     873         146 :         return result;
     874             : }
     875             : #endif
     876             : 
     877             : 
     878             : /* {{{ _php_pgsql_trim_message */
     879          20 : static char * _php_pgsql_trim_message(const char *message, int *len)
     880             : {
     881          20 :         register int i = strlen(message)-1;
     882             : 
     883          20 :         if (i>1 && (message[i-1] == '\r' || message[i-1] == '\n') && message[i] == '.') {
     884           0 :                 --i;
     885             :         }
     886          57 :         while (i>0 && (message[i] == '\r' || message[i] == '\n')) {
     887          17 :                 --i;
     888             :         }
     889          20 :         ++i;
     890          20 :         if (len) {
     891           6 :                 *len = i;
     892             :         }
     893          20 :         return estrndup(message, i);
     894             : }
     895             : /* }}} */
     896             : 
     897             : /* {{{ _php_pgsql_trim_result */
     898           3 : static inline char * _php_pgsql_trim_result(PGconn * pgsql, char **buf)
     899             : {
     900           3 :         return *buf = _php_pgsql_trim_message(PQerrorMessage(pgsql), NULL);
     901             : }
     902             : /* }}} */
     903             : 
     904             : #define PQErrorMessageTrim(pgsql, buf) _php_pgsql_trim_result(pgsql, buf)
     905             : 
     906             : #define PHP_PQ_ERROR(text, pgsql) {                                                                             \
     907             :                 char *msgbuf = _php_pgsql_trim_message(PQerrorMessage(pgsql), NULL); \
     908             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, text, msgbuf);              \
     909             :                 efree(msgbuf);                                                                                                  \
     910             : } \
     911             : 
     912             : /* {{{ php_pgsql_set_default_link
     913             :  */
     914         113 : static void php_pgsql_set_default_link(int id TSRMLS_DC)
     915             : {
     916         113 :         zend_list_addref(id);
     917             : 
     918         113 :         if (PGG(default_link) != -1) {
     919           0 :                 zend_list_delete(PGG(default_link));
     920             :         }
     921             : 
     922         113 :         PGG(default_link) = id;
     923         113 : }
     924             : /* }}} */
     925             : 
     926             : /* {{{ _close_pgsql_link
     927             :  */
     928         112 : static void _close_pgsql_link(zend_rsrc_list_entry *rsrc TSRMLS_DC)
     929             : {
     930         112 :         PGconn *link = (PGconn *)rsrc->ptr;
     931             :         PGresult *res;
     932             : 
     933         227 :         while ((res = PQgetResult(link))) {
     934           3 :                 PQclear(res);
     935             :         }
     936         112 :         PQfinish(link);
     937         112 :         PGG(num_links)--;
     938         112 : }
     939             : /* }}} */
     940             : 
     941             : /* {{{ _close_pgsql_plink
     942             :  */
     943           1 : static void _close_pgsql_plink(zend_rsrc_list_entry *rsrc TSRMLS_DC)
     944             : {
     945           1 :         PGconn *link = (PGconn *)rsrc->ptr;
     946             :         PGresult *res;
     947             : 
     948           2 :         while ((res = PQgetResult(link))) {
     949           0 :                 PQclear(res);
     950             :         }
     951           1 :         PQfinish(link);
     952           1 :         PGG(num_persistent)--;
     953           1 :         PGG(num_links)--;
     954           1 : }
     955             : /* }}} */
     956             : 
     957             : /* {{{ _php_pgsql_notice_handler
     958             :  */
     959           6 : static void _php_pgsql_notice_handler(void *resource_id, const char *message)
     960             : {
     961             :         php_pgsql_notice *notice;
     962             :         
     963             :         TSRMLS_FETCH();
     964           6 :         if (! PGG(ignore_notices)) {
     965           6 :                 notice = (php_pgsql_notice *)emalloc(sizeof(php_pgsql_notice));
     966           6 :                 notice->message = _php_pgsql_trim_message(message, (int *)&notice->len);
     967           6 :                 if (PGG(log_notices)) {
     968           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "%s", notice->message);
     969             :                 }
     970           6 :                 zend_hash_index_update(&PGG(notices), (ulong)resource_id, (void **)&notice, sizeof(php_pgsql_notice *), NULL);
     971             :         }
     972           6 : }
     973             : /* }}} */
     974             : 
     975             : #define PHP_PGSQL_NOTICE_PTR_DTOR (void (*)(void *))_php_pgsql_notice_ptr_dtor
     976             : 
     977             : /* {{{ _php_pgsql_notice_dtor
     978             :  */
     979           6 : static void _php_pgsql_notice_ptr_dtor(void **ptr) 
     980             : {
     981           6 :         php_pgsql_notice *notice = (php_pgsql_notice *)*ptr;
     982           6 :         if (notice) {
     983           6 :                 efree(notice->message);
     984           6 :                 efree(notice);
     985           6 :                 notice = NULL;
     986             :         }
     987           6 : }
     988             : /* }}} */
     989             : 
     990             : /* {{{ _rollback_transactions
     991             :  */
     992         625 : static int _rollback_transactions(zend_rsrc_list_entry *rsrc TSRMLS_DC)
     993             : {
     994             :         PGconn *link;
     995             :         PGresult *res;
     996             :         int orig;
     997             : 
     998         625 :         if (Z_TYPE_P(rsrc) != le_plink) 
     999         624 :                 return 0;
    1000             : 
    1001           1 :         link = (PGconn *) rsrc->ptr;
    1002             : 
    1003           1 :         if (PQ_SETNONBLOCKING(link, 0)) {
    1004           0 :                 php_error_docref("ref.pgsql" TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode");
    1005           0 :                 return -1;
    1006             :         }
    1007             :         
    1008           2 :         while ((res = PQgetResult(link))) {
    1009           0 :                 PQclear(res);
    1010             :         }
    1011             : #if HAVE_PGTRANSACTIONSTATUS && HAVE_PQPROTOCOLVERSION
    1012           1 :         if ((PQprotocolVersion(link) >= 3 && PQtransactionStatus(link) != PQTRANS_IDLE) || PQprotocolVersion(link) < 3)
    1013             : #endif
    1014             :         {
    1015           0 :                 orig = PGG(ignore_notices);
    1016           0 :                 PGG(ignore_notices) = 1;
    1017             : #if HAVE_PGTRANSACTIONSTATUS && HAVE_PQPROTOCOLVERSION
    1018           0 :                 res = PQexec(link,"ROLLBACK;");
    1019             : #else
    1020             :                 res = PQexec(link,"BEGIN;");
    1021             :                 PQclear(res);
    1022             :                 res = PQexec(link,"ROLLBACK;");
    1023             : #endif
    1024           0 :                 PQclear(res);
    1025           0 :                 PGG(ignore_notices) = orig;
    1026             :         }
    1027             : 
    1028           1 :         return 0;
    1029             : }
    1030             : /* }}} */
    1031             : 
    1032             : /* {{{ _free_ptr
    1033             :  */
    1034        2903 : static void _free_ptr(zend_rsrc_list_entry *rsrc TSRMLS_DC)
    1035             : {
    1036        2903 :         pgLofp *lofp = (pgLofp *)rsrc->ptr;
    1037        2903 :         efree(lofp);
    1038        2903 : }
    1039             : /* }}} */
    1040             : 
    1041             : /* {{{ _free_result
    1042             :  */
    1043        1174 : static void _free_result(zend_rsrc_list_entry *rsrc TSRMLS_DC)
    1044             : {
    1045        1174 :         pgsql_result_handle *pg_result = (pgsql_result_handle *)rsrc->ptr;
    1046             : 
    1047        1174 :         PQclear(pg_result->result);
    1048        1174 :         efree(pg_result);
    1049        1174 : }
    1050             : /* }}} */
    1051             : 
    1052             : 
    1053         139 : static int _php_pgsql_detect_identifier_escape(const char *identifier, size_t len)
    1054             : {
    1055             :         size_t i;
    1056             : 
    1057             :         /* Handle edge case. Cannot be a escaped string */
    1058         139 :         if (len <= 2) {
    1059          30 :                 return FAILURE;
    1060             :         }
    1061             :         /* Detect double qoutes */
    1062         109 :         if (identifier[0] == '"' && identifier[len-1] == '"') {
    1063             :                 /* Detect wrong format of " inside of escaped string */
    1064           0 :                 for (i = 1; i < len-1; i++) {
    1065           0 :                         if (identifier[i] == '"' && (identifier[++i] != '"' || i == len-1)) {
    1066           0 :                                 return FAILURE;
    1067             :                         }
    1068             :                 }
    1069             :         } else {
    1070         109 :                 return FAILURE;
    1071             :         }
    1072             :         /* Escaped properly */
    1073           0 :         return SUCCESS;
    1074             : }
    1075             : 
    1076             : 
    1077             : 
    1078             : /* {{{ PHP_INI
    1079             :  */
    1080             : PHP_INI_BEGIN()
    1081             : STD_PHP_INI_BOOLEAN( "pgsql.allow_persistent",      "1",  PHP_INI_SYSTEM, OnUpdateBool, allow_persistent,      zend_pgsql_globals, pgsql_globals)
    1082             : STD_PHP_INI_ENTRY_EX("pgsql.max_persistent",       "-1",  PHP_INI_SYSTEM, OnUpdateLong, max_persistent,        zend_pgsql_globals, pgsql_globals, display_link_numbers)
    1083             : STD_PHP_INI_ENTRY_EX("pgsql.max_links",            "-1",  PHP_INI_SYSTEM, OnUpdateLong, max_links,             zend_pgsql_globals, pgsql_globals, display_link_numbers)
    1084             : STD_PHP_INI_BOOLEAN( "pgsql.auto_reset_persistent", "0",  PHP_INI_SYSTEM, OnUpdateBool, auto_reset_persistent, zend_pgsql_globals, pgsql_globals)
    1085             : STD_PHP_INI_BOOLEAN( "pgsql.ignore_notice",         "0",  PHP_INI_ALL,    OnUpdateBool, ignore_notices,        zend_pgsql_globals, pgsql_globals)
    1086             : STD_PHP_INI_BOOLEAN( "pgsql.log_notice",            "0",  PHP_INI_ALL,    OnUpdateBool, log_notices,           zend_pgsql_globals, pgsql_globals)
    1087             : PHP_INI_END()
    1088             : /* }}} */
    1089             : 
    1090             : /* {{{ PHP_GINIT_FUNCTION
    1091             :  */
    1092       21159 : static PHP_GINIT_FUNCTION(pgsql)
    1093             : {
    1094       21159 :         memset(pgsql_globals, 0, sizeof(zend_pgsql_globals));
    1095             :         /* Initilize notice message hash at MINIT only */
    1096       21159 :         zend_hash_init_ex(&pgsql_globals->notices, 0, NULL, PHP_PGSQL_NOTICE_PTR_DTOR, 1, 0); 
    1097       21159 : }
    1098             : /* }}} */
    1099             : 
    1100             : /* {{{ PHP_MINIT_FUNCTION
    1101             :  */
    1102       21159 : PHP_MINIT_FUNCTION(pgsql)
    1103             : {
    1104       21159 :         REGISTER_INI_ENTRIES();
    1105             : 
    1106       21159 :         le_link = zend_register_list_destructors_ex(_close_pgsql_link, NULL, "pgsql link", module_number);
    1107       21159 :         le_plink = zend_register_list_destructors_ex(NULL, _close_pgsql_plink, "pgsql link persistent", module_number);
    1108       21159 :         le_result = zend_register_list_destructors_ex(_free_result, NULL, "pgsql result", module_number);
    1109       21159 :         le_lofp = zend_register_list_destructors_ex(_free_ptr, NULL, "pgsql large object", module_number);
    1110       21159 :         le_string = zend_register_list_destructors_ex(_free_ptr, NULL, "pgsql string", module_number);
    1111             : #if HAVE_PG_CONFIG_H
    1112             :         /* PG_VERSION - libpq version */
    1113       21159 :         REGISTER_STRING_CONSTANT("PGSQL_LIBPQ_VERSION", PG_VERSION, CONST_CS | CONST_PERSISTENT);
    1114       21159 :         REGISTER_STRING_CONSTANT("PGSQL_LIBPQ_VERSION_STR", PG_VERSION_STR, CONST_CS | CONST_PERSISTENT);
    1115             : #endif
    1116             :         /* For connection option */
    1117       21159 :         REGISTER_LONG_CONSTANT("PGSQL_CONNECT_FORCE_NEW", PGSQL_CONNECT_FORCE_NEW, CONST_CS | CONST_PERSISTENT);
    1118       21159 :         REGISTER_LONG_CONSTANT("PGSQL_CONNECT_ASYNC", PGSQL_CONNECT_ASYNC, CONST_CS | CONST_PERSISTENT);
    1119             :         /* For pg_fetch_array() */
    1120       21159 :         REGISTER_LONG_CONSTANT("PGSQL_ASSOC", PGSQL_ASSOC, CONST_CS | CONST_PERSISTENT);
    1121       21159 :         REGISTER_LONG_CONSTANT("PGSQL_NUM", PGSQL_NUM, CONST_CS | CONST_PERSISTENT);
    1122       21159 :         REGISTER_LONG_CONSTANT("PGSQL_BOTH", PGSQL_BOTH, CONST_CS | CONST_PERSISTENT);
    1123             :         /* For pg_connection_status() */
    1124       21159 :         REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_BAD", CONNECTION_BAD, CONST_CS | CONST_PERSISTENT);
    1125       21159 :         REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_OK", CONNECTION_OK, CONST_CS | CONST_PERSISTENT);
    1126       21159 :         REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_STARTED", CONNECTION_STARTED, CONST_CS | CONST_PERSISTENT);
    1127       21159 :         REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_MADE", CONNECTION_MADE, CONST_CS | CONST_PERSISTENT);
    1128       21159 :         REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_AWAITING_RESPONSE", CONNECTION_AWAITING_RESPONSE, CONST_CS | CONST_PERSISTENT);
    1129       21159 :         REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_AUTH_OK", CONNECTION_AUTH_OK, CONST_CS | CONST_PERSISTENT);
    1130       21159 :         REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_SSL_STARTUP", CONNECTION_SSL_STARTUP, CONST_CS | CONST_PERSISTENT);
    1131       21159 :         REGISTER_LONG_CONSTANT("PGSQL_CONNECTION_SETENV", CONNECTION_SETENV, CONST_CS | CONST_PERSISTENT);
    1132             :         /* For pg_connect_poll() */
    1133       21159 :         REGISTER_LONG_CONSTANT("PGSQL_POLLING_FAILED", PGRES_POLLING_FAILED, CONST_CS | CONST_PERSISTENT);
    1134       21159 :         REGISTER_LONG_CONSTANT("PGSQL_POLLING_READING", PGRES_POLLING_READING, CONST_CS | CONST_PERSISTENT);
    1135       21159 :         REGISTER_LONG_CONSTANT("PGSQL_POLLING_WRITING", PGRES_POLLING_WRITING, CONST_CS | CONST_PERSISTENT);
    1136       21159 :         REGISTER_LONG_CONSTANT("PGSQL_POLLING_OK", PGRES_POLLING_OK, CONST_CS | CONST_PERSISTENT);
    1137       21159 :         REGISTER_LONG_CONSTANT("PGSQL_POLLING_ACTIVE", PGRES_POLLING_ACTIVE, CONST_CS | CONST_PERSISTENT);
    1138             : #if HAVE_PGTRANSACTIONSTATUS
    1139             :         /* For pg_transaction_status() */
    1140       21159 :         REGISTER_LONG_CONSTANT("PGSQL_TRANSACTION_IDLE", PQTRANS_IDLE, CONST_CS | CONST_PERSISTENT);
    1141       21159 :         REGISTER_LONG_CONSTANT("PGSQL_TRANSACTION_ACTIVE", PQTRANS_ACTIVE, CONST_CS | CONST_PERSISTENT);
    1142       21159 :         REGISTER_LONG_CONSTANT("PGSQL_TRANSACTION_INTRANS", PQTRANS_INTRANS, CONST_CS | CONST_PERSISTENT);
    1143       21159 :         REGISTER_LONG_CONSTANT("PGSQL_TRANSACTION_INERROR", PQTRANS_INERROR, CONST_CS | CONST_PERSISTENT);
    1144       21159 :         REGISTER_LONG_CONSTANT("PGSQL_TRANSACTION_UNKNOWN", PQTRANS_UNKNOWN, CONST_CS | CONST_PERSISTENT);
    1145             : #endif
    1146             : #if HAVE_PQSETERRORVERBOSITY
    1147             :         /* For pg_set_error_verbosity() */
    1148       21159 :         REGISTER_LONG_CONSTANT("PGSQL_ERRORS_TERSE", PQERRORS_TERSE, CONST_CS | CONST_PERSISTENT);
    1149       21159 :         REGISTER_LONG_CONSTANT("PGSQL_ERRORS_DEFAULT", PQERRORS_DEFAULT, CONST_CS | CONST_PERSISTENT);
    1150       21159 :         REGISTER_LONG_CONSTANT("PGSQL_ERRORS_VERBOSE", PQERRORS_VERBOSE, CONST_CS | CONST_PERSISTENT);
    1151             : #endif
    1152             :         /* For lo_seek() */
    1153       21159 :         REGISTER_LONG_CONSTANT("PGSQL_SEEK_SET", SEEK_SET, CONST_CS | CONST_PERSISTENT);
    1154       21159 :         REGISTER_LONG_CONSTANT("PGSQL_SEEK_CUR", SEEK_CUR, CONST_CS | CONST_PERSISTENT);
    1155       21159 :         REGISTER_LONG_CONSTANT("PGSQL_SEEK_END", SEEK_END, CONST_CS | CONST_PERSISTENT);
    1156             :         /* For pg_result_status() return value type */
    1157       21159 :         REGISTER_LONG_CONSTANT("PGSQL_STATUS_LONG", PGSQL_STATUS_LONG, CONST_CS | CONST_PERSISTENT);
    1158       21159 :         REGISTER_LONG_CONSTANT("PGSQL_STATUS_STRING", PGSQL_STATUS_STRING, CONST_CS | CONST_PERSISTENT);
    1159             :         /* For pg_result_status() return value */
    1160       21159 :         REGISTER_LONG_CONSTANT("PGSQL_EMPTY_QUERY", PGRES_EMPTY_QUERY, CONST_CS | CONST_PERSISTENT);
    1161       21159 :         REGISTER_LONG_CONSTANT("PGSQL_COMMAND_OK", PGRES_COMMAND_OK, CONST_CS | CONST_PERSISTENT);
    1162       21159 :         REGISTER_LONG_CONSTANT("PGSQL_TUPLES_OK", PGRES_TUPLES_OK, CONST_CS | CONST_PERSISTENT);
    1163       21159 :         REGISTER_LONG_CONSTANT("PGSQL_COPY_OUT", PGRES_COPY_OUT, CONST_CS | CONST_PERSISTENT);
    1164       21159 :         REGISTER_LONG_CONSTANT("PGSQL_COPY_IN", PGRES_COPY_IN, CONST_CS | CONST_PERSISTENT);
    1165       21159 :         REGISTER_LONG_CONSTANT("PGSQL_BAD_RESPONSE", PGRES_BAD_RESPONSE, CONST_CS | CONST_PERSISTENT);
    1166       21159 :         REGISTER_LONG_CONSTANT("PGSQL_NONFATAL_ERROR", PGRES_NONFATAL_ERROR, CONST_CS | CONST_PERSISTENT);
    1167       21159 :         REGISTER_LONG_CONSTANT("PGSQL_FATAL_ERROR", PGRES_FATAL_ERROR, CONST_CS | CONST_PERSISTENT);
    1168             : #if HAVE_PQRESULTERRORFIELD
    1169             :         /* For pg_result_error_field() field codes */
    1170       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DIAG_SEVERITY", PG_DIAG_SEVERITY, CONST_CS | CONST_PERSISTENT);
    1171       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DIAG_SQLSTATE", PG_DIAG_SQLSTATE, CONST_CS | CONST_PERSISTENT);
    1172       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DIAG_MESSAGE_PRIMARY", PG_DIAG_MESSAGE_PRIMARY, CONST_CS | CONST_PERSISTENT);
    1173       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DIAG_MESSAGE_DETAIL", PG_DIAG_MESSAGE_DETAIL, CONST_CS | CONST_PERSISTENT);
    1174       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DIAG_MESSAGE_HINT", PG_DIAG_MESSAGE_HINT, CONST_CS | CONST_PERSISTENT);
    1175       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DIAG_STATEMENT_POSITION", PG_DIAG_STATEMENT_POSITION, CONST_CS | CONST_PERSISTENT);
    1176             : #ifdef PG_DIAG_INTERNAL_POSITION
    1177       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DIAG_INTERNAL_POSITION", PG_DIAG_INTERNAL_POSITION, CONST_CS | CONST_PERSISTENT);
    1178             : #endif
    1179             : #ifdef PG_DIAG_INTERNAL_QUERY
    1180       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DIAG_INTERNAL_QUERY", PG_DIAG_INTERNAL_QUERY, CONST_CS | CONST_PERSISTENT);
    1181             : #endif
    1182       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DIAG_CONTEXT", PG_DIAG_CONTEXT, CONST_CS | CONST_PERSISTENT);
    1183       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DIAG_SOURCE_FILE", PG_DIAG_SOURCE_FILE, CONST_CS | CONST_PERSISTENT);
    1184       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DIAG_SOURCE_LINE", PG_DIAG_SOURCE_LINE, CONST_CS | CONST_PERSISTENT);
    1185       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DIAG_SOURCE_FUNCTION", PG_DIAG_SOURCE_FUNCTION, CONST_CS | CONST_PERSISTENT);
    1186             : #endif
    1187             :         /* pg_convert options */
    1188       21159 :         REGISTER_LONG_CONSTANT("PGSQL_CONV_IGNORE_DEFAULT", PGSQL_CONV_IGNORE_DEFAULT, CONST_CS | CONST_PERSISTENT);
    1189       21159 :         REGISTER_LONG_CONSTANT("PGSQL_CONV_FORCE_NULL", PGSQL_CONV_FORCE_NULL, CONST_CS | CONST_PERSISTENT);
    1190       21159 :         REGISTER_LONG_CONSTANT("PGSQL_CONV_IGNORE_NOT_NULL", PGSQL_CONV_IGNORE_NOT_NULL, CONST_CS | CONST_PERSISTENT);
    1191             :         /* pg_insert/update/delete/select options */
    1192       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DML_ESCAPE", PGSQL_DML_ESCAPE, CONST_CS | CONST_PERSISTENT);
    1193       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DML_NO_CONV", PGSQL_DML_NO_CONV, CONST_CS | CONST_PERSISTENT);
    1194       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DML_EXEC", PGSQL_DML_EXEC, CONST_CS | CONST_PERSISTENT);
    1195       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DML_ASYNC", PGSQL_DML_ASYNC, CONST_CS | CONST_PERSISTENT);
    1196       21159 :         REGISTER_LONG_CONSTANT("PGSQL_DML_STRING", PGSQL_DML_STRING, CONST_CS | CONST_PERSISTENT);
    1197       21159 :         return SUCCESS;
    1198             : }
    1199             : /* }}} */
    1200             : 
    1201             : /* {{{ PHP_MSHUTDOWN_FUNCTION
    1202             :  */
    1203       21192 : PHP_MSHUTDOWN_FUNCTION(pgsql)
    1204             : {
    1205       21192 :         UNREGISTER_INI_ENTRIES();
    1206       21192 :         zend_hash_destroy(&PGG(notices));
    1207             : 
    1208       21192 :         return SUCCESS;
    1209             : }
    1210             : /* }}} */
    1211             : 
    1212             : /* {{{ PHP_RINIT_FUNCTION
    1213             :  */
    1214       21116 : PHP_RINIT_FUNCTION(pgsql)
    1215             : {
    1216       21116 :         PGG(default_link)=-1;
    1217       21116 :         PGG(num_links) = PGG(num_persistent);
    1218       21116 :         return SUCCESS;
    1219             : }
    1220             : /* }}} */
    1221             : 
    1222             : /* {{{ PHP_RSHUTDOWN_FUNCTION
    1223             :  */
    1224       21151 : PHP_RSHUTDOWN_FUNCTION(pgsql)
    1225             : {
    1226             :         /* clean up notice messages */
    1227       21151 :         zend_hash_clean(&PGG(notices));
    1228             :         /* clean up persistent connection */
    1229       21151 :         zend_hash_apply(&EG(persistent_list), (apply_func_t) _rollback_transactions TSRMLS_CC);
    1230       21151 :         return SUCCESS;
    1231             : }
    1232             : /* }}} */
    1233             : 
    1234             : /* {{{ PHP_MINFO_FUNCTION
    1235             :  */
    1236         150 : PHP_MINFO_FUNCTION(pgsql)
    1237             : {
    1238             :         char buf[256];
    1239             : 
    1240         150 :         php_info_print_table_start();
    1241         150 :         php_info_print_table_header(2, "PostgreSQL Support", "enabled");
    1242             : #if HAVE_PG_CONFIG_H
    1243         150 :         php_info_print_table_row(2, "PostgreSQL(libpq) Version", PG_VERSION);
    1244         150 :         php_info_print_table_row(2, "PostgreSQL(libpq) ", PG_VERSION_STR);
    1245             : #ifdef HAVE_PGSQL_WITH_MULTIBYTE_SUPPORT
    1246         150 :         php_info_print_table_row(2, "Multibyte character support", "enabled");
    1247             : #else
    1248             :         php_info_print_table_row(2, "Multibyte character support", "disabled");
    1249             : #endif
    1250             : #ifdef USE_SSL
    1251         150 :         php_info_print_table_row(2, "SSL support", "enabled");
    1252             : #else
    1253             :         php_info_print_table_row(2, "SSL support", "disabled");
    1254             : #endif
    1255             : #endif /* HAVE_PG_CONFIG_H */
    1256         150 :         snprintf(buf, sizeof(buf), "%ld", PGG(num_persistent));
    1257         150 :         php_info_print_table_row(2, "Active Persistent Links", buf);
    1258         150 :         snprintf(buf, sizeof(buf), "%ld", PGG(num_links));
    1259         150 :         php_info_print_table_row(2, "Active Links", buf);
    1260         150 :         php_info_print_table_end();
    1261             : 
    1262         150 :         DISPLAY_INI_ENTRIES();
    1263         150 : }
    1264             : /* }}} */
    1265             : 
    1266             : 
    1267             : /* {{{ php_pgsql_do_connect
    1268             :  */
    1269         113 : static void php_pgsql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
    1270             : {
    1271         113 :         char *host=NULL,*port=NULL,*options=NULL,*tty=NULL,*dbname=NULL,*connstring=NULL;
    1272             :         PGconn *pgsql;
    1273         113 :         smart_str str = {0};
    1274             :         zval **args[5];
    1275         113 :         int i, connect_type = 0;
    1276             :         PGresult *pg_result;
    1277             : 
    1278         226 :         if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 5
    1279         113 :                         || zend_get_parameters_array_ex(ZEND_NUM_ARGS(), args) == FAILURE) {
    1280           0 :                 WRONG_PARAM_COUNT;
    1281             :         }
    1282             : 
    1283         113 :         smart_str_appends(&str, "pgsql");
    1284             :         
    1285         227 :         for (i = 0; i < ZEND_NUM_ARGS(); i++) {
    1286             :                 /* make sure that the PGSQL_CONNECT_FORCE_NEW bit is not part of the hash so that subsequent connections
    1287             :                  * can re-use this connection. Bug #39979
    1288             :                  */ 
    1289         114 :                 if (i == 1 && ZEND_NUM_ARGS() == 2 && Z_TYPE_PP(args[i]) == IS_LONG) {
    1290           1 :                         if (Z_LVAL_PP(args[1]) == PGSQL_CONNECT_FORCE_NEW) {
    1291           0 :                                 continue;
    1292           1 :                         } else if (Z_LVAL_PP(args[1]) & PGSQL_CONNECT_FORCE_NEW) {
    1293           0 :                                 smart_str_append_long(&str, Z_LVAL_PP(args[1]) ^ PGSQL_CONNECT_FORCE_NEW);
    1294             :                         }
    1295             :                 }
    1296         117 :                 convert_to_string_ex(args[i]);
    1297         114 :                 smart_str_appendc(&str, '_');
    1298         114 :                 smart_str_appendl(&str, Z_STRVAL_PP(args[i]), Z_STRLEN_PP(args[i]));
    1299             :         }
    1300             : 
    1301         113 :         smart_str_0(&str);
    1302             : 
    1303         113 :         if (ZEND_NUM_ARGS() == 1) { /* new style, using connection string */
    1304         112 :                 connstring = Z_STRVAL_PP(args[0]);
    1305           1 :         } else if (ZEND_NUM_ARGS() == 2 ) { /* Safe to add conntype_option, since 2 args was illegal */
    1306           1 :                 connstring = Z_STRVAL_PP(args[0]);
    1307           4 :                 convert_to_long_ex(args[1]);
    1308           1 :                 connect_type = Z_LVAL_PP(args[1]);
    1309             :         } else {
    1310           0 :                 host = Z_STRVAL_PP(args[0]);
    1311           0 :                 port = Z_STRVAL_PP(args[1]);
    1312           0 :                 dbname = Z_STRVAL_PP(args[ZEND_NUM_ARGS()-1]);
    1313             : 
    1314           0 :                 switch (ZEND_NUM_ARGS()) {
    1315             :                 case 5:
    1316           0 :                         tty = Z_STRVAL_PP(args[3]);
    1317             :                         /* fall through */
    1318             :                 case 4:
    1319           0 :                         options = Z_STRVAL_PP(args[2]);
    1320             :                         break;
    1321             :                 }
    1322             :         }
    1323             : 
    1324         114 :         if (persistent && PGG(allow_persistent)) {
    1325             :                 zend_rsrc_list_entry *le;
    1326             :                 
    1327             :                 /* try to find if we already have this link in our persistent list */
    1328           1 :                 if (zend_hash_find(&EG(persistent_list), str.c, str.len+1, (void **) &le)==FAILURE) {  /* we don't */
    1329             :                         zend_rsrc_list_entry new_le;
    1330             :                         
    1331           1 :                         if (PGG(max_links)!=-1 && PGG(num_links)>=PGG(max_links)) {
    1332           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING,
    1333             :                                                                  "Cannot create new link. Too many open links (%ld)", PGG(num_links));
    1334           0 :                                 goto err;
    1335             :                         }
    1336           1 :                         if (PGG(max_persistent)!=-1 && PGG(num_persistent)>=PGG(max_persistent)) {
    1337           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING,
    1338             :                                                                  "Cannot create new link. Too many open persistent links (%ld)", PGG(num_persistent));
    1339           0 :                                 goto err;
    1340             :                         }
    1341             : 
    1342             :                         /* create the link */
    1343           1 :                         if (connstring) {
    1344           1 :                                 pgsql=PQconnectdb(connstring);
    1345             :                         } else {
    1346           0 :                                 pgsql=PQsetdb(host,port,options,tty,dbname);
    1347             :                         }
    1348           1 :                         if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
    1349           0 :                                 PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql)
    1350           0 :                                 if (pgsql) {
    1351           0 :                                         PQfinish(pgsql);
    1352             :                                 }
    1353           0 :                                 goto err;
    1354             :                         }
    1355             : 
    1356             :                         /* hash it up */
    1357           1 :                         Z_TYPE(new_le) = le_plink;
    1358           1 :                         new_le.ptr = pgsql;
    1359           1 :                         if (zend_hash_update(&EG(persistent_list), str.c, str.len+1, (void *) &new_le, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) {
    1360           0 :                                 goto err;
    1361             :                         }
    1362           1 :                         PGG(num_links)++;
    1363           1 :                         PGG(num_persistent)++;
    1364             :                 } else {  /* we do */
    1365           0 :                         if (Z_TYPE_P(le) != le_plink) {
    1366           0 :                                 RETURN_FALSE;
    1367             :                         }
    1368             :                         /* ensure that the link did not die */
    1369           0 :                         if (PGG(auto_reset_persistent) & 1) {
    1370             :                                 /* need to send & get something from backend to
    1371             :                                    make sure we catch CONNECTION_BAD everytime */
    1372             :                                 PGresult *pg_result;
    1373           0 :                                 pg_result = PQexec(le->ptr, "select 1");
    1374           0 :                                 PQclear(pg_result);
    1375             :                         }
    1376           0 :                         if (PQstatus(le->ptr)==CONNECTION_BAD) { /* the link died */
    1377           0 :                                 if (le->ptr == NULL) {
    1378           0 :                                         if (connstring) {
    1379           0 :                                                 le->ptr=PQconnectdb(connstring);
    1380             :                                         } else {
    1381           0 :                                                 le->ptr=PQsetdb(host,port,options,tty,dbname);
    1382             :                                         }
    1383             :                                 }
    1384             :                                 else {
    1385           0 :                                         PQreset(le->ptr);
    1386             :                                 }
    1387           0 :                                 if (le->ptr==NULL || PQstatus(le->ptr)==CONNECTION_BAD) {
    1388           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING,"PostgreSQL link lost, unable to reconnect");
    1389           0 :                                         zend_hash_del(&EG(persistent_list),str.c,str.len+1);
    1390           0 :                                         goto err;
    1391             :                                 }
    1392             :                         }
    1393           0 :                         pgsql = (PGconn *) le->ptr;
    1394             : #if HAVE_PQPROTOCOLVERSION && HAVE_PQPARAMETERSTATUS
    1395           0 :                         if (PQprotocolVersion(pgsql) >= 3 && atof(PQparameterStatus(pgsql, "server_version")) >= 7.2) {
    1396             : #else
    1397             :                         if (atof(PG_VERSION) >= 7.2) {
    1398             : #endif
    1399           0 :                                 pg_result = PQexec(pgsql, "RESET ALL;");
    1400           0 :                                 PQclear(pg_result);
    1401             :                         }
    1402             :                 }
    1403           1 :                 ZEND_REGISTER_RESOURCE(return_value, pgsql, le_plink);
    1404             :         } else { /* Non persistent connection */
    1405             :                 zend_rsrc_list_entry *index_ptr,new_index_ptr;
    1406             : 
    1407             :                 /* first we check the hash for the hashed_details key.  if it exists,
    1408             :                  * it should point us to the right offset where the actual pgsql link sits.
    1409             :                  * if it doesn't, open a new pgsql link, add it to the resource list,
    1410             :                  * and add a pointer to it with hashed_details as the key.
    1411             :                  */
    1412         224 :                 if (!(connect_type & PGSQL_CONNECT_FORCE_NEW)
    1413         112 :                         && zend_hash_find(&EG(regular_list),str.c,str.len+1,(void **) &index_ptr)==SUCCESS) {
    1414             :                         int type;
    1415             :                         ulong link;
    1416             :                         void *ptr;
    1417             : 
    1418           1 :                         if (Z_TYPE_P(index_ptr) != le_index_ptr) {
    1419           0 :                                 RETURN_FALSE;
    1420             :                         }
    1421           1 :                         link = (ulong) index_ptr->ptr;
    1422           1 :                         ptr = zend_list_find(link,&type);   /* check if the link is still there */
    1423           1 :                         if (ptr && (type==le_link || type==le_plink)) {
    1424           0 :                                 Z_LVAL_P(return_value) = link;
    1425           0 :                                 zend_list_addref(link);
    1426           0 :                                 php_pgsql_set_default_link(link TSRMLS_CC);
    1427           0 :                                 Z_TYPE_P(return_value) = IS_RESOURCE;
    1428           0 :                                 goto cleanup;
    1429             :                         } else {
    1430           1 :                                 zend_hash_del(&EG(regular_list),str.c,str.len+1);
    1431             :                         }
    1432             :                 }
    1433         112 :                 if (PGG(max_links)!=-1 && PGG(num_links)>=PGG(max_links)) {
    1434           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot create new link. Too many open links (%ld)", PGG(num_links));
    1435           0 :                         goto err;
    1436             :                 }
    1437             : 
    1438             :                 /* Non-blocking connect */
    1439         112 :                 if (connect_type & PGSQL_CONNECT_ASYNC) {
    1440           1 :                         if (connstring) {
    1441           1 :                                 pgsql = PQconnectStart(connstring);
    1442           1 :                                 if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
    1443           0 :                                         PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql);
    1444           0 :                                         if (pgsql) {
    1445           0 :                                                 PQfinish(pgsql);
    1446             :                                         }
    1447           0 :                                         goto err;
    1448             :                                 }
    1449             :                         } else {
    1450           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Connection string required for async connections");
    1451           0 :                                 goto err;
    1452             :                         }
    1453             :                 } else {
    1454         111 :                         if (connstring) {
    1455         111 :                                 pgsql = PQconnectdb(connstring);
    1456             :                         } else {
    1457           0 :                                 pgsql = PQsetdb(host,port,options,tty,dbname);
    1458             :                         }
    1459         111 :                         if (pgsql==NULL || PQstatus(pgsql)==CONNECTION_BAD) {
    1460           0 :                                 PHP_PQ_ERROR("Unable to connect to PostgreSQL server: %s", pgsql);
    1461           0 :                                 if (pgsql) {
    1462           0 :                                         PQfinish(pgsql);
    1463             :                                 }
    1464           0 :                                 goto err;
    1465             :                         }
    1466             :                 }
    1467             : 
    1468             :                 /* add it to the list */
    1469         112 :                 ZEND_REGISTER_RESOURCE(return_value, pgsql, le_link);
    1470             : 
    1471             :                 /* add it to the hash */
    1472         112 :                 new_index_ptr.ptr = (void *) Z_LVAL_P(return_value);
    1473         112 :                 Z_TYPE(new_index_ptr) = le_index_ptr;
    1474         112 :                 if (zend_hash_update(&EG(regular_list),str.c,str.len+1,(void *) &new_index_ptr, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) {
    1475           0 :                         goto err;
    1476             :                 }
    1477         112 :                 PGG(num_links)++;
    1478             :         }
    1479             :         /* set notice processer */
    1480         113 :         if (! PGG(ignore_notices) && Z_TYPE_P(return_value) == IS_RESOURCE) {
    1481         113 :                 PQsetNoticeProcessor(pgsql, _php_pgsql_notice_handler, (void*)Z_RESVAL_P(return_value));
    1482             :         }
    1483         113 :         php_pgsql_set_default_link(Z_LVAL_P(return_value) TSRMLS_CC);
    1484             :         
    1485             : cleanup:
    1486         113 :         smart_str_free(&str);
    1487         113 :         return;
    1488             : 
    1489             : err:
    1490           0 :         smart_str_free(&str);
    1491           0 :         RETURN_FALSE;
    1492             : }
    1493             : /* }}} */
    1494             : 
    1495             : #if 0
    1496             : /* {{{ php_pgsql_get_default_link
    1497             :  */
    1498             : static int php_pgsql_get_default_link(INTERNAL_FUNCTION_PARAMETERS)
    1499             : {
    1500             :         if (PGG(default_link)==-1) { /* no link opened yet, implicitly open one */
    1501             :                 ht = 0;
    1502             :                 php_pgsql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
    1503             :         }
    1504             :         return PGG(default_link);
    1505             : }
    1506             : /* }}} */
    1507             : #endif
    1508             : 
    1509             : /* {{{ proto resource pg_connect(string connection_string[, int connect_type] | [string host, string port [, string options [, string tty,]]] string database)
    1510             :    Open a PostgreSQL connection */
    1511         112 : PHP_FUNCTION(pg_connect)
    1512             : {
    1513         112 :         php_pgsql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,0);
    1514         112 : }
    1515             : /* }}} */
    1516             : 
    1517             : /* {{{ proto resource pg_connect_poll(resource connection)
    1518             :    Poll the status of an in-progress async PostgreSQL connection attempt*/
    1519           1 : PHP_FUNCTION(pg_connect_poll)
    1520             : {
    1521             :         zval *pgsql_link;
    1522           1 :         int id = -1;
    1523             :         PGconn *pgsql;
    1524             :         int ret;
    1525             : 
    1526           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_link) == FAILURE) {
    1527           0 :                 return;
    1528             :         }
    1529             : 
    1530           1 :         if (pgsql_link == NULL && id == -1) {
    1531           0 :                 RETURN_FALSE;
    1532             :         }
    1533             : 
    1534           1 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    1535             : 
    1536           1 :         ret = PQconnectPoll(pgsql);
    1537             : 
    1538           1 :         RETURN_LONG(ret);
    1539             : }
    1540             : /* }}} */
    1541             : 
    1542             : /* {{{ proto resource pg_pconnect(string connection_string | [string host, string port [, string options [, string tty,]]] string database)
    1543             :    Open a persistent PostgreSQL connection */
    1544           1 : PHP_FUNCTION(pg_pconnect)
    1545             : {
    1546           1 :         php_pgsql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU,1);
    1547           1 : }
    1548             : /* }}} */
    1549             : 
    1550             : /* {{{ proto bool pg_close([resource connection])
    1551             :    Close a PostgreSQL connection */ 
    1552          23 : PHP_FUNCTION(pg_close)
    1553             : {
    1554          23 :         zval *pgsql_link = NULL;
    1555          23 :         int id = -1, argc = ZEND_NUM_ARGS();
    1556             :         PGconn *pgsql;
    1557             : 
    1558          23 :         if (zend_parse_parameters(argc TSRMLS_CC, "|r", &pgsql_link) == FAILURE) {
    1559           0 :                 return;
    1560             :         }
    1561             : 
    1562          23 :         if (argc == 0) {
    1563           0 :                 id = PGG(default_link);
    1564           0 :                 CHECK_DEFAULT_LINK(id);
    1565             :         }
    1566             : 
    1567          23 :         if (pgsql_link == NULL && id == -1) {
    1568           0 :                 RETURN_FALSE;
    1569             :         }
    1570             : 
    1571          23 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    1572             : 
    1573          23 :         if (id==-1) { /* explicit resource number */
    1574          23 :                 zend_list_delete(Z_RESVAL_P(pgsql_link));
    1575             :         }
    1576             : 
    1577          69 :         if (id!=-1
    1578          46 :                 || (pgsql_link && Z_RESVAL_P(pgsql_link)==PGG(default_link))) {
    1579          23 :                 zend_list_delete(PGG(default_link));
    1580          23 :                 PGG(default_link) = -1;
    1581             :         }
    1582             : 
    1583          23 :         RETURN_TRUE;
    1584             : }
    1585             : /* }}} */
    1586             : 
    1587             : 
    1588             : #define PHP_PG_DBNAME 1
    1589             : #define PHP_PG_ERROR_MESSAGE 2
    1590             : #define PHP_PG_OPTIONS 3
    1591             : #define PHP_PG_PORT 4
    1592             : #define PHP_PG_TTY 5
    1593             : #define PHP_PG_HOST 6
    1594             : #define PHP_PG_VERSION 7
    1595             : 
    1596             : 
    1597             : /* {{{ php_pgsql_get_link_info
    1598             :  */
    1599          19 : static void php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
    1600             : {
    1601          19 :         zval *pgsql_link = NULL;
    1602          19 :         int id = -1, argc = ZEND_NUM_ARGS();
    1603             :         PGconn *pgsql;
    1604             :         char *msgbuf;
    1605             : 
    1606          19 :         if (zend_parse_parameters(argc TSRMLS_CC, "|r", &pgsql_link) == FAILURE) {
    1607           0 :                 return;
    1608             :         }
    1609             :         
    1610          19 :         if (argc == 0) {
    1611           4 :                 id = PGG(default_link);
    1612           4 :                 CHECK_DEFAULT_LINK(id);
    1613             :         }
    1614             :         
    1615          19 :         if (pgsql_link == NULL && id == -1) {
    1616           0 :                 RETURN_FALSE;
    1617             :         }
    1618             : 
    1619          19 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    1620             : 
    1621          19 :         switch(entry_type) {
    1622             :                 case PHP_PG_DBNAME:
    1623           1 :                         Z_STRVAL_P(return_value) = PQdb(pgsql);
    1624           1 :                         break;
    1625             :                 case PHP_PG_ERROR_MESSAGE:
    1626           3 :                         RETURN_STRING(PQErrorMessageTrim(pgsql, &msgbuf), 0);
    1627             :                         return;
    1628             :                 case PHP_PG_OPTIONS:
    1629           1 :                         Z_STRVAL_P(return_value) = PQoptions(pgsql);
    1630           1 :                         break;
    1631             :                 case PHP_PG_PORT:
    1632           1 :                         Z_STRVAL_P(return_value) = PQport(pgsql);
    1633           1 :                         break;
    1634             :                 case PHP_PG_TTY:
    1635           1 :                         Z_STRVAL_P(return_value) = PQtty(pgsql);
    1636           1 :                         break;
    1637             :                 case PHP_PG_HOST:
    1638           1 :                         Z_STRVAL_P(return_value) = PQhost(pgsql);
    1639           1 :                         break;
    1640             :                 case PHP_PG_VERSION:
    1641          11 :                         array_init(return_value);
    1642          11 :                         add_assoc_string(return_value, "client", PG_VERSION, 1);
    1643             : #if HAVE_PQPROTOCOLVERSION
    1644          11 :                         add_assoc_long(return_value, "protocol", PQprotocolVersion(pgsql));
    1645             : #if HAVE_PQPARAMETERSTATUS
    1646          11 :                         if (PQprotocolVersion(pgsql) >= 3) {
    1647             :                                 /* 8.0 or grater supports protorol version 3 */
    1648             :                                 char *tmp;
    1649          11 :                                 add_assoc_string(return_value, "server", (char*)PQparameterStatus(pgsql, "server_version"), 1);
    1650          11 :                                 tmp = (char*)PQparameterStatus(pgsql, "server_encoding");
    1651          11 :                                 add_assoc_string(return_value, "server_encoding", tmp, 1);
    1652          11 :                                 tmp = (char*)PQparameterStatus(pgsql, "client_encoding");
    1653          11 :                                 add_assoc_string(return_value, "client_encoding", tmp, 1);
    1654          11 :                                 tmp = (char*)PQparameterStatus(pgsql, "is_superuser");
    1655          11 :                                 add_assoc_string(return_value, "is_superuser", tmp, 1);
    1656          11 :                                 tmp = (char*)PQparameterStatus(pgsql, "session_authorization");
    1657          11 :                                 add_assoc_string(return_value, "session_authorization", tmp, 1);
    1658          11 :                                 tmp = (char*)PQparameterStatus(pgsql, "DateStyle");
    1659          11 :                                 add_assoc_string(return_value, "DateStyle", tmp, 1);
    1660          11 :                                 tmp = (char*)PQparameterStatus(pgsql, "IntervalStyle");
    1661          11 :                                 add_assoc_string(return_value, "IntervalStyle", tmp ? tmp : "", 1);
    1662          11 :                                 tmp = (char*)PQparameterStatus(pgsql, "TimeZone");
    1663          11 :                                 add_assoc_string(return_value, "TimeZone", tmp ? tmp : "", 1);
    1664          11 :                                 tmp = (char*)PQparameterStatus(pgsql, "integer_datetimes");
    1665          11 :                                 add_assoc_string(return_value, "integer_datetimes", tmp ? tmp : "", 1);
    1666          11 :                                 tmp = (char*)PQparameterStatus(pgsql, "standard_conforming_strings");
    1667          11 :                                 add_assoc_string(return_value, "standard_conforming_strings", tmp ? tmp : "", 1);
    1668          11 :                                 tmp = (char*)PQparameterStatus(pgsql, "application_name");
    1669          11 :                                 add_assoc_string(return_value, "application_name", tmp ? tmp : "", 1);
    1670             :                         }
    1671             : #endif
    1672             : #endif
    1673          11 :                         return;
    1674             :                 default:
    1675           0 :                         RETURN_FALSE;
    1676             :         }
    1677           5 :         if (Z_STRVAL_P(return_value)) {
    1678           5 :                 Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
    1679           5 :                 Z_STRVAL_P(return_value) = (char *) estrdup(Z_STRVAL_P(return_value));
    1680             :         } else {
    1681           0 :                 Z_STRLEN_P(return_value) = 0;
    1682           0 :                 Z_STRVAL_P(return_value) = (char *) estrdup("");
    1683             :         }
    1684           5 :         Z_TYPE_P(return_value) = IS_STRING;
    1685             : }
    1686             : /* }}} */
    1687             : 
    1688             : /* {{{ proto string pg_dbname([resource connection])
    1689             :    Get the database name */ 
    1690           1 : PHP_FUNCTION(pg_dbname)
    1691             : {
    1692           1 :         php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_DBNAME);
    1693           1 : }
    1694             : /* }}} */
    1695             : 
    1696             : /* {{{ proto string pg_last_error([resource connection])
    1697             :    Get the error message string */
    1698           3 : PHP_FUNCTION(pg_last_error)
    1699             : {
    1700           3 :         php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_ERROR_MESSAGE);
    1701           3 : }
    1702             : /* }}} */
    1703             : 
    1704             : /* {{{ proto string pg_options([resource connection])
    1705             :    Get the options associated with the connection */
    1706           1 : PHP_FUNCTION(pg_options)
    1707             : {
    1708           1 :         php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_OPTIONS);
    1709           1 : }
    1710             : /* }}} */
    1711             : 
    1712             : /* {{{ proto int pg_port([resource connection])
    1713             :    Return the port number associated with the connection */
    1714           1 : PHP_FUNCTION(pg_port)
    1715             : {
    1716           1 :         php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_PORT);
    1717           1 : }
    1718             : /* }}} */
    1719             : 
    1720             : /* {{{ proto string pg_tty([resource connection])
    1721             :    Return the tty name associated with the connection */
    1722           1 : PHP_FUNCTION(pg_tty)
    1723             : {
    1724           1 :         php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_TTY);
    1725           1 : }
    1726             : /* }}} */
    1727             : 
    1728             : /* {{{ proto string pg_host([resource connection])
    1729             :    Returns the host name associated with the connection */
    1730           1 : PHP_FUNCTION(pg_host)
    1731             : {
    1732           1 :         php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_HOST);
    1733           1 : }
    1734             : /* }}} */
    1735             : 
    1736             : /* {{{ proto array pg_version([resource connection])
    1737             :    Returns an array with client, protocol and server version (when available) */
    1738          11 : PHP_FUNCTION(pg_version)
    1739             : {
    1740          11 :         php_pgsql_get_link_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_VERSION);
    1741          11 : }
    1742             : /* }}} */
    1743             : 
    1744             : #if HAVE_PQPARAMETERSTATUS
    1745             : /* {{{ proto string|false pg_parameter_status([resource connection,] string param_name)
    1746             :    Returns the value of a server parameter */
    1747           4 : PHP_FUNCTION(pg_parameter_status)
    1748             : {
    1749             :         zval *pgsql_link;
    1750             :         int id;
    1751             :         PGconn *pgsql;
    1752             :         char *param;
    1753             :         int len;
    1754             : 
    1755           4 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pgsql_link, &param, &len) == SUCCESS) {
    1756           0 :                 id = -1;
    1757           4 :         } else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &param, &len) == SUCCESS) {
    1758           4 :                 pgsql_link = NULL;
    1759           4 :                 id = PGG(default_link);
    1760             :         } else {
    1761           0 :                 RETURN_FALSE;
    1762             :         }
    1763           4 :         if (pgsql_link == NULL && id == -1) {
    1764           0 :                 RETURN_FALSE;
    1765             :         }
    1766             : 
    1767           4 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    1768             : 
    1769           4 :         param = (char*)PQparameterStatus(pgsql, param);
    1770           4 :         if (param) {
    1771           4 :                 RETURN_STRING(param, 1);
    1772             :         } else {
    1773           0 :                 RETURN_FALSE;
    1774             :         }
    1775             : }
    1776             : /* }}} */
    1777             : #endif
    1778             : 
    1779             : /* {{{ proto bool pg_ping([resource connection])
    1780             :    Ping database. If connection is bad, try to reconnect. */
    1781           1 : PHP_FUNCTION(pg_ping)
    1782             : {
    1783             :         zval *pgsql_link;
    1784             :         int id;
    1785             :         PGconn *pgsql;
    1786             :         PGresult *res;
    1787             : 
    1788           1 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_link) == SUCCESS) {
    1789           1 :                 id = -1;
    1790             :         } else {
    1791           0 :                 pgsql_link = NULL;
    1792           0 :                 id = PGG(default_link);
    1793             :         }
    1794           1 :         if (pgsql_link == NULL && id == -1) {
    1795           0 :                 RETURN_FALSE;
    1796             :         }
    1797             : 
    1798           1 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    1799             : 
    1800             :         /* ping connection */
    1801           1 :         res = PQexec(pgsql, "SELECT 1;");
    1802           1 :         PQclear(res);
    1803             : 
    1804             :         /* check status. */
    1805           1 :         if (PQstatus(pgsql) == CONNECTION_OK)
    1806           1 :                 RETURN_TRUE;
    1807             : 
    1808             :         /* reset connection if it's broken */
    1809           0 :         PQreset(pgsql);
    1810           0 :         if (PQstatus(pgsql) == CONNECTION_OK) {
    1811           0 :                 RETURN_TRUE;
    1812             :         }
    1813           0 :         RETURN_FALSE;
    1814             : }
    1815             : /* }}} */
    1816             : 
    1817             : /* {{{ proto resource pg_query([resource connection,] string query)
    1818             :    Execute a query */
    1819        1137 : PHP_FUNCTION(pg_query)
    1820             : {
    1821        1137 :         zval *pgsql_link = NULL;
    1822             :         char *query;
    1823        1137 :         int id = -1, query_len, argc = ZEND_NUM_ARGS();
    1824        1137 :         int leftover = 0;
    1825             :         PGconn *pgsql;
    1826             :         PGresult *pgsql_result;
    1827             :         ExecStatusType status;
    1828             :         pgsql_result_handle *pg_result;
    1829             : 
    1830        1137 :         if (argc == 1) {
    1831          83 :                 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &query, &query_len) == FAILURE) {
    1832           0 :                         return;
    1833             :                 }
    1834          83 :                 id = PGG(default_link);
    1835          83 :                 CHECK_DEFAULT_LINK(id);
    1836             :         } else {
    1837        1054 :                 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pgsql_link, &query, &query_len) == FAILURE) {
    1838           0 :                         return;
    1839             :                 }
    1840             :         }
    1841             : 
    1842        1137 :         if (pgsql_link == NULL && id == -1) {
    1843           0 :                 RETURN_FALSE;
    1844             :         }
    1845             : 
    1846        1137 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    1847             : 
    1848        1137 :         if (PQ_SETNONBLOCKING(pgsql, 0)) {
    1849           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE,"Cannot set connection to blocking mode");
    1850           0 :                 RETURN_FALSE;
    1851             :         }
    1852        2274 :         while ((pgsql_result = PQgetResult(pgsql))) {
    1853           0 :                 PQclear(pgsql_result);
    1854           0 :                 leftover = 1;
    1855             :         }
    1856        1137 :         if (leftover) {
    1857           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
    1858             :         }
    1859        1137 :         pgsql_result = PQexec(pgsql, query);
    1860        1137 :         if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
    1861           0 :                 PQclear(pgsql_result);
    1862           0 :                 PQreset(pgsql);
    1863           0 :                 pgsql_result = PQexec(pgsql, query);
    1864             :         }
    1865             : 
    1866        1137 :         if (pgsql_result) {
    1867        1137 :                 status = PQresultStatus(pgsql_result);
    1868             :         } else {
    1869           0 :                 status = (ExecStatusType) PQstatus(pgsql);
    1870             :         }
    1871             : 
    1872        1137 :         switch (status) {
    1873             :                 case PGRES_EMPTY_QUERY:
    1874             :                 case PGRES_BAD_RESPONSE:
    1875             :                 case PGRES_NONFATAL_ERROR:
    1876             :                 case PGRES_FATAL_ERROR:
    1877          11 :                         PHP_PQ_ERROR("Query failed: %s", pgsql);
    1878          11 :                         PQclear(pgsql_result);
    1879          11 :                         RETURN_FALSE;
    1880             :                         break;
    1881             :                 case PGRES_COMMAND_OK: /* successful command that did not return rows */
    1882             :                 default:
    1883        1126 :                         if (pgsql_result) {
    1884        1126 :                                 pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
    1885        1126 :                                 pg_result->conn = pgsql;
    1886        1126 :                                 pg_result->result = pgsql_result;
    1887        1126 :                                 pg_result->row = 0;
    1888        1126 :                                 ZEND_REGISTER_RESOURCE(return_value, pg_result, le_result);
    1889             :                         } else {
    1890           0 :                                 PQclear(pgsql_result);
    1891           0 :                                 RETURN_FALSE;
    1892             :                         }
    1893             :                         break;
    1894             :         }
    1895             : }
    1896             : /* }}} */
    1897             : 
    1898             : #if HAVE_PQEXECPARAMS || HAVE_PQEXECPREPARED || HAVE_PQSENDQUERYPARAMS || HAVE_PQSENDQUERYPREPARED
    1899             : /* {{{ _php_pgsql_free_params */
    1900          25 : static void _php_pgsql_free_params(char **params, int num_params)
    1901             : {
    1902          25 :         if (num_params > 0) {
    1903             :                 int i;
    1904          56 :                 for (i = 0; i < num_params; i++) {
    1905          31 :                         if (params[i]) {
    1906          31 :                                 efree(params[i]);
    1907             :                         }
    1908             :                 }
    1909          25 :                 efree(params);
    1910             :         }
    1911          25 : }
    1912             : /* }}} */
    1913             : #endif
    1914             : 
    1915             : #if HAVE_PQEXECPARAMS
    1916             : /* {{{ proto resource pg_query_params([resource connection,] string query, array params)
    1917             :    Execute a query */
    1918          13 : PHP_FUNCTION(pg_query_params)
    1919             : {
    1920          13 :         zval *pgsql_link = NULL;
    1921             :         zval *pv_param_arr, **tmp;
    1922             :         char *query;
    1923          13 :         int query_len, id = -1, argc = ZEND_NUM_ARGS();
    1924          13 :         int leftover = 0;
    1925          13 :         int num_params = 0;
    1926          13 :         char **params = NULL;
    1927             :         PGconn *pgsql;
    1928             :         PGresult *pgsql_result;
    1929             :         ExecStatusType status;
    1930             :         pgsql_result_handle *pg_result;
    1931             :         
    1932          13 :         if (argc == 2) {
    1933           1 :                 if (zend_parse_parameters(argc TSRMLS_CC, "sa", &query, &query_len, &pv_param_arr) == FAILURE) {
    1934           0 :                         return;
    1935             :                 }
    1936           1 :                 id = PGG(default_link);
    1937           1 :                 CHECK_DEFAULT_LINK(id);
    1938             :         } else {
    1939          12 :                 if (zend_parse_parameters(argc TSRMLS_CC, "rsa", &pgsql_link, &query, &query_len, &pv_param_arr) == FAILURE) {
    1940           0 :                         return;
    1941             :                 }
    1942             :         }
    1943             : 
    1944          13 :         if (pgsql_link == NULL && id == -1) {
    1945           0 :                 RETURN_FALSE;
    1946             :         }
    1947             : 
    1948          13 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    1949             : 
    1950          13 :         if (PQ_SETNONBLOCKING(pgsql, 0)) {
    1951           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE,"Cannot set connection to blocking mode");
    1952           0 :                 RETURN_FALSE;
    1953             :         }
    1954          26 :         while ((pgsql_result = PQgetResult(pgsql))) {
    1955           0 :                 PQclear(pgsql_result);
    1956           0 :                 leftover = 1;
    1957             :         }
    1958          13 :         if (leftover) {
    1959           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
    1960             :         }
    1961             : 
    1962          13 :         zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr));
    1963          13 :         num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr));
    1964          13 :         if (num_params > 0) {
    1965          13 :                 int i = 0;
    1966          13 :                 params = (char **)safe_emalloc(sizeof(char *), num_params, 0);
    1967             : 
    1968          27 :                 for(i = 0; i < num_params; i++) {
    1969          14 :                         if (zend_hash_get_current_data(Z_ARRVAL_P(pv_param_arr), (void **) &tmp) == FAILURE) {
    1970           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error getting parameter");
    1971           0 :                                 _php_pgsql_free_params(params, num_params);
    1972           0 :                                 RETURN_FALSE;
    1973             :                         }
    1974             : 
    1975          14 :                         if (Z_TYPE_PP(tmp) == IS_NULL) {
    1976           0 :                                 params[i] = NULL;
    1977             :                         } else {
    1978          14 :                                 zval tmp_val = **tmp;
    1979             :                                 zval_copy_ctor(&tmp_val);
    1980          14 :                                 convert_to_cstring(&tmp_val);
    1981          14 :                                 if (Z_TYPE(tmp_val) != IS_STRING) {
    1982           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter");
    1983             :                                         zval_dtor(&tmp_val);
    1984           0 :                                         _php_pgsql_free_params(params, num_params);
    1985           0 :                                         RETURN_FALSE;
    1986             :                                 }
    1987          14 :                                 params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
    1988             :                                 zval_dtor(&tmp_val);
    1989             :                         }
    1990             : 
    1991          14 :                         zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr));
    1992             :                 }
    1993             :         }
    1994             : 
    1995          13 :         pgsql_result = PQexecParams(pgsql, query, num_params, 
    1996             :                                         NULL, (const char * const *)params, NULL, NULL, 0);
    1997          13 :         if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
    1998           0 :                 PQclear(pgsql_result);
    1999           0 :                 PQreset(pgsql);
    2000           0 :                 pgsql_result = PQexecParams(pgsql, query, num_params, 
    2001             :                                                 NULL, (const char * const *)params, NULL, NULL, 0);
    2002             :         }
    2003             : 
    2004          13 :         if (pgsql_result) {
    2005          13 :                 status = PQresultStatus(pgsql_result);
    2006             :         } else {
    2007           0 :                 status = (ExecStatusType) PQstatus(pgsql);
    2008             :         }
    2009             :         
    2010          13 :         _php_pgsql_free_params(params, num_params);
    2011             : 
    2012          13 :         switch (status) {
    2013             :                 case PGRES_EMPTY_QUERY:
    2014             :                 case PGRES_BAD_RESPONSE:
    2015             :                 case PGRES_NONFATAL_ERROR:
    2016             :                 case PGRES_FATAL_ERROR:
    2017           0 :                         PHP_PQ_ERROR("Query failed: %s", pgsql);
    2018           0 :                         PQclear(pgsql_result);
    2019           0 :                         RETURN_FALSE;
    2020             :                         break;
    2021             :                 case PGRES_COMMAND_OK: /* successful command that did not return rows */
    2022             :                 default:
    2023          13 :                         if (pgsql_result) {
    2024          13 :                                 pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
    2025          13 :                                 pg_result->conn = pgsql;
    2026          13 :                                 pg_result->result = pgsql_result;
    2027          13 :                                 pg_result->row = 0;
    2028          13 :                                 ZEND_REGISTER_RESOURCE(return_value, pg_result, le_result);
    2029             :                         } else {
    2030           0 :                                 PQclear(pgsql_result);
    2031           0 :                                 RETURN_FALSE;
    2032             :                         }
    2033             :                         break;
    2034             :         }
    2035             : }
    2036             : /* }}} */
    2037             : #endif
    2038             : 
    2039             : #if HAVE_PQPREPARE
    2040             : /* {{{ proto resource pg_prepare([resource connection,] string stmtname, string query)
    2041             :    Prepare a query for future execution */
    2042           2 : PHP_FUNCTION(pg_prepare)
    2043             : {
    2044           2 :         zval *pgsql_link = NULL;
    2045             :         char *query, *stmtname;
    2046           2 :         int query_len, stmtname_len, id = -1, argc = ZEND_NUM_ARGS();
    2047           2 :         int leftover = 0;
    2048             :         PGconn *pgsql;
    2049             :         PGresult *pgsql_result;
    2050             :         ExecStatusType status;
    2051             :         pgsql_result_handle *pg_result;
    2052             : 
    2053           2 :         if (argc == 2) {
    2054           0 :                 if (zend_parse_parameters(argc TSRMLS_CC, "ss", &stmtname, &stmtname_len, &query, &query_len) == FAILURE) {
    2055           0 :                         return;
    2056             :                 }
    2057           0 :                 id = PGG(default_link);
    2058           0 :                 CHECK_DEFAULT_LINK(id);
    2059             :         } else {
    2060           2 :                 if (zend_parse_parameters(argc TSRMLS_CC, "rss", &pgsql_link, &stmtname, &stmtname_len, &query, &query_len) == FAILURE) {
    2061           0 :                         return;
    2062             :                 }
    2063             :         }
    2064             : 
    2065           2 :         if (pgsql_link == NULL && id == -1) {
    2066           0 :                 RETURN_FALSE;
    2067             :         }
    2068             : 
    2069           2 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    2070             : 
    2071           2 :         if (PQ_SETNONBLOCKING(pgsql, 0)) {
    2072           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE,"Cannot set connection to blocking mode");
    2073           0 :                 RETURN_FALSE;
    2074             :         }
    2075           4 :         while ((pgsql_result = PQgetResult(pgsql))) {
    2076           0 :                 PQclear(pgsql_result);
    2077           0 :                 leftover = 1;
    2078             :         }
    2079           2 :         if (leftover) {
    2080           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
    2081             :         }
    2082           2 :         pgsql_result = PQprepare(pgsql, stmtname, query, 0, NULL);
    2083           2 :         if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
    2084           0 :                 PQclear(pgsql_result);
    2085           0 :                 PQreset(pgsql);
    2086           0 :                 pgsql_result = PQprepare(pgsql, stmtname, query, 0, NULL);
    2087             :         }
    2088             : 
    2089           2 :         if (pgsql_result) {
    2090           2 :                 status = PQresultStatus(pgsql_result);
    2091             :         } else {
    2092           0 :                 status = (ExecStatusType) PQstatus(pgsql);
    2093             :         }
    2094             : 
    2095           2 :         switch (status) {
    2096             :                 case PGRES_EMPTY_QUERY:
    2097             :                 case PGRES_BAD_RESPONSE:
    2098             :                 case PGRES_NONFATAL_ERROR:
    2099             :                 case PGRES_FATAL_ERROR:
    2100           0 :                         PHP_PQ_ERROR("Query failed: %s", pgsql);
    2101           0 :                         PQclear(pgsql_result);
    2102           0 :                         RETURN_FALSE;
    2103             :                         break;
    2104             :                 case PGRES_COMMAND_OK: /* successful command that did not return rows */
    2105             :                 default:
    2106           2 :                         if (pgsql_result) {
    2107           2 :                                 pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
    2108           2 :                                 pg_result->conn = pgsql;
    2109           2 :                                 pg_result->result = pgsql_result;
    2110           2 :                                 pg_result->row = 0;
    2111           2 :                                 ZEND_REGISTER_RESOURCE(return_value, pg_result, le_result);
    2112             :                         } else {
    2113           0 :                                 PQclear(pgsql_result);
    2114           0 :                                 RETURN_FALSE;
    2115             :                         }
    2116             :                         break;
    2117             :         }
    2118             : }
    2119             : /* }}} */
    2120             : #endif
    2121             : 
    2122             : #if HAVE_PQEXECPREPARED
    2123             : /* {{{ proto resource pg_execute([resource connection,] string stmtname, array params)
    2124             :    Execute a prepared query  */
    2125           4 : PHP_FUNCTION(pg_execute)
    2126             : {
    2127           4 :         zval *pgsql_link = NULL;
    2128             :         zval *pv_param_arr, **tmp;
    2129             :         char *stmtname;
    2130           4 :         int stmtname_len, id = -1, argc = ZEND_NUM_ARGS();
    2131           4 :         int leftover = 0;
    2132           4 :         int num_params = 0;
    2133           4 :         char **params = NULL;
    2134             :         PGconn *pgsql;
    2135             :         PGresult *pgsql_result;
    2136             :         ExecStatusType status;
    2137             :         pgsql_result_handle *pg_result;
    2138             : 
    2139           4 :         if (argc == 2) {
    2140           0 :                 if (zend_parse_parameters(argc TSRMLS_CC, "sa/", &stmtname, &stmtname_len, &pv_param_arr)==FAILURE) {
    2141           0 :                         return;
    2142             :                 }
    2143           0 :                 id = PGG(default_link);
    2144           0 :                 CHECK_DEFAULT_LINK(id);
    2145             :         } else {
    2146           4 :                 if (zend_parse_parameters(argc TSRMLS_CC, "rsa/", &pgsql_link, &stmtname, &stmtname_len, &pv_param_arr) == FAILURE) {
    2147           0 :                         return;
    2148             :                 }
    2149             :         }
    2150             : 
    2151           4 :         if (pgsql_link == NULL && id == -1) {
    2152           0 :                 RETURN_FALSE;
    2153             :         }
    2154             : 
    2155           4 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    2156             : 
    2157           4 :         if (PQ_SETNONBLOCKING(pgsql, 0)) {
    2158           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE,"Cannot set connection to blocking mode");
    2159           0 :                 RETURN_FALSE;
    2160             :         }
    2161           8 :         while ((pgsql_result = PQgetResult(pgsql))) {
    2162           0 :                 PQclear(pgsql_result);
    2163           0 :                 leftover = 1;
    2164             :         }
    2165           4 :         if (leftover) {
    2166           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Found results on this connection. Use pg_get_result() to get these results first");
    2167             :         }
    2168             : 
    2169           4 :         zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr));
    2170           4 :         num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr));
    2171           4 :         if (num_params > 0) {
    2172           4 :                 int i = 0;
    2173           4 :                 params = (char **)safe_emalloc(sizeof(char *), num_params, 0);
    2174             : 
    2175           9 :                 for(i = 0; i < num_params; i++) {
    2176           5 :                         if (zend_hash_get_current_data(Z_ARRVAL_P(pv_param_arr), (void **) &tmp) == FAILURE) {
    2177           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error getting parameter");
    2178           0 :                                 _php_pgsql_free_params(params, num_params);
    2179           0 :                                 RETURN_FALSE;
    2180             :                         }
    2181             : 
    2182           5 :                         if (Z_TYPE_PP(tmp) == IS_NULL) {
    2183           0 :                                 params[i] = NULL;
    2184             :                         } else {
    2185           5 :                                 zval tmp_val = **tmp;
    2186             :                                 zval_copy_ctor(&tmp_val);
    2187           5 :                                 convert_to_string(&tmp_val);
    2188           5 :                                 if (Z_TYPE(tmp_val) != IS_STRING) {
    2189           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter");
    2190             :                                         zval_dtor(&tmp_val);
    2191           0 :                                         _php_pgsql_free_params(params, num_params);
    2192           0 :                                         RETURN_FALSE;
    2193             :                                 }
    2194           5 :                                 params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
    2195             :                                 zval_dtor(&tmp_val);
    2196             :                         }
    2197             : 
    2198           5 :                         zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr));
    2199             :                 }
    2200             :         }
    2201             : 
    2202           4 :         pgsql_result = PQexecPrepared(pgsql, stmtname, num_params, 
    2203             :                                         (const char * const *)params, NULL, NULL, 0);
    2204           4 :         if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
    2205           0 :                 PQclear(pgsql_result);
    2206           0 :                 PQreset(pgsql);
    2207           0 :                 pgsql_result = PQexecPrepared(pgsql, stmtname, num_params, 
    2208             :                                                 (const char * const *)params, NULL, NULL, 0);
    2209             :         }
    2210             : 
    2211           4 :         if (pgsql_result) {
    2212           4 :                 status = PQresultStatus(pgsql_result);
    2213             :         } else {
    2214           0 :                 status = (ExecStatusType) PQstatus(pgsql);
    2215             :         }
    2216             : 
    2217           4 :         _php_pgsql_free_params(params, num_params);
    2218             : 
    2219           4 :         switch (status) {
    2220             :                 case PGRES_EMPTY_QUERY:
    2221             :                 case PGRES_BAD_RESPONSE:
    2222             :                 case PGRES_NONFATAL_ERROR:
    2223             :                 case PGRES_FATAL_ERROR:
    2224           0 :                         PHP_PQ_ERROR("Query failed: %s", pgsql);
    2225           0 :                         PQclear(pgsql_result);
    2226           0 :                         RETURN_FALSE;
    2227             :                         break;
    2228             :                 case PGRES_COMMAND_OK: /* successful command that did not return rows */
    2229             :                 default:
    2230           4 :                         if (pgsql_result) {
    2231           4 :                                 pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
    2232           4 :                                 pg_result->conn = pgsql;
    2233           4 :                                 pg_result->result = pgsql_result;
    2234           4 :                                 pg_result->row = 0;
    2235           4 :                                 ZEND_REGISTER_RESOURCE(return_value, pg_result, le_result);
    2236             :                         } else {
    2237           0 :                                 PQclear(pgsql_result);
    2238           0 :                                 RETURN_FALSE;
    2239             :                         }
    2240             :                         break;
    2241             :         }
    2242             : }
    2243             : /* }}} */
    2244             : #endif
    2245             : 
    2246             : #define PHP_PG_NUM_ROWS 1
    2247             : #define PHP_PG_NUM_FIELDS 2
    2248             : #define PHP_PG_CMD_TUPLES 3
    2249             : 
    2250             : /* {{{ php_pgsql_get_result_info
    2251             :  */
    2252          33 : static void php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
    2253             : {
    2254             :         zval *result;
    2255             :         PGresult *pgsql_result;
    2256             :         pgsql_result_handle *pg_result;
    2257             : 
    2258          33 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) {
    2259           1 :                 return;
    2260             :         }
    2261             :         
    2262          32 :         ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
    2263             : 
    2264          32 :         pgsql_result = pg_result->result;
    2265             : 
    2266          32 :         switch (entry_type) {
    2267             :                 case PHP_PG_NUM_ROWS:
    2268          21 :                         Z_LVAL_P(return_value) = PQntuples(pgsql_result);
    2269          21 :                         break;
    2270             :                 case PHP_PG_NUM_FIELDS:
    2271          10 :                         Z_LVAL_P(return_value) = PQnfields(pgsql_result);
    2272          10 :                         break;
    2273             :                 case PHP_PG_CMD_TUPLES:
    2274             : #if HAVE_PQCMDTUPLES
    2275           1 :                         Z_LVAL_P(return_value) = atoi(PQcmdTuples(pgsql_result));
    2276             : #else
    2277             :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Not supported under this build");
    2278             :                         Z_LVAL_P(return_value) = 0;
    2279             : #endif
    2280           1 :                         break;
    2281             :                 default:
    2282           0 :                         RETURN_FALSE;
    2283             :         }
    2284          32 :         Z_TYPE_P(return_value) = IS_LONG;
    2285             : }
    2286             : /* }}} */
    2287             : 
    2288             : /* {{{ proto int pg_num_rows(resource result)
    2289             :    Return the number of rows in the result */
    2290          22 : PHP_FUNCTION(pg_num_rows)
    2291             : {
    2292          22 :         php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_NUM_ROWS);
    2293          22 : }
    2294             : /* }}} */
    2295             : 
    2296             : /* {{{ proto int pg_num_fields(resource result)
    2297             :    Return the number of fields in the result */
    2298          10 : PHP_FUNCTION(pg_num_fields)
    2299             : {
    2300          10 :         php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_NUM_FIELDS);
    2301          10 : }
    2302             : /* }}} */
    2303             : 
    2304             : #if HAVE_PQCMDTUPLES
    2305             : /* {{{ proto int pg_affected_rows(resource result)
    2306             :    Returns the number of affected tuples */
    2307           1 : PHP_FUNCTION(pg_affected_rows)
    2308             : {
    2309           1 :         php_pgsql_get_result_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_CMD_TUPLES);
    2310           1 : }
    2311             : /* }}} */
    2312             : #endif
    2313             : 
    2314             : /* {{{ proto string pg_last_notice(resource connection)
    2315             :    Returns the last notice set by the backend */
    2316           0 : PHP_FUNCTION(pg_last_notice) 
    2317             : {
    2318             :         zval *pgsql_link;
    2319             :         PGconn *pg_link;
    2320           0 :         int id = -1;
    2321             :         php_pgsql_notice **notice;
    2322             :         
    2323           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_link) == FAILURE) {
    2324           0 :                 return;
    2325             :         }
    2326             :         /* Just to check if user passed valid resoruce */
    2327           0 :         ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    2328             : 
    2329           0 :         if (zend_hash_index_find(&PGG(notices), Z_RESVAL_P(pgsql_link), (void **)&notice) == FAILURE) {
    2330           0 :                 RETURN_FALSE;
    2331             :         }
    2332           0 :         RETURN_STRINGL((*notice)->message, (*notice)->len, 1);
    2333             : }
    2334             : /* }}} */
    2335             : 
    2336             : /* {{{ get_field_name
    2337             :  */
    2338          10 : static char *get_field_name(PGconn *pgsql, Oid oid, HashTable *list TSRMLS_DC)
    2339             : {
    2340             :         PGresult *result;
    2341          10 :         smart_str str = {0};
    2342             :         zend_rsrc_list_entry *field_type;
    2343          10 :         char *ret=NULL;
    2344             : 
    2345             :         /* try to lookup the type in the resource list */
    2346          10 :         smart_str_appends(&str, "pgsql_oid_");
    2347          10 :         smart_str_append_unsigned(&str, oid);
    2348          10 :         smart_str_0(&str);
    2349             : 
    2350          10 :         if (zend_hash_find(list,str.c,str.len+1,(void **) &field_type)==SUCCESS) {
    2351           0 :                 ret = estrdup((char *)field_type->ptr);
    2352             :         } else { /* hash all oid's */
    2353             :                 int i,num_rows;
    2354             :                 int oid_offset,name_offset;
    2355             :                 char *tmp_oid, *end_ptr, *tmp_name;
    2356             :                 zend_rsrc_list_entry new_oid_entry;
    2357             : 
    2358          10 :                 if ((result = PQexec(pgsql,"select oid,typname from pg_type")) == NULL || PQresultStatus(result) != PGRES_TUPLES_OK) {
    2359           0 :                         if (result) {
    2360           0 :                                 PQclear(result);
    2361             :                         }
    2362           0 :                         smart_str_free(&str);
    2363           0 :                         return STR_EMPTY_ALLOC();
    2364             :                 }
    2365          10 :                 num_rows = PQntuples(result);
    2366          10 :                 oid_offset = PQfnumber(result,"oid");
    2367          10 :                 name_offset = PQfnumber(result,"typname");
    2368             : 
    2369        2910 :                 for (i=0; i<num_rows; i++) {
    2370        2900 :                         if ((tmp_oid = PQgetvalue(result,i,oid_offset))==NULL) {
    2371           0 :                                 continue;
    2372             :                         }
    2373             :                         
    2374        2900 :                         str.len = 0;
    2375        2900 :                         smart_str_appends(&str, "pgsql_oid_");
    2376        2900 :                         smart_str_appends(&str, tmp_oid);
    2377        2900 :                         smart_str_0(&str);
    2378             : 
    2379        2900 :                         if ((tmp_name = PQgetvalue(result,i,name_offset))==NULL) {
    2380           0 :                                 continue;
    2381             :                         }
    2382        2900 :                         Z_TYPE(new_oid_entry) = le_string;
    2383        2900 :                         new_oid_entry.ptr = estrdup(tmp_name);
    2384        2900 :                         zend_hash_update(list,str.c,str.len+1,(void *) &new_oid_entry, sizeof(zend_rsrc_list_entry), NULL);
    2385        2900 :                         if (!ret && strtoul(tmp_oid, &end_ptr, 10)==oid) {
    2386          10 :                                 ret = estrdup(tmp_name);
    2387             :                         }
    2388             :                 }
    2389          10 :                 PQclear(result);
    2390             :         }
    2391             : 
    2392          10 :         smart_str_free(&str);
    2393          10 :         return ret;
    2394             : }
    2395             : /* }}} */
    2396             : 
    2397             : #ifdef HAVE_PQFTABLE
    2398             : /* {{{ proto mixed pg_field_table(resource result, int field_number[, bool oid_only])
    2399             :    Returns the name of the table field belongs to, or table's oid if oid_only is true */
    2400           0 : PHP_FUNCTION(pg_field_table)
    2401             : {
    2402             :         zval *result;
    2403             :         pgsql_result_handle *pg_result;
    2404           0 :         long fnum = -1;
    2405           0 :         zend_bool return_oid = 0;
    2406             :         Oid oid;
    2407           0 :         smart_str hash_key = {0};
    2408             :         char *table_name;
    2409             :         zend_rsrc_list_entry *field_table;
    2410             : 
    2411           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|b", &result, &fnum, &return_oid) == FAILURE) {
    2412           0 :                 return;
    2413             :         }
    2414             : 
    2415           0 :         ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
    2416             : 
    2417           0 :         if (fnum < 0 || fnum >= PQnfields(pg_result->result)) {
    2418           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad field offset specified");
    2419           0 :                 RETURN_FALSE;
    2420             :         }
    2421             : 
    2422           0 :         oid = PQftable(pg_result->result, fnum);
    2423             : 
    2424           0 :         if (InvalidOid == oid) {
    2425           0 :                 RETURN_FALSE;
    2426             :         }
    2427             : 
    2428           0 :         if (return_oid) {
    2429             : #if UINT_MAX > LONG_MAX /* Oid is unsigned int, we don't need this code, where LONG is wider */
    2430             :                 if (oid > LONG_MAX) {
    2431             :                         smart_str oidstr = {0};
    2432             :                         smart_str_append_unsigned(&oidstr, oid);
    2433             :                         smart_str_0(&oidstr);
    2434             :                         RETURN_STRINGL(oidstr.c, oidstr.len, 0);
    2435             :                 } else
    2436             : #endif
    2437           0 :                         RETURN_LONG((long)oid);
    2438             :         }
    2439             : 
    2440             :         /* try to lookup the table name in the resource list */
    2441           0 :         smart_str_appends(&hash_key, "pgsql_table_oid_");
    2442           0 :         smart_str_append_unsigned(&hash_key, oid);
    2443           0 :         smart_str_0(&hash_key);
    2444             : 
    2445           0 :         if (zend_hash_find(&EG(regular_list), hash_key.c, hash_key.len+1, (void **) &field_table) == SUCCESS) {
    2446           0 :                 smart_str_free(&hash_key);
    2447           0 :                 RETURN_STRING((char *)field_table->ptr, 1);
    2448             :         } else { /* Not found, lookup by querying PostgreSQL system tables */
    2449             :                 PGresult *tmp_res;
    2450           0 :                 smart_str querystr = {0};
    2451             :                 zend_rsrc_list_entry new_field_table;
    2452             : 
    2453           0 :                 smart_str_appends(&querystr, "select relname from pg_class where oid=");
    2454           0 :                 smart_str_append_unsigned(&querystr, oid);
    2455           0 :                 smart_str_0(&querystr);
    2456             : 
    2457           0 :                 if ((tmp_res = PQexec(pg_result->conn, querystr.c)) == NULL || PQresultStatus(tmp_res) != PGRES_TUPLES_OK) {
    2458           0 :                         if (tmp_res) {
    2459           0 :                                 PQclear(tmp_res);
    2460             :                         }
    2461           0 :                         smart_str_free(&querystr);
    2462           0 :                         smart_str_free(&hash_key);
    2463           0 :                         RETURN_FALSE;
    2464             :                 }
    2465             : 
    2466           0 :                 smart_str_free(&querystr);
    2467             : 
    2468           0 :                 if ((table_name = PQgetvalue(tmp_res, 0, 0)) == NULL) {
    2469           0 :                         PQclear(tmp_res);
    2470           0 :                         smart_str_free(&hash_key);
    2471           0 :                         RETURN_FALSE;
    2472             :                 }
    2473             : 
    2474           0 :                 Z_TYPE(new_field_table) = le_string;
    2475           0 :                 new_field_table.ptr = estrdup(table_name);
    2476           0 :                 zend_hash_update(&EG(regular_list), hash_key.c, hash_key.len+1, (void *) &new_field_table, sizeof(zend_rsrc_list_entry), NULL);
    2477             : 
    2478           0 :                 smart_str_free(&hash_key);
    2479           0 :                 PQclear(tmp_res);
    2480           0 :                 RETURN_STRING(table_name, 1);
    2481             :         }
    2482             : 
    2483             : }
    2484             : /* }}} */
    2485             : #endif
    2486             : 
    2487             : #define PHP_PG_FIELD_NAME 1
    2488             : #define PHP_PG_FIELD_SIZE 2
    2489             : #define PHP_PG_FIELD_TYPE 3
    2490             : #define PHP_PG_FIELD_TYPE_OID 4
    2491             : 
    2492             : /* {{{ php_pgsql_get_field_info
    2493             :  */
    2494          30 : static void php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
    2495             : {
    2496             :         zval *result;
    2497             :         long field;
    2498             :         PGresult *pgsql_result;
    2499             :         pgsql_result_handle *pg_result;
    2500             :         Oid oid;
    2501             : 
    2502          30 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &result, &field) == FAILURE) {
    2503           0 :                 return;
    2504             :         }
    2505             : 
    2506          30 :         ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
    2507             : 
    2508          30 :         pgsql_result = pg_result->result;
    2509             :         
    2510          30 :         if (field < 0 || field >= PQnfields(pgsql_result)) {
    2511           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad field offset specified");
    2512           0 :                 RETURN_FALSE;
    2513             :         }
    2514             : 
    2515          30 :         switch (entry_type) {
    2516             :                 case PHP_PG_FIELD_NAME:
    2517          10 :                         Z_STRVAL_P(return_value) = PQfname(pgsql_result, field);
    2518          10 :                         Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
    2519          10 :                         Z_STRVAL_P(return_value) = estrndup(Z_STRVAL_P(return_value),Z_STRLEN_P(return_value));
    2520          10 :                         Z_TYPE_P(return_value) = IS_STRING;
    2521          10 :                         break;
    2522             :                 case PHP_PG_FIELD_SIZE:
    2523          10 :                         Z_LVAL_P(return_value) = PQfsize(pgsql_result, field);
    2524          10 :                         Z_TYPE_P(return_value) = IS_LONG;
    2525          10 :                         break;
    2526             :                 case PHP_PG_FIELD_TYPE:
    2527          10 :                         Z_STRVAL_P(return_value) = get_field_name(pg_result->conn, PQftype(pgsql_result, field), &EG(regular_list) TSRMLS_CC);
    2528          10 :                         Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
    2529          10 :                         Z_TYPE_P(return_value) = IS_STRING;
    2530          10 :                         break;
    2531             :                 case PHP_PG_FIELD_TYPE_OID:
    2532             :                         
    2533           0 :                         oid = PQftype(pgsql_result, field);
    2534             : #if UINT_MAX > LONG_MAX
    2535             :                         if (oid > LONG_MAX) {
    2536             :                                 smart_str s = {0};
    2537             :                                 smart_str_append_unsigned(&s, oid);
    2538             :                                 smart_str_0(&s);
    2539             :                                 Z_STRVAL_P(return_value) = s.c;
    2540             :                                 Z_STRLEN_P(return_value) = s.len;
    2541             :                                 Z_TYPE_P(return_value) = IS_STRING;
    2542             :                         } else
    2543             : #endif
    2544             :                         {
    2545           0 :                                 Z_LVAL_P(return_value) = (long)oid;
    2546           0 :                                 Z_TYPE_P(return_value) = IS_LONG;
    2547             :                         }
    2548           0 :                         break;
    2549             :                 default:
    2550           0 :                         RETURN_FALSE;
    2551             :         }
    2552             : }
    2553             : /* }}} */
    2554             : 
    2555             : /* {{{ proto string pg_field_name(resource result, int field_number)
    2556             :    Returns the name of the field */
    2557          10 : PHP_FUNCTION(pg_field_name)
    2558             : {
    2559          10 :         php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_NAME);
    2560          10 : }
    2561             : /* }}} */
    2562             : 
    2563             : /* {{{ proto int pg_field_size(resource result, int field_number)
    2564             :    Returns the internal size of the field */ 
    2565          10 : PHP_FUNCTION(pg_field_size)
    2566             : {
    2567          10 :         php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_SIZE);
    2568          10 : }
    2569             : /* }}} */
    2570             : 
    2571             : /* {{{ proto string pg_field_type(resource result, int field_number)
    2572             :    Returns the type name for the given field */
    2573          10 : PHP_FUNCTION(pg_field_type)
    2574             : {
    2575          10 :         php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_TYPE);
    2576          10 : }
    2577             : /* }}} */
    2578             : 
    2579             : 
    2580             : /* {{{ proto string pg_field_type_oid(resource result, int field_number)
    2581             :    Returns the type oid for the given field */
    2582           0 : PHP_FUNCTION(pg_field_type_oid)
    2583             : {
    2584           0 :         php_pgsql_get_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU,PHP_PG_FIELD_TYPE_OID);
    2585           0 : }
    2586             : /* }}} */
    2587             : 
    2588             : /* {{{ proto int pg_field_num(resource result, string field_name)
    2589             :    Returns the field number of the named field */
    2590           9 : PHP_FUNCTION(pg_field_num)
    2591             : {
    2592             :         zval *result;
    2593             :         char *field;
    2594             :         int field_len;
    2595             :         PGresult *pgsql_result;
    2596             :         pgsql_result_handle *pg_result;
    2597             : 
    2598           9 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &result, &field, &field_len) == FAILURE) {
    2599           0 :                 return;
    2600             :         }
    2601             : 
    2602           9 :         ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
    2603             : 
    2604           9 :         pgsql_result = pg_result->result;
    2605             : 
    2606           9 :         Z_LVAL_P(return_value) = PQfnumber(pgsql_result, field);
    2607           9 :         Z_TYPE_P(return_value) = IS_LONG;
    2608             : }
    2609             : /* }}} */
    2610             : 
    2611             : /* {{{ proto mixed pg_fetch_result(resource result, [int row_number,] mixed field_name)
    2612             :    Returns values from a result identifier */
    2613        8451 : PHP_FUNCTION(pg_fetch_result)
    2614             : {
    2615        8451 :         zval *result, **field=NULL;
    2616             :         long row;
    2617             :         PGresult *pgsql_result;
    2618             :         pgsql_result_handle *pg_result;
    2619        8451 :         int field_offset, pgsql_row, argc = ZEND_NUM_ARGS();
    2620             : 
    2621        8451 :         if (argc == 2) {
    2622           2 :                 if (zend_parse_parameters(argc TSRMLS_CC, "rZ", &result, &field) == FAILURE) {
    2623           0 :                         return;
    2624             :                 }
    2625             :         } else {
    2626        8449 :                 if (zend_parse_parameters(argc TSRMLS_CC, "rlZ", &result, &row, &field) == FAILURE) {
    2627           0 :                         return;
    2628             :                 }
    2629             :         }
    2630             :         
    2631        8451 :         ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
    2632             : 
    2633        8451 :         pgsql_result = pg_result->result;
    2634        8451 :         if (argc == 2) {
    2635           2 :                 if (pg_result->row < 0) {
    2636           0 :                         pg_result->row = 0;
    2637             :                 }
    2638           2 :                 pgsql_row = pg_result->row;
    2639           2 :                 if (pgsql_row >= PQntuples(pgsql_result)) {
    2640           0 :                         RETURN_FALSE;
    2641             :                 }
    2642             :         } else {
    2643        8449 :                 pgsql_row = row;
    2644        8449 :                 if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
    2645           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %ld on PostgreSQL result index %ld",
    2646           0 :                                                         row, Z_LVAL_P(result));
    2647           0 :                         RETURN_FALSE;
    2648             :                 }
    2649             :         }
    2650        8451 :         switch(Z_TYPE_PP(field)) {
    2651             :                 case IS_STRING:
    2652           0 :                         field_offset = PQfnumber(pgsql_result, Z_STRVAL_PP(field));
    2653           0 :                         break;
    2654             :                 default:
    2655        8451 :                         convert_to_long_ex(field);
    2656        8451 :                         field_offset = Z_LVAL_PP(field);
    2657             :                         break;
    2658             :         }
    2659        8451 :         if (field_offset<0 || field_offset>=PQnfields(pgsql_result)) {
    2660           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad column offset specified");
    2661           0 :                 RETURN_FALSE;
    2662             :         }
    2663             : 
    2664        8451 :         if (PQgetisnull(pgsql_result, pgsql_row, field_offset)) {
    2665           0 :                 Z_TYPE_P(return_value) = IS_NULL;
    2666             :         } else {
    2667        8451 :                 char *value = PQgetvalue(pgsql_result, pgsql_row, field_offset);
    2668        8451 :                 int value_len = PQgetlength(pgsql_result, pgsql_row, field_offset);
    2669        8451 :                 ZVAL_STRINGL(return_value, value, value_len, 1);
    2670             :         }
    2671             : }
    2672             : /* }}} */
    2673             : 
    2674             : /* {{{ void php_pgsql_fetch_hash */
    2675       25361 : static void php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, long result_type, int into_object)
    2676             : {
    2677       25361 :         zval                *result, *zrow = NULL;
    2678             :         PGresult            *pgsql_result;
    2679             :         pgsql_result_handle *pg_result;
    2680             :         int             i, num_fields, pgsql_row, use_row;
    2681       25361 :         long            row = -1;
    2682             :         char            *field_name;
    2683       25361 :         zval            *ctor_params = NULL;
    2684       25361 :         zend_class_entry *ce = NULL;
    2685             : 
    2686       25361 :         if (into_object) {
    2687        8450 :                 char *class_name = NULL;
    2688             :                 int class_name_len;
    2689             : 
    2690        8450 :                 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|z!sz", &result, &zrow, &class_name, &class_name_len, &ctor_params) == FAILURE) {
    2691           0 :                         return;
    2692             :                 }
    2693        8450 :                 if (!class_name) {
    2694        8449 :                         ce = zend_standard_class_def;
    2695             :                 } else {
    2696           1 :                         ce = zend_fetch_class(class_name, class_name_len, ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
    2697             :                 }
    2698        8450 :                 if (!ce) {
    2699           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not find class '%s'", class_name);
    2700           0 :                         return;
    2701             :                 }
    2702        8450 :                 result_type = PGSQL_ASSOC;
    2703             :         } else {
    2704       16911 :                 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|z!l", &result, &zrow, &result_type) == FAILURE) {
    2705           0 :                         return;
    2706             :                 }
    2707             :         }
    2708       25361 :         if (zrow == NULL) {
    2709        8459 :                 row = -1;
    2710             :         } else {
    2711       16902 :                 convert_to_long(zrow);
    2712       16902 :                 row = Z_LVAL_P(zrow);
    2713       16902 :                 if (row < 0) {
    2714           4 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "The row parameter must be greater or equal to zero");
    2715           4 :                         RETURN_FALSE;
    2716             :                 }
    2717             :         }
    2718       25357 :         use_row = ZEND_NUM_ARGS() > 1 && row != -1;
    2719             : 
    2720       25357 :         if (!(result_type & PGSQL_BOTH)) {
    2721           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid result type");
    2722           0 :                 RETURN_FALSE;
    2723             :         }
    2724             :         
    2725       25357 :         ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
    2726             : 
    2727       25357 :         pgsql_result = pg_result->result;
    2728             : 
    2729       25357 :         if (use_row) { 
    2730       16898 :                 pgsql_row = row;
    2731       16898 :                 pg_result->row = pgsql_row;
    2732       16898 :                 if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
    2733           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %ld on PostgreSQL result index %ld",
    2734           0 :                                                         row, Z_LVAL_P(result));
    2735           0 :                         RETURN_FALSE;
    2736             :                 }
    2737             :         } else {
    2738             :                 /* If 2nd param is NULL, use internal row counter to access next row */
    2739        8459 :                 pgsql_row = pg_result->row;
    2740        8459 :                 if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
    2741        8438 :                         RETURN_FALSE;
    2742             :                 }
    2743          21 :                 pg_result->row++;
    2744             :         }
    2745             : 
    2746       16919 :         array_init(return_value);
    2747       67649 :         for (i = 0, num_fields = PQnfields(pgsql_result); i < num_fields; i++) {
    2748       50730 :                 if (PQgetisnull(pgsql_result, pgsql_row, i)) {
    2749       16904 :                         if (result_type & PGSQL_NUM) {
    2750       16892 :                                 add_index_null(return_value, i);
    2751             :                         }
    2752       16904 :                         if (result_type & PGSQL_ASSOC) {
    2753          13 :                                 field_name = PQfname(pgsql_result, i);
    2754          13 :                                 add_assoc_null(return_value, field_name);
    2755             :                         }
    2756             :                 } else {
    2757       33826 :                         char *element = PQgetvalue(pgsql_result, pgsql_row, i);
    2758       33826 :                         if (element) {
    2759             :                                 char *data;
    2760             :                                 int data_len;
    2761       33826 :                                 int should_copy=0;
    2762       33826 :                                 const uint element_len = strlen(element);
    2763             : 
    2764       33826 :                                 data = safe_estrndup(element, element_len);
    2765       33826 :                                 data_len = element_len;
    2766             : 
    2767       33826 :                                 if (result_type & PGSQL_NUM) {
    2768       33788 :                                         add_index_stringl(return_value, i, data, data_len, should_copy);
    2769       33788 :                                         should_copy=1;
    2770             :                                 }
    2771             : 
    2772       33826 :                                 if (result_type & PGSQL_ASSOC) {
    2773          43 :                                         field_name = PQfname(pgsql_result, i);
    2774          43 :                                         add_assoc_stringl(return_value, field_name, data, data_len, should_copy);
    2775             :                                 }
    2776             :                         }
    2777             :                 }
    2778             :         }
    2779             : 
    2780       16919 :         if (into_object) {
    2781          13 :                 zval dataset = *return_value;
    2782             :                 zend_fcall_info fci;
    2783             :                 zend_fcall_info_cache fcc;
    2784             :                 zval *retval_ptr;
    2785             : 
    2786          13 :                 object_and_properties_init(return_value, ce, NULL);
    2787          13 :                 zend_merge_properties(return_value, Z_ARRVAL(dataset), 1 TSRMLS_CC);
    2788             : 
    2789          13 :                 if (ce->constructor) {
    2790           1 :                         fci.size = sizeof(fci);
    2791           1 :                         fci.function_table = &ce->function_table;
    2792           1 :                         fci.function_name = NULL;
    2793           1 :                         fci.symbol_table = NULL;
    2794           1 :                         fci.object_ptr = return_value;
    2795           1 :                         fci.retval_ptr_ptr = &retval_ptr;
    2796           1 :                         fci.params = NULL;
    2797           1 :                         fci.param_count = 0;
    2798           1 :                         fci.no_separation = 1;
    2799             : 
    2800           1 :                         if (ctor_params && Z_TYPE_P(ctor_params) != IS_NULL) {
    2801           1 :                                 if (zend_fcall_info_args(&fci, ctor_params TSRMLS_CC) == FAILURE) {
    2802             :                                         /* Two problems why we throw exceptions here: PHP is typeless
    2803             :                                          * and hence passing one argument that's not an array could be
    2804             :                                          * by mistake and the other way round is possible, too. The
    2805             :                                          * single value is an array. Also we'd have to make that one
    2806             :                                          * argument passed by reference.
    2807             :                                          */
    2808           0 :                                         zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Parameter ctor_params must be an array", 0 TSRMLS_CC);
    2809           0 :                                         return;
    2810             :                                 }
    2811             :                         }
    2812             : 
    2813           1 :                         fcc.initialized = 1;
    2814           1 :                         fcc.function_handler = ce->constructor;
    2815           1 :                         fcc.calling_scope = EG(scope);
    2816           1 :                         fcc.called_scope = Z_OBJCE_P(return_value);
    2817           1 :                         fcc.object_ptr = return_value;
    2818             : 
    2819           1 :                         if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) {
    2820           0 :                                 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);
    2821             :                         } else {
    2822           1 :                                 if (retval_ptr) {
    2823           1 :                                         zval_ptr_dtor(&retval_ptr);
    2824             :                                 }
    2825             :                         }
    2826           1 :                         if (fci.params) {
    2827           1 :                                 efree(fci.params);
    2828             :                         }
    2829          12 :                 } else if (ctor_params) {
    2830           0 :                         zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Class %s does not have a constructor hence you cannot use ctor_params", ce->name);
    2831             :                 }
    2832             :         }
    2833             : }
    2834             : /* }}} */
    2835             : 
    2836             : /* {{{ proto array pg_fetch_row(resource result [, int row [, int result_type]])
    2837             :    Get a row as an enumerated array */ 
    2838        8448 : PHP_FUNCTION(pg_fetch_row)
    2839             : {
    2840        8448 :         php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_NUM, 0);
    2841        8448 : }
    2842             : /* }}} */
    2843             : 
    2844             : /* {{{ proto array pg_fetch_assoc(resource result [, int row])
    2845             :    Fetch a row as an assoc array */
    2846           7 : PHP_FUNCTION(pg_fetch_assoc)
    2847             : {
    2848             :         /* pg_fetch_assoc() is added from PHP 4.3.0. It should raise error, when
    2849             :            there is 3rd parameter */
    2850           7 :         if (ZEND_NUM_ARGS() > 2)
    2851           0 :                 WRONG_PARAM_COUNT;
    2852           7 :         php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_ASSOC, 0);
    2853             : }
    2854             : /* }}} */
    2855             : 
    2856             : /* {{{ proto array pg_fetch_array(resource result [, int row [, int result_type]])
    2857             :    Fetch a row as an array */
    2858        8456 : PHP_FUNCTION(pg_fetch_array)
    2859             : {
    2860        8456 :         php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_BOTH, 0);
    2861        8456 : }
    2862             : /* }}} */
    2863             : 
    2864             : /* {{{ proto object pg_fetch_object(resource result [, int row [, string class_name [, NULL|array ctor_params]]])
    2865             :    Fetch a row as an object */
    2866        8450 : PHP_FUNCTION(pg_fetch_object)
    2867             : {
    2868             :         /* pg_fetch_object() allowed result_type used to be. 3rd parameter
    2869             :            must be allowed for compatibility */
    2870        8450 :         php_pgsql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, PGSQL_ASSOC, 1);
    2871        8450 : }
    2872             : /* }}} */
    2873             : 
    2874             : /* {{{ proto array pg_fetch_all(resource result)
    2875             :    Fetch all rows into array */
    2876           6 : PHP_FUNCTION(pg_fetch_all)
    2877             : {
    2878             :         zval *result;
    2879             :         PGresult *pgsql_result;
    2880             :         pgsql_result_handle *pg_result;
    2881             : 
    2882           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) {
    2883           0 :                 return;
    2884             :         }
    2885             : 
    2886           6 :         ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
    2887             : 
    2888           6 :         pgsql_result = pg_result->result;
    2889           6 :         array_init(return_value);
    2890           6 :         if (php_pgsql_result2array(pgsql_result, return_value TSRMLS_CC) == FAILURE) {
    2891             :                 zval_dtor(return_value);
    2892           0 :                 RETURN_FALSE;
    2893             :         }
    2894             : }
    2895             : /* }}} */
    2896             : 
    2897             : /* {{{ proto array pg_fetch_all_columns(resource result [, int column_number])
    2898             :    Fetch all rows into array */
    2899           0 : PHP_FUNCTION(pg_fetch_all_columns)
    2900             : {
    2901             :         zval *result;
    2902             :         PGresult *pgsql_result;
    2903             :         pgsql_result_handle *pg_result;
    2904           0 :         unsigned long colno=0;
    2905             :         int pg_numrows, pg_row;
    2906             :         size_t num_fields;
    2907             : 
    2908           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &result, &colno) == FAILURE) {
    2909           0 :                 RETURN_FALSE;
    2910             :         }
    2911             : 
    2912           0 :         ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
    2913             : 
    2914           0 :         pgsql_result = pg_result->result;
    2915             : 
    2916           0 :         num_fields = PQnfields(pgsql_result);
    2917           0 :         if (colno >= num_fields || colno < 0) {
    2918           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid column number '%ld'", colno);
    2919           0 :                 RETURN_FALSE;
    2920             :         }
    2921             : 
    2922           0 :         array_init(return_value);
    2923             : 
    2924           0 :         if ((pg_numrows = PQntuples(pgsql_result)) <= 0) {
    2925           0 :                 return;
    2926             :         }
    2927             : 
    2928           0 :         for (pg_row = 0; pg_row < pg_numrows; pg_row++) {
    2929           0 :                 if (PQgetisnull(pgsql_result, pg_row, colno)) {
    2930           0 :                         add_next_index_null(return_value);
    2931             :                 } else {
    2932           0 :                         add_next_index_string(return_value, PQgetvalue(pgsql_result, pg_row, colno), 1); 
    2933             :                 }
    2934             :         }
    2935             : }
    2936             : /* }}} */
    2937             : 
    2938             : /* {{{ proto bool pg_result_seek(resource result, int offset)
    2939             :    Set internal row offset */
    2940           2 : PHP_FUNCTION(pg_result_seek)
    2941             : {
    2942             :         zval *result;
    2943             :         long row;
    2944             :         pgsql_result_handle *pg_result;
    2945             : 
    2946           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &result, &row) == FAILURE) {
    2947           0 :                 return;
    2948             :         }
    2949             : 
    2950           2 :         ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
    2951             : 
    2952           2 :         if (row < 0 || row >= PQntuples(pg_result->result)) {
    2953           0 :                 RETURN_FALSE;
    2954             :         }
    2955             : 
    2956             :         /* seek to offset */
    2957           2 :         pg_result->row = row;
    2958           2 :         RETURN_TRUE;
    2959             : }
    2960             : /* }}} */
    2961             : 
    2962             : 
    2963             : #define PHP_PG_DATA_LENGTH 1
    2964             : #define PHP_PG_DATA_ISNULL 2
    2965             : 
    2966             : /* {{{ php_pgsql_data_info
    2967             :  */
    2968          20 : static void php_pgsql_data_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
    2969             : {
    2970             :         zval *result, **field;
    2971             :         long row;
    2972             :         PGresult *pgsql_result;
    2973             :         pgsql_result_handle *pg_result;
    2974          20 :         int field_offset, pgsql_row, argc = ZEND_NUM_ARGS();
    2975             : 
    2976          20 :         if (argc == 2) {
    2977          20 :                 if (zend_parse_parameters(argc TSRMLS_CC, "rZ", &result, &field) == FAILURE) {
    2978           0 :                         return;
    2979             :                 }
    2980             :         } else {
    2981           0 :                 if (zend_parse_parameters(argc TSRMLS_CC, "rlZ", &result, &row, &field) == FAILURE) {
    2982           0 :                         return;
    2983             :                 }
    2984             :         }
    2985             : 
    2986          20 :         ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
    2987             : 
    2988          20 :         pgsql_result = pg_result->result;
    2989          20 :         if (argc == 2) {
    2990          20 :                 if (pg_result->row < 0) {
    2991           0 :                         pg_result->row = 0;
    2992             :                 }
    2993          20 :                 pgsql_row = pg_result->row;
    2994          20 :                 if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
    2995           0 :                         RETURN_FALSE;
    2996             :                 }
    2997             :         } else {
    2998           0 :                 pgsql_row = row;
    2999           0 :                 if (pgsql_row < 0 || pgsql_row >= PQntuples(pgsql_result)) {
    3000           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %ld on PostgreSQL result index %ld",
    3001           0 :                                                         row, Z_LVAL_P(result));
    3002           0 :                         RETURN_FALSE;
    3003             :                 }
    3004             :         }
    3005             : 
    3006          20 :         switch(Z_TYPE_PP(field)) {
    3007             :                 case IS_STRING:
    3008           0 :                         convert_to_string_ex(field);
    3009           0 :                         field_offset = PQfnumber(pgsql_result, Z_STRVAL_PP(field));
    3010           0 :                         break;
    3011             :                 default:
    3012          20 :                         convert_to_long_ex(field);
    3013          20 :                         field_offset = Z_LVAL_PP(field);
    3014             :                         break;
    3015             :         }
    3016          20 :         if (field_offset < 0 || field_offset >= PQnfields(pgsql_result)) {
    3017           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad column offset specified");
    3018           0 :                 RETURN_FALSE;
    3019             :         }
    3020             : 
    3021          20 :         switch (entry_type) {
    3022             :                 case PHP_PG_DATA_LENGTH:
    3023          10 :                         Z_LVAL_P(return_value) = PQgetlength(pgsql_result, pgsql_row, field_offset);
    3024          10 :                         break;
    3025             :                 case PHP_PG_DATA_ISNULL:
    3026          10 :                         Z_LVAL_P(return_value) = PQgetisnull(pgsql_result, pgsql_row, field_offset);
    3027             :                         break;
    3028             :         }
    3029          20 :         Z_TYPE_P(return_value) = IS_LONG;
    3030             : }
    3031             : /* }}} */
    3032             : 
    3033             : /* {{{ proto int pg_field_prtlen(resource result, [int row,] mixed field_name_or_number)
    3034             :    Returns the printed length */
    3035          10 : PHP_FUNCTION(pg_field_prtlen)
    3036             : {
    3037          10 :         php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_LENGTH);
    3038          10 : }
    3039             : /* }}} */
    3040             : 
    3041             : /* {{{ proto int pg_field_is_null(resource result, [int row,] mixed field_name_or_number)
    3042             :    Test if a field is NULL */
    3043          10 : PHP_FUNCTION(pg_field_is_null)
    3044             : {
    3045          10 :         php_pgsql_data_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_DATA_ISNULL);
    3046          10 : }
    3047             : /* }}} */
    3048             : 
    3049             : /* {{{ proto bool pg_free_result(resource result)
    3050             :    Free result memory */
    3051          16 : PHP_FUNCTION(pg_free_result)
    3052             : {
    3053             :         zval *result;
    3054             :         pgsql_result_handle *pg_result;
    3055             : 
    3056          16 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) {
    3057           0 :                 return;
    3058             :         }
    3059             : 
    3060          16 :         ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
    3061          16 :         if (Z_LVAL_P(result) == 0) {
    3062           0 :                 RETURN_FALSE;
    3063             :         }
    3064          16 :         zend_list_delete(Z_RESVAL_P(result));
    3065          16 :         RETURN_TRUE;
    3066             : }
    3067             : /* }}} */
    3068             : 
    3069             : /* {{{ proto string pg_last_oid(resource result)
    3070             :    Returns the last object identifier */
    3071          10 : PHP_FUNCTION(pg_last_oid)
    3072             : {
    3073             :         zval *result;
    3074             :         PGresult *pgsql_result;
    3075             :         pgsql_result_handle *pg_result;
    3076             : #ifdef HAVE_PQOIDVALUE
    3077             :         Oid oid;
    3078             : #endif
    3079             : 
    3080          10 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) {
    3081           0 :                 return;
    3082             :         }
    3083             : 
    3084          10 :         ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
    3085          10 :         pgsql_result = pg_result->result;
    3086             : #ifdef HAVE_PQOIDVALUE
    3087          10 :         oid = PQoidValue(pgsql_result);
    3088          10 :         if (oid == InvalidOid) {
    3089          10 :                 RETURN_FALSE;
    3090             :         }
    3091           0 :         PGSQL_RETURN_OID(oid);
    3092             : #else
    3093             :         Z_STRVAL_P(return_value) = (char *) PQoidStatus(pgsql_result);
    3094             :         if (Z_STRVAL_P(return_value)) {
    3095             :                 RETURN_STRING(Z_STRVAL_P(return_value), 1);
    3096             :         }
    3097             :         RETURN_STRING("", 1);
    3098             : #endif
    3099             : }
    3100             : /* }}} */
    3101             : 
    3102             : /* {{{ proto bool pg_trace(string filename [, string mode [, resource connection]])
    3103             :    Enable tracing a PostgreSQL connection */
    3104           1 : PHP_FUNCTION(pg_trace)
    3105             : {
    3106           1 :         char *z_filename, *mode = "w";
    3107             :         int z_filename_len, mode_len;
    3108           1 :         zval *pgsql_link = NULL;
    3109           1 :         int id = -1, argc = ZEND_NUM_ARGS();
    3110             :         PGconn *pgsql;
    3111           1 :         FILE *fp = NULL;
    3112             :         php_stream *stream;
    3113           1 :         id = PGG(default_link);
    3114             : 
    3115           1 :         if (zend_parse_parameters(argc TSRMLS_CC, "s|sr", &z_filename, &z_filename_len, &mode, &mode_len, &pgsql_link) == FAILURE) {
    3116           0 :                 return;
    3117             :         }
    3118             : 
    3119           1 :         if (argc < 3) {
    3120           0 :                 CHECK_DEFAULT_LINK(id);
    3121             :         }
    3122             : 
    3123           1 :         if (pgsql_link == NULL && id == -1) {
    3124           0 :                 RETURN_FALSE;
    3125             :         }
    3126             : 
    3127           1 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    3128             : 
    3129           1 :         stream = php_stream_open_wrapper(z_filename, mode, REPORT_ERRORS, NULL);
    3130             : 
    3131           1 :         if (!stream) {
    3132           0 :                 RETURN_FALSE;
    3133             :         }
    3134             : 
    3135           1 :         if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void**)&fp, REPORT_ERRORS))    {
    3136           0 :                 php_stream_close(stream);
    3137           0 :                 RETURN_FALSE;
    3138             :         }
    3139             :         php_stream_auto_cleanup(stream);
    3140           1 :         PQtrace(pgsql, fp);
    3141           1 :         RETURN_TRUE;
    3142             : }
    3143             : /* }}} */
    3144             : 
    3145             : /* {{{ proto bool pg_untrace([resource connection])
    3146             :    Disable tracing of a PostgreSQL connection */
    3147           0 : PHP_FUNCTION(pg_untrace)
    3148             : {
    3149           0 :         zval *pgsql_link = NULL;
    3150           0 :         int id = -1, argc = ZEND_NUM_ARGS();
    3151             :         PGconn *pgsql;
    3152             :         
    3153           0 :         if (zend_parse_parameters(argc TSRMLS_CC, "|r", &pgsql_link) == FAILURE) {
    3154           0 :                 return;
    3155             :         }
    3156             : 
    3157           0 :         if (argc == 0) { 
    3158           0 :                 id = PGG(default_link);
    3159           0 :                 CHECK_DEFAULT_LINK(id);
    3160             :         }
    3161             : 
    3162           0 :         if (pgsql_link == NULL && id == -1) {
    3163           0 :                 RETURN_FALSE;
    3164             :         }
    3165             : 
    3166           0 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    3167           0 :         PQuntrace(pgsql);
    3168           0 :         RETURN_TRUE;
    3169             : }
    3170             : /* }}} */
    3171             : 
    3172             : /* {{{ proto mixed pg_lo_create([resource connection],[mixed large_object_oid])
    3173             :    Create a large object */
    3174           6 : PHP_FUNCTION(pg_lo_create)
    3175             : {
    3176           6 :         zval *pgsql_link = NULL, *oid = NULL;
    3177             :         PGconn *pgsql;
    3178           6 :         Oid pgsql_oid, wanted_oid = InvalidOid;
    3179           6 :         int id = -1, argc = ZEND_NUM_ARGS();
    3180             : 
    3181           6 :         if (zend_parse_parameters(argc TSRMLS_CC, "|zz", &pgsql_link, &oid) == FAILURE) {
    3182           0 :                 return;
    3183             :         }
    3184             : 
    3185           6 :         if ((argc == 1) && (Z_TYPE_P(pgsql_link) != IS_RESOURCE)) {
    3186           1 :                 oid = pgsql_link;
    3187           1 :                 pgsql_link = NULL;
    3188             :         }
    3189             :         
    3190           6 :         if (pgsql_link == NULL) {
    3191           1 :                 id = PGG(default_link);
    3192           1 :                 CHECK_DEFAULT_LINK(id);
    3193           1 :                 if (id == -1) {
    3194           0 :                         RETURN_FALSE;
    3195             :                 }
    3196             :         }
    3197             : 
    3198           6 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    3199             :         
    3200           6 :         if (oid) {
    3201             : #ifndef HAVE_PG_LO_CREATE
    3202             :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Passing OID value is not supported. Upgrade your PostgreSQL");
    3203             : #else
    3204           3 :                 switch (Z_TYPE_P(oid)) {
    3205             :                 case IS_STRING:
    3206             :                         {
    3207             :                                 char *end_ptr;
    3208           1 :                                 wanted_oid = (Oid)strtoul(Z_STRVAL_P(oid), &end_ptr, 10);
    3209           1 :                                 if ((Z_STRVAL_P(oid)+Z_STRLEN_P(oid)) != end_ptr) {
    3210             :                                 /* wrong integer format */
    3211           0 :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "invalid OID value passed");
    3212           0 :                                 RETURN_FALSE;
    3213             :                                 }
    3214             :                         }
    3215           1 :                         break;
    3216             :                 case IS_LONG:
    3217           2 :                         if (Z_LVAL_P(oid) < (long)InvalidOid) {
    3218           0 :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "invalid OID value passed");
    3219           0 :                                 RETURN_FALSE;
    3220             :                         }
    3221           2 :                         wanted_oid = (Oid)Z_LVAL_P(oid);
    3222           2 :                         break;
    3223             :                 default:
    3224           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "invalid OID value passed");
    3225           0 :                         RETURN_FALSE;
    3226             :         }
    3227           3 :                 if ((pgsql_oid = lo_create(pgsql, wanted_oid)) == InvalidOid) {
    3228           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create PostgreSQL large object");
    3229           0 :                         RETURN_FALSE;
    3230             :                 }
    3231             : 
    3232           3 :                 PGSQL_RETURN_OID(pgsql_oid);
    3233             : #endif
    3234             :         }
    3235             : 
    3236           3 :         if ((pgsql_oid = lo_creat(pgsql, INV_READ|INV_WRITE)) == InvalidOid) {
    3237           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create PostgreSQL large object");
    3238           0 :                 RETURN_FALSE;
    3239             :         }
    3240             : 
    3241           3 :         PGSQL_RETURN_OID(pgsql_oid);
    3242             : }
    3243             : /* }}} */
    3244             : 
    3245             : /* {{{ proto bool pg_lo_unlink([resource connection,] string large_object_oid)
    3246             :    Delete a large object */
    3247           9 : PHP_FUNCTION(pg_lo_unlink)
    3248             : {
    3249           9 :         zval *pgsql_link = NULL;
    3250             :         long oid_long;
    3251             :         char *oid_string, *end_ptr;
    3252             :         int oid_strlen;
    3253             :         PGconn *pgsql;
    3254             :         Oid oid;
    3255           9 :         int id = -1;
    3256           9 :         int argc = ZEND_NUM_ARGS();
    3257             : 
    3258             :         /* accept string type since Oid type is unsigned int */
    3259           9 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3260             :                                                                  "rs", &pgsql_link, &oid_string, &oid_strlen) == SUCCESS) {
    3261           6 :                 oid = (Oid)strtoul(oid_string, &end_ptr, 10);
    3262           6 :                 if ((oid_string+oid_strlen) != end_ptr) {
    3263             :                         /* wrong integer format */
    3264           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed");
    3265           0 :                         RETURN_FALSE;
    3266             :                 }
    3267             :         }
    3268           3 :         else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3269             :                                                                  "rl", &pgsql_link, &oid_long) == SUCCESS) {
    3270           0 :                 if (oid_long <= InvalidOid) {
    3271           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified");
    3272           0 :                         RETURN_FALSE;
    3273             :                 }
    3274           0 :                 oid = (Oid)oid_long;
    3275             :         }
    3276           3 :         else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3277             :                                                                  "s", &oid_string, &oid_strlen) == SUCCESS) {
    3278           3 :                 oid = (Oid)strtoul(oid_string, &end_ptr, 10);
    3279           3 :                 if ((oid_string+oid_strlen) != end_ptr) {
    3280             :                         /* wrong integer format */
    3281           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed");
    3282           0 :                         RETURN_FALSE;
    3283             :                 }
    3284           3 :                 id = PGG(default_link);
    3285           3 :                 CHECK_DEFAULT_LINK(id);
    3286             :         }
    3287           0 :         else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3288             :                                                                  "l", &oid_long) == SUCCESS) {
    3289           0 :                 if (oid_long <= InvalidOid) {
    3290           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID is specified");
    3291           0 :                         RETURN_FALSE;
    3292             :                 }
    3293           0 :                 oid = (Oid)oid_long;
    3294           0 :                 id = PGG(default_link);
    3295           0 :                 CHECK_DEFAULT_LINK(id);
    3296             :         }
    3297             :         else {
    3298           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Requires 1 or 2 arguments");
    3299           0 :                 RETURN_FALSE;
    3300             :         }
    3301           9 :         if (pgsql_link == NULL && id == -1) {
    3302           0 :                 RETURN_FALSE;
    3303             :         }
    3304             : 
    3305           9 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    3306             : 
    3307           9 :         if (lo_unlink(pgsql, oid) == -1) {
    3308           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to delete PostgreSQL large object %u", oid);
    3309           0 :                 RETURN_FALSE;
    3310             :         }
    3311           9 :         RETURN_TRUE;
    3312             : }
    3313             : /* }}} */
    3314             : 
    3315             : /* {{{ proto resource pg_lo_open([resource connection,] int large_object_oid, string mode)
    3316             :    Open a large object and return fd */
    3317           3 : PHP_FUNCTION(pg_lo_open)
    3318             : {
    3319           3 :         zval *pgsql_link = NULL;
    3320             :         long oid_long;
    3321             :         char *oid_string, *end_ptr, *mode_string;
    3322             :         int oid_strlen, mode_strlen;
    3323             :         PGconn *pgsql;
    3324             :         Oid oid;
    3325           3 :         int id = -1, pgsql_mode=0, pgsql_lofd;
    3326           3 :         int create=0;
    3327             :         pgLofp *pgsql_lofp;
    3328           3 :         int argc = ZEND_NUM_ARGS();
    3329             : 
    3330             :         /* accept string type since Oid is unsigned int */
    3331           3 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3332             :                                                                  "rss", &pgsql_link, &oid_string, &oid_strlen, &mode_string, &mode_strlen) == SUCCESS) {
    3333           3 :                 oid = (Oid)strtoul(oid_string, &end_ptr, 10);
    3334           3 :                 if ((oid_string+oid_strlen) != end_ptr) {
    3335             :                         /* wrong integer format */
    3336           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed");
    3337           0 :                         RETURN_FALSE;
    3338             :                 }
    3339             :         }
    3340           0 :         else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3341             :                                                                  "rls", &pgsql_link, &oid_long, &mode_string, &mode_strlen) == SUCCESS) {
    3342           0 :                 if (oid_long <= InvalidOid) {
    3343           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified");
    3344           0 :                         RETURN_FALSE;
    3345             :                 }
    3346           0 :                 oid = (Oid)oid_long;
    3347             :         }
    3348           0 :         else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3349             :                                                                  "ss", &oid_string, &oid_strlen, &mode_string, &mode_strlen) == SUCCESS) {
    3350           0 :                 oid = (Oid)strtoul(oid_string, &end_ptr, 10);
    3351           0 :                 if ((oid_string+oid_strlen) != end_ptr) {
    3352             :                         /* wrong integer format */
    3353           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed");
    3354           0 :                         RETURN_FALSE;
    3355             :                 }
    3356           0 :                 id = PGG(default_link);
    3357           0 :                 CHECK_DEFAULT_LINK(id);
    3358             :         }
    3359           0 :         else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3360             :                                                                  "ls", &oid_long, &mode_string, &mode_strlen) == SUCCESS) {
    3361           0 :                 if (oid_long <= InvalidOid) {
    3362           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified");
    3363           0 :                         RETURN_FALSE;
    3364             :                 }
    3365           0 :                 oid = (Oid)oid_long;
    3366           0 :                 id = PGG(default_link);
    3367           0 :                 CHECK_DEFAULT_LINK(id);
    3368             :         }
    3369             :         else {
    3370           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Requires 1 or 2 arguments");
    3371           0 :                 RETURN_FALSE;
    3372             :         }
    3373           3 :         if (pgsql_link == NULL && id == -1) {
    3374           0 :                 RETURN_FALSE;
    3375             :         }
    3376             : 
    3377           3 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    3378             :         
    3379             :         /* r/w/+ is little bit more PHP-like than INV_READ/INV_WRITE and a lot of
    3380             :            faster to type. Unfortunately, doesn't behave the same way as fopen()...
    3381             :            (Jouni)
    3382             :         */
    3383             : 
    3384           3 :         if (strchr(mode_string, 'r') == mode_string) {
    3385           0 :                 pgsql_mode |= INV_READ;
    3386           0 :                 if (strchr(mode_string, '+') == mode_string+1) {
    3387           0 :                         pgsql_mode |= INV_WRITE;
    3388             :                 }
    3389             :         }
    3390           3 :         if (strchr(mode_string, 'w') == mode_string) {
    3391           3 :                 pgsql_mode |= INV_WRITE;
    3392           3 :                 create = 1;
    3393           3 :                 if (strchr(mode_string, '+') == mode_string+1) {
    3394           0 :                         pgsql_mode |= INV_READ;
    3395             :                 }
    3396             :         }
    3397             : 
    3398           3 :         pgsql_lofp = (pgLofp *) emalloc(sizeof(pgLofp));
    3399             : 
    3400           3 :         if ((pgsql_lofd = lo_open(pgsql, oid, pgsql_mode)) == -1) {
    3401           0 :                 if (create) {
    3402           0 :                         if ((oid = lo_creat(pgsql, INV_READ|INV_WRITE)) == 0) {
    3403           0 :                                 efree(pgsql_lofp);
    3404           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create PostgreSQL large object");
    3405           0 :                                 RETURN_FALSE;
    3406             :                         } else {
    3407           0 :                                 if ((pgsql_lofd = lo_open(pgsql, oid, pgsql_mode)) == -1) {
    3408           0 :                                         if (lo_unlink(pgsql, oid) == -1) {
    3409           0 :                                                 efree(pgsql_lofp);
    3410           0 :                                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Something is really messed up! Your database is badly corrupted in a way NOT related to PHP");
    3411           0 :                                                 RETURN_FALSE;
    3412             :                                         }
    3413           0 :                                         efree(pgsql_lofp);
    3414           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open PostgreSQL large object");
    3415           0 :                                         RETURN_FALSE;
    3416             :                                 } else {
    3417           0 :                                         pgsql_lofp->conn = pgsql;
    3418           0 :                                         pgsql_lofp->lofd = pgsql_lofd;
    3419           0 :                                         Z_LVAL_P(return_value) = zend_list_insert(pgsql_lofp, le_lofp TSRMLS_CC);
    3420           0 :                                         Z_TYPE_P(return_value) = IS_LONG;
    3421             :                                 }
    3422             :                         }
    3423             :                 } else {
    3424           0 :                         efree(pgsql_lofp);
    3425           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to open PostgreSQL large object");
    3426           0 :                         RETURN_FALSE;
    3427             :                 }
    3428             :         } else {
    3429           3 :                 pgsql_lofp->conn = pgsql;
    3430           3 :                 pgsql_lofp->lofd = pgsql_lofd;
    3431           3 :                 ZEND_REGISTER_RESOURCE(return_value, pgsql_lofp, le_lofp);
    3432             :         }
    3433             : }
    3434             : /* }}} */
    3435             : 
    3436             : /* {{{ proto bool pg_lo_close(resource large_object)
    3437             :    Close a large object */
    3438           3 : PHP_FUNCTION(pg_lo_close)
    3439             : {
    3440             :         zval *pgsql_lofp;
    3441             :         pgLofp *pgsql;
    3442             : 
    3443           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_lofp) == FAILURE) {
    3444           0 :                 return;
    3445             :         }
    3446             : 
    3447           3 :         ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_lofp, -1, "PostgreSQL large object", le_lofp);
    3448             :         
    3449           3 :         if (lo_close((PGconn *)pgsql->conn, pgsql->lofd) < 0) {
    3450           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to close PostgreSQL large object descriptor %d", pgsql->lofd);
    3451           0 :                 RETVAL_FALSE;
    3452             :         } else {
    3453           3 :                 RETVAL_TRUE;
    3454             :         }
    3455             : 
    3456           3 :         zend_list_delete(Z_RESVAL_P(pgsql_lofp));
    3457           3 :         return;
    3458             : }
    3459             : /* }}} */
    3460             : 
    3461             : #define PGSQL_LO_READ_BUF_SIZE  8192
    3462             : 
    3463             : /* {{{ proto string pg_lo_read(resource large_object [, int len])
    3464             :    Read a large object */
    3465           1 : PHP_FUNCTION(pg_lo_read)
    3466             : {
    3467             :         zval *pgsql_id;
    3468             :         long len;
    3469           1 :         int buf_len = PGSQL_LO_READ_BUF_SIZE, nbytes, argc = ZEND_NUM_ARGS();
    3470             :         char *buf;
    3471             :         pgLofp *pgsql;
    3472             : 
    3473           1 :         if (zend_parse_parameters(argc TSRMLS_CC, "r|l", &pgsql_id, &len) == FAILURE) {
    3474           0 :                 return;
    3475             :         }
    3476             : 
    3477           1 :         ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp);
    3478             : 
    3479           1 :         if (argc > 1) {
    3480           1 :                 buf_len = len;
    3481             :         }
    3482             :         
    3483           1 :         buf = (char *) safe_emalloc(sizeof(char), (buf_len+1), 0);
    3484           1 :         if ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, buf, buf_len))<0) {
    3485           0 :                 efree(buf);
    3486           0 :                 RETURN_FALSE;
    3487             :         }
    3488             : 
    3489           1 :         buf[nbytes] = '\0';
    3490           1 :         RETURN_STRINGL(buf, nbytes, 0);
    3491             : }
    3492             : /* }}} */
    3493             : 
    3494             : /* {{{ proto int pg_lo_write(resource large_object, string buf [, int len])
    3495             :    Write a large object */
    3496           1 : PHP_FUNCTION(pg_lo_write)
    3497             : {
    3498             :         zval *pgsql_id;
    3499             :         char *str;
    3500             :         long z_len;
    3501             :         int str_len, nbytes;
    3502             :         int len;
    3503             :         pgLofp *pgsql;
    3504           1 :         int argc = ZEND_NUM_ARGS();
    3505             : 
    3506           1 :         if (zend_parse_parameters(argc TSRMLS_CC, "rs|l", &pgsql_id, &str, &str_len, &z_len) == FAILURE) {
    3507           0 :                 return;
    3508             :         }
    3509             : 
    3510           1 :         if (argc > 2) {
    3511           0 :                 if (z_len > str_len) {
    3512           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot write more than buffer size %d. Tried to write %ld", str_len, z_len);
    3513           0 :                         RETURN_FALSE;
    3514             :                 }
    3515           0 :                 if (z_len < 0) {
    3516           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Buffer size must be larger than 0, but %ld was specified", z_len);
    3517           0 :                         RETURN_FALSE;
    3518             :                 }
    3519           0 :                 len = z_len;
    3520             :         }
    3521             :         else {
    3522           1 :                 len = str_len;
    3523             :         }
    3524             : 
    3525           1 :         ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp);
    3526             : 
    3527           1 :         if ((nbytes = lo_write((PGconn *)pgsql->conn, pgsql->lofd, str, len)) == -1) {
    3528           0 :                 RETURN_FALSE;
    3529             :         }
    3530             : 
    3531           1 :         RETURN_LONG(nbytes);
    3532             : }
    3533             : /* }}} */
    3534             : 
    3535             : /* {{{ proto int pg_lo_read_all(resource large_object)
    3536             :    Read a large object and send straight to browser */
    3537           1 : PHP_FUNCTION(pg_lo_read_all)
    3538             : {
    3539             :         zval *pgsql_id;
    3540             :         int tbytes;
    3541             :         volatile int nbytes;
    3542             :         char buf[PGSQL_LO_READ_BUF_SIZE];
    3543             :         pgLofp *pgsql;
    3544             :         
    3545           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_id) == FAILURE) {
    3546           0 :                 return;
    3547             :         }
    3548             : 
    3549           1 :         ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp);
    3550             : 
    3551           1 :         tbytes = 0;
    3552           3 :         while ((nbytes = lo_read((PGconn *)pgsql->conn, pgsql->lofd, buf, PGSQL_LO_READ_BUF_SIZE))>0) {
    3553           1 :                 PHPWRITE(buf, nbytes);
    3554           1 :                 tbytes += nbytes;
    3555             :         }
    3556           1 :         RETURN_LONG(tbytes);
    3557             : }
    3558             : /* }}} */
    3559             : 
    3560             : /* {{{ proto int pg_lo_import([resource connection, ] string filename [, mixed oid])
    3561             :    Import large object direct from filesystem */
    3562           4 : PHP_FUNCTION(pg_lo_import)
    3563             : {
    3564           4 :         zval *pgsql_link = NULL, *oid = NULL;
    3565             :         char *file_in;
    3566           4 :         int id = -1, name_len;
    3567           4 :         int argc = ZEND_NUM_ARGS();
    3568             :         PGconn *pgsql;
    3569             :         Oid returned_oid;
    3570             : 
    3571           4 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3572             :                                                                  "rp|z", &pgsql_link, &file_in, &name_len, &oid) == SUCCESS) {
    3573             :                 ;
    3574             :         }
    3575           0 :         else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3576             :                                                                           "p|z", &file_in, &name_len, &oid) == SUCCESS) {
    3577           0 :                 id = PGG(default_link);
    3578           0 :                 CHECK_DEFAULT_LINK(id);
    3579             :         }
    3580             :         /* old calling convention, deprecated since PHP 4.2 */
    3581           0 :         else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3582             :                                                                           "pr", &file_in, &name_len, &pgsql_link ) == SUCCESS) {
    3583           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Old API is used");
    3584             :         }
    3585             :         else {
    3586           0 :                 WRONG_PARAM_COUNT;
    3587             :         }
    3588             :         
    3589           4 :         if (php_check_open_basedir(file_in TSRMLS_CC)) {
    3590           0 :                 RETURN_FALSE;
    3591             :         }
    3592             : 
    3593           4 :         if (pgsql_link == NULL && id == -1) {
    3594           0 :                 RETURN_FALSE;
    3595             :         }
    3596             : 
    3597           4 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    3598             : 
    3599           4 :         if (oid) {
    3600             : #ifndef HAVE_PG_LO_IMPORT_WITH_OID
    3601             :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "OID value passing not supported");
    3602             : #else
    3603             :                 Oid wanted_oid;
    3604           3 :                 switch (Z_TYPE_P(oid)) {
    3605             :                 case IS_STRING:
    3606             :                         {
    3607             :                                 char *end_ptr;
    3608           1 :                                 wanted_oid = (Oid)strtoul(Z_STRVAL_P(oid), &end_ptr, 10);
    3609           1 :                                 if ((Z_STRVAL_P(oid)+Z_STRLEN_P(oid)) != end_ptr) {
    3610             :                                 /* wrong integer format */
    3611           0 :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "invalid OID value passed");
    3612           0 :                                 RETURN_FALSE;
    3613             :                                 }
    3614             :                         }
    3615           1 :                         break;
    3616             :                 case IS_LONG:
    3617           2 :                         if (Z_LVAL_P(oid) < (long)InvalidOid) {
    3618           0 :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "invalid OID value passed");
    3619           0 :                                 RETURN_FALSE;
    3620             :                         }
    3621           2 :                         wanted_oid = (Oid)Z_LVAL_P(oid);
    3622           2 :                         break;
    3623             :                 default:
    3624           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "invalid OID value passed");
    3625           0 :                         RETURN_FALSE;
    3626             :         }
    3627             : 
    3628           3 :        returned_oid = lo_import_with_oid(pgsql, file_in, wanted_oid);
    3629             : 
    3630           3 :            if (returned_oid == InvalidOid) {
    3631           0 :                    RETURN_FALSE;
    3632             :            }
    3633             : 
    3634           3 :            PGSQL_RETURN_OID(returned_oid);
    3635             : #endif
    3636             :         }
    3637             : 
    3638           1 :         returned_oid = lo_import(pgsql, file_in);
    3639             : 
    3640           1 :         if (returned_oid == InvalidOid) {
    3641           0 :                 RETURN_FALSE;
    3642             :         }
    3643           1 :         PGSQL_RETURN_OID(returned_oid);
    3644             : }
    3645             : /* }}} */
    3646             : 
    3647             : /* {{{ proto bool pg_lo_export([resource connection, ] int objoid, string filename)
    3648             :    Export large object direct to filesystem */
    3649           1 : PHP_FUNCTION(pg_lo_export)
    3650             : {
    3651           1 :         zval *pgsql_link = NULL;
    3652             :         char *file_out, *oid_string, *end_ptr;
    3653             :         int oid_strlen;
    3654           1 :         int id = -1, name_len;
    3655             :         long oid_long;
    3656             :         Oid oid;
    3657             :         PGconn *pgsql;
    3658           1 :         int argc = ZEND_NUM_ARGS();
    3659             : 
    3660             :         /* allow string to handle large OID value correctly */
    3661           1 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3662             :                                                                  "rlp", &pgsql_link, &oid_long, &file_out, &name_len) == SUCCESS) {
    3663           0 :                 if (oid_long <= InvalidOid) {
    3664           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified");
    3665           0 :                         RETURN_FALSE;
    3666             :                 }
    3667           0 :                 oid = (Oid)oid_long;
    3668             :         }
    3669           1 :         else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3670             :                                                                  "rss", &pgsql_link, &oid_string, &oid_strlen, &file_out, &name_len) == SUCCESS) {
    3671           0 :                 oid = (Oid)strtoul(oid_string, &end_ptr, 10);
    3672           0 :                 if ((oid_string+oid_strlen) != end_ptr) {
    3673             :                         /* wrong integer format */
    3674           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed");
    3675           0 :                         RETURN_FALSE;
    3676             :                 }
    3677             :         }
    3678           1 :         else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3679             :                                                                           "lp",  &oid_long, &file_out, &name_len) == SUCCESS) {
    3680           0 :                 if (oid_long <= InvalidOid) {
    3681           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified");
    3682           0 :                         RETURN_FALSE;
    3683             :                 }
    3684           0 :                 oid = (Oid)oid_long;
    3685           0 :                 id = PGG(default_link);
    3686           0 :                 CHECK_DEFAULT_LINK(id);
    3687             :         }
    3688           1 :         else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3689             :                                                                  "sp", &oid_string, &oid_strlen, &file_out, &name_len) == SUCCESS) {
    3690           0 :                 oid = (Oid)strtoul(oid_string, &end_ptr, 10);
    3691           0 :                 if ((oid_string+oid_strlen) != end_ptr) {
    3692             :                         /* wrong integer format */
    3693           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed");
    3694           0 :                         RETURN_FALSE;
    3695             :                 }
    3696           0 :                 id = PGG(default_link);
    3697           0 :                 CHECK_DEFAULT_LINK(id);
    3698             :         }
    3699           1 :         else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3700             :                                                                  "spr", &oid_string, &oid_strlen, &file_out, &name_len, &pgsql_link) == SUCCESS) {
    3701           1 :                 oid = (Oid)strtoul(oid_string, &end_ptr, 10);
    3702           1 :                 if ((oid_string+oid_strlen) != end_ptr) {
    3703             :                         /* wrong integer format */
    3704           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Wrong OID value passed");
    3705           0 :                         RETURN_FALSE;
    3706             :                 }
    3707             :         }
    3708           0 :         else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC,
    3709             :                                                                           "lpr", &oid_long, &file_out, &name_len, &pgsql_link) == SUCCESS) {
    3710           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Old API is used");
    3711           0 :                 if (oid_long <= InvalidOid) {
    3712           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid OID specified");
    3713           0 :                         RETURN_FALSE;
    3714             :                 }
    3715           0 :                 oid = (Oid)oid_long;
    3716             :         }
    3717             :         else {
    3718           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Requires 2 or 3 arguments");
    3719           0 :                 RETURN_FALSE;
    3720             :         }
    3721             :         
    3722           1 :         if (php_check_open_basedir(file_out TSRMLS_CC)) {
    3723           0 :                 RETURN_FALSE;
    3724             :         }
    3725             : 
    3726           1 :         if (pgsql_link == NULL && id == -1) {
    3727           0 :                 RETURN_FALSE;
    3728             :         }
    3729             : 
    3730           1 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    3731             : 
    3732           1 :         if (lo_export(pgsql, oid, file_out)) {
    3733           1 :                 RETURN_TRUE;
    3734             :         }
    3735           0 :         RETURN_FALSE;
    3736             : }
    3737             : /* }}} */
    3738             : 
    3739             : /* {{{ proto bool pg_lo_seek(resource large_object, int offset [, int whence])
    3740             :    Seeks position of large object */
    3741           1 : PHP_FUNCTION(pg_lo_seek)
    3742             : {
    3743           1 :         zval *pgsql_id = NULL;
    3744           1 :         long result, offset = 0, whence = SEEK_CUR;
    3745             :         pgLofp *pgsql;
    3746           1 :         int argc = ZEND_NUM_ARGS();
    3747             : 
    3748           1 :         if (zend_parse_parameters(argc TSRMLS_CC, "rl|l", &pgsql_id, &offset, &whence) == FAILURE) {
    3749           0 :                 return;
    3750             :         }
    3751           1 :         if (whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END) {
    3752           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid whence parameter");
    3753           0 :                 return;
    3754             :         }
    3755             : 
    3756           1 :         ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp);
    3757             : 
    3758             : #if HAVE_PG_LO64
    3759             :         if (PQserverVersion((PGconn *)pgsql->conn) >= 90300) {
    3760             :                 result = lo_lseek64((PGconn *)pgsql->conn, pgsql->lofd, offset, whence);
    3761             :         } else {
    3762             :                 result = lo_lseek((PGconn *)pgsql->conn, pgsql->lofd, offset, whence);
    3763             :         }
    3764             : #else
    3765           1 :         result = lo_lseek((PGconn *)pgsql->conn, pgsql->lofd, offset, whence);
    3766             : #endif
    3767           1 :         if (result > -1) {
    3768           1 :                 RETURN_TRUE;
    3769             :         } else {
    3770           0 :                 RETURN_FALSE;
    3771             :         }
    3772             : }
    3773             : /* }}} */
    3774             : 
    3775             : /* {{{ proto int pg_lo_tell(resource large_object)
    3776             :    Returns current position of large object */
    3777           1 : PHP_FUNCTION(pg_lo_tell)
    3778             : {
    3779           1 :         zval *pgsql_id = NULL;
    3780           1 :         long offset = 0;
    3781             :         pgLofp *pgsql;
    3782           1 :         int argc = ZEND_NUM_ARGS();
    3783             : 
    3784           1 :         if (zend_parse_parameters(argc TSRMLS_CC, "r", &pgsql_id) == FAILURE) {
    3785           0 :                 return;
    3786             :         }
    3787             : 
    3788           1 :         ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp);
    3789             : 
    3790             : #if HAVE_PG_LO64
    3791             :         if (PQserverVersion((PGconn *)pgsql->conn) >= 90300) {
    3792             :                 offset = lo_tell64((PGconn *)pgsql->conn, pgsql->lofd);
    3793             :         } else {
    3794             :                 offset = lo_tell((PGconn *)pgsql->conn, pgsql->lofd);
    3795             :         }
    3796             : #else
    3797           1 :         offset = lo_tell((PGconn *)pgsql->conn, pgsql->lofd);
    3798             : #endif
    3799           1 :         RETURN_LONG(offset);
    3800             : }
    3801             : /* }}} */
    3802             : 
    3803             : #if HAVE_PG_LO_TRUNCATE
    3804             : /* {{{ proto bool pg_lo_truncate(resource large_object, int size)
    3805             :    Truncate large object to size */
    3806           0 : PHP_FUNCTION(pg_lo_truncate)
    3807             : {
    3808           0 :         zval *pgsql_id = NULL;
    3809             :         size_t size;
    3810             :         pgLofp *pgsql;
    3811           0 :         int argc = ZEND_NUM_ARGS();
    3812             :         int result;
    3813             : 
    3814           0 :         if (zend_parse_parameters(argc TSRMLS_CC, "rl", &pgsql_id, &size) == FAILURE) {
    3815           0 :                 return;
    3816             :         }
    3817             : 
    3818           0 :         ZEND_FETCH_RESOURCE(pgsql, pgLofp *, &pgsql_id, -1, "PostgreSQL large object", le_lofp);
    3819             : 
    3820             : #if HAVE_PG_LO64
    3821             :         if (PQserverVersion((PGconn *)pgsql->conn) >= 90300) {
    3822             :                 result = lo_truncate64((PGconn *)pgsql->conn, pgsql->lofd, size);
    3823             :         } else {
    3824             :                 result = lo_truncate((PGconn *)pgsql->conn, pgsql->lofd, size);
    3825             :         }
    3826             : #else
    3827           0 :         result = lo_truncate((PGconn *)pgsql->conn, pgsql->lofd, size);
    3828             : #endif
    3829           0 :         if (!result) {
    3830           0 :                 RETURN_TRUE;
    3831             :         } else {
    3832           0 :                 RETURN_FALSE;
    3833             :         }
    3834             : }
    3835             : /* }}} */
    3836             : #endif
    3837             : 
    3838             : #if HAVE_PQSETERRORVERBOSITY
    3839             : /* {{{ proto int pg_set_error_verbosity([resource connection,] int verbosity)
    3840             :    Set error verbosity */
    3841           3 : PHP_FUNCTION(pg_set_error_verbosity)
    3842             : {
    3843           3 :         zval *pgsql_link = NULL;
    3844             :         long verbosity;
    3845           3 :         int id = -1, argc = ZEND_NUM_ARGS();
    3846             :         PGconn *pgsql;
    3847             : 
    3848           3 :         if (argc == 1) {
    3849           3 :                 if (zend_parse_parameters(argc TSRMLS_CC, "l", &verbosity) == FAILURE) {
    3850           0 :                         return;
    3851             :                 }
    3852           3 :                 id = PGG(default_link);
    3853           3 :                 CHECK_DEFAULT_LINK(id);
    3854             :         } else {
    3855           0 :                 if (zend_parse_parameters(argc TSRMLS_CC, "rl", &pgsql_link, &verbosity) == FAILURE) {
    3856           0 :                         return;
    3857             :                 }
    3858             :         }
    3859             : 
    3860           3 :         if (pgsql_link == NULL && id == -1) {
    3861           0 :                 RETURN_FALSE;
    3862             :         }       
    3863             : 
    3864           3 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    3865             : 
    3866           3 :         if (verbosity & (PQERRORS_TERSE|PQERRORS_DEFAULT|PQERRORS_VERBOSE)) {
    3867           2 :                 Z_LVAL_P(return_value) = PQsetErrorVerbosity(pgsql, verbosity);
    3868           2 :                 Z_TYPE_P(return_value) = IS_LONG;
    3869             :         } else {
    3870           1 :                 RETURN_FALSE;
    3871             :         }
    3872             : }
    3873             : /* }}} */
    3874             : #endif
    3875             : 
    3876             : #ifdef HAVE_PQCLIENTENCODING
    3877             : /* {{{ proto int pg_set_client_encoding([resource connection,] string encoding)
    3878             :    Set client encoding */
    3879           1 : PHP_FUNCTION(pg_set_client_encoding)
    3880             : {
    3881             :         char *encoding;
    3882             :         int encoding_len;
    3883           1 :         zval *pgsql_link = NULL;
    3884           1 :         int id = -1, argc = ZEND_NUM_ARGS();
    3885             :         PGconn *pgsql;
    3886             : 
    3887           1 :         if (argc == 1) {
    3888           0 :                 if (zend_parse_parameters(argc TSRMLS_CC, "s", &encoding, &encoding_len) == FAILURE) {
    3889           0 :                         return;
    3890             :                 }
    3891           0 :                 id = PGG(default_link);
    3892           0 :                 CHECK_DEFAULT_LINK(id);
    3893             :         } else {
    3894           1 :                 if (zend_parse_parameters(argc TSRMLS_CC, "rs", &pgsql_link, &encoding, &encoding_len) == FAILURE) {
    3895           0 :                         return;
    3896             :                 }
    3897             :         }
    3898             : 
    3899           1 :         if (pgsql_link == NULL && id == -1) {
    3900           0 :                 RETURN_FALSE;
    3901             :         }
    3902             : 
    3903           1 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    3904             : 
    3905           1 :         Z_LVAL_P(return_value) = PQsetClientEncoding(pgsql, encoding);
    3906           1 :         Z_TYPE_P(return_value) = IS_LONG;
    3907             : }
    3908             : /* }}} */
    3909             : 
    3910             : /* {{{ proto string pg_client_encoding([resource connection])
    3911             :    Get the current client encoding */
    3912           1 : PHP_FUNCTION(pg_client_encoding)
    3913             : {
    3914           1 :         zval *pgsql_link = NULL;
    3915           1 :         int id = -1, argc = ZEND_NUM_ARGS();
    3916             :         PGconn *pgsql;
    3917             : 
    3918           1 :         if (zend_parse_parameters(argc TSRMLS_CC, "|r", &pgsql_link) == FAILURE) {
    3919           0 :                 return;
    3920             :         }
    3921             :         
    3922           1 :         if (argc == 0) {
    3923           0 :                 id = PGG(default_link);
    3924           0 :                 CHECK_DEFAULT_LINK(id);
    3925             :         }
    3926             : 
    3927           1 :         if (pgsql_link == NULL && id == -1) {
    3928           0 :                 RETURN_FALSE;
    3929             :         }       
    3930             : 
    3931           1 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    3932             : 
    3933             :         /* Just do the same as found in PostgreSQL sources... */
    3934             : 
    3935           1 :         Z_STRVAL_P(return_value) = (char *) pg_encoding_to_char(PQclientEncoding(pgsql));
    3936           1 :         Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
    3937           1 :         Z_STRVAL_P(return_value) = (char *) estrdup(Z_STRVAL_P(return_value));
    3938           1 :         Z_TYPE_P(return_value) = IS_STRING;
    3939             : }
    3940             : /* }}} */
    3941             : #endif
    3942             : 
    3943             : #if !HAVE_PQGETCOPYDATA
    3944             : #define COPYBUFSIZ      8192
    3945             : #endif
    3946             : 
    3947             : /* {{{ proto bool pg_end_copy([resource connection])
    3948             :    Sync with backend. Completes the Copy command */
    3949           0 : PHP_FUNCTION(pg_end_copy)
    3950             : {
    3951           0 :         zval *pgsql_link = NULL;
    3952           0 :         int id = -1, argc = ZEND_NUM_ARGS();
    3953             :         PGconn *pgsql;
    3954           0 :         int result = 0;
    3955             : 
    3956           0 :         if (zend_parse_parameters(argc TSRMLS_CC, "|r", &pgsql_link) == FAILURE) {
    3957           0 :                 return;
    3958             :         }
    3959             :         
    3960           0 :         if (argc == 0) {
    3961           0 :                 id = PGG(default_link);
    3962           0 :                 CHECK_DEFAULT_LINK(id);
    3963             :         }
    3964             : 
    3965           0 :         if (pgsql_link == NULL && id == -1) {
    3966           0 :                 RETURN_FALSE;
    3967             :         }
    3968             : 
    3969           0 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    3970             : 
    3971           0 :         result = PQendcopy(pgsql);
    3972             : 
    3973           0 :         if (result!=0) {
    3974           0 :                 PHP_PQ_ERROR("Query failed: %s", pgsql);
    3975           0 :                 RETURN_FALSE;
    3976             :         }
    3977           0 :         RETURN_TRUE;
    3978             : }
    3979             : /* }}} */
    3980             : 
    3981             : 
    3982             : /* {{{ proto bool pg_put_line([resource connection,] string query)
    3983             :    Send null-terminated string to backend server*/
    3984           0 : PHP_FUNCTION(pg_put_line)
    3985             : {
    3986             :         char *query;
    3987           0 :         zval *pgsql_link = NULL;
    3988           0 :         int query_len, id = -1;
    3989             :         PGconn *pgsql;
    3990           0 :         int result = 0, argc = ZEND_NUM_ARGS();
    3991             : 
    3992           0 :         if (argc == 1) {
    3993           0 :                 if (zend_parse_parameters(argc TSRMLS_CC, "s", &query, &query_len) == FAILURE) {
    3994           0 :                         return;
    3995             :                 }
    3996           0 :                 id = PGG(default_link);
    3997           0 :                 CHECK_DEFAULT_LINK(id);
    3998             :         } else {
    3999           0 :                 if (zend_parse_parameters(argc TSRMLS_CC, "rs", &pgsql_link, &query, &query_len) == FAILURE) {
    4000           0 :                         return;
    4001             :                 }
    4002             :         }
    4003             : 
    4004           0 :         if (pgsql_link == NULL && id == -1) {
    4005           0 :                 RETURN_FALSE;
    4006             :         }       
    4007             : 
    4008           0 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    4009             : 
    4010           0 :         result = PQputline(pgsql, query);
    4011           0 :         if (result==EOF) {
    4012           0 :                 PHP_PQ_ERROR("Query failed: %s", pgsql);
    4013           0 :                 RETURN_FALSE;
    4014             :         }
    4015           0 :         RETURN_TRUE;
    4016             : }
    4017             : /* }}} */
    4018             : 
    4019             : /* {{{ proto array pg_copy_to(resource connection, string table_name [, string delimiter [, string null_as]])
    4020             :    Copy table to array */
    4021           1 : PHP_FUNCTION(pg_copy_to)
    4022             : {
    4023             :         zval *pgsql_link;
    4024           1 :         char *table_name, *pg_delim = NULL, *pg_null_as = NULL;
    4025           1 :         int table_name_len, pg_delim_len, pg_null_as_len, free_pg_null = 0;
    4026             :         char *query;
    4027           1 :         int id = -1;
    4028             :         PGconn *pgsql;
    4029             :         PGresult *pgsql_result;
    4030             :         ExecStatusType status;
    4031           1 :         int copydone = 0;
    4032             : #if !HAVE_PQGETCOPYDATA
    4033             :         char copybuf[COPYBUFSIZ];
    4034             : #endif
    4035           1 :         char *csv = (char *)NULL;
    4036             :         int ret;
    4037           1 :         int argc = ZEND_NUM_ARGS();
    4038             : 
    4039           1 :         if (zend_parse_parameters(argc TSRMLS_CC, "rs|ss",
    4040             :                                                           &pgsql_link, &table_name, &table_name_len,
    4041             :                                                           &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len) == FAILURE) {
    4042           0 :                 return;
    4043             :         }
    4044           1 :         if (!pg_delim) {
    4045           1 :                 pg_delim = "\t";
    4046             :         }
    4047             : 
    4048           1 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    4049             : 
    4050           1 :         if (!pg_null_as) {
    4051           1 :                 pg_null_as = safe_estrdup("\\\\N");
    4052           1 :                 free_pg_null = 1;
    4053             :         }
    4054             : 
    4055           1 :         spprintf(&query, 0, "COPY %s TO STDOUT DELIMITERS E'%c' WITH NULL AS E'%s'", table_name, *pg_delim, pg_null_as);
    4056             : 
    4057           2 :         while ((pgsql_result = PQgetResult(pgsql))) {
    4058           0 :                 PQclear(pgsql_result);
    4059             :         }
    4060           1 :         pgsql_result = PQexec(pgsql, query);
    4061           1 :         if (free_pg_null) {
    4062           1 :                 efree(pg_null_as);
    4063             :         }
    4064           1 :         efree(query);
    4065             : 
    4066           1 :         if (pgsql_result) {
    4067           1 :                 status = PQresultStatus(pgsql_result);
    4068             :         } else {
    4069           0 :                 status = (ExecStatusType) PQstatus(pgsql);
    4070             :         }
    4071             : 
    4072           1 :         switch (status) {
    4073             :                 case PGRES_COPY_OUT:
    4074           1 :                         if (pgsql_result) {
    4075           1 :                                 PQclear(pgsql_result);
    4076           1 :                                 array_init(return_value);
    4077             : #if HAVE_PQGETCOPYDATA
    4078        1005 :                                 while (!copydone)
    4079             :                                 {
    4080        1003 :                                         ret = PQgetCopyData(pgsql, &csv, 0);
    4081        1003 :                                         switch (ret) {
    4082             :                                                 case -1:
    4083           1 :                                                         copydone = 1;
    4084           1 :                                                         break;
    4085             :                                                 case 0:
    4086             :                                                 case -2:
    4087           0 :                                                         PHP_PQ_ERROR("getline failed: %s", pgsql);
    4088           0 :                                                         RETURN_FALSE;
    4089             :                                                         break;
    4090             :                                                 default:
    4091        1002 :                                                         add_next_index_string(return_value, csv, 1);
    4092        1002 :                                                         PQfreemem(csv);
    4093             :                                                         break;
    4094             :                                         }
    4095             :                                 }
    4096             : #else
    4097             :                                 while (!copydone)
    4098             :                                 {
    4099             :                                         if ((ret = PQgetline(pgsql, copybuf, COPYBUFSIZ))) {
    4100             :                                                 PHP_PQ_ERROR("getline failed: %s", pgsql);
    4101             :                                                 RETURN_FALSE;
    4102             :                                         }
    4103             : 
    4104             :                                         if (copybuf[0] == '\\' &&
    4105             :                                                 copybuf[1] == '.' &&
    4106             :                                                 copybuf[2] == '\0')
    4107             :                                         {
    4108             :                                                 copydone = 1;
    4109             :                                         }
    4110             :                                         else
    4111             :                                         {
    4112             :                                                 if (csv == (char *)NULL) {
    4113             :                                                         csv = estrdup(copybuf);
    4114             :                                                 } else {
    4115             :                                                         csv = (char *)erealloc(csv, strlen(csv) + sizeof(char)*(COPYBUFSIZ+1));
    4116             :                                                         strcat(csv, copybuf);
    4117             :                                                 }
    4118             :                                                         
    4119             :                                                 switch (ret)
    4120             :                                                 {
    4121             :                                                         case EOF:
    4122             :                                                                 copydone = 1;
    4123             :                                                         case 0:
    4124             :                                                                 add_next_index_string(return_value, csv, 1);
    4125             :                                                                 efree(csv);
    4126             :                                                                 csv = (char *)NULL;
    4127             :                                                                 break;
    4128             :                                                         case 1:
    4129             :                                                                 break;
    4130             :                                                 }
    4131             :                                         }
    4132             :                                 }
    4133             :                                 if (PQendcopy(pgsql)) {
    4134             :                                         PHP_PQ_ERROR("endcopy failed: %s", pgsql);
    4135             :                                         RETURN_FALSE;
    4136             :                                 }
    4137             : #endif
    4138           3 :                                 while ((pgsql_result = PQgetResult(pgsql))) {
    4139           1 :                                         PQclear(pgsql_result);
    4140             :                                 }
    4141             :                         } else {
    4142           0 :                                 PQclear(pgsql_result);
    4143           0 :                                 RETURN_FALSE;
    4144             :                         }
    4145           1 :                         break;
    4146             :                 default:
    4147           0 :                         PQclear(pgsql_result);
    4148           0 :                         PHP_PQ_ERROR("Copy command failed: %s", pgsql);
    4149           0 :                         RETURN_FALSE;
    4150             :                         break;
    4151             :         }
    4152             : }
    4153             : /* }}} */
    4154             : 
    4155             : /* {{{ proto bool pg_copy_from(resource connection, string table_name , array rows [, string delimiter [, string null_as]])
    4156             :    Copy table from array */
    4157           1 : PHP_FUNCTION(pg_copy_from)
    4158             : {
    4159           1 :         zval *pgsql_link = NULL, *pg_rows;
    4160             :         zval **tmp;
    4161           1 :         char *table_name, *pg_delim = NULL, *pg_null_as = NULL;
    4162             :         int  table_name_len, pg_delim_len, pg_null_as_len;
    4163           1 :         int  pg_null_as_free = 0;
    4164             :         char *query;
    4165             :         HashPosition pos;
    4166           1 :         int id = -1;
    4167             :         PGconn *pgsql;
    4168             :         PGresult *pgsql_result;
    4169             :         ExecStatusType status;
    4170           1 :         int argc = ZEND_NUM_ARGS();
    4171             : 
    4172           1 :         if (zend_parse_parameters(argc TSRMLS_CC, "rsa|ss",
    4173             :                                                           &pgsql_link, &table_name, &table_name_len, &pg_rows,
    4174             :                                                           &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len) == FAILURE) {
    4175           0 :                 return;
    4176             :         }
    4177           1 :         if (!pg_delim) {
    4178           1 :                 pg_delim = "\t";
    4179             :         }
    4180           1 :         if (!pg_null_as) {
    4181           1 :                 pg_null_as = safe_estrdup("\\\\N");
    4182           1 :                 pg_null_as_free = 1;
    4183             :         }
    4184             : 
    4185           1 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    4186             : 
    4187           1 :         spprintf(&query, 0, "COPY %s FROM STDIN DELIMITERS E'%c' WITH NULL AS E'%s'", table_name, *pg_delim, pg_null_as);
    4188           2 :         while ((pgsql_result = PQgetResult(pgsql))) {
    4189           0 :                 PQclear(pgsql_result);
    4190             :         }
    4191           1 :         pgsql_result = PQexec(pgsql, query);
    4192             : 
    4193           1 :         if (pg_null_as_free) {
    4194           1 :                 efree(pg_null_as);
    4195             :         }
    4196           1 :         efree(query);
    4197             : 
    4198           1 :         if (pgsql_result) {
    4199           1 :                 status = PQresultStatus(pgsql_result);
    4200             :         } else {
    4201           0 :                 status = (ExecStatusType) PQstatus(pgsql);
    4202             :         }
    4203             : 
    4204           1 :         switch (status) {
    4205             :                 case PGRES_COPY_IN:
    4206           1 :                         if (pgsql_result) {
    4207           1 :                                 int command_failed = 0;
    4208           1 :                                 PQclear(pgsql_result);
    4209           1 :                                 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(pg_rows), &pos);
    4210             : #if HAVE_PQPUTCOPYDATA
    4211        1004 :                                 while (zend_hash_get_current_data_ex(Z_ARRVAL_P(pg_rows), (void **) &tmp, &pos) == SUCCESS) {
    4212        1002 :                                         convert_to_string_ex(tmp);
    4213        1002 :                                         query = (char *)emalloc(Z_STRLEN_PP(tmp) + 2);
    4214        1002 :                                         strlcpy(query, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp) + 2);
    4215        1002 :                                         if(Z_STRLEN_PP(tmp) > 0 && *(query + Z_STRLEN_PP(tmp) - 1) != '\n') {
    4216           0 :                                                 strlcat(query, "\n", Z_STRLEN_PP(tmp) + 2);
    4217             :                                         }
    4218        1002 :                                         if (PQputCopyData(pgsql, query, strlen(query)) != 1) {
    4219           0 :                                                 efree(query);
    4220           0 :                                                 PHP_PQ_ERROR("copy failed: %s", pgsql);
    4221           0 :                                                 RETURN_FALSE;
    4222             :                                         }
    4223        1002 :                                         efree(query);
    4224        1002 :                                         zend_hash_move_forward_ex(Z_ARRVAL_P(pg_rows), &pos);
    4225             :                                 }
    4226           1 :                                 if (PQputCopyEnd(pgsql, NULL) != 1) {
    4227           0 :                                         PHP_PQ_ERROR("putcopyend failed: %s", pgsql);
    4228           0 :                                         RETURN_FALSE;
    4229             :                                 }
    4230             : #else
    4231             :                                 while (zend_hash_get_current_data_ex(Z_ARRVAL_P(pg_rows), (void **) &tmp, &pos) == SUCCESS) {
    4232             :                                         convert_to_string_ex(tmp);
    4233             :                                         query = (char *)emalloc(Z_STRLEN_PP(tmp) + 2);
    4234             :                                         strlcpy(query, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp) + 2);
    4235             :                                         if(Z_STRLEN_PP(tmp) > 0 && *(query + Z_STRLEN_PP(tmp) - 1) != '\n') {
    4236             :                                                 strlcat(query, "\n", Z_STRLEN_PP(tmp) + 2);
    4237             :                                         }
    4238             :                                         if (PQputline(pgsql, query)==EOF) {
    4239             :                                                 efree(query);
    4240             :                                                 PHP_PQ_ERROR("copy failed: %s", pgsql);
    4241             :                                                 RETURN_FALSE;
    4242             :                                         }
    4243             :                                         efree(query);
    4244             :                                         zend_hash_move_forward_ex(Z_ARRVAL_P(pg_rows), &pos);
    4245             :                                 }
    4246             :                                 if (PQputline(pgsql, "\\.\n") == EOF) {
    4247             :                                         PHP_PQ_ERROR("putline failed: %s", pgsql);
    4248             :                                         RETURN_FALSE;
    4249             :                                 }
    4250             :                                 if (PQendcopy(pgsql)) {
    4251             :                                         PHP_PQ_ERROR("endcopy failed: %s", pgsql);
    4252             :                                         RETURN_FALSE;
    4253             :                                 }
    4254             : #endif
    4255           3 :                                 while ((pgsql_result = PQgetResult(pgsql))) {
    4256           1 :                                         if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
    4257           0 :                                                 PHP_PQ_ERROR("Copy command failed: %s", pgsql);
    4258           0 :                                                 command_failed = 1;
    4259             :                                         }
    4260           1 :                                         PQclear(pgsql_result);
    4261             :                                 }
    4262           1 :                                 if (command_failed) {
    4263           0 :                                         RETURN_FALSE;
    4264             :                                 }
    4265             :                         } else {
    4266           0 :                                 PQclear(pgsql_result);
    4267           0 :                                 RETURN_FALSE;
    4268             :                         }
    4269           1 :                         RETURN_TRUE;
    4270             :                         break;
    4271             :                 default:
    4272           0 :                         PQclear(pgsql_result);
    4273           0 :                         PHP_PQ_ERROR("Copy command failed: %s", pgsql);
    4274           0 :                         RETURN_FALSE;
    4275             :                         break;
    4276             :         }
    4277             : }
    4278             : /* }}} */
    4279             : 
    4280             : #ifdef HAVE_PQESCAPE
    4281             : /* {{{ proto string pg_escape_string([resource connection,] string data)
    4282             :    Escape string for text/char type */
    4283           1 : PHP_FUNCTION(pg_escape_string)
    4284             : {
    4285           1 :         char *from = NULL, *to = NULL;
    4286             :         zval *pgsql_link;
    4287             : #ifdef HAVE_PQESCAPE_CONN
    4288             :         PGconn *pgsql;
    4289             : #endif
    4290             :         int to_len;
    4291             :         int from_len;
    4292           1 :         int id = -1;
    4293             : 
    4294           1 :         switch (ZEND_NUM_ARGS()) {
    4295             :                 case 1:
    4296           1 :                         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &from, &from_len) == FAILURE) {
    4297           0 :                                 return;
    4298             :                         }
    4299           1 :                         pgsql_link = NULL;
    4300           1 :                         id = PGG(default_link);
    4301           1 :                         break;
    4302             : 
    4303             :                 default:
    4304           0 :                         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pgsql_link, &from, &from_len) == FAILURE) {
    4305           0 :                                 return;
    4306             :                         }
    4307             :                         break;
    4308             :         }
    4309             : 
    4310           1 :         to = (char *) safe_emalloc(from_len, 2, 1);
    4311             : #ifdef HAVE_PQESCAPE_CONN
    4312           1 :         if (pgsql_link != NULL || id != -1) {
    4313           0 :                 ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    4314           0 :                 to_len = (int) PQescapeStringConn(pgsql, to, from, (size_t)from_len, NULL);
    4315             :         } else
    4316             : #endif
    4317           1 :                 to_len = (int) PQescapeString(to, from, (size_t)from_len);
    4318             : 
    4319           1 :         RETURN_STRINGL(to, to_len, 0);
    4320             : }
    4321             : /* }}} */
    4322             : 
    4323             : /* {{{ proto string pg_escape_bytea([resource connection,] string data)
    4324             :    Escape binary for bytea type  */
    4325          13 : PHP_FUNCTION(pg_escape_bytea)
    4326             : {
    4327          13 :         char *from = NULL, *to = NULL;
    4328             :         size_t to_len;
    4329          13 :         int from_len, id = -1;
    4330             : #ifdef HAVE_PQESCAPE_BYTEA_CONN
    4331             :         PGconn *pgsql;
    4332             : #endif
    4333             :         zval *pgsql_link;
    4334             : 
    4335          13 :         switch (ZEND_NUM_ARGS()) {
    4336             :                 case 1:
    4337          13 :                         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &from, &from_len) == FAILURE) {
    4338           0 :                                 return;
    4339             :                         }
    4340          13 :                         pgsql_link = NULL;
    4341          13 :                         id = PGG(default_link);
    4342          13 :                         break;
    4343             : 
    4344             :                 default:
    4345           0 :                         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pgsql_link, &from, &from_len) == FAILURE) {
    4346           0 :                                 return;
    4347             :                         }
    4348             :                         break;
    4349             :         }
    4350             : 
    4351             : #ifdef HAVE_PQESCAPE_BYTEA_CONN
    4352          24 :         if (pgsql_link != NULL || id != -1) {
    4353          11 :                 ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    4354          11 :                 to = (char *)PQescapeByteaConn(pgsql, (unsigned char *)from, (size_t)from_len, &to_len);
    4355             :         } else
    4356             : #endif
    4357           2 :                 to = (char *)PQescapeBytea((unsigned char*)from, from_len, &to_len);
    4358             : 
    4359          13 :         RETVAL_STRINGL(to, to_len-1, 1); /* to_len includes addtional '\0' */
    4360          13 :         PQfreemem(to);
    4361             : }
    4362             : /* }}} */
    4363             : 
    4364             : #if !HAVE_PQUNESCAPEBYTEA
    4365             : /* PQunescapeBytea() from PostgreSQL 7.3 to provide bytea unescape feature to 7.2 users.
    4366             :    Renamed to php_pgsql_unescape_bytea() */
    4367             : /*
    4368             :  *              PQunescapeBytea - converts the null terminated string representation
    4369             :  *              of a bytea, strtext, into binary, filling a buffer. It returns a
    4370             :  *              pointer to the buffer which is NULL on error, and the size of the
    4371             :  *              buffer in retbuflen. The pointer may subsequently be used as an
    4372             :  *              argument to the function free(3). It is the reverse of PQescapeBytea.
    4373             :  *
    4374             :  *              The following transformations are reversed:
    4375             :  *              '\0' == ASCII  0 == \000
    4376             :  *              '\'' == ASCII 39 == \'
    4377             :  *              '\\' == ASCII 92 == \\
    4378             :  *
    4379             :  *              States:
    4380             :  *              0       normal          0->1->2->3->4
    4381             :  *              1       \                          1->5
    4382             :  *              2       \0                         1->6
    4383             :  *              3       \00
    4384             :  *              4       \000
    4385             :  *              5       \'
    4386             :  *              6       \\
    4387             :  */
    4388             : static unsigned char * php_pgsql_unescape_bytea(unsigned char *strtext, size_t *retbuflen)
    4389             : {
    4390             :         size_t     buflen;
    4391             :         unsigned char *buffer,
    4392             :                            *sp,
    4393             :                            *bp;
    4394             :         unsigned int state = 0;
    4395             : 
    4396             :         if (strtext == NULL)
    4397             :                 return NULL;
    4398             :         buflen = strlen(strtext);       /* will shrink, also we discover if
    4399             :                                                                  * strtext */
    4400             :         buffer = (unsigned char *) emalloc(buflen);     /* isn't NULL terminated */
    4401             :         for (bp = buffer, sp = strtext; *sp != '\0'; bp++, sp++)
    4402             :         {
    4403             :                 switch (state)
    4404             :                 {
    4405             :                         case 0:
    4406             :                                 if (*sp == '\\')
    4407             :                                         state = 1;
    4408             :                                 *bp = *sp;
    4409             :                                 break;
    4410             :                         case 1:
    4411             :                                 if (*sp == '\'')        /* state=5 */
    4412             :                                 {                               /* replace \' with 39 */
    4413             :                                         bp--;
    4414             :                                         *bp = '\'';
    4415             :                                         buflen--;
    4416             :                                         state = 0;
    4417             :                                 }
    4418             :                                 else if (*sp == '\\')   /* state=6 */
    4419             :                                 {                               /* replace \\ with 92 */
    4420             :                                         bp--;
    4421             :                                         *bp = '\\';
    4422             :                                         buflen--;
    4423             :                                         state = 0;
    4424             :                                 }
    4425             :                                 else
    4426             :                                 {
    4427             :                                         if (isdigit(*sp))
    4428             :                                                 state = 2;
    4429             :                                         else
    4430             :                                                 state = 0;
    4431             :                                         *bp = *sp;
    4432             :                                 }
    4433             :                                 break;
    4434             :                         case 2:
    4435             :                                 if (isdigit(*sp))
    4436             :                                         state = 3;
    4437             :                                 else
    4438             :                                         state = 0;
    4439             :                                 *bp = *sp;
    4440             :                                 break;
    4441             :                         case 3:
    4442             :                                 if (isdigit(*sp))               /* state=4 */
    4443             :                                 {
    4444             :                                         unsigned char *start, *end, buf[4]; /* 000 + '\0' */
    4445             :                                         
    4446             :                                         bp -= 3;
    4447             :                                         memcpy(buf, sp-2, 3);
    4448             :                                         buf[3] = '\0';
    4449             :                                         start = buf;
    4450             :                                         *bp = (unsigned char)strtoul(start, (char **)&end, 8);
    4451             :                                         buflen -= 3;
    4452             :                                         state = 0;
    4453             :                                 }
    4454             :                                 else
    4455             :                                 {
    4456             :                                         *bp = *sp;
    4457             :                                         state = 0;
    4458             :                                 }
    4459             :                                 break;
    4460             :                 }
    4461             :         }
    4462             :         buffer = erealloc(buffer, buflen+1);
    4463             :         buffer[buflen] = '\0';
    4464             : 
    4465             :         *retbuflen = buflen;
    4466             :         return buffer;
    4467             : }
    4468             : #endif
    4469             : 
    4470             : /* {{{ proto string pg_unescape_bytea(string data)
    4471             :    Unescape binary for bytea type  */
    4472           4 : PHP_FUNCTION(pg_unescape_bytea)
    4473             : {
    4474           4 :         char *from = NULL, *to = NULL, *tmp = NULL;
    4475             :         size_t to_len;
    4476             :         int from_len;
    4477           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
    4478             :                                                           &from, &from_len) == FAILURE) {
    4479           0 :                 return;
    4480             :         }
    4481             : 
    4482             : #if HAVE_PQUNESCAPEBYTEA
    4483           4 :         tmp = (char *)PQunescapeBytea((unsigned char*)from, &to_len);
    4484           4 :         to = estrndup(tmp, to_len);
    4485           4 :         PQfreemem(tmp);
    4486             : #else
    4487             :         to = (char *)php_pgsql_unescape_bytea((unsigned char*)from, &to_len);
    4488             : #endif
    4489           4 :         if (!to) {
    4490           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING,"Invalid parameter");
    4491           0 :                 RETURN_FALSE;
    4492             :         }
    4493           4 :         RETVAL_STRINGL(to, to_len, 0);
    4494             : }
    4495             : /* }}} */
    4496             : #endif
    4497             : 
    4498             : #ifdef HAVE_PQESCAPE
    4499           2 : static void php_pgsql_escape_internal(INTERNAL_FUNCTION_PARAMETERS, int escape_literal) {
    4500           2 :         char *from = NULL, *to = NULL;
    4501           2 :         zval *pgsql_link = NULL;
    4502             :         PGconn *pgsql;
    4503             :         int from_len;
    4504           2 :         int id = -1;
    4505             :         char *tmp;
    4506             : 
    4507           2 :         switch (ZEND_NUM_ARGS()) {
    4508             :                 case 1:
    4509           2 :                         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &from, &from_len) == FAILURE) {
    4510           0 :                                 return;
    4511             :                         }
    4512           2 :                         pgsql_link = NULL;
    4513           2 :                         id = PGG(default_link);
    4514           2 :                         break;
    4515             : 
    4516             :                 default:
    4517           0 :                         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pgsql_link, &from, &from_len) == FAILURE) {
    4518           0 :                                 return;
    4519             :                         }
    4520             :                         break;
    4521             :         }
    4522             : 
    4523           2 :         if (pgsql_link == NULL && id == -1) {
    4524           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING,"Cannot get default pgsql link");
    4525           0 :                 RETURN_FALSE;
    4526             :         }
    4527             : 
    4528           2 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    4529           2 :         if (pgsql == NULL) {
    4530           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING,"Cannot get pgsql link");
    4531           0 :                 RETURN_FALSE;
    4532             :         }
    4533             : 
    4534           2 :         if (escape_literal) {
    4535           1 :                 tmp = PGSQLescapeLiteral(pgsql, from, (size_t)from_len);
    4536             :         } else {
    4537           1 :                 tmp = PGSQLescapeIdentifier(pgsql, from, (size_t)from_len);
    4538             :         }
    4539           2 :         if (!tmp) {
    4540           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING,"Failed to escape");
    4541           0 :                 RETURN_FALSE;
    4542             :         }
    4543           2 :         to = estrdup(tmp);
    4544           2 :         PGSQLfree(tmp);
    4545             : 
    4546           2 :         RETURN_STRING(to, 0);
    4547             : }
    4548             : 
    4549             : /* {{{ proto string pg_escape_literal([resource connection,] string data)
    4550             :    Escape parameter as string literal (i.e. parameter)  */
    4551           1 : PHP_FUNCTION(pg_escape_literal)
    4552             : {
    4553           1 :         php_pgsql_escape_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
    4554           1 : }
    4555             : /* }}} */
    4556             : 
    4557             : /* {{{ proto string pg_escape_identifier([resource connection,] string data)
    4558             :    Escape identifier (i.e. table name, field name)      */
    4559           1 : PHP_FUNCTION(pg_escape_identifier)
    4560             : {
    4561           1 :         php_pgsql_escape_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
    4562           1 : }
    4563             : /* }}} */
    4564             : #endif
    4565             : 
    4566             : 
    4567             : /* {{{ proto string pg_result_error(resource result)
    4568             :    Get error message associated with result */
    4569           5 : PHP_FUNCTION(pg_result_error)
    4570             : {
    4571             :         zval *result;
    4572             :         PGresult *pgsql_result;
    4573             :         pgsql_result_handle *pg_result;
    4574           5 :         char *err = NULL;
    4575             : 
    4576           5 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
    4577             :                                                                  &result) == FAILURE) {
    4578           0 :                 RETURN_FALSE;
    4579             :         }
    4580             :         
    4581           5 :         ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
    4582             : 
    4583           5 :         pgsql_result = pg_result->result;
    4584           5 :         if (!pgsql_result) {
    4585           0 :                 RETURN_FALSE;
    4586             :         }
    4587           5 :         err = (char *)PQresultErrorMessage(pgsql_result);
    4588           5 :         RETURN_STRING(err,1);
    4589             : }
    4590             : /* }}} */
    4591             : 
    4592             : 
    4593             : #if HAVE_PQRESULTERRORFIELD
    4594             : /* {{{ proto string pg_result_error_field(resource result, int fieldcode)
    4595             :    Get error message field associated with result */
    4596          12 : PHP_FUNCTION(pg_result_error_field)
    4597             : {
    4598             :         zval *result;
    4599             :         long fieldcode;
    4600             :         PGresult *pgsql_result;
    4601             :         pgsql_result_handle *pg_result;
    4602          12 :         char *field = NULL;
    4603             : 
    4604          12 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "rl",
    4605             :                                                                  &result, &fieldcode) == FAILURE) {
    4606           0 :                 RETURN_FALSE;
    4607             :         }
    4608             :         
    4609          12 :         ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
    4610             : 
    4611          12 :         pgsql_result = pg_result->result;
    4612          12 :         if (!pgsql_result) {
    4613           0 :                 RETURN_FALSE;
    4614             :         }
    4615          12 :         if (fieldcode & (PG_DIAG_SEVERITY|PG_DIAG_SQLSTATE|PG_DIAG_MESSAGE_PRIMARY|PG_DIAG_MESSAGE_DETAIL
    4616             :                                 |PG_DIAG_MESSAGE_HINT|PG_DIAG_STATEMENT_POSITION
    4617             : #if PG_DIAG_INTERNAL_POSITION
    4618             :                                 |PG_DIAG_INTERNAL_POSITION
    4619             : #endif
    4620             : #if PG_DIAG_INTERNAL_QUERY
    4621             :                                 |PG_DIAG_INTERNAL_QUERY
    4622             : #endif
    4623             :                                 |PG_DIAG_CONTEXT|PG_DIAG_SOURCE_FILE|PG_DIAG_SOURCE_LINE
    4624             :                                 |PG_DIAG_SOURCE_FUNCTION)) {
    4625          12 :                 field = (char *)PQresultErrorField(pgsql_result, fieldcode);
    4626          12 :                 if (field == NULL) {
    4627          12 :                         RETURN_NULL();
    4628             :                 } else {
    4629           0 :                         RETURN_STRING(field, 1);
    4630             :                 }
    4631             :         } else {
    4632           0 :                 RETURN_FALSE;
    4633             :         }
    4634             : }
    4635             : /* }}} */
    4636             : #endif
    4637             : 
    4638             : 
    4639             : /* {{{ proto int pg_connection_status(resource connection)
    4640             :    Get connection status */
    4641           9 : PHP_FUNCTION(pg_connection_status)
    4642             : {
    4643           9 :         zval *pgsql_link = NULL;
    4644           9 :         int id = -1;
    4645             :         PGconn *pgsql;
    4646             : 
    4647           9 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
    4648             :                                                                  &pgsql_link) == FAILURE) {
    4649           0 :                 RETURN_FALSE;
    4650             :         }
    4651             : 
    4652           9 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    4653             : 
    4654           9 :         RETURN_LONG(PQstatus(pgsql));
    4655             : }
    4656             : 
    4657             : /* }}} */
    4658             : 
    4659             : 
    4660             : #if HAVE_PGTRANSACTIONSTATUS
    4661             : /* {{{ proto int pg_transaction_status(resource connection)
    4662             :    Get transaction status */
    4663           1 : PHP_FUNCTION(pg_transaction_status)
    4664             : {
    4665           1 :         zval *pgsql_link = NULL;
    4666           1 :         int id = -1;
    4667             :         PGconn *pgsql;
    4668             : 
    4669           1 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
    4670             :                                                                  &pgsql_link) == FAILURE) {
    4671           0 :                 RETURN_FALSE;
    4672             :         }
    4673             : 
    4674           1 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    4675             : 
    4676           1 :         RETURN_LONG(PQtransactionStatus(pgsql));
    4677             : }
    4678             : #endif
    4679             : 
    4680             : /* }}} */
    4681             : 
    4682             : 
    4683             : /* {{{ proto bool pg_connection_reset(resource connection)
    4684             :    Reset connection (reconnect) */
    4685           1 : PHP_FUNCTION(pg_connection_reset)
    4686             : {
    4687             :         zval *pgsql_link;
    4688           1 :         int id = -1;
    4689             :         PGconn *pgsql;
    4690             :         
    4691           1 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
    4692             :                                                                  &pgsql_link) == FAILURE) {
    4693           0 :                 RETURN_FALSE;
    4694             :         }
    4695             : 
    4696           1 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    4697             :         
    4698           1 :         PQreset(pgsql);
    4699           1 :         if (PQstatus(pgsql) == CONNECTION_BAD) {
    4700           0 :                 RETURN_FALSE;
    4701             :         }
    4702           1 :         RETURN_TRUE;
    4703             : }
    4704             : /* }}} */
    4705             : 
    4706             : 
    4707             : #define PHP_PG_ASYNC_IS_BUSY            1
    4708             : #define PHP_PG_ASYNC_REQUEST_CANCEL 2
    4709             : 
    4710             : 
    4711             : /* {{{ php_pgsql_flush_query
    4712             :  */
    4713          49 : static int php_pgsql_flush_query(PGconn *pgsql TSRMLS_DC) 
    4714             : {
    4715             :         PGresult *res;
    4716          49 :         int leftover = 0;
    4717             :         
    4718          49 :         if (PQ_SETNONBLOCKING(pgsql, 1)) {
    4719           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE,"Cannot set connection to nonblocking mode");
    4720           0 :                 return -1;
    4721             :         }
    4722          98 :         while ((res = PQgetResult(pgsql))) {
    4723           0 :                 PQclear(res);
    4724           0 :                 leftover++;
    4725             :         }
    4726          49 :         PQ_SETNONBLOCKING(pgsql, 0);
    4727          49 :         return leftover;
    4728             : }
    4729             : /* }}} */
    4730             : 
    4731             : 
    4732             : /* {{{ php_pgsql_do_async
    4733             :  */
    4734         109 : static void php_pgsql_do_async(INTERNAL_FUNCTION_PARAMETERS, int entry_type) 
    4735             : {
    4736             :         zval *pgsql_link;
    4737         109 :         int id = -1;
    4738             :         PGconn *pgsql;
    4739             :         PGresult *pgsql_result;
    4740             : 
    4741         109 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
    4742             :                                                                  &pgsql_link) == FAILURE) {
    4743           0 :                 RETURN_FALSE;
    4744             :         }
    4745             : 
    4746         109 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    4747             : 
    4748         109 :         if (PQ_SETNONBLOCKING(pgsql, 1)) {
    4749           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode");
    4750           0 :                 RETURN_FALSE;
    4751             :         }
    4752         109 :         switch(entry_type) {
    4753             :                 case PHP_PG_ASYNC_IS_BUSY:
    4754         109 :                         PQconsumeInput(pgsql);
    4755         109 :                         Z_LVAL_P(return_value) = PQisBusy(pgsql);
    4756         109 :                         Z_TYPE_P(return_value) = IS_LONG;
    4757         109 :                         break;
    4758             :                 case PHP_PG_ASYNC_REQUEST_CANCEL:
    4759           0 :                         Z_LVAL_P(return_value) = PQrequestCancel(pgsql);
    4760           0 :                         Z_TYPE_P(return_value) = IS_LONG;
    4761           0 :                         while ((pgsql_result = PQgetResult(pgsql))) {
    4762           0 :                                 PQclear(pgsql_result);
    4763             :                         }
    4764           0 :                         break;
    4765             :                 default:
    4766           0 :                         php_error_docref(NULL TSRMLS_CC, E_ERROR, "PostgreSQL module error, please report this error");
    4767             :                         break;
    4768             :         }
    4769         109 :         if (PQ_SETNONBLOCKING(pgsql, 0)) {
    4770           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode");
    4771             :         }
    4772         436 :         convert_to_boolean_ex(&return_value);
    4773             : }
    4774             : /* }}} */
    4775             : 
    4776             : /* {{{ proto bool pg_cancel_query(resource connection)
    4777             :    Cancel request */
    4778           0 : PHP_FUNCTION(pg_cancel_query)
    4779             : {
    4780           0 :         php_pgsql_do_async(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_ASYNC_REQUEST_CANCEL);
    4781           0 : }
    4782             : /* }}} */
    4783             : 
    4784             : /* {{{ proto bool pg_connection_busy(resource connection)
    4785             :    Get connection is busy or not */
    4786         109 : PHP_FUNCTION(pg_connection_busy)
    4787             : {
    4788         109 :         php_pgsql_do_async(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_PG_ASYNC_IS_BUSY);
    4789         109 : }
    4790             : /* }}} */
    4791             : 
    4792          16 : static int _php_pgsql_link_has_results(PGconn *pgsql)
    4793             : {
    4794             :         PGresult *result;
    4795          32 :         while ((result = PQgetResult(pgsql))) {
    4796           0 :                 PQclear(result);
    4797           0 :                 return 1;
    4798             :         }
    4799          16 :         return 0;
    4800             : }
    4801             : 
    4802             : /* {{{ proto bool pg_send_query(resource connection, string query)
    4803             :    Send asynchronous query */
    4804           4 : PHP_FUNCTION(pg_send_query)
    4805             : {
    4806             :         zval *pgsql_link;
    4807             :         char *query;
    4808             :         int len;
    4809           4 :         int id = -1;
    4810             :         PGconn *pgsql;
    4811             :         int is_non_blocking;
    4812             :         int ret;
    4813             : 
    4814           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &pgsql_link, &query, &len) == FAILURE) {
    4815           0 :                 return;
    4816             :         }
    4817             : 
    4818           4 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    4819             : 
    4820           4 :         is_non_blocking = PQisnonblocking(pgsql);
    4821             : 
    4822           4 :         if (is_non_blocking == 0 && PQ_SETNONBLOCKING(pgsql, 1) == -1) {
    4823           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode");
    4824           0 :                 RETURN_FALSE;
    4825             :         }
    4826             : 
    4827           4 :         if (_php_pgsql_link_has_results(pgsql)) {
    4828           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE,
    4829             :                         "There are results on this connection. Call pg_get_result() until it returns FALSE");
    4830             :         }
    4831             : 
    4832           4 :         if (is_non_blocking) {
    4833           0 :                 if (!PQsendQuery(pgsql, query)) {
    4834           0 :                         RETURN_FALSE;
    4835             :                 }
    4836           0 :                 ret = PQflush(pgsql);
    4837             :         } else {
    4838           4 :                 if (!PQsendQuery(pgsql, query)) {
    4839           0 :                         if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
    4840           0 :                                 PQreset(pgsql);
    4841             :                         }
    4842           0 :                         if (!PQsendQuery(pgsql, query)) {
    4843           0 :                                 RETURN_FALSE;
    4844             :                         }
    4845             :                 }
    4846             : 
    4847             :                 /* Wait to finish sending buffer */
    4848           8 :                 while ((ret = PQflush(pgsql))) {
    4849           0 :                         if (ret == -1) {
    4850           0 :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not empty PostgreSQL send buffer");
    4851           0 :                                 break;
    4852             :                         }
    4853           0 :                         usleep(10000);
    4854             :                 }
    4855             : 
    4856           4 :                 if (PQ_SETNONBLOCKING(pgsql, 0)) {
    4857           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode");
    4858             :                 }
    4859             :         }
    4860             : 
    4861           4 :         if (ret == 0) {
    4862           4 :                 RETURN_TRUE;
    4863           0 :         } else if (ret == -1) {
    4864           0 :                 RETURN_FALSE;
    4865             :         } else {
    4866           0 :                 RETURN_LONG(0);
    4867             :         }
    4868             : }
    4869             : /* }}} */
    4870             : 
    4871             : #if HAVE_PQSENDQUERYPARAMS
    4872             : /* {{{ proto bool pg_send_query_params(resource connection, string query, array params)
    4873             :    Send asynchronous parameterized query */
    4874           4 : PHP_FUNCTION(pg_send_query_params)
    4875             : {
    4876             :         zval *pgsql_link, *pv_param_arr, **tmp;
    4877           4 :         int num_params = 0;
    4878           4 :         char **params = NULL;
    4879             :         char *query;
    4880           4 :         int query_len, id = -1;
    4881             :         PGconn *pgsql;
    4882             :         int is_non_blocking;
    4883             :         int ret;
    4884             : 
    4885           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsa/", &pgsql_link, &query, &query_len, &pv_param_arr) == FAILURE) {
    4886           0 :                 return;
    4887             :         }
    4888             : 
    4889           4 :         if (pgsql_link == NULL && id == -1) {
    4890           0 :                 RETURN_FALSE;
    4891             :         }
    4892             : 
    4893           4 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    4894             : 
    4895           4 :         is_non_blocking = PQisnonblocking(pgsql);
    4896             : 
    4897           4 :         if (is_non_blocking == 0 && PQ_SETNONBLOCKING(pgsql, 1) == -1) {
    4898           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode");
    4899           0 :                 RETURN_FALSE;
    4900             :         }
    4901             : 
    4902           4 :         if (_php_pgsql_link_has_results(pgsql)) {
    4903           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE,
    4904             :                         "There are results on this connection. Call pg_get_result() until it returns FALSE");
    4905             :         }
    4906             : 
    4907           4 :         zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr));
    4908           4 :         num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr));
    4909           4 :         if (num_params > 0) {
    4910           4 :                 int i = 0;
    4911           4 :                 params = (char **)safe_emalloc(sizeof(char *), num_params, 0);
    4912             :                 
    4913          10 :                 for(i = 0; i < num_params; i++) {
    4914           6 :                         if (zend_hash_get_current_data(Z_ARRVAL_P(pv_param_arr), (void **) &tmp) == FAILURE) {
    4915           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error getting parameter");
    4916           0 :                                 _php_pgsql_free_params(params, num_params);
    4917           0 :                                 RETURN_FALSE;
    4918             :                         }
    4919             : 
    4920           6 :                         if (Z_TYPE_PP(tmp) == IS_NULL) {
    4921           0 :                                 params[i] = NULL;
    4922             :                         } else {
    4923           6 :                                 zval tmp_val = **tmp;
    4924             :                                 zval_copy_ctor(&tmp_val);
    4925           6 :                                 convert_to_string(&tmp_val);
    4926           6 :                                 if (Z_TYPE(tmp_val) != IS_STRING) {
    4927           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter");
    4928             :                                         zval_dtor(&tmp_val);
    4929           0 :                                         _php_pgsql_free_params(params, num_params);
    4930           0 :                                         RETURN_FALSE;
    4931             :                                 }
    4932           6 :                                 params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
    4933             :                                 zval_dtor(&tmp_val);
    4934             :                         }
    4935             : 
    4936           6 :                         zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr));
    4937             :                 }
    4938             :         }
    4939             : 
    4940           4 :         if (PQsendQueryParams(pgsql, query, num_params, NULL, (const char * const *)params, NULL, NULL, 0)) {
    4941           4 :                 _php_pgsql_free_params(params, num_params);
    4942           0 :         } else if (is_non_blocking) {
    4943           0 :                 _php_pgsql_free_params(params, num_params);
    4944           0 :                 RETURN_FALSE;
    4945             :         } else {
    4946           0 :                 if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
    4947           0 :                         PQreset(pgsql);
    4948             :                 }
    4949           0 :                 if (!PQsendQueryParams(pgsql, query, num_params, NULL, (const char * const *)params, NULL, NULL, 0)) {
    4950           0 :                         _php_pgsql_free_params(params, num_params);
    4951           0 :                         RETURN_FALSE;
    4952             :                 }
    4953             :         }
    4954             : 
    4955           4 :         if (is_non_blocking) {
    4956           0 :                 ret = PQflush(pgsql);
    4957             :         } else {
    4958             :                 /* Wait to finish sending buffer */
    4959           8 :                 while ((ret = PQflush(pgsql))) {
    4960           0 :                         if (ret == -1) {
    4961           0 :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not empty PostgreSQL send buffer");
    4962           0 :                                 break;
    4963             :                         }
    4964           0 :                         usleep(10000);
    4965             :                 }
    4966             : 
    4967           4 :                 if (PQ_SETNONBLOCKING(pgsql, 0) != 0) {
    4968           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode");
    4969             :                 }
    4970             :         }
    4971             : 
    4972           4 :         if (ret == 0) {
    4973           4 :                 RETURN_TRUE;
    4974           0 :         } else if (ret == -1) {
    4975           0 :                 RETURN_FALSE;
    4976             :         } else {
    4977           0 :                 RETURN_LONG(0);
    4978             :         }
    4979             : }
    4980             : /* }}} */
    4981             : #endif
    4982             : 
    4983             : #if HAVE_PQSENDPREPARE
    4984             : /* {{{ proto bool pg_send_prepare(resource connection, string stmtname, string query)
    4985             :    Asynchronously prepare a query for future execution */
    4986           4 : PHP_FUNCTION(pg_send_prepare)
    4987             : {
    4988             :         zval *pgsql_link;
    4989             :         char *query, *stmtname;
    4990           4 :         int stmtname_len, query_len, id = -1;
    4991             :         PGconn *pgsql;
    4992             :         int is_non_blocking;
    4993             :         int ret;
    4994             : 
    4995           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &pgsql_link, &stmtname, &stmtname_len, &query, &query_len) == FAILURE) {
    4996           0 :                 return;
    4997             :         }
    4998             : 
    4999           4 :         if (pgsql_link == NULL && id == -1) {
    5000           0 :                 RETURN_FALSE;
    5001             :         }
    5002             : 
    5003           4 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    5004             : 
    5005           4 :         is_non_blocking = PQisnonblocking(pgsql);
    5006             : 
    5007           4 :         if (is_non_blocking == 0 && PQ_SETNONBLOCKING(pgsql, 1) == -1) {
    5008           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode");
    5009           0 :                 RETURN_FALSE;
    5010             :         }
    5011             : 
    5012           4 :         if (_php_pgsql_link_has_results(pgsql)) {
    5013           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE,
    5014             :                         "There are results on this connection. Call pg_get_result() until it returns FALSE");
    5015             :         }
    5016             : 
    5017           4 :         if (!PQsendPrepare(pgsql, stmtname, query, 0, NULL)) {
    5018           0 :                 if (is_non_blocking) {
    5019           0 :                         RETURN_FALSE;
    5020             :                 } else {
    5021           0 :                         if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
    5022           0 :                                 PQreset(pgsql);
    5023             :                         }
    5024           0 :                         if (!PQsendPrepare(pgsql, stmtname, query, 0, NULL)) {
    5025           0 :                                 RETURN_FALSE;
    5026             :                         }
    5027             :                 }
    5028             :         }
    5029             : 
    5030           4 :         if (is_non_blocking) {
    5031           0 :                 ret = PQflush(pgsql);
    5032             :         } else {
    5033             :                 /* Wait to finish sending buffer */
    5034           8 :                 while ((ret = PQflush(pgsql))) {
    5035           0 :                         if (ret == -1) {
    5036           0 :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not empty PostgreSQL send buffer");
    5037           0 :                                 break;
    5038             :                         }
    5039           0 :                         usleep(10000);
    5040             :                 }
    5041           4 :                 if (PQ_SETNONBLOCKING(pgsql, 0) != 0) {
    5042           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode");
    5043             :                 }
    5044             :         }
    5045             : 
    5046           4 :         if (ret == 0) {
    5047           4 :                 RETURN_TRUE;
    5048           0 :         } else if (ret == -1) {
    5049           0 :                 RETURN_FALSE;
    5050             :         } else {
    5051           0 :                 RETURN_LONG(0);
    5052             :         }
    5053             : }
    5054             : /* }}} */
    5055             : #endif
    5056             : 
    5057             : #if HAVE_PQSENDQUERYPREPARED
    5058             : /* {{{ proto bool pg_send_execute(resource connection, string stmtname, array params)
    5059             :    Executes prevriously prepared stmtname asynchronously */
    5060           4 : PHP_FUNCTION(pg_send_execute)
    5061             : {
    5062             :         zval *pgsql_link;
    5063             :         zval *pv_param_arr, **tmp;
    5064           4 :         int num_params = 0;
    5065           4 :         char **params = NULL;
    5066             :         char *stmtname;
    5067           4 :         int stmtname_len, id = -1;
    5068             :         PGconn *pgsql;
    5069             :         int is_non_blocking;
    5070             :         int ret;
    5071             : 
    5072           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsa", &pgsql_link, &stmtname, &stmtname_len, &pv_param_arr) == FAILURE) {
    5073           0 :                 return;
    5074             :         }
    5075             : 
    5076           4 :         if (pgsql_link == NULL && id == -1) {
    5077           0 :                 RETURN_FALSE;
    5078             :         }
    5079             : 
    5080           4 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    5081             : 
    5082           4 :         is_non_blocking = PQisnonblocking(pgsql);
    5083             : 
    5084           4 :         if (is_non_blocking == 0 && PQ_SETNONBLOCKING(pgsql, 1) == -1) {
    5085           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode");
    5086           0 :                 RETURN_FALSE;
    5087             :         }
    5088             : 
    5089           4 :         if (_php_pgsql_link_has_results(pgsql)) {
    5090           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE,
    5091             :                         "There are results on this connection. Call pg_get_result() until it returns FALSE");
    5092             :         }
    5093             : 
    5094           4 :         zend_hash_internal_pointer_reset(Z_ARRVAL_P(pv_param_arr));
    5095           4 :         num_params = zend_hash_num_elements(Z_ARRVAL_P(pv_param_arr));
    5096           4 :         if (num_params > 0) {
    5097           4 :                 int i = 0;
    5098           4 :                 params = (char **)safe_emalloc(sizeof(char *), num_params, 0);
    5099             : 
    5100          10 :                 for (i = 0; i < num_params; i++) {
    5101           6 :                         if (zend_hash_get_current_data(Z_ARRVAL_P(pv_param_arr), (void **) &tmp) == FAILURE) {
    5102           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error getting parameter");
    5103           0 :                                 _php_pgsql_free_params(params, num_params);
    5104           0 :                                 RETURN_FALSE;
    5105             :                         }
    5106             : 
    5107           6 :                         if (Z_TYPE_PP(tmp) == IS_NULL) {
    5108           0 :                                 params[i] = NULL;
    5109             :                         } else {
    5110           6 :                                 zval tmp_val = **tmp;
    5111             :                                 zval_copy_ctor(&tmp_val);
    5112           6 :                                 convert_to_string(&tmp_val);
    5113           6 :                                 if (Z_TYPE(tmp_val) != IS_STRING) {
    5114           0 :                                         php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter");
    5115             :                                         zval_dtor(&tmp_val);
    5116           0 :                                         _php_pgsql_free_params(params, num_params);
    5117           0 :                                         RETURN_FALSE;
    5118             :                                 }
    5119           6 :                                 params[i] = estrndup(Z_STRVAL(tmp_val), Z_STRLEN(tmp_val));
    5120             :                                 zval_dtor(&tmp_val);
    5121             :                         }
    5122             : 
    5123           6 :                         zend_hash_move_forward(Z_ARRVAL_P(pv_param_arr));
    5124             :                 }
    5125             :         }
    5126             : 
    5127           4 :         if (PQsendQueryPrepared(pgsql, stmtname, num_params, (const char * const *)params, NULL, NULL, 0)) {
    5128           4 :                 _php_pgsql_free_params(params, num_params);
    5129           0 :         } else if (is_non_blocking) {
    5130           0 :                 _php_pgsql_free_params(params, num_params);
    5131           0 :                 RETURN_FALSE;
    5132             :         } else {
    5133           0 :                 if ((PGG(auto_reset_persistent) & 2) && PQstatus(pgsql) != CONNECTION_OK) {
    5134           0 :                         PQreset(pgsql);
    5135             :                 }
    5136           0 :                 if (!PQsendQueryPrepared(pgsql, stmtname, num_params, (const char * const *)params, NULL, NULL, 0)) {
    5137           0 :                         _php_pgsql_free_params(params, num_params);
    5138           0 :                         RETURN_FALSE;
    5139             :                 }
    5140             :         }
    5141             : 
    5142           4 :         if (is_non_blocking) {
    5143           0 :                 ret = PQflush(pgsql);
    5144             :         } else {
    5145             :                 /* Wait to finish sending buffer */
    5146           8 :                 while ((ret = PQflush(pgsql))) {
    5147           0 :                         if (ret == -1) {
    5148           0 :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not empty PostgreSQL send buffer");
    5149           0 :                                 break;
    5150             :                         }
    5151           0 :                         usleep(10000);
    5152             :                 }
    5153           4 :                 if (PQ_SETNONBLOCKING(pgsql, 0) != 0) {
    5154           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to blocking mode");
    5155             :                 }
    5156             :         }
    5157             : 
    5158           4 :         if (ret == 0) {
    5159           4 :                 RETURN_TRUE;
    5160           0 :         } else if (ret == -1) {
    5161           0 :                 RETURN_FALSE;
    5162             :         } else {
    5163           0 :                 RETURN_LONG(0);
    5164             :         }
    5165             : }
    5166             : /* }}} */
    5167             : #endif
    5168             : 
    5169             : /* {{{ proto resource pg_get_result(resource connection)
    5170             :    Get asynchronous query result */
    5171          13 : PHP_FUNCTION(pg_get_result)
    5172             : {
    5173             :         zval *pgsql_link;
    5174          13 :         int id = -1;
    5175             :         PGconn *pgsql;
    5176             :         PGresult *pgsql_result;
    5177             :         pgsql_result_handle *pg_result;
    5178             : 
    5179          13 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_link) == FAILURE) {
    5180           0 :                 RETURN_FALSE;
    5181             :         }
    5182             : 
    5183          13 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    5184             :         
    5185          13 :         pgsql_result = PQgetResult(pgsql);
    5186          13 :         if (!pgsql_result) {
    5187             :                 /* no result */
    5188           0 :                 RETURN_FALSE;
    5189             :         }
    5190          13 :         pg_result = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
    5191          13 :         pg_result->conn = pgsql;
    5192          13 :         pg_result->result = pgsql_result;
    5193          13 :         pg_result->row = 0;
    5194          13 :         ZEND_REGISTER_RESOURCE(return_value, pg_result, le_result);
    5195             : }
    5196             : /* }}} */
    5197             : 
    5198             : /* {{{ proto mixed pg_result_status(resource result[, long result_type])
    5199             :    Get status of query result */
    5200           2 : PHP_FUNCTION(pg_result_status)
    5201             : {
    5202             :         zval *result;
    5203           2 :         long result_type = PGSQL_STATUS_LONG;
    5204             :         ExecStatusType status;
    5205             :         PGresult *pgsql_result;
    5206             :         pgsql_result_handle *pg_result;
    5207             : 
    5208           2 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r|l",
    5209             :                                                                  &result, &result_type) == FAILURE) {
    5210           0 :                 RETURN_FALSE;
    5211             :         }
    5212             : 
    5213           2 :         ZEND_FETCH_RESOURCE(pg_result, pgsql_result_handle *, &result, -1, "PostgreSQL result", le_result);
    5214             : 
    5215           2 :         pgsql_result = pg_result->result;
    5216           2 :         if (result_type == PGSQL_STATUS_LONG) {
    5217           1 :                 status = PQresultStatus(pgsql_result);
    5218           1 :                 RETURN_LONG((int)status);
    5219             :         }
    5220           1 :         else if (result_type == PGSQL_STATUS_STRING) {
    5221           1 :                 RETURN_STRING(PQcmdStatus(pgsql_result), 1);
    5222             :         }
    5223             :         else {
    5224           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Optional 2nd parameter should be PGSQL_STATUS_LONG or PGSQL_STATUS_STRING");
    5225           0 :                 RETURN_FALSE;
    5226             :         }
    5227             : }
    5228             : /* }}} */
    5229             : 
    5230             : 
    5231             : /* {{{ proto array pg_get_notify([resource connection[, result_type]])
    5232             :    Get asynchronous notification */
    5233           1 : PHP_FUNCTION(pg_get_notify)
    5234             : {
    5235             :         zval *pgsql_link;
    5236           1 :         int id = -1;
    5237           1 :         long result_type = PGSQL_ASSOC;
    5238             :         PGconn *pgsql;
    5239             :         PGnotify *pgsql_notify;
    5240             : 
    5241           1 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r|l",
    5242             :                                                                  &pgsql_link, &result_type) == FAILURE) {
    5243           0 :                 RETURN_FALSE;
    5244             :         }
    5245             : 
    5246           1 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    5247             : 
    5248           1 :         if (!(result_type & PGSQL_BOTH)) {
    5249           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid result type");
    5250           0 :                 RETURN_FALSE;
    5251             :         }
    5252             : 
    5253           1 :         PQconsumeInput(pgsql);
    5254           1 :         pgsql_notify = PQnotifies(pgsql);
    5255           1 :         if (!pgsql_notify) {
    5256             :                 /* no notify message */
    5257           0 :                 RETURN_FALSE;
    5258             :         }
    5259           1 :         array_init(return_value);
    5260           1 :         if (result_type & PGSQL_NUM) {
    5261           0 :                 add_index_string(return_value, 0, pgsql_notify->relname, 1);
    5262           0 :                 add_index_long(return_value, 1, pgsql_notify->be_pid);
    5263             : #if HAVE_PQPROTOCOLVERSION && HAVE_PQPARAMETERSTATUS 
    5264           0 :                 if (PQprotocolVersion(pgsql) >= 3 && atof(PQparameterStatus(pgsql, "server_version")) >= 9.0) {
    5265             : #else 
    5266             :                 if (atof(PG_VERSION) >= 9.0) {
    5267             : #endif 
    5268             : #if HAVE_PQPARAMETERSTATUS
    5269           0 :                         add_index_string(return_value, 2, pgsql_notify->extra, 1);
    5270             : #endif
    5271             :                 }
    5272             :         }
    5273           1 :         if (result_type & PGSQL_ASSOC) {
    5274           1 :                 add_assoc_string(return_value, "message", pgsql_notify->relname, 1);
    5275           1 :                 add_assoc_long(return_value, "pid", pgsql_notify->be_pid);
    5276             : #if HAVE_PQPROTOCOLVERSION && HAVE_PQPARAMETERSTATUS 
    5277           1 :                 if (PQprotocolVersion(pgsql) >= 3 && atof(PQparameterStatus(pgsql, "server_version")) >= 9.0) {
    5278             : #else 
    5279             :                 if (atof(PG_VERSION) >= 9.0) {
    5280             : #endif 
    5281             : #if HAVE_PQPARAMETERSTATUS
    5282           0 :                         add_assoc_string(return_value, "payload", pgsql_notify->extra, 1);
    5283             : #endif
    5284             :                 }
    5285             :         }
    5286           1 :         PQfreemem(pgsql_notify);
    5287             : }
    5288             : /* }}} */
    5289             : 
    5290             : /* {{{ proto int pg_get_pid([resource connection)
    5291             :    Get backend(server) pid */
    5292           1 : PHP_FUNCTION(pg_get_pid)
    5293             : {
    5294             :         zval *pgsql_link;
    5295           1 :         int id = -1;
    5296             :         PGconn *pgsql;
    5297             : 
    5298           1 :         if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "r",
    5299             :                                                                  &pgsql_link) == FAILURE) {
    5300           0 :                 RETURN_FALSE;
    5301             :         }
    5302             : 
    5303           1 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    5304             : 
    5305           1 :         RETURN_LONG(PQbackendPID(pgsql));
    5306             : }
    5307             : /* }}} */
    5308             : 
    5309           0 : static size_t php_pgsql_fd_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
    5310             : {
    5311           0 :         return 0;
    5312             : }
    5313             : 
    5314           0 : static size_t php_pgsql_fd_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
    5315             : {
    5316           0 :         return 0;
    5317             : }
    5318             : 
    5319           4 : static int php_pgsql_fd_close(php_stream *stream, int close_handle TSRMLS_DC)
    5320             : {
    5321           4 :         return EOF;
    5322             : }
    5323             : 
    5324           4 : static int php_pgsql_fd_flush(php_stream *stream TSRMLS_DC)
    5325             : {
    5326           4 :         return FAILURE;
    5327             : }
    5328             : 
    5329           4 : static int php_pgsql_fd_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC)
    5330             : {
    5331           4 :         PGconn *pgsql = (PGconn *) stream->abstract;
    5332           4 :         switch (option) {
    5333             :                 case PHP_STREAM_OPTION_BLOCKING:
    5334           4 :                         return PQ_SETNONBLOCKING(pgsql, value);
    5335             :                 default:
    5336           0 :                         return FAILURE;
    5337             :         }
    5338             : }
    5339             : 
    5340          22 : static int php_pgsql_fd_cast(php_stream *stream, int cast_as, void **ret TSRMLS_DC)
    5341             : {
    5342          22 :         PGconn *pgsql = (PGconn *) stream->abstract;
    5343             :         int fd_number;
    5344             : 
    5345          22 :         switch (cast_as)        {
    5346             :                 case PHP_STREAM_AS_FD_FOR_SELECT:
    5347             :                 case PHP_STREAM_AS_FD:
    5348             :                 case PHP_STREAM_AS_SOCKETD:
    5349          22 :                         if (ret) {
    5350          22 :                                 fd_number = PQsocket(pgsql);
    5351          22 :                                 if (fd_number == -1) {
    5352           0 :                                         return FAILURE;
    5353             :                                 }
    5354             : 
    5355          22 :                                 *(php_socket_t *)ret = fd_number;
    5356          22 :                                 return SUCCESS;
    5357             :                         }
    5358             :                 default:
    5359           0 :                         return FAILURE;
    5360             :         }
    5361             : }
    5362             : 
    5363             : /* {{{ proto resource pg_socket(resource)
    5364             :    Get a read-only handle to the socket underlying the pgsql connection */
    5365           4 : PHP_FUNCTION(pg_socket)
    5366             : {
    5367             :         zval *pgsql_link;
    5368             :         php_stream *stream;
    5369             :         PGconn *pgsql;
    5370           4 :         int id = -1;
    5371             : 
    5372           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_link) == FAILURE) {
    5373           0 :                 return;
    5374             :         }
    5375             : 
    5376           4 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    5377             : 
    5378           4 :         stream = php_stream_alloc(&php_stream_pgsql_fd_ops, pgsql, NULL, "r");
    5379             : 
    5380           4 :         if (stream) {
    5381           4 :                 php_stream_to_zval(stream, return_value);
    5382           4 :                 return;
    5383             :         }
    5384             : 
    5385           0 :         RETURN_FALSE;
    5386             : }
    5387             : /* }}} */
    5388             : 
    5389             : /* {{{ proto bool pg_consume_input(resource)
    5390             :    Reads input on the connection */
    5391          10 : PHP_FUNCTION(pg_consume_input)
    5392             : {
    5393             :         zval *pgsql_link;
    5394          10 :         int id = -1;
    5395             :         PGconn *pgsql;
    5396             : 
    5397          10 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_link) == FAILURE) {
    5398           0 :                 return;
    5399             :         }
    5400             : 
    5401          10 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    5402             : 
    5403          10 :         RETURN_BOOL(PQconsumeInput(pgsql));
    5404             : }
    5405             : /* }}} */
    5406             : 
    5407             : /* {{{ proto mixed pg_flush(resource)
    5408             :    Flush outbound query data on the connection */
    5409           0 : PHP_FUNCTION(pg_flush)
    5410             : {
    5411             :         zval *pgsql_link;
    5412           0 :         int id = -1;
    5413             :         PGconn *pgsql;
    5414             :         int ret;
    5415             :         int is_non_blocking;
    5416             : 
    5417           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &pgsql_link) == FAILURE) {
    5418           0 :                 return;
    5419             :         }
    5420             : 
    5421           0 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    5422             : 
    5423           0 :         is_non_blocking = PQisnonblocking(pgsql);
    5424             : 
    5425           0 :         if (is_non_blocking == 0 && PQ_SETNONBLOCKING(pgsql, 1) == -1) {
    5426           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Cannot set connection to nonblocking mode");
    5427           0 :                 RETURN_FALSE;
    5428             :         }
    5429             : 
    5430           0 :         ret = PQflush(pgsql);
    5431             : 
    5432           0 :         if (is_non_blocking == 0 && PQ_SETNONBLOCKING(pgsql, 0) == -1) {
    5433           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Failed resetting connection to blocking mode");
    5434             :         }
    5435             : 
    5436           0 :         switch (ret) {
    5437           0 :                 case 0: RETURN_TRUE; break;
    5438           0 :                 case 1: RETURN_LONG(0); break;
    5439           0 :                 default: RETURN_FALSE;
    5440             :         }
    5441             : }
    5442             : /* }}} */
    5443             : 
    5444             : /* {{{ php_pgsql_meta_data
    5445             :  * TODO: Add meta_data cache for better performance
    5446             :  */
    5447          58 : PHP_PGSQL_API int php_pgsql_meta_data(PGconn *pg_link, const char *table_name, zval *meta, zend_bool extended TSRMLS_DC)
    5448             : {
    5449             :         PGresult *pg_result;
    5450          58 :         char *src, *tmp_name, *tmp_name2 = NULL;
    5451             :         char *escaped;
    5452          58 :         smart_str querystr = {0};
    5453             :         size_t new_len;
    5454             :         int i, num_rows;
    5455             :         zval *elem;
    5456             : 
    5457          58 :         if (!*table_name) {
    5458           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "The table name must be specified");
    5459           0 :                 return FAILURE;
    5460             :         }
    5461             : 
    5462          58 :         src = estrdup(table_name);
    5463          58 :         tmp_name = php_strtok_r(src, ".", &tmp_name2);
    5464             :         
    5465          58 :         if (!tmp_name2 || !*tmp_name2) {
    5466             :                 /* Default schema */
    5467          38 :                 tmp_name2 = tmp_name;
    5468          38 :                 tmp_name = "public";
    5469             :         }
    5470             : 
    5471          58 :         if (extended) {
    5472           1 :                 smart_str_appends(&querystr,
    5473             :                                                   "SELECT a.attname, a.attnum, t.typname, a.attlen, a.attnotNULL, a.atthasdef, a.attndims, t.typtype, "
    5474             :                                                   "d.description "
    5475             :                                                   "FROM pg_class as c "
    5476             :                                                   " JOIN pg_attribute a ON (a.attrelid = c.oid) "
    5477             :                                                   " JOIN pg_type t ON (a.atttypid = t.oid) "
    5478             :                                                   " JOIN pg_namespace n ON (c.relnamespace = n.oid) "
    5479             :                                                   " LEFT JOIN pg_description d ON (d.objoid=a.attrelid AND d.objsubid=a.attnum AND c.oid=d.objoid) "
    5480             :                                                   "WHERE a.attnum > 0  AND c.relname = '");
    5481             :         } else {
    5482          57 :                 smart_str_appends(&querystr,
    5483             :                                                   "SELECT a.attname, a.attnum, t.typname, a.attlen, a.attnotnull, a.atthasdef, a.attndims, t.typtype "
    5484             :                                                   "FROM pg_class as c "
    5485             :                                                   " JOIN pg_attribute a ON (a.attrelid = c.oid) "
    5486             :                                                   " JOIN pg_type t ON (a.atttypid = t.oid) "
    5487             :                                                   " JOIN pg_namespace n ON (c.relnamespace = n.oid) "
    5488             :                                                   "WHERE a.attnum > 0 AND c.relname = '");
    5489             :         }
    5490          58 :         escaped = (char *)safe_emalloc(strlen(tmp_name2), 2, 1);
    5491          58 :         new_len = PQescapeStringConn(pg_link, escaped, tmp_name2, strlen(tmp_name2), NULL);
    5492          58 :         if (new_len) {
    5493          58 :                 smart_str_appendl(&querystr, escaped, new_len);
    5494             :         }
    5495          58 :         efree(escaped);
    5496             : 
    5497          58 :         smart_str_appends(&querystr, "' AND n.nspname = '");
    5498          58 :         escaped = (char *)safe_emalloc(strlen(tmp_name), 2, 1);
    5499          58 :         new_len = PQescapeStringConn(pg_link, escaped, tmp_name, strlen(tmp_name), NULL);
    5500          58 :         if (new_len) {
    5501          58 :                 smart_str_appendl(&querystr, escaped, new_len);
    5502             :         }
    5503          58 :         efree(escaped);
    5504             : 
    5505          58 :         smart_str_appends(&querystr, "' ORDER BY a.attnum;");
    5506          58 :         smart_str_0(&querystr);
    5507          58 :         efree(src);
    5508             : 
    5509          58 :         pg_result = PQexec(pg_link, querystr.c);
    5510          58 :         if (PQresultStatus(pg_result) != PGRES_TUPLES_OK || (num_rows = PQntuples(pg_result)) == 0) {
    5511           4 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Table '%s' doesn't exists", table_name);
    5512           4 :                 smart_str_free(&querystr);
    5513           4 :                 PQclear(pg_result);
    5514           4 :                 return FAILURE;
    5515             :         }
    5516          54 :         smart_str_free(&querystr);
    5517             : 
    5518         173 :         for (i = 0; i < num_rows; i++) {
    5519             :                 char *name;
    5520         119 :                 MAKE_STD_ZVAL(elem);
    5521         119 :                 array_init(elem);
    5522             :                 /* pg_attribute.attnum */
    5523         119 :                 add_assoc_long(elem, "num", atoi(PQgetvalue(pg_result,i,1)));
    5524             :                 /* pg_type.typname */
    5525         119 :                 add_assoc_string(elem, "type", PQgetvalue(pg_result,i,2), 1);
    5526             :                 /* pg_attribute.attlen */
    5527         119 :                 add_assoc_long(elem, "len", atoi(PQgetvalue(pg_result,i,3)));
    5528             :                 /* pg_attribute.attnonull */
    5529         119 :                 add_assoc_bool(elem, "not null", !strcmp(PQgetvalue(pg_result,i,4), "t"));
    5530             :                 /* pg_attribute.atthasdef */
    5531         119 :                 add_assoc_bool(elem, "has default", !strcmp(PQgetvalue(pg_result,i,5), "t"));
    5532             :                 /* pg_attribute.attndims */
    5533         119 :                 add_assoc_long(elem, "array dims", atoi(PQgetvalue(pg_result,i,6)));
    5534             :                 /* pg_type.typtype */
    5535         119 :                 add_assoc_bool(elem, "is enum", !strcmp(PQgetvalue(pg_result,i,7), "e"));
    5536         119 :                 if (extended) {
    5537             :                         /* pg_type.typtype */
    5538           2 :                         add_assoc_bool(elem, "is base", !strcmp(PQgetvalue(pg_result,i,7), "b"));
    5539           2 :                         add_assoc_bool(elem, "is composite", !strcmp(PQgetvalue(pg_result,i,7), "c"));
    5540           2 :                         add_assoc_bool(elem, "is pesudo", !strcmp(PQgetvalue(pg_result,i,7), "p"));
    5541             :                         /* pg_description.description */
    5542           2 :                         add_assoc_string(elem, "description", PQgetvalue(pg_result,i,8), 1);
    5543             :                 }
    5544             :                 /* pg_attribute.attname */
    5545         119 :                 name = PQgetvalue(pg_result,i,0);
    5546         119 :                 add_assoc_zval(meta, name, elem);
    5547             :         }
    5548          54 :         PQclear(pg_result);
    5549             :         
    5550          54 :         return SUCCESS;
    5551             : }
    5552             : 
    5553             : /* }}} */
    5554             : 
    5555             : 
    5556             : /* {{{ proto array pg_meta_data(resource db, string table [, bool extended])
    5557             :    Get meta_data */
    5558           4 : PHP_FUNCTION(pg_meta_data)
    5559             : {
    5560             :         zval *pgsql_link;
    5561             :         char *table_name;
    5562             :         uint table_name_len;
    5563           4 :         zend_bool extended=0;
    5564             :         PGconn *pgsql;
    5565           4 :         int id = -1;
    5566             : 
    5567           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|b",
    5568             :                                                           &pgsql_link, &table_name, &table_name_len, &extended) == FAILURE) {
    5569           0 :                 return;
    5570             :         }
    5571             : 
    5572           4 :         ZEND_FETCH_RESOURCE2(pgsql, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    5573             : 
    5574           4 :         array_init(return_value);
    5575           4 :         if (php_pgsql_meta_data(pgsql, table_name, return_value, extended TSRMLS_CC) == FAILURE) {
    5576             :                 zval_dtor(return_value); /* destroy array */
    5577           0 :                 RETURN_FALSE;
    5578             :         }
    5579             : }
    5580             : /* }}} */
    5581             : 
    5582             : 
    5583             : /* {{{ php_pgsql_get_data_type
    5584             :  */
    5585          81 : static php_pgsql_data_type php_pgsql_get_data_type(const char *type_name, size_t len)
    5586             : {
    5587             :         /* This is stupid way to do. I'll fix it when I decied how to support
    5588             :            user defined types. (Yasuo) */
    5589             : 
    5590             :         /* boolean */
    5591          81 :         if (!strcmp(type_name, "bool")|| !strcmp(type_name, "boolean"))
    5592           0 :                 return PG_BOOL;
    5593             :         /* object id */
    5594          81 :         if (!strcmp(type_name, "oid"))
    5595           0 :                 return PG_OID;
    5596             :         /* integer */
    5597          81 :         if (!strcmp(type_name, "int2") || !strcmp(type_name, "smallint"))
    5598           0 :                 return PG_INT2;
    5599          81 :         if (!strcmp(type_name, "int4") || !strcmp(type_name, "integer"))
    5600          69 :                 return PG_INT4;
    5601          12 :         if (!strcmp(type_name, "int8") || !strcmp(type_name, "bigint"))
    5602           0 :                 return PG_INT8;
    5603             :         /* real and other */
    5604          12 :         if (!strcmp(type_name, "float4") || !strcmp(type_name, "real"))
    5605           0 :                 return PG_FLOAT4;
    5606          12 :         if (!strcmp(type_name, "float8") || !strcmp(type_name, "double precision"))
    5607           0 :                 return PG_FLOAT8;
    5608          12 :         if (!strcmp(type_name, "numeric"))
    5609           0 :                 return PG_NUMERIC;
    5610          12 :         if (!strcmp(type_name, "money"))
    5611           0 :                 return PG_MONEY;
    5612             :         /* character */
    5613          12 :         if (!strcmp(type_name, "text"))
    5614           5 :                 return PG_TEXT;
    5615           7 :         if (!strcmp(type_name, "bpchar") || !strcmp(type_name, "character"))
    5616           0 :                 return PG_CHAR;
    5617           7 :         if (!strcmp(type_name, "varchar") || !strcmp(type_name, "character varying"))
    5618           0 :                 return PG_VARCHAR;
    5619             :         /* time and interval */
    5620           7 :         if (!strcmp(type_name, "abstime"))
    5621           0 :                 return PG_UNIX_TIME;
    5622           7 :         if (!strcmp(type_name, "reltime"))
    5623           0 :                 return PG_UNIX_TIME_INTERVAL;
    5624           7 :         if (!strcmp(type_name, "tinterval"))
    5625           0 :                 return PG_UNIX_TIME_INTERVAL;
    5626           7 :         if (!strcmp(type_name, "date"))
    5627           0 :                 return PG_DATE;
    5628           7 :         if (!strcmp(type_name, "time"))
    5629           0 :                 return PG_TIME;
    5630           7 :         if (!strcmp(type_name, "time with time zone") || !strcmp(type_name, "timetz"))
    5631           0 :                 return PG_TIME_WITH_TIMEZONE;
    5632           7 :         if (!strcmp(type_name, "timestamp without time zone") || !strcmp(type_name, "timestamp"))
    5633           2 :                 return PG_TIMESTAMP;
    5634           5 :         if (!strcmp(type_name, "timestamp with time zone") || !strcmp(type_name, "timestamptz"))
    5635           0 :                 return PG_TIMESTAMP_WITH_TIMEZONE;
    5636           5 :         if (!strcmp(type_name, "interval"))
    5637           0 :                 return PG_INTERVAL;
    5638             :         /* binary */
    5639           5 :         if (!strcmp(type_name, "bytea"))
    5640           5 :                 return PG_BYTEA;
    5641             :         /* network */
    5642           0 :         if (!strcmp(type_name, "cidr"))
    5643           0 :                 return PG_CIDR;
    5644           0 :         if (!strcmp(type_name, "inet"))
    5645           0 :                 return PG_INET;
    5646           0 :         if (!strcmp(type_name, "macaddr"))
    5647           0 :                 return PG_MACADDR;
    5648             :         /* bit */
    5649           0 :         if (!strcmp(type_name, "bit"))
    5650           0 :                 return PG_BIT;
    5651           0 :         if (!strcmp(type_name, "bit varying"))
    5652           0 :                 return PG_VARBIT;
    5653             :         /* geometric */
    5654           0 :         if (!strcmp(type_name, "line"))
    5655           0 :                 return PG_LINE;
    5656           0 :         if (!strcmp(type_name, "lseg"))
    5657           0 :                 return PG_LSEG;
    5658           0 :         if (!strcmp(type_name, "box"))
    5659           0 :                 return PG_BOX;
    5660           0 :         if (!strcmp(type_name, "path"))
    5661           0 :                 return PG_PATH;
    5662           0 :         if (!strcmp(type_name, "point"))
    5663           0 :                 return PG_POINT;
    5664           0 :         if (!strcmp(type_name, "polygon"))
    5665           0 :                 return PG_POLYGON;
    5666           0 :         if (!strcmp(type_name, "circle"))
    5667           0 :                 return PG_CIRCLE;
    5668             : 
    5669           0 :         return PG_UNKNOWN;
    5670             : }
    5671             : /* }}} */
    5672             : 
    5673             : /* {{{ php_pgsql_convert_match
    5674             :  * test field value with regular expression specified.
    5675             :  */
    5676          11 : static int php_pgsql_convert_match(const char *str, size_t str_len, const char *regex , int icase TSRMLS_DC)
    5677             : {
    5678             :         regex_t re;
    5679             :         regmatch_t *subs;
    5680          11 :         int regopt = REG_EXTENDED;
    5681          11 :         int regerr, ret = SUCCESS;
    5682             :         int i;
    5683             : 
    5684             :         /* Check invalid chars for POSIX regex */
    5685          55 :         for (i = 0; i < str_len; i++) {
    5686         132 :                 if (str[i] == '\n' ||
    5687          44 :                         str[i] == '\r' ||
    5688          44 :                         str[i] == '\0' ) {
    5689           0 :                         return FAILURE;
    5690             :                 }
    5691             :         }
    5692             : 
    5693          11 :         if (icase) {
    5694           0 :                 regopt |= REG_ICASE;
    5695             :         }
    5696             : 
    5697          11 :         regerr = regcomp(&re, regex, regopt);
    5698          11 :         if (regerr) {
    5699           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot compile regex");
    5700           0 :                 regfree(&re);
    5701           0 :                 return FAILURE;
    5702             :         }
    5703          11 :         subs = (regmatch_t *)ecalloc(sizeof(regmatch_t), re.re_nsub+1);
    5704             : 
    5705          11 :         regerr = regexec(&re, str, re.re_nsub+1, subs, 0);
    5706          11 :         if (regerr == REG_NOMATCH) {
    5707             : #ifdef PHP_DEBUG
    5708           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "'%s' does not match with '%s'", str, regex);
    5709             : #endif
    5710           0 :                 ret = FAILURE;
    5711             :         }
    5712          11 :         else if (regerr) {
    5713           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot exec regex");
    5714           0 :                 ret = FAILURE;
    5715             :         }
    5716          11 :         regfree(&re);
    5717          11 :         efree(subs);
    5718          11 :         return ret;
    5719             : }
    5720             : 
    5721             : /* }}} */
    5722             : 
    5723             : /* {{{ php_pgsql_add_quote
    5724             :  * add quotes around string.
    5725             :  */
    5726          11 : static int php_pgsql_add_quotes(zval *src, zend_bool should_free TSRMLS_DC) 
    5727             : {
    5728          11 :         smart_str str = {0};
    5729             : 
    5730             :         assert(Z_TYPE_P(src) == IS_STRING);
    5731             :         assert(should_free == 1 || should_free == 0);
    5732             : 
    5733          11 :         smart_str_appendc(&str, 'E');
    5734          11 :         smart_str_appendc(&str, '\'');
    5735          11 :         smart_str_appendl(&str, Z_STRVAL_P(src), Z_STRLEN_P(src));
    5736          11 :         smart_str_appendc(&str, '\'');
    5737          11 :         smart_str_0(&str);
    5738             : 
    5739          11 :         if (should_free) {
    5740          11 :                 efree(Z_STRVAL_P(src));
    5741             :         }
    5742          11 :         Z_STRVAL_P(src) = str.c;
    5743          11 :         Z_STRLEN_P(src) = str.len;
    5744             : 
    5745          11 :         return SUCCESS;
    5746             : }
    5747             : /* }}} */
    5748             : 
    5749             : #define PGSQL_CONV_CHECK_IGNORE() \
    5750             :                                 if (!err && Z_TYPE_P(new_val) == IS_STRING && !strcmp(Z_STRVAL_P(new_val), "NULL")) { \
    5751             :                                         /* if new_value is string "NULL" and field has default value, remove element to use default value */ \
    5752             :                                         if (!(opt & PGSQL_CONV_IGNORE_DEFAULT) && Z_BVAL_PP(has_default)) { \
    5753             :                                                 zval_dtor(new_val); \
    5754             :                                                 FREE_ZVAL(new_val); \
    5755             :                                                 skip_field = 1; \
    5756             :                                         } \
    5757             :                                         /* raise error if it's not null and cannot be ignored */ \
    5758             :                                         else if (!(opt & PGSQL_CONV_IGNORE_NOT_NULL) && Z_BVAL_PP(not_null)) { \
    5759             :                                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected NULL for 'NOT NULL' field '%s'", field ); \
    5760             :                                                 err = 1; \
    5761             :                                         } \
    5762             :                                 }
    5763             : 
    5764             : /* {{{ php_pgsql_convert
    5765             :  * check and convert array values (fieldname=>vlaue pair) for sql
    5766             :  */
    5767          54 : PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, const zval *values, zval *result, ulong opt TSRMLS_DC) 
    5768             : {
    5769             :         HashPosition pos;
    5770          54 :         char *field = NULL;
    5771          54 :         uint field_len = -1;
    5772          54 :         ulong num_idx = -1;
    5773             :         zval *meta, **def, **type, **not_null, **has_default, **is_enum, **val, *new_val;
    5774          54 :         int key_type, err = 0, skip_field;
    5775             :         php_pgsql_data_type data_type;
    5776             : 
    5777             :         assert(pg_link != NULL);
    5778             :         assert(Z_TYPE_P(values) == IS_ARRAY);
    5779             :         assert(Z_TYPE_P(result) == IS_ARRAY);
    5780             :         assert(!(opt & ~PGSQL_CONV_OPTS));
    5781             : 
    5782          54 :         if (!table_name) {
    5783           0 :                 return FAILURE;
    5784             :         }
    5785          54 :         MAKE_STD_ZVAL(meta);
    5786          54 :         array_init(meta);
    5787             : 
    5788             : /* table_name is escaped by php_pgsql_meta_data */
    5789          54 :         if (php_pgsql_meta_data(pg_link, table_name, meta, 0 TSRMLS_CC) == FAILURE) {
    5790             :                 zval_dtor(meta);
    5791           4 :                 FREE_ZVAL(meta);
    5792           4 :                 return FAILURE;
    5793             :         }
    5794         182 :         for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(values), &pos);
    5795         132 :                  zend_hash_get_current_data_ex(Z_ARRVAL_P(values), (void **)&val, &pos) == SUCCESS;
    5796          82 :                  zend_hash_move_forward_ex(Z_ARRVAL_P(values), &pos)) {
    5797          83 :                 skip_field = 0;
    5798          83 :                 new_val = NULL;
    5799             : 
    5800          83 :                 if ((key_type = zend_hash_get_current_key_ex(Z_ARRVAL_P(values), &field, &field_len, &num_idx, 0, &pos)) == HASH_KEY_NON_EXISTENT) {
    5801           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to get array key type");
    5802           0 :                         err = 1;
    5803             :                 }
    5804          83 :                 if (!err && key_type == HASH_KEY_IS_LONG) {
    5805           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Accepts only string key for values");
    5806           0 :                         err = 1;
    5807             :                 }
    5808          83 :                 if (!err && key_type == HASH_KEY_NON_EXISTENT) {
    5809           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "Accepts only string key for values");
    5810           0 :                         err = 1;
    5811             :                 }
    5812          83 :                 if (!err && zend_hash_find(Z_ARRVAL_P(meta), field, field_len, (void **)&def) == FAILURE) {
    5813           1 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Invalid field name (%s) in values", field);
    5814           1 :                         err = 1;
    5815             :                 }
    5816          83 :                 if (!err && zend_hash_find(Z_ARRVAL_PP(def), "type", sizeof("type"), (void **)&type) == FAILURE) {
    5817           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected broken meta data. Missing 'type'");
    5818           0 :                         err = 1;
    5819             :                 }
    5820          83 :                 if (!err && zend_hash_find(Z_ARRVAL_PP(def), "not null", sizeof("not null"), (void **)&not_null) == FAILURE) {
    5821           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected broken meta data. Missing 'not null'");
    5822           0 :                         err = 1;
    5823             :                 }
    5824          83 :                 if (!err && zend_hash_find(Z_ARRVAL_PP(def), "has default", sizeof("has default"), (void **)&has_default) == FAILURE) {
    5825           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected broken meta data. Missing 'has default'");
    5826           0 :                         err = 1;
    5827             :                 }
    5828          83 :                 if (!err && zend_hash_find(Z_ARRVAL_PP(def), "is enum", sizeof("is enum"), (void **)&is_enum) == FAILURE) {
    5829           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected broken meta data. Missing 'is enum'");
    5830           0 :                         err = 1;
    5831             :                 }
    5832          83 :                 if (!err && (Z_TYPE_PP(val) == IS_ARRAY || Z_TYPE_PP(val) == IS_OBJECT)) {
    5833           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects scalar values as field values");
    5834           0 :                         err = 1;
    5835             :                 }
    5836          83 :                 if (err) {
    5837           1 :                         break; /* break out for() */
    5838             :                 }
    5839          82 :                 ALLOC_INIT_ZVAL(new_val);
    5840             : 
    5841          82 :                 if (Z_BVAL_PP(is_enum)) {
    5842             :                         /* enums need to be treated like strings */
    5843           1 :                         data_type = PG_TEXT;
    5844             :                 }
    5845             :                 else {
    5846          81 :                         data_type = php_pgsql_get_data_type(Z_STRVAL_PP(type), Z_STRLEN_PP(type));
    5847             :                 }
    5848             : 
    5849          82 :                 switch(data_type)
    5850             :                 {
    5851             :                         case PG_BOOL:
    5852           0 :                                 switch (Z_TYPE_PP(val)) {
    5853             :                                         case IS_STRING:
    5854           0 :                                                 if (Z_STRLEN_PP(val) == 0) {
    5855           0 :                                                         ZVAL_STRING(new_val, "NULL", 1);
    5856             :                                                 }
    5857             :                                                 else {
    5858           0 :                                                         if (!strcmp(Z_STRVAL_PP(val), "t") || !strcmp(Z_STRVAL_PP(val), "T") ||
    5859           0 :                                                                 !strcmp(Z_STRVAL_PP(val), "y") || !strcmp(Z_STRVAL_PP(val), "Y") ||
    5860           0 :                                                                 !strcmp(Z_STRVAL_PP(val), "true") || !strcmp(Z_STRVAL_PP(val), "True") ||
    5861           0 :                                                                 !strcmp(Z_STRVAL_PP(val), "yes") || !strcmp(Z_STRVAL_PP(val), "Yes") ||
    5862           0 :                                                                 !strcmp(Z_STRVAL_PP(val), "1")) {
    5863           0 :                                                                 ZVAL_STRINGL(new_val, "'t'", sizeof("'t'")-1, 1);
    5864             :                                                         }
    5865           0 :                                                         else if (!strcmp(Z_STRVAL_PP(val), "f") || !strcmp(Z_STRVAL_PP(val), "F") ||
    5866           0 :                                                                          !strcmp(Z_STRVAL_PP(val), "n") || !strcmp(Z_STRVAL_PP(val), "N") ||
    5867           0 :                                                                          !strcmp(Z_STRVAL_PP(val), "false") ||  !strcmp(Z_STRVAL_PP(val), "False") ||
    5868           0 :                                                                          !strcmp(Z_STRVAL_PP(val), "no") ||  !strcmp(Z_STRVAL_PP(val), "No") ||
    5869           0 :                                                                          !strcmp(Z_STRVAL_PP(val), "0")) {
    5870           0 :                                                                 ZVAL_STRINGL(new_val, "'f'", sizeof("'f'")-1, 1);
    5871             :                                                         }
    5872             :                                                         else {
    5873           0 :                                                                 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);
    5874           0 :                                                                 err = 1;
    5875             :                                                         }
    5876             :                                                 }
    5877           0 :                                                 break;
    5878             : 
    5879             :                                         case IS_LONG:
    5880             :                                         case IS_BOOL:
    5881           0 :                                                 if (Z_LVAL_PP(val)) {
    5882           0 :                                                         ZVAL_STRINGL(new_val, "'t'", sizeof("'t'")-1, 1);
    5883             :                                                 }
    5884             :                                                 else {
    5885           0 :                                                         ZVAL_STRINGL(new_val, "'f'", sizeof("'f'")-1, 1);
    5886             :                                                 }
    5887           0 :                                                 break;
    5888             : 
    5889             :                                         case IS_NULL:
    5890           0 :                                                 ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    5891           0 :                                                 break;
    5892             : 
    5893             :                                         default:
    5894           0 :                                                 err = 1;
    5895             :                                 }
    5896           0 :                                 PGSQL_CONV_CHECK_IGNORE();
    5897           0 :                                 if (err) {
    5898           0 :                                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects string, null, long or boolelan value for PostgreSQL '%s' (%s)", Z_STRVAL_PP(type), field);
    5899             :                                 }
    5900           0 :                                 break;
    5901             : 
    5902             :                         case PG_OID:
    5903             :                         case PG_INT2:
    5904             :                         case PG_INT4:
    5905             :                         case PG_INT8:
    5906          69 :                                 switch (Z_TYPE_PP(val)) {
    5907             :                                         case IS_STRING:
    5908          12 :                                                 if (Z_STRLEN_PP(val) == 0) {
    5909           1 :                                                         ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    5910             :                                                 }
    5911             :                                                 else {
    5912             :                                                         /* FIXME: better regex must be used */
    5913          11 :                                                         if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val), "^([+-]{0,1}[0-9]+)$", 0 TSRMLS_CC) == FAILURE) {
    5914           0 :                                                                 err = 1;
    5915             :                                                         }
    5916             :                                                         else {
    5917          11 :                                                                 ZVAL_STRINGL(new_val, Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1);
    5918             :                                                         }
    5919             :                                                 }
    5920          12 :                                                 break;
    5921             : 
    5922             :                                         case IS_DOUBLE:
    5923           0 :                                                 ZVAL_DOUBLE(new_val, Z_DVAL_PP(val));
    5924           0 :                                                 convert_to_long_ex(&new_val);
    5925           0 :                                                 break;
    5926             :                                                 
    5927             :                                         case IS_LONG:
    5928          53 :                                                 ZVAL_LONG(new_val, Z_LVAL_PP(val));
    5929          53 :                                                 break;
    5930             :                                                 
    5931             :                                         case IS_NULL:
    5932           4 :                                                 ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    5933           4 :                                                 break;
    5934             : 
    5935             :                                         default:
    5936           0 :                                                 err = 1;
    5937             :                                 }
    5938          69 :                                 PGSQL_CONV_CHECK_IGNORE();
    5939          69 :                                 if (err) {
    5940           0 :                                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for pgsql '%s' (%s)", Z_STRVAL_PP(type), field);
    5941             :                                 }
    5942          69 :                                 break;
    5943             : 
    5944             :                         case PG_NUMERIC:
    5945             :                         case PG_MONEY:
    5946             :                         case PG_FLOAT4:
    5947             :                         case PG_FLOAT8:
    5948           0 :                                 switch (Z_TYPE_PP(val)) {
    5949             :                                         case IS_STRING:
    5950           0 :                                                 if (Z_STRLEN_PP(val) == 0) {
    5951           0 :                                                         ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    5952             :                                                 }
    5953             :                                                 else {
    5954             :                                                         /* better regex? */
    5955           0 :                                                         if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val), "^[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?$", 0 TSRMLS_CC) == FAILURE) {
    5956           0 :                                                                 err = 1;
    5957             :                                                         }
    5958             :                                                         else {
    5959           0 :                                                                 ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1);
    5960             :                                                         }
    5961             :                                                 }
    5962           0 :                                                 break;
    5963             : 
    5964             :                                         case IS_LONG:
    5965           0 :                                                 ZVAL_LONG(new_val, Z_LVAL_PP(val));
    5966           0 :                                                 break;
    5967             : 
    5968             :                                         case IS_DOUBLE:
    5969           0 :                                                 ZVAL_DOUBLE(new_val, Z_DVAL_PP(val));
    5970           0 :                                                 break;
    5971             : 
    5972             :                                         case IS_NULL:
    5973           0 :                                                 ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    5974           0 :                                                 break;
    5975             : 
    5976             :                                         default:
    5977           0 :                                                 err = 1;
    5978             :                                 }
    5979           0 :                                 PGSQL_CONV_CHECK_IGNORE();
    5980           0 :                                 if (err) {
    5981           0 :                                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for PostgreSQL '%s' (%s)", Z_STRVAL_PP(type), field);
    5982             :                                 }
    5983           0 :                                 break;
    5984             : 
    5985             :                                 /* Exotic types are handled as string also.
    5986             :                                    Please feel free to add more valitions. Invalid query fails
    5987             :                                    at execution anyway. */
    5988             :                         case PG_TEXT:
    5989             :                         case PG_CHAR:
    5990             :                         case PG_VARCHAR:
    5991             :                                 /* bit */
    5992             :                         case PG_BIT:
    5993             :                         case PG_VARBIT:
    5994             :                                 /* geometric */
    5995             :                         case PG_LINE:
    5996             :                         case PG_LSEG:
    5997             :                         case PG_POINT:
    5998             :                         case PG_BOX:
    5999             :                         case PG_PATH:
    6000             :                         case PG_POLYGON:
    6001             :                         case PG_CIRCLE:
    6002             :                                 /* unknown. JSON, Array etc */
    6003             :                         case PG_UNKNOWN:
    6004           6 :                                 switch (Z_TYPE_PP(val)) {
    6005             :                                         case IS_STRING:
    6006           6 :                                                 if (Z_STRLEN_PP(val) == 0) {
    6007           0 :                                                         if (opt & PGSQL_CONV_FORCE_NULL) {
    6008           0 :                                                                 ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    6009             :                                                         } else {
    6010           0 :                                                                 ZVAL_STRINGL(new_val, "''", sizeof("''")-1, 1);
    6011             :                                                         }
    6012             :                                                 }
    6013             :                                                 else {
    6014             :                                                         char *tmp;
    6015             :                                                         /* PostgreSQL ignores \0 */
    6016           6 :                                                         Z_TYPE_P(new_val) = IS_STRING;
    6017           6 :                                                         tmp = (char *)safe_emalloc(Z_STRLEN_PP(val), 2, 1);
    6018             :                                                         /* better to use PGSQLescapeLiteral since PGescapeStringConn does not handle special \ */
    6019           6 :                                                         Z_STRLEN_P(new_val) = (int)PQescapeStringConn(pg_link, tmp, Z_STRVAL_PP(val), Z_STRLEN_PP(val), NULL);
    6020           6 :                                                         Z_STRVAL_P(new_val) = tmp;
    6021           6 :                                                         php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
    6022             :                                                 }
    6023           6 :                                                 break;
    6024             : 
    6025             :                                         case IS_LONG:
    6026           0 :                                                 ZVAL_LONG(new_val, Z_LVAL_PP(val));
    6027           0 :                                                 convert_to_string_ex(&new_val);
    6028           0 :                                                 break;
    6029             : 
    6030             :                                         case IS_DOUBLE:
    6031           0 :                                                 ZVAL_DOUBLE(new_val, Z_DVAL_PP(val));
    6032           0 :                                                 convert_to_string_ex(&new_val);
    6033           0 :                                                 break;
    6034             : 
    6035             :                                         case IS_NULL:
    6036           0 :                                                 ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    6037           0 :                                                 break;
    6038             : 
    6039             :                                         default:
    6040           0 :                                                 err = 1;
    6041             :                                 }
    6042           6 :                                 PGSQL_CONV_CHECK_IGNORE();
    6043           6 :                                 if (err) {
    6044           0 :                                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for PostgreSQL '%s' (%s)", Z_STRVAL_PP(type), field);
    6045             :                                 }
    6046           6 :                                 break;
    6047             : 
    6048             :                         case PG_UNIX_TIME:
    6049             :                         case PG_UNIX_TIME_INTERVAL:
    6050             :                                 /* these are the actallay a integer */
    6051           0 :                                 switch (Z_TYPE_PP(val)) {
    6052             :                                         case IS_STRING:
    6053           0 :                                                 if (Z_STRLEN_PP(val) == 0) {
    6054           0 :                                                         ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    6055             :                                                 }
    6056             :                                                 else {
    6057             :                                                         /* better regex? */
    6058           0 :                                                         if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val), "^[0-9]+$", 0 TSRMLS_CC) == FAILURE) {
    6059           0 :                                                                 err = 1;
    6060             :                                                         }
    6061             :                                                         else {
    6062           0 :                                                                 ZVAL_STRINGL(new_val, Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1);
    6063           0 :                                                                 convert_to_long_ex(&new_val);
    6064             :                                                         }
    6065             :                                                 }
    6066           0 :                                                 break;
    6067             : 
    6068             :                                         case IS_DOUBLE:
    6069           0 :                                                 ZVAL_DOUBLE(new_val, Z_DVAL_PP(val));
    6070           0 :                                                 convert_to_long_ex(&new_val);
    6071           0 :                                                 break;
    6072             : 
    6073             :                                         case IS_LONG:
    6074           0 :                                                 ZVAL_LONG(new_val, Z_LVAL_PP(val));
    6075           0 :                                                 break;
    6076             : 
    6077             :                                         case IS_NULL:
    6078           0 :                                                 ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    6079           0 :                                                 break;
    6080             : 
    6081             :                                         default:
    6082           0 :                                                 err = 1;
    6083             :                                 }
    6084           0 :                                 PGSQL_CONV_CHECK_IGNORE();
    6085           0 :                                 if (err) {
    6086           0 :                                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for '%s' (%s)", Z_STRVAL_PP(type), field);
    6087             :                                 }
    6088           0 :                                 break;
    6089             : 
    6090             :                         case PG_CIDR:
    6091             :                         case PG_INET:
    6092           0 :                                 switch (Z_TYPE_PP(val)) {
    6093             :                                         case IS_STRING:
    6094           0 :                                                 if (Z_STRLEN_PP(val) == 0) {
    6095           0 :                                                         ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    6096             :                                                 }
    6097             :                                                 else {
    6098             :                                                         /* better regex? IPV6 and IPV4 */
    6099           0 :                                                         if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val), "^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$", 0 TSRMLS_CC) == FAILURE) {
    6100           0 :                                                                 err = 1;
    6101             :                                                         }
    6102             :                                                         else {
    6103           0 :                                                                 ZVAL_STRINGL(new_val, Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1);
    6104           0 :                                                                 php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
    6105             :                                                         }
    6106             :                                                 }
    6107           0 :                                                 break;
    6108             :                                                 
    6109             :                                         case IS_NULL:
    6110           0 :                                                 ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    6111           0 :                                                 break;
    6112             : 
    6113             :                                         default:
    6114           0 :                                                 err = 1;
    6115             :                                 }
    6116           0 :                                 PGSQL_CONV_CHECK_IGNORE();
    6117           0 :                                 if (err) {
    6118           0 :                                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for '%s' (%s)", Z_STRVAL_PP(type), field);
    6119             :                                 }
    6120           0 :                                 break;
    6121             : 
    6122             :                         case PG_TIME_WITH_TIMEZONE:
    6123             :                         case PG_TIMESTAMP:
    6124             :                         case PG_TIMESTAMP_WITH_TIMEZONE:
    6125           2 :                                 switch(Z_TYPE_PP(val)) {
    6126             :                                         case IS_STRING:
    6127           2 :                                                 if (Z_STRLEN_PP(val) == 0) {
    6128           0 :                                                         ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    6129           2 :                                                 } else if (!strcasecmp(Z_STRVAL_PP(val), "now()")) {
    6130           2 :                                                         ZVAL_STRINGL(new_val, "NOW()", sizeof("NOW()")-1, 1);
    6131             :                                                 } else {
    6132             :                                                         /* better regex? */
    6133           0 :                                                         if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val), "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})([ \\t]+(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1}(\\.[0-9]+){0,1}([ \\t]*([+-][0-9]{1,4}(:[0-9]{1,2}){0,1}|[-a-zA-Z_/+]{1,50})){0,1})){0,1}$", 1 TSRMLS_CC) == FAILURE) {
    6134           0 :                                                                 err = 1;
    6135             :                                                         } else {
    6136           0 :                                                                 ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1);
    6137           0 :                                                                 php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
    6138             :                                                         }
    6139             :                                                 }
    6140           2 :                                                 break;
    6141             : 
    6142             :                                         case IS_NULL:
    6143           0 :                                                 ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    6144           0 :                                                 break;
    6145             : 
    6146             :                                         default:
    6147           0 :                                                 err = 1;
    6148             :                                 }
    6149           2 :                                 PGSQL_CONV_CHECK_IGNORE();
    6150           2 :                                 if (err) {
    6151           0 :                                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_PP(type), field);
    6152             :                                 }
    6153           2 :                                 break;
    6154             : 
    6155             :                         case PG_DATE:
    6156           0 :                                 switch(Z_TYPE_PP(val)) {
    6157             :                                         case IS_STRING:
    6158           0 :                                                 if (Z_STRLEN_PP(val) == 0) {
    6159           0 :                                                         ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    6160             :                                                 }
    6161             :                                                 else {
    6162             :                                                         /* FIXME: better regex must be used */
    6163           0 :                                                         if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val), "^([0-9]{4}[/-][0-9]{1,2}[/-][0-9]{1,2})$", 1 TSRMLS_CC) == FAILURE) {
    6164           0 :                                                                 err = 1;
    6165             :                                                         }
    6166             :                                                         else {
    6167           0 :                                                                 ZVAL_STRINGL(new_val, Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1);
    6168           0 :                                                                 php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
    6169             :                                                         }
    6170             :                                                 }
    6171           0 :                                                 break;
    6172             : 
    6173             :                                         case IS_NULL:
    6174           0 :                                                 ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    6175           0 :                                                 break;
    6176             : 
    6177             :                                         default:
    6178           0 :                                                 err = 1;
    6179             :                                 }
    6180           0 :                                 PGSQL_CONV_CHECK_IGNORE();
    6181           0 :                                 if (err) {
    6182           0 :                                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_PP(type), field);
    6183             :                                 }
    6184           0 :                                 break;
    6185             : 
    6186             :                         case PG_TIME:
    6187           0 :                                 switch(Z_TYPE_PP(val)) {
    6188             :                                         case IS_STRING:
    6189           0 :                                                 if (Z_STRLEN_PP(val) == 0) {
    6190           0 :                                                         ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    6191             :                                                 }
    6192             :                                                 else {
    6193             :                                                         /* FIXME: better regex must be used */
    6194           0 :                                                         if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val), "^(([0-9]{1,2}:[0-9]{1,2}){1}(:[0-9]{1,2}){0,1})){0,1}$", 1 TSRMLS_CC) == FAILURE) {
    6195           0 :                                                                 err = 1;
    6196             :                                                         }
    6197             :                                                         else {
    6198           0 :                                                                 ZVAL_STRINGL(new_val, Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1);
    6199           0 :                                                                 php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
    6200             :                                                         }
    6201             :                                                 }
    6202           0 :                                                 break;
    6203             : 
    6204             :                                         case IS_NULL:
    6205           0 :                                                 ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    6206           0 :                                                 break;
    6207             : 
    6208             :                                         default:
    6209           0 :                                                 err = 1;
    6210             :                                 }
    6211           0 :                                 PGSQL_CONV_CHECK_IGNORE();
    6212           0 :                                 if (err) {
    6213           0 :                                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_PP(type), field);
    6214             :                                 }
    6215           0 :                                 break;
    6216             : 
    6217             :                         case PG_INTERVAL:
    6218           0 :                                 switch(Z_TYPE_PP(val)) {
    6219             :                                         case IS_STRING:
    6220           0 :                                                 if (Z_STRLEN_PP(val) == 0) {
    6221           0 :                                                         ZVAL_STRING(new_val, "NULL", 1);
    6222             :                                                 }
    6223             :                                                 else {
    6224             : 
    6225             :                                                         /* From the Postgres docs:
    6226             : 
    6227             :                                                            interval values can be written with the following syntax:
    6228             :                                                            [@] quantity unit [quantity unit...] [direction]
    6229             :                                                            
    6230             :                                                            Where: quantity is a number (possibly signed); unit is second, minute, hour,
    6231             :                                                            day, week, month, year, decade, century, millennium, or abbreviations or
    6232             :                                                            plurals of these units [note not *all* abbreviations] ; direction can be
    6233             :                                                            ago or empty. The at sign (@) is optional noise.
    6234             :                                                            
    6235             :                                                            ...
    6236             :                                                            
    6237             :                                                            Quantities of days, hours, minutes, and seconds can be specified without explicit
    6238             :                                                            unit markings. For example, '1 12:59:10' is read the same as '1 day 12 hours 59 min 10
    6239             :                                                            sec'.
    6240             :                                                         */
    6241           0 :                                                         if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val),
    6242             :                                                                                                                 "^(@?[ \\t]+)?("
    6243             : 
    6244             :                                                                                                                 /* Textual time units and their abbreviations: */
    6245             :                                                                                                                 "(([-+]?[ \\t]+)?"
    6246             :                                                                                                                 "[0-9]+(\\.[0-9]*)?[ \\t]*"
    6247             :                                                                                                                 "(millenniums|millennia|millennium|mil|mils|"
    6248             :                                                                                                                 "centuries|century|cent|c|"
    6249             :                                                                                                                 "decades|decade|dec|decs|"
    6250             :                                                                                                                 "years|year|y|"
    6251             :                                                                                                                 "months|month|mon|"
    6252             :                                                                                                                 "weeks|week|w|" 
    6253             :                                                                                                                 "days|day|d|"
    6254             :                                                                                                                 "hours|hour|hr|hrs|h|"
    6255             :                                                                                                                 "minutes|minute|mins|min|m|"
    6256             :                                                                                                                 "seconds|second|secs|sec|s))+|"
    6257             : 
    6258             :                                                                                                                 /* Textual time units plus (dd)* hh[:mm[:ss]] */
    6259             :                                                                                                                 "((([-+]?[ \\t]+)?"
    6260             :                                                                                                                 "[0-9]+(\\.[0-9]*)?[ \\t]*"
    6261             :                                                                                                                 "(millenniums|millennia|millennium|mil|mils|"
    6262             :                                                                                                                 "centuries|century|cent|c|"
    6263             :                                                                                                                 "decades|decade|dec|decs|"
    6264             :                                                                                                                 "years|year|y|"
    6265             :                                                                                                                 "months|month|mon|"
    6266             :                                                                                                                 "weeks|week|w|"
    6267             :                                                                                                                 "days|day|d))+" 
    6268             :                                                                                                                 "([-+]?[ \\t]+"
    6269             :                                                                                                                 "([0-9]+[ \\t]+)+"                             /* dd */
    6270             :                                                                                                                 "(([0-9]{1,2}:){0,2}[0-9]{0,2})" /* hh:[mm:[ss]] */
    6271             :                                                                                                                 ")?))"
    6272             :                                                                                                                 "([ \\t]+ago)?$",
    6273             :                                                                                                                 1 TSRMLS_CC) == FAILURE) {
    6274           0 :                                                                 err = 1;
    6275             :                                                         }
    6276             :                                                         else {
    6277           0 :                                                                 ZVAL_STRING(new_val, Z_STRVAL_PP(val), 1);
    6278           0 :                                                                 php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
    6279             :                                                         }
    6280             :                                                 }
    6281           0 :                                                 break;
    6282             : 
    6283             :                                         case IS_NULL:
    6284           0 :                                                 ZVAL_STRING(new_val, "NULL", 1);
    6285           0 :                                                 break;
    6286             : 
    6287             :                                         default:
    6288           0 :                                                 err = 1;
    6289             :                                 }
    6290           0 :                                 PGSQL_CONV_CHECK_IGNORE();
    6291           0 :                                 if (err) {
    6292           0 :                                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_PP(type), field);
    6293             :                                 }
    6294           0 :                                 break;
    6295             : #ifdef HAVE_PQESCAPE
    6296             :                         case PG_BYTEA:
    6297           5 :                                 switch (Z_TYPE_PP(val)) {
    6298             :                                         case IS_STRING:
    6299           5 :                                                 if (Z_STRLEN_PP(val) == 0) {
    6300           0 :                                                         ZVAL_STRING(new_val, "NULL", 1);
    6301             :                                                 }
    6302             :                                                 else {
    6303             :                                                         unsigned char *tmp;
    6304             :                                                         size_t to_len;
    6305           5 :                                                         smart_str s = {0};
    6306             : #ifdef HAVE_PQESCAPE_BYTEA_CONN
    6307           5 :                                                         tmp = PQescapeByteaConn(pg_link, (unsigned char *)Z_STRVAL_PP(val), Z_STRLEN_PP(val), &to_len);
    6308             : #else
    6309             :                                                         tmp = PQescapeBytea(Z_STRVAL_PP(val), (unsigned char *)Z_STRLEN_PP(val), &to_len);
    6310             : #endif
    6311           5 :                                                         Z_TYPE_P(new_val) = IS_STRING;
    6312           5 :                                                         Z_STRLEN_P(new_val) = to_len-1; /* PQescapeBytea's to_len includes additional '\0' */
    6313           5 :                                                         Z_STRVAL_P(new_val) = emalloc(to_len);
    6314           5 :                                                         memcpy(Z_STRVAL_P(new_val), tmp, to_len);
    6315           5 :                                                         PQfreemem(tmp);
    6316           5 :                                                         php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
    6317           5 :                                                         smart_str_appendl(&s, Z_STRVAL_P(new_val), Z_STRLEN_P(new_val));
    6318           5 :                                                         smart_str_0(&s);
    6319           5 :                                                         efree(Z_STRVAL_P(new_val));
    6320           5 :                                                         Z_STRVAL_P(new_val) = s.c;
    6321           5 :                                                         Z_STRLEN_P(new_val) = s.len;
    6322             :                                                 }
    6323           5 :                                                 break;
    6324             : 
    6325             :                                         case IS_LONG:
    6326           0 :                                                 ZVAL_LONG(new_val, Z_LVAL_PP(val));
    6327           0 :                                                 convert_to_string_ex(&new_val);
    6328           0 :                                                 break;
    6329             : 
    6330             :                                         case IS_DOUBLE:
    6331           0 :                                                 ZVAL_DOUBLE(new_val, Z_DVAL_PP(val));
    6332           0 :                                                 convert_to_string_ex(&new_val);
    6333           0 :                                                 break;
    6334             : 
    6335             :                                         case IS_NULL:
    6336           0 :                                                 ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    6337           0 :                                                 break;
    6338             : 
    6339             :                                         default:
    6340           0 :                                                 err = 1;
    6341             :                                 }
    6342           5 :                                 PGSQL_CONV_CHECK_IGNORE();
    6343           5 :                                 if (err) {
    6344           0 :                                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL, string, long or double value for PostgreSQL '%s' (%s)", Z_STRVAL_PP(type), field);
    6345             :                                 }
    6346           5 :                                 break;
    6347             :                                 
    6348             : #endif
    6349             :                         case PG_MACADDR:
    6350           0 :                                 switch(Z_TYPE_PP(val)) {
    6351             :                                         case IS_STRING:
    6352           0 :                                                 if (Z_STRLEN_PP(val) == 0) {
    6353           0 :                                                         ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    6354             :                                                 }
    6355             :                                                 else {
    6356           0 :                                                         if (php_pgsql_convert_match(Z_STRVAL_PP(val), Z_STRLEN_PP(val), "^([0-9a-f]{2,2}:){5,5}[0-9a-f]{2,2}$", 1 TSRMLS_CC) == FAILURE) {
    6357           0 :                                                                 err = 1;
    6358             :                                                         }
    6359             :                                                         else {
    6360           0 :                                                                 ZVAL_STRINGL(new_val, Z_STRVAL_PP(val), Z_STRLEN_PP(val), 1);
    6361           0 :                                                                 php_pgsql_add_quotes(new_val, 1 TSRMLS_CC);
    6362             :                                                         }
    6363             :                                                 }
    6364           0 :                                                 break;
    6365             : 
    6366             :                                         case IS_NULL:
    6367           0 :                                                 ZVAL_STRINGL(new_val, "NULL", sizeof("NULL")-1, 1);
    6368           0 :                                                 break;
    6369             : 
    6370             :                                         default:
    6371           0 :                                                 err = 1;
    6372             :                                 }
    6373           0 :                                 PGSQL_CONV_CHECK_IGNORE();
    6374           0 :                                 if (err) {
    6375           0 :                                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects NULL or string for PostgreSQL %s field (%s)", Z_STRVAL_PP(type), field);
    6376             :                                 }
    6377           0 :                                 break;
    6378             : 
    6379             :                         default:
    6380             :                                 /* should not happen */
    6381           0 :                                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unknown or system data type '%s' for '%s'. Report error", Z_STRVAL_PP(type), field);
    6382           0 :                                 err = 1;
    6383             :                                 break;
    6384             :                 } /* switch */
    6385             : 
    6386          82 :                 if (err) {
    6387           0 :                         zval_dtor(new_val);
    6388           0 :                         FREE_ZVAL(new_val);
    6389           0 :                         break; /* break out for() */
    6390             :                 }
    6391             :                 /* If field is NULL and HAS DEFAULT, should be skipped */
    6392          82 :                 if (!skip_field) {
    6393             :                         char *escaped;
    6394          82 :                         size_t field_len = strlen(field);
    6395             : 
    6396          82 :                         if (_php_pgsql_detect_identifier_escape(field, field_len) == SUCCESS) {
    6397           0 :                                 add_assoc_zval(result, field, new_val);
    6398             :                         } else {
    6399          82 :                                 escaped = PGSQLescapeIdentifier(pg_link, field, field_len);
    6400          82 :                                 add_assoc_zval(result, escaped, new_val);
    6401          82 :                                 PGSQLfree(escaped);
    6402             :                         }
    6403             :                 }
    6404             :         } /* for */
    6405             :         zval_dtor(meta);
    6406          50 :         FREE_ZVAL(meta);
    6407             : 
    6408          50 :         if (err) {
    6409             :                 /* shouldn't destroy & free zval here */
    6410           1 :                 return FAILURE;
    6411             :         }
    6412          49 :         return SUCCESS;
    6413             : }
    6414             : /* }}} */
    6415             : 
    6416             : 
    6417             : /* {{{ proto array pg_convert(resource db, string table, array values[, int options])
    6418             :    Check and convert values for PostgreSQL SQL statement */
    6419           2 : PHP_FUNCTION(pg_convert)
    6420             : {
    6421             :         zval *pgsql_link, *values;
    6422             :         char *table_name;
    6423             :         int table_name_len;
    6424           2 :         ulong option = 0;
    6425             :         PGconn *pg_link;
    6426           2 :         int id = -1;
    6427             : 
    6428           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
    6429             :                                                           "rsa|l", &pgsql_link, &table_name, &table_name_len, &values, &option) == FAILURE) {
    6430           0 :                 return;
    6431             :         }
    6432           2 :         if (option & ~PGSQL_CONV_OPTS) {
    6433           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid option is specified");
    6434           0 :                 RETURN_FALSE;
    6435             :         }
    6436           2 :         if (!table_name_len) {
    6437           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Table name is invalid");
    6438           0 :                 RETURN_FALSE;
    6439             :         }
    6440             : 
    6441           2 :         ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    6442             : 
    6443           2 :         if (php_pgsql_flush_query(pg_link TSRMLS_CC)) {
    6444           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected unhandled result(s) in connection");
    6445             :         }
    6446           2 :         array_init(return_value);
    6447           2 :         if (php_pgsql_convert(pg_link, table_name, values, return_value, option TSRMLS_CC) == FAILURE) {
    6448             :                 zval_dtor(return_value);
    6449           0 :                 RETURN_FALSE;
    6450             :         }
    6451             : }
    6452             : /* }}} */
    6453             : 
    6454          11 : static int do_exec(smart_str *querystr, int expect, PGconn *pg_link, ulong opt TSRMLS_DC)
    6455             : {
    6456          11 :         if (opt & PGSQL_DML_ASYNC) {
    6457           0 :                 if (PQsendQuery(pg_link, querystr->c)) {
    6458           0 :                         return 0;
    6459             :                 }
    6460             :         }
    6461             :         else {
    6462             :                 PGresult *pg_result;
    6463             : 
    6464          11 :                 pg_result = PQexec(pg_link, querystr->c);
    6465          11 :                 if (PQresultStatus(pg_result) == expect) {
    6466          11 :                         PQclear(pg_result);
    6467          11 :                         return 0;
    6468             :                 } else {
    6469           0 :                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", PQresultErrorMessage(pg_result));
    6470           0 :                         PQclear(pg_result);
    6471             :                 }
    6472             :         }
    6473             : 
    6474           0 :         return -1;
    6475             : }
    6476             : 
    6477          42 : static inline void build_tablename(smart_str *querystr, PGconn *pg_link, const char *table)
    6478             : {
    6479             :         char *table_copy, *escaped, *token, *tmp;
    6480             :         size_t len;
    6481             : 
    6482             :         /* schame.table should be "schame"."table" */
    6483          42 :         table_copy = estrdup(table);
    6484          42 :         token = php_strtok_r(table_copy, ".", &tmp);
    6485          42 :         len = strlen(token);
    6486          42 :         if (_php_pgsql_detect_identifier_escape(token, len) == SUCCESS) {
    6487           0 :                 smart_str_appendl(querystr, token, len);
    6488             :         } else {
    6489          42 :                 escaped = PGSQLescapeIdentifier(pg_link, token, len);
    6490          42 :                 smart_str_appends(querystr, escaped);
    6491          42 :                 PGSQLfree(escaped);
    6492             :         }
    6493          42 :         if (tmp && *tmp) {
    6494          15 :                 len = strlen(tmp);
    6495             :                 /* "schema"."table" format */
    6496          15 :                 if (_php_pgsql_detect_identifier_escape(tmp, len) == SUCCESS) {
    6497           0 :                         smart_str_appendc(querystr, '.');
    6498           0 :                         smart_str_appendl(querystr, tmp, len);
    6499             :                 } else {
    6500          15 :                         escaped = PGSQLescapeIdentifier(pg_link, tmp, len);
    6501          15 :                         smart_str_appendc(querystr, '.');
    6502          15 :                         smart_str_appends(querystr, escaped);
    6503          15 :                         PGSQLfree(escaped);
    6504             :                 }
    6505             :         }
    6506          42 :         efree(table_copy);
    6507          42 : }
    6508             : 
    6509             : /* {{{ php_pgsql_insert
    6510             :  */
    6511          19 : PHP_PGSQL_API int php_pgsql_insert(PGconn *pg_link, const char *table, zval *var_array, ulong opt, char **sql TSRMLS_DC)
    6512             : {
    6513          19 :         zval **val, *converted = NULL;
    6514             :         char buf[256];
    6515             :         char *fld, *tmp;
    6516          19 :         smart_str querystr = {0};
    6517          19 :         int key_type, ret = FAILURE;
    6518             :         uint fld_len;
    6519             :         ulong num_idx;
    6520             :         HashPosition pos;
    6521             : 
    6522             :         assert(pg_link != NULL);
    6523             :         assert(table != NULL);
    6524             :         assert(Z_TYPE_P(var_array) == IS_ARRAY);
    6525             : 
    6526          19 :         if (zend_hash_num_elements(Z_ARRVAL_P(var_array)) == 0) {
    6527           1 :                 smart_str_appends(&querystr, "INSERT INTO ");
    6528           1 :                 build_tablename(&querystr, pg_link, table);
    6529           1 :                 smart_str_appends(&querystr, " DEFAULT VALUES");
    6530             : 
    6531           1 :                 goto no_values;
    6532             :         }
    6533             : 
    6534             :         /* convert input array if needed */
    6535          18 :         if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) {
    6536          18 :                 MAKE_STD_ZVAL(converted);
    6537          18 :                 array_init(converted);
    6538          18 :                 if (php_pgsql_convert(pg_link, table, var_array, converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) {
    6539           1 :                         goto cleanup;
    6540             :                 }
    6541          17 :                 var_array = converted;
    6542             :         }
    6543             : 
    6544          17 :         smart_str_appends(&querystr, "INSERT INTO ");
    6545          17 :         build_tablename(&querystr, pg_link, table);
    6546          17 :         smart_str_appends(&querystr, " (");
    6547             : 
    6548          17 :         zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(var_array), &pos);
    6549          69 :         while ((key_type = zend_hash_get_current_key_ex(Z_ARRVAL_P(var_array), &fld,
    6550             :                                         &fld_len, &num_idx, 0, &pos)) != HASH_KEY_NON_EXISTENT) {
    6551          35 :                 if (key_type == HASH_KEY_IS_LONG) {
    6552           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects associative array for values to be inserted");
    6553           0 :                         goto cleanup;
    6554             :                 }
    6555          35 :                 if (opt & PGSQL_DML_ESCAPE) {
    6556           0 :                         tmp = PGSQLescapeIdentifier(pg_link, fld, fld_len);
    6557           0 :                         smart_str_appends(&querystr, tmp);
    6558           0 :                         PGSQLfree(tmp);
    6559             :                 } else {
    6560          35 :                         smart_str_appendl(&querystr, fld, fld_len - 1);
    6561             :                 }
    6562          35 :                 smart_str_appendc(&querystr, ',');
    6563          35 :                 zend_hash_move_forward_ex(Z_ARRVAL_P(var_array), &pos);
    6564             :         }
    6565          17 :         querystr.len--;
    6566          17 :         smart_str_appends(&querystr, ") VALUES (");
    6567             :         
    6568             :         /* make values string */
    6569          69 :         for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(var_array), &pos);
    6570          52 :                  zend_hash_get_current_data_ex(Z_ARRVAL_P(var_array), (void **)&val, &pos) == SUCCESS;
    6571          35 :                  zend_hash_move_forward_ex(Z_ARRVAL_P(var_array), &pos)) {
    6572             :                 
    6573             :                 /* we can avoid the key_type check here, because we tested it in the other loop */
    6574          35 :                 switch(Z_TYPE_PP(val)) {
    6575             :                         case IS_STRING:
    6576           9 :                                 if (opt & PGSQL_DML_ESCAPE) {
    6577             :                                         size_t new_len;
    6578             :                                         char *tmp;
    6579           0 :                                         tmp = (char *)safe_emalloc(Z_STRLEN_PP(val), 2, 1);
    6580           0 :                                         new_len = PQescapeStringConn(pg_link, tmp, Z_STRVAL_PP(val), Z_STRLEN_PP(val), NULL);
    6581           0 :                                         smart_str_appendc(&querystr, '\'');
    6582           0 :                                         smart_str_appendl(&querystr, tmp, new_len);
    6583           0 :                                         smart_str_appendc(&querystr, '\'');
    6584           0 :                                         efree(tmp);
    6585             :                                 } else {
    6586           9 :                                         smart_str_appendl(&querystr, Z_STRVAL_PP(val), Z_STRLEN_PP(val));
    6587             :                                 }
    6588           9 :                                 break;
    6589             :                         case IS_LONG:
    6590          26 :                                 smart_str_append_long(&querystr, Z_LVAL_PP(val));
    6591          26 :                                 break;
    6592             :                         case IS_DOUBLE:
    6593           0 :                                 smart_str_appendl(&querystr, buf, snprintf(buf, sizeof(buf), "%F", Z_DVAL_PP(val)));
    6594           0 :                                 break;
    6595             :                         case IS_NULL:
    6596           0 :                                 smart_str_appendl(&querystr, "NULL", sizeof("NULL")-1);
    6597           0 :                                 break;
    6598             :                         default:
    6599           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expects scaler values. type = %d", Z_TYPE_PP(val));
    6600           0 :                                 goto cleanup;
    6601             :                                 break;
    6602             :                 }
    6603          35 :                 smart_str_appendc(&querystr, ',');
    6604             :         }
    6605             :         /* Remove the trailing "," */
    6606          17 :         querystr.len--;
    6607          17 :         smart_str_appends(&querystr, ");");
    6608             : 
    6609             : no_values:
    6610             : 
    6611          18 :         smart_str_0(&querystr);
    6612             : 
    6613          18 :         if ((opt & (PGSQL_DML_EXEC|PGSQL_DML_ASYNC)) &&
    6614           0 :                 do_exec(&querystr, PGRES_COMMAND_OK, pg_link, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == 0) {
    6615           0 :                 ret = SUCCESS;
    6616             :         }
    6617          18 :         else if (opt & PGSQL_DML_STRING) {
    6618          18 :                 ret = SUCCESS;
    6619             :         }
    6620             :         
    6621             : cleanup:
    6622          19 :         if (!(opt & PGSQL_DML_NO_CONV) && converted) {
    6623             :                 zval_dtor(converted);
    6624          18 :                 FREE_ZVAL(converted);
    6625             :         }
    6626          37 :         if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) {
    6627          18 :                 *sql = querystr.c;
    6628             :         }
    6629             :         else {
    6630           1 :                 smart_str_free(&querystr);
    6631             :         }
    6632          19 :         return ret;
    6633             : }
    6634             : /* }}} */
    6635             : 
    6636             : /* {{{ proto mixed pg_insert(resource db, string table, array values[, int options])
    6637             :    Insert values (filed=>value) to table */
    6638          19 : PHP_FUNCTION(pg_insert)
    6639             : {
    6640             :         zval *pgsql_link, *values;
    6641          19 :         char *table, *sql = NULL;
    6642             :         int table_len;
    6643          19 :         ulong option = PGSQL_DML_EXEC, return_sql;
    6644             :         PGconn *pg_link;
    6645             :         PGresult *pg_result;
    6646             :         ExecStatusType status;
    6647             :         pgsql_result_handle *pgsql_handle;
    6648          19 :         int id = -1, argc = ZEND_NUM_ARGS();
    6649             : 
    6650          19 :         if (zend_parse_parameters(argc TSRMLS_CC, "rsa|l",
    6651             :                                                           &pgsql_link, &table, &table_len, &values, &option) == FAILURE) {
    6652           0 :                 return;
    6653             :         }
    6654          19 :         if (option & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)) {
    6655           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid option is specified");
    6656           0 :                 RETURN_FALSE;
    6657             :         }
    6658             :         
    6659          19 :         ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    6660             : 
    6661          19 :         if (php_pgsql_flush_query(pg_link TSRMLS_CC)) {
    6662           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected unhandled result(s) in connection");
    6663             :         }
    6664          19 :         return_sql = option & PGSQL_DML_STRING;
    6665          19 :         if (option & PGSQL_DML_EXEC) {
    6666             :                 /* return resource when executed */
    6667          17 :                 option = option & ~PGSQL_DML_EXEC;
    6668          17 :                 if (php_pgsql_insert(pg_link, table, values, option|PGSQL_DML_STRING, &sql TSRMLS_CC) == FAILURE) {
    6669           1 :                         RETURN_FALSE;
    6670             :                 }
    6671          16 :                 pg_result = PQexec(pg_link, sql);
    6672          16 :                 if ((PGG(auto_reset_persistent) & 2) && PQstatus(pg_link) != CONNECTION_OK) {
    6673           0 :                         PQclear(pg_result);
    6674           0 :                         PQreset(pg_link);
    6675           0 :                         pg_result = PQexec(pg_link, sql);
    6676             :                 }
    6677          16 :                 efree(sql);
    6678             : 
    6679          16 :                 if (pg_result) {
    6680          16 :                         status = PQresultStatus(pg_result);
    6681             :                 } else {
    6682           0 :                         status = (ExecStatusType) PQstatus(pg_link);
    6683             :                 }
    6684             : 
    6685          16 :                 switch (status) {
    6686             :                         case PGRES_EMPTY_QUERY:
    6687             :                         case PGRES_BAD_RESPONSE:
    6688             :                         case PGRES_NONFATAL_ERROR:
    6689             :                         case PGRES_FATAL_ERROR:
    6690           0 :                                 PHP_PQ_ERROR("Query failed: %s", pg_link);
    6691           0 :                                 PQclear(pg_result);
    6692           0 :                                 RETURN_FALSE;
    6693             :                                 break;
    6694             :                         case PGRES_COMMAND_OK: /* successful command that did not return rows */
    6695             :                         default:
    6696          16 :                                 if (pg_result) {
    6697          16 :                                         pgsql_handle = (pgsql_result_handle *) emalloc(sizeof(pgsql_result_handle));
    6698          16 :                                         pgsql_handle->conn = pg_link;
    6699          16 :                                         pgsql_handle->result = pg_result;
    6700          16 :                                         pgsql_handle->row = 0;
    6701          16 :                                         ZEND_REGISTER_RESOURCE(return_value, pgsql_handle, le_result);
    6702          16 :                                         return;
    6703             :                                 } else {
    6704           0 :                                         PQclear(pg_result);
    6705           0 :                                         RETURN_FALSE;
    6706             :                                 }
    6707             :                         break;
    6708             :                 }
    6709           2 :         } else if (php_pgsql_insert(pg_link, table, values, option, &sql TSRMLS_CC) == FAILURE) {
    6710           0 :                 RETURN_FALSE;
    6711             :         }
    6712           2 :         if (return_sql) {
    6713           2 :                 RETURN_STRING(sql, 0);
    6714             :         }
    6715           0 :         RETURN_TRUE;
    6716             : }
    6717             : /* }}} */
    6718             : 
    6719          33 : static inline int build_assignment_string(PGconn *pg_link, smart_str *querystr, HashTable *ht, int where_cond, const char *pad, int pad_len, ulong opt TSRMLS_DC)
    6720             : {
    6721             :         HashPosition pos;
    6722             :         uint fld_len;
    6723             :         int key_type;
    6724             :         ulong num_idx;
    6725             :         char *fld, *tmp;
    6726             :         char buf[256];
    6727             :         zval **val;
    6728             : 
    6729         114 :         for (zend_hash_internal_pointer_reset_ex(ht, &pos);
    6730          81 :                  zend_hash_get_current_data_ex(ht, (void **)&val, &pos) == SUCCESS;
    6731          48 :                  zend_hash_move_forward_ex(ht, &pos)) {
    6732          48 :                  key_type = zend_hash_get_current_key_ex(ht, &fld, &fld_len, &num_idx, 0, &pos);
    6733          48 :                 if (key_type == HASH_KEY_IS_LONG) {
    6734           0 :                         php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Expects associative array for values to be inserted");
    6735           0 :                         return -1;
    6736             :                 }
    6737          48 :                 if (opt & PGSQL_DML_ESCAPE) {
    6738           5 :                         tmp = PGSQLescapeIdentifier(pg_link, fld, fld_len);
    6739           5 :                         smart_str_appends(querystr, tmp);
    6740           5 :                         PGSQLfree(tmp);
    6741             :                 } else {
    6742          43 :                         smart_str_appendl(querystr, fld, fld_len - 1);
    6743             :                 }
    6744          50 :                 if (where_cond && (Z_TYPE_PP(val) == IS_BOOL || (Z_TYPE_PP(val) == IS_STRING && !strcmp(Z_STRVAL_PP(val), "NULL")))) {
    6745           2 :                         smart_str_appends(querystr, " IS ");
    6746             :                 } else {
    6747          46 :                         smart_str_appendc(querystr, '=');
    6748             :                 }
    6749             : 
    6750          48 :                 switch(Z_TYPE_PP(val)) {
    6751             :                         case IS_STRING:
    6752          21 :                                 if (opt & PGSQL_DML_ESCAPE) {
    6753             :                                         size_t new_len;
    6754           5 :                                         tmp = (char *)safe_emalloc(Z_STRLEN_PP(val), 2, 1);
    6755           5 :                                         new_len = PQescapeStringConn(pg_link, tmp, Z_STRVAL_PP(val), Z_STRLEN_PP(val), NULL);
    6756           5 :                                         smart_str_appendc(querystr, '\'');
    6757           5 :                                         smart_str_appendl(querystr, tmp, new_len);
    6758           5 :                                         smart_str_appendc(querystr, '\'');
    6759           5 :                                         efree(tmp);
    6760             :                                 } else {
    6761          16 :                                         smart_str_appendl(querystr, Z_STRVAL_PP(val), Z_STRLEN_PP(val));
    6762             :                                 }
    6763          21 :                                 break;
    6764             :                         case IS_LONG:
    6765          27 :                                 smart_str_append_long(querystr, Z_LVAL_PP(val));
    6766          27 :                                 break;
    6767             :                         case IS_DOUBLE:
    6768           0 :                                 smart_str_appendl(querystr, buf, MIN(snprintf(buf, sizeof(buf), "%F", Z_DVAL_PP(val)), sizeof(buf)-1));
    6769           0 :                                 break;
    6770             :                         case IS_NULL:
    6771           0 :                                 smart_str_appendl(querystr, "NULL", sizeof("NULL")-1);
    6772           0 :                                 break;
    6773             :                         default:
    6774           0 :                                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expects scaler values. type=%d", Z_TYPE_PP(val));
    6775           0 :                                 return -1;
    6776             :                 }
    6777          48 :                 smart_str_appendl(querystr, pad, pad_len);
    6778             :         }
    6779          33 :         querystr->len -= pad_len;
    6780             : 
    6781          33 :         return 0;
    6782             : }
    6783             : 
    6784             : /* {{{ php_pgsql_update
    6785             :  */
    6786           9 : 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) 
    6787             : {
    6788           9 :         zval *var_converted = NULL, *ids_converted = NULL;
    6789           9 :         smart_str querystr = {0};
    6790           9 :         int ret = FAILURE;
    6791             : 
    6792             :         assert(pg_link != NULL);
    6793             :         assert(table != NULL);
    6794             :         assert(Z_TYPE_P(var_array) == IS_ARRAY);
    6795             :         assert(Z_TYPE_P(ids_array) == IS_ARRAY);
    6796             :         assert(!(opt & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)));
    6797             : 
    6798          27 :         if (zend_hash_num_elements(Z_ARRVAL_P(var_array)) == 0
    6799          18 :                         || zend_hash_num_elements(Z_ARRVAL_P(ids_array)) == 0) {
    6800           0 :                 return FAILURE;
    6801             :         }
    6802             : 
    6803           9 :         if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) {
    6804           8 :                 MAKE_STD_ZVAL(var_converted);
    6805           8 :                 array_init(var_converted);
    6806           8 :                 if (php_pgsql_convert(pg_link, table, var_array, var_converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) {
    6807           0 :                         goto cleanup;
    6808             :                 }
    6809           8 :                 var_array = var_converted;
    6810           8 :                 MAKE_STD_ZVAL(ids_converted);
    6811           8 :                 array_init(ids_converted);
    6812           8 :                 if (php_pgsql_convert(pg_link, table, ids_array, ids_converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) {
    6813           0 :                         goto cleanup;
    6814             :                 }
    6815           8 :                 ids_array = ids_converted;
    6816             :         }
    6817             : 
    6818           9 :         smart_str_appends(&querystr, "UPDATE ");
    6819           9 :         build_tablename(&querystr, pg_link, table);
    6820           9 :         smart_str_appends(&querystr, " SET ");
    6821             : 
    6822           9 :         if (build_assignment_string(pg_link, &querystr, Z_ARRVAL_P(var_array), 0, ",", 1, opt TSRMLS_CC))
    6823           0 :                 goto cleanup;
    6824             :         
    6825           9 :         smart_str_appends(&querystr, " WHERE ");
    6826             :         
    6827           9 :         if (build_assignment_string(pg_link, &querystr, Z_ARRVAL_P(ids_array), 1, " AND ", sizeof(" AND ")-1, opt TSRMLS_CC))
    6828           0 :                 goto cleanup;
    6829             : 
    6830           9 :         smart_str_appendc(&querystr, ';');
    6831           9 :         smart_str_0(&querystr);
    6832             : 
    6833          14 :         if ((opt & PGSQL_DML_EXEC) && do_exec(&querystr, PGRES_COMMAND_OK, pg_link, opt TSRMLS_CC) == 0) {
    6834           5 :                 ret = SUCCESS;
    6835           4 :         } else if (opt & PGSQL_DML_STRING) {
    6836           4 :                 ret = SUCCESS;
    6837             :         }
    6838             : 
    6839             : cleanup:
    6840           9 :         if (var_converted) {
    6841             :                 zval_dtor(var_converted);
    6842           8 :                 FREE_ZVAL(var_converted);
    6843             :         }
    6844           9 :         if (ids_converted) {
    6845             :                 zval_dtor(ids_converted);
    6846           8 :                 FREE_ZVAL(ids_converted);
    6847             :         }
    6848          14 :         if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) {
    6849           5 :                 *sql = querystr.c;
    6850             :         }
    6851             :         else {
    6852           4 :                 smart_str_free(&querystr);
    6853             :         }
    6854           9 :         return ret;
    6855             : }
    6856             : /* }}} */
    6857             : 
    6858             : /* {{{ proto mixed pg_update(resource db, string table, array fields, array ids[, int options])
    6859             :    Update table using values (field=>value) and ids (id=>value) */
    6860           9 : PHP_FUNCTION(pg_update)
    6861             : {
    6862             :         zval *pgsql_link, *values, *ids;
    6863           9 :         char *table, *sql = NULL;
    6864             :         int table_len;
    6865           9 :         ulong option =  PGSQL_DML_EXEC;
    6866             :         PGconn *pg_link;
    6867           9 :         int id = -1, argc = ZEND_NUM_ARGS();
    6868             : 
    6869           9 :         if (zend_parse_parameters(argc TSRMLS_CC, "rsaa|l",
    6870             :                                                           &pgsql_link, &table, &table_len, &values, &ids, &option) == FAILURE) {
    6871           0 :                 return;
    6872             :         }
    6873           9 :         if (option & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)) {
    6874           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid option is specified");
    6875           0 :                 RETURN_FALSE;
    6876             :         }
    6877             :         
    6878           9 :         ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    6879             : 
    6880           9 :         if (php_pgsql_flush_query(pg_link TSRMLS_CC)) {
    6881           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected unhandled result(s) in connection");
    6882             :         }
    6883           9 :         if (php_pgsql_update(pg_link, table, values, ids, option, &sql TSRMLS_CC) == FAILURE) {
    6884           0 :                 RETURN_FALSE;
    6885             :         }
    6886           9 :         if (option & PGSQL_DML_STRING) {
    6887           5 :                 RETURN_STRING(sql, 0);
    6888             :         }
    6889           4 :         RETURN_TRUE;
    6890             : }
    6891             : /* }}} */
    6892             : 
    6893             : /* {{{ php_pgsql_delete
    6894             :  */
    6895          12 : PHP_PGSQL_API int php_pgsql_delete(PGconn *pg_link, const char *table, zval *ids_array, ulong opt, char **sql TSRMLS_DC) 
    6896             : {
    6897          12 :         zval *ids_converted = NULL;
    6898          12 :         smart_str querystr = {0};
    6899          12 :         int ret = FAILURE;
    6900             : 
    6901             :         assert(pg_link != NULL);
    6902             :         assert(table != NULL);
    6903             :         assert(Z_TYPE_P(ids_array) == IS_ARRAY);
    6904             :         assert(!(opt & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_EXEC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)));
    6905             : 
    6906          12 :         if (zend_hash_num_elements(Z_ARRVAL_P(ids_array)) == 0) {
    6907           0 :                 return FAILURE;
    6908             :         }
    6909             : 
    6910          12 :         if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) {
    6911          11 :                 MAKE_STD_ZVAL(ids_converted);
    6912          11 :                 array_init(ids_converted);
    6913          11 :                 if (php_pgsql_convert(pg_link, table, ids_array, ids_converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) {
    6914           2 :                         goto cleanup;
    6915             :                 }
    6916           9 :                 ids_array = ids_converted;
    6917             :         }
    6918             : 
    6919          10 :         smart_str_appends(&querystr, "DELETE FROM ");
    6920          10 :         build_tablename(&querystr, pg_link, table);
    6921          10 :         smart_str_appends(&querystr, " WHERE ");
    6922             : 
    6923          10 :         if (build_assignment_string(pg_link, &querystr, Z_ARRVAL_P(ids_array), 1, " AND ", sizeof(" AND ")-1, opt TSRMLS_CC))
    6924           0 :                 goto cleanup;
    6925             : 
    6926          10 :         smart_str_appendc(&querystr, ';');
    6927          10 :         smart_str_0(&querystr);
    6928             : 
    6929          16 :         if ((opt & PGSQL_DML_EXEC) && do_exec(&querystr, PGRES_COMMAND_OK, pg_link, opt TSRMLS_CC) == 0) {
    6930           6 :                 ret = SUCCESS;
    6931           4 :         } else if (opt & PGSQL_DML_STRING) {
    6932           4 :                 ret = SUCCESS;
    6933             :         }
    6934             : 
    6935             : cleanup:
    6936          12 :         if (ids_converted) {
    6937             :                 zval_dtor(ids_converted);
    6938          11 :                 FREE_ZVAL(ids_converted);
    6939             :         }
    6940          17 :         if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) {
    6941           5 :                 *sql = querystr.c;
    6942             :         }
    6943             :         else {
    6944           7 :                 smart_str_free(&querystr);
    6945             :         }
    6946          12 :         return ret;
    6947             : }
    6948             : /* }}} */
    6949             : 
    6950             : /* {{{ proto mixed pg_delete(resource db, string table, array ids[, int options])
    6951             :    Delete records has ids (id=>value) */
    6952          12 : PHP_FUNCTION(pg_delete)
    6953             : {
    6954             :         zval *pgsql_link, *ids;
    6955          12 :         char *table, *sql = NULL;
    6956             :         int table_len;
    6957          12 :         ulong option = PGSQL_DML_EXEC;
    6958             :         PGconn *pg_link;
    6959          12 :         int id = -1, argc = ZEND_NUM_ARGS();
    6960             : 
    6961          12 :         if (zend_parse_parameters(argc TSRMLS_CC, "rsa|l",
    6962             :                                                           &pgsql_link, &table, &table_len, &ids, &option) == FAILURE) {
    6963           0 :                 return;
    6964             :         }
    6965          12 :         if (option & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)) {
    6966           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid option is specified");
    6967           0 :                 RETURN_FALSE;
    6968             :         }
    6969             :         
    6970          12 :         ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    6971             : 
    6972          12 :         if (php_pgsql_flush_query(pg_link TSRMLS_CC)) {
    6973           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected unhandled result(s) in connection");
    6974             :         }
    6975          12 :         if (php_pgsql_delete(pg_link, table, ids, option, &sql TSRMLS_CC) == FAILURE) {
    6976           2 :                 RETURN_FALSE;
    6977             :         }
    6978          10 :         if (option & PGSQL_DML_STRING) {
    6979           5 :                 RETURN_STRING(sql, 0);
    6980             :         }
    6981           5 :         RETURN_TRUE;
    6982             : } 
    6983             : /* }}} */
    6984             : 
    6985             : /* {{{ php_pgsql_result2array
    6986             :  */
    6987          11 : PHP_PGSQL_API int php_pgsql_result2array(PGresult *pg_result, zval *ret_array TSRMLS_DC) 
    6988             : {
    6989             :         zval *row;
    6990             :         char *field_name;
    6991             :         size_t num_fields;
    6992             :         int pg_numrows, pg_row;
    6993             :         uint i;
    6994             :         assert(Z_TYPE_P(ret_array) == IS_ARRAY);
    6995             : 
    6996          11 :         if ((pg_numrows = PQntuples(pg_result)) <= 0) {
    6997           0 :                 return FAILURE;
    6998             :         }
    6999          26 :         for (pg_row = 0; pg_row < pg_numrows; pg_row++) {
    7000          15 :                 MAKE_STD_ZVAL(row);
    7001          15 :                 array_init(row);
    7002          15 :                 add_index_zval(ret_array, pg_row, row);
    7003          50 :                 for (i = 0, num_fields = PQnfields(pg_result); i < num_fields; i++) {
    7004          35 :                         if (PQgetisnull(pg_result, pg_row, i)) {
    7005           6 :                                 field_name = PQfname(pg_result, i);
    7006           6 :                                 add_assoc_null(row, field_name);
    7007             :                         } else {
    7008          29 :                                 char *element = PQgetvalue(pg_result, pg_row, i);
    7009          29 :                                 if (element) {
    7010             :                                         char *data;
    7011             :                                         size_t data_len;
    7012          29 :                                         const size_t element_len = strlen(element);
    7013             : 
    7014          29 :                                         data = safe_estrndup(element, element_len);
    7015          29 :                                         data_len = element_len;
    7016             :                                         
    7017          29 :                                         field_name = PQfname(pg_result, i);
    7018          29 :                                         add_assoc_stringl(row, field_name, data, data_len, 0);
    7019             :                                 }
    7020             :                         }
    7021             :                 }
    7022             :         }
    7023          11 :         return SUCCESS;
    7024             : }
    7025             : /* }}} */
    7026             : 
    7027             : /* {{{ php_pgsql_select
    7028             :  */
    7029           7 : 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)
    7030             : {
    7031           7 :         zval *ids_converted = NULL;
    7032           7 :         smart_str querystr = {0};
    7033           7 :         int ret = FAILURE;
    7034             :         PGresult *pg_result;
    7035             : 
    7036             :         assert(pg_link != NULL);
    7037             :         assert(table != NULL);
    7038             :         assert(Z_TYPE_P(ids_array) == IS_ARRAY);
    7039             :         assert(Z_TYPE_P(ret_array) == IS_ARRAY);
    7040             :         assert(!(opt & ~(PGSQL_CONV_OPTS|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)));
    7041             : 
    7042           7 :         if (zend_hash_num_elements(Z_ARRVAL_P(ids_array)) == 0) {
    7043           0 :                 return FAILURE;
    7044             :         }
    7045             : 
    7046           7 :         if (!(opt & (PGSQL_DML_NO_CONV|PGSQL_DML_ESCAPE))) {
    7047           7 :                 MAKE_STD_ZVAL(ids_converted);
    7048           7 :                 array_init(ids_converted);
    7049           7 :                 if (php_pgsql_convert(pg_link, table, ids_array, ids_converted, (opt & PGSQL_CONV_OPTS) TSRMLS_CC) == FAILURE) {
    7050           2 :                         goto cleanup;
    7051             :                 }
    7052           5 :                 ids_array = ids_converted;
    7053             :         }
    7054             : 
    7055           5 :         smart_str_appends(&querystr, "SELECT * FROM ");
    7056           5 :         build_tablename(&querystr, pg_link, table);
    7057           5 :         smart_str_appends(&querystr, " WHERE ");
    7058             : 
    7059           5 :         if (build_assignment_string(pg_link, &querystr, Z_ARRVAL_P(ids_array), 1, " AND ", sizeof(" AND ")-1, opt TSRMLS_CC))
    7060           0 :                 goto cleanup;
    7061             : 
    7062           5 :         smart_str_appendc(&querystr, ';');
    7063           5 :         smart_str_0(&querystr);
    7064             : 
    7065           5 :         pg_result = PQexec(pg_link, querystr.c);
    7066           5 :         if (PQresultStatus(pg_result) == PGRES_TUPLES_OK) {
    7067           5 :                 ret = php_pgsql_result2array(pg_result, ret_array TSRMLS_CC);
    7068             :         } else {
    7069           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Failed to execute '%s'", querystr.c);
    7070             :         }
    7071           5 :         PQclear(pg_result);
    7072             : 
    7073             : cleanup:
    7074           7 :         if (ids_converted) {
    7075             :                 zval_dtor(ids_converted);
    7076           7 :                 FREE_ZVAL(ids_converted);
    7077             :         }
    7078           8 :         if (ret == SUCCESS && (opt & PGSQL_DML_STRING)) {
    7079           1 :                 *sql = querystr.c;
    7080             :         }
    7081             :         else {
    7082           6 :                 smart_str_free(&querystr);
    7083             :         }
    7084           7 :         return ret;
    7085             : }
    7086             : /* }}} */
    7087             : 
    7088             : /* {{{ proto mixed pg_select(resource db, string table, array ids[, int options])
    7089             :    Select records that has ids (id=>value) */
    7090           7 : PHP_FUNCTION(pg_select)
    7091             : {
    7092             :         zval *pgsql_link, *ids;
    7093           7 :         char *table, *sql = NULL;
    7094             :         int table_len;
    7095           7 :         ulong option = PGSQL_DML_EXEC;
    7096             :         PGconn *pg_link;
    7097           7 :         int id = -1, argc = ZEND_NUM_ARGS();
    7098             : 
    7099           7 :         if (zend_parse_parameters(argc TSRMLS_CC, "rsa|l",
    7100             :                                                           &pgsql_link, &table, &table_len, &ids, &option) == FAILURE) {
    7101           0 :                 return;
    7102             :         }
    7103           7 :         if (option & ~(PGSQL_CONV_FORCE_NULL|PGSQL_DML_NO_CONV|PGSQL_DML_EXEC|PGSQL_DML_ASYNC|PGSQL_DML_STRING|PGSQL_DML_ESCAPE)) {
    7104           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid option is specified");
    7105           0 :                 RETURN_FALSE;
    7106             :         }
    7107             :         
    7108           7 :         ZEND_FETCH_RESOURCE2(pg_link, PGconn *, &pgsql_link, id, "PostgreSQL link", le_link, le_plink);
    7109             : 
    7110           7 :         if (php_pgsql_flush_query(pg_link TSRMLS_CC)) {
    7111           0 :                 php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Detected unhandled result(s) in connection");
    7112             :         }
    7113           7 :         array_init(return_value);
    7114           7 :         if (php_pgsql_select(pg_link, table, ids, return_value, option, &sql TSRMLS_CC) == FAILURE) {
    7115             :                 zval_dtor(return_value);
    7116           2 :                 RETURN_FALSE;
    7117             :         }
    7118           5 :         if (option & PGSQL_DML_STRING) {
    7119             :                 zval_dtor(return_value);
    7120           1 :                 RETURN_STRING(sql, 0);
    7121             :         }
    7122           4 :         return;
    7123             : }
    7124             : /* }}} */
    7125             : 
    7126             : #endif
    7127             : 
    7128             : /*
    7129             :  * Local variables:
    7130             :  * tab-width: 4
    7131             :  * c-basic-offset: 4
    7132             :  * End:
    7133             :  * vim600: sw=4 ts=4 fdm=marker
    7134             :  * vim<600: sw=4 ts=4
    7135             :  */

Generated by: LCOV version 1.10

Generated at Wed, 16 Apr 2014 12:47:54 +0000 (8 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.