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: 1843 2980 61.8 %
Date: 2019-05-06 Functions: 125 137 91.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Mon, 06 May 2019 17:58:24 +0000 (992 days ago)

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