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: 1681 2798 60.1 %
Date: 2015-05-21 Functions: 126 138 91.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:59:01 +0000 (39 hours ago)

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