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/pdo_pgsql - pgsql_driver.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 461 600 76.8 %
Date: 2015-08-29 Functions: 32 35 91.4 %
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: Edin Kadribasic <edink@emini.dk>                            |
      16             :   |          Ilia Alshanestsky <ilia@prohost.org>                        |
      17             :   |          Wez Furlong <wez@php.net>                                   |
      18             :   +----------------------------------------------------------------------+
      19             : */
      20             : 
      21             : /* $Id$ */
      22             : 
      23             : #ifdef HAVE_CONFIG_H
      24             : #include "config.h"
      25             : #endif
      26             : 
      27             : #include "php.h"
      28             : #include "php_ini.h"
      29             : #include "ext/standard/info.h"
      30             : #include "ext/standard/php_string.h"
      31             : #include "main/php_network.h"
      32             : #include "pdo/php_pdo.h"
      33             : #include "pdo/php_pdo_driver.h"
      34             : #include "pdo/php_pdo_error.h"
      35             : #include "ext/standard/file.h"
      36             : 
      37             : #undef PACKAGE_BUGREPORT
      38             : #undef PACKAGE_NAME
      39             : #undef PACKAGE_STRING
      40             : #undef PACKAGE_TARNAME
      41             : #undef PACKAGE_VERSION
      42             : #include "pg_config.h" /* needed for PG_VERSION */
      43             : #include "php_pdo_pgsql.h"
      44             : #include "php_pdo_pgsql_int.h"
      45             : #include "zend_exceptions.h"
      46             : 
      47         445 : static char * _pdo_pgsql_trim_message(const char *message, int persistent)
      48             : {
      49         445 :         register int i = strlen(message)-1;
      50             :         char *tmp;
      51             : 
      52         445 :         if (i>1 && (message[i-1] == '\r' || message[i-1] == '\n') && message[i] == '.') {
      53           0 :                 --i;
      54             :         }
      55        1335 :         while (i>0 && (message[i] == '\r' || message[i] == '\n')) {
      56         445 :                 --i;
      57             :         }
      58         445 :         ++i;
      59         890 :         tmp = pemalloc(i + 1, persistent);
      60         445 :         memcpy(tmp, message, i);
      61         445 :         tmp[i] = '\0';
      62             : 
      63         445 :         return tmp;
      64             : }
      65             : 
      66         324 : static zend_string* _pdo_pgsql_escape_credentials(char *str)
      67             : {
      68         324 :         if (str) {
      69           0 :                 zend_string *tmp = zend_string_init(str, strlen(str), 0);
      70             : 
      71           0 :                 return php_addcslashes(tmp, 1, "\\'", sizeof("\\'"));
      72             :         }
      73             : 
      74         324 :         return NULL;
      75             : }
      76             : 
      77         447 : int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *sqlstate, const char *msg, const char *file, int line) /* {{{ */
      78             : {
      79         447 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
      80         447 :         pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code;
      81         447 :         pdo_pgsql_error_info *einfo = &H->einfo;
      82         447 :         char *errmsg = PQerrorMessage(H->server);
      83             : 
      84         447 :         einfo->errcode = errcode;
      85         447 :         einfo->file = file;
      86         447 :         einfo->line = line;
      87             : 
      88         447 :         if (einfo->errmsg) {
      89         286 :                 pefree(einfo->errmsg, dbh->is_persistent);
      90         286 :                 einfo->errmsg = NULL;
      91             :         }
      92             : 
      93         449 :         if (sqlstate == NULL || strlen(sqlstate) >= sizeof(pdo_error_type)) {
      94           2 :                 strcpy(*pdo_err, "HY000");
      95             :         }
      96             :         else {
      97         445 :                 strcpy(*pdo_err, sqlstate);
      98             :         }
      99             : 
     100         447 :         if (msg) {
     101           2 :                 einfo->errmsg = estrdup(msg);
     102             :         }
     103         445 :         else if (errmsg) {
     104         445 :                 einfo->errmsg = _pdo_pgsql_trim_message(errmsg, dbh->is_persistent);
     105             :         }
     106             : 
     107         447 :         if (!dbh->methods) {
     108           0 :                 zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode, "SQLSTATE[%s] [%d] %s",
     109             :                                 *pdo_err, einfo->errcode, einfo->errmsg);
     110             :         }
     111             : 
     112         447 :         return errcode;
     113             : }
     114             : /* }}} */
     115             : 
     116          53 : static void _pdo_pgsql_notice(pdo_dbh_t *dbh, const char *message) /* {{{ */
     117             : {
     118             : /*      pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; */
     119          53 : }
     120             : /* }}} */
     121             : 
     122          38 : static int pdo_pgsql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info) /* {{{ */
     123             : {
     124          38 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     125          38 :         pdo_pgsql_error_info *einfo = &H->einfo;
     126             : 
     127          38 :         if (einfo->errcode) {
     128          38 :                 add_next_index_long(info, einfo->errcode);
     129          38 :                 add_next_index_string(info, einfo->errmsg);
     130             :         }
     131             : 
     132          38 :         return 1;
     133             : }
     134             : /* }}} */
     135             : 
     136             : /* {{{ pdo_pgsql_create_lob_stream */
     137           1 : static size_t pgsql_lob_write(php_stream *stream, const char *buf, size_t count)
     138             : {
     139           1 :         struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
     140           1 :         return lo_write(self->conn, self->lfd, (char*)buf, count);
     141             : }
     142             : 
     143           2 : static size_t pgsql_lob_read(php_stream *stream, char *buf, size_t count)
     144             : {
     145           2 :         struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
     146           2 :         return lo_read(self->conn, self->lfd, buf, count);
     147             : }
     148             : 
     149           3 : static int pgsql_lob_close(php_stream *stream, int close_handle)
     150             : {
     151           3 :         struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
     152             : 
     153           3 :         if (close_handle) {
     154           3 :                 lo_close(self->conn, self->lfd);
     155             :         }
     156           3 :         zval_ptr_dtor(&self->dbh);
     157           3 :         efree(self);
     158           3 :         return 0;
     159             : }
     160             : 
     161           1 : static int pgsql_lob_flush(php_stream *stream)
     162             : {
     163           1 :         return 0;
     164             : }
     165             : 
     166           0 : static int pgsql_lob_seek(php_stream *stream, zend_off_t offset, int whence,
     167             :                 zend_off_t *newoffset)
     168             : {
     169           0 :         struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
     170             : #if HAVE_PG_LO64 && ZEND_ENABLE_ZVAL_LONG64
     171             :         zend_off_t pos = lo_lseek64(self->conn, self->lfd, offset, whence);
     172             : #else
     173           0 :         zend_off_t pos = lo_lseek(self->conn, self->lfd, offset, whence);
     174             : #endif
     175           0 :         *newoffset = pos;
     176           0 :         return pos >= 0 ? 0 : -1;
     177             : }
     178             : 
     179             : php_stream_ops pdo_pgsql_lob_stream_ops = {
     180             :         pgsql_lob_write,
     181             :         pgsql_lob_read,
     182             :         pgsql_lob_close,
     183             :         pgsql_lob_flush,
     184             :         "pdo_pgsql lob stream",
     185             :         pgsql_lob_seek,
     186             :         NULL,
     187             :         NULL,
     188             :         NULL
     189             : };
     190             : 
     191           3 : php_stream *pdo_pgsql_create_lob_stream(zval *dbh, int lfd, Oid oid)
     192             : {
     193             :         php_stream *stm;
     194           3 :         struct pdo_pgsql_lob_self *self = ecalloc(1, sizeof(*self));
     195           3 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)(Z_PDO_DBH_P(dbh))->driver_data;
     196             : 
     197           3 :         ZVAL_COPY_VALUE(&self->dbh, dbh);
     198           3 :         self->lfd = lfd;
     199           3 :         self->oid = oid;
     200           3 :         self->conn = H->server;
     201             : 
     202           3 :         stm = php_stream_alloc(&pdo_pgsql_lob_stream_ops, self, 0, "r+b");
     203             : 
     204           3 :         if (stm) {
     205             :                 Z_ADDREF_P(dbh);
     206           3 :                 return stm;
     207             :         }
     208             : 
     209           0 :         efree(self);
     210           0 :         return NULL;
     211             : }
     212             : /* }}} */
     213             : 
     214         162 : static int pgsql_handle_closer(pdo_dbh_t *dbh) /* {{{ */
     215             : {
     216         162 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     217         162 :         if (H) {
     218         162 :                 if (H->server) {
     219         162 :                         PQfinish(H->server);
     220         162 :                         H->server = NULL;
     221             :                 }
     222         162 :                 if (H->einfo.errmsg) {
     223         161 :                         pefree(H->einfo.errmsg, dbh->is_persistent);
     224         161 :                         H->einfo.errmsg = NULL;
     225             :                 }
     226         162 :                 pefree(H, dbh->is_persistent);
     227         162 :                 dbh->driver_data = NULL;
     228             :         }
     229         162 :         return 0;
     230             : }
     231             : /* }}} */
     232             : 
     233         173 : static int pgsql_handle_preparer(pdo_dbh_t *dbh, const char *sql, size_t sql_len, pdo_stmt_t *stmt, zval *driver_options)
     234             : {
     235         173 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     236         173 :         pdo_pgsql_stmt *S = ecalloc(1, sizeof(pdo_pgsql_stmt));
     237             :         int scrollable;
     238             :         int ret;
     239         173 :         char *nsql = NULL;
     240         173 :         size_t nsql_len = 0;
     241         173 :         int emulate = 0;
     242         173 :         int execute_only = 0;
     243             : 
     244         173 :         S->H = H;
     245         173 :         stmt->driver_data = S;
     246         173 :         stmt->methods = &pgsql_stmt_methods;
     247             : 
     248         173 :         scrollable = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR,
     249             :                 PDO_CURSOR_FWDONLY) == PDO_CURSOR_SCROLL;
     250             : 
     251         173 :         if (scrollable) {
     252           2 :                 if (S->cursor_name) {
     253           0 :                         efree(S->cursor_name);
     254             :                 }
     255           2 :                 spprintf(&S->cursor_name, 0, "pdo_crsr_%08x", ++H->stmt_counter);
     256           2 :                 emulate = 1;
     257         171 :         } else if (driver_options) {
     258          18 :                 if (pdo_attr_lval(driver_options, PDO_ATTR_EMULATE_PREPARES, H->emulate_prepares) == 1) {
     259           5 :                         emulate = 1;
     260             :                 }
     261          18 :                 if (pdo_attr_lval(driver_options, PDO_PGSQL_ATTR_DISABLE_PREPARES, H->disable_prepares) == 1) {
     262           1 :                         execute_only = 1;
     263             :                 }
     264             :         } else {
     265         153 :                 emulate = H->disable_native_prepares || H->emulate_prepares;
     266         153 :                 execute_only = H->disable_prepares;
     267             :         }
     268             : 
     269         173 :         if (!emulate && PQprotocolVersion(H->server) > 2) {
     270         160 :                 stmt->supports_placeholders = PDO_PLACEHOLDER_NAMED;
     271         160 :                 stmt->named_rewrite_template = "$%d";
     272         160 :                 ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len);
     273             : 
     274         160 :                 if (ret == 1) {
     275             :                         /* query was re-written */
     276          46 :                         sql = nsql;
     277         114 :                 } else if (ret == -1) {
     278             :                         /* couldn't grok it */
     279           0 :                         strcpy(dbh->error_code, stmt->error_code);
     280           0 :                         return 0;
     281             :                 }
     282             : 
     283         160 :                 if (!execute_only) {
     284             :                         /* prepared query: set the query name and defer the
     285             :                            actual prepare until the first execute call */
     286         159 :                         spprintf(&S->stmt_name, 0, "pdo_stmt_%08x", ++H->stmt_counter);
     287             :                 }
     288             : 
     289         160 :                 if (nsql) {
     290          46 :                         S->query = nsql;
     291             :                 } else {
     292         114 :                         S->query = estrdup(sql);
     293             :                 }
     294             : 
     295         160 :                 return 1;
     296             :         }
     297             : 
     298          13 :         stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
     299          13 :         return 1;
     300             : }
     301             : 
     302         652 : static zend_long pgsql_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len)
     303             : {
     304         652 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     305             :         PGresult *res;
     306         652 :         zend_long ret = 1;
     307             :         ExecStatusType qs;
     308             : 
     309         652 :         if (!(res = PQexec(H->server, sql))) {
     310             :                 /* fatal error */
     311           0 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
     312           0 :                 return -1;
     313             :         }
     314         652 :         qs = PQresultStatus(res);
     315         652 :         if (qs != PGRES_COMMAND_OK && qs != PGRES_TUPLES_OK) {
     316         427 :                 pdo_pgsql_error(dbh, qs, pdo_pgsql_sqlstate(res));
     317         427 :                 PQclear(res);
     318         427 :                 return -1;
     319             :         }
     320         225 :         H->pgoid = PQoidValue(res);
     321         225 :         if (qs == PGRES_COMMAND_OK) {
     322         224 :                 ZEND_ATOL(ret, PQcmdTuples(res));
     323             :         } else {
     324           1 :                 ret = Z_L(0);
     325             :         }
     326         225 :         PQclear(res);
     327             : 
     328         225 :         return ret;
     329             : }
     330             : 
     331          58 : static int pgsql_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype)
     332             : {
     333             :         unsigned char *escaped;
     334          58 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     335             :         size_t tmp_len;
     336             : 
     337          58 :         switch (paramtype) {
     338             :                 case PDO_PARAM_LOB:
     339             :                         /* escapedlen returned by PQescapeBytea() accounts for trailing 0 */
     340           0 :                         escaped = PQescapeByteaConn(H->server, (unsigned char *)unquoted, unquotedlen, &tmp_len);
     341           0 :                         *quotedlen = tmp_len + 1;
     342           0 :                         *quoted = emalloc(*quotedlen + 1);
     343           0 :                         memcpy((*quoted)+1, escaped, *quotedlen-2);
     344           0 :                         (*quoted)[0] = '\'';
     345           0 :                         (*quoted)[*quotedlen-1] = '\'';
     346           0 :                         (*quoted)[*quotedlen] = '\0';
     347           0 :                         PQfreemem(escaped);
     348           0 :                         break;
     349             :                 default:
     350          58 :                         *quoted = safe_emalloc(2, unquotedlen, 3);
     351          58 :                         (*quoted)[0] = '\'';
     352          58 :                         *quotedlen = PQescapeStringConn(H->server, *quoted + 1, unquoted, unquotedlen, NULL);
     353          58 :                         (*quoted)[*quotedlen + 1] = '\'';
     354          58 :                         (*quoted)[*quotedlen + 2] = '\0';
     355          58 :                         *quotedlen += 2;
     356             :         }
     357          58 :         return 1;
     358             : }
     359             : 
     360           0 : static char *pdo_pgsql_last_insert_id(pdo_dbh_t *dbh, const char *name, size_t *len)
     361             : {
     362           0 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     363           0 :         char *id = NULL;
     364             : 
     365           0 :         if (name == NULL) {
     366           0 :                 if (H->pgoid == InvalidOid) {
     367           0 :                         return NULL;
     368             :                 }
     369           0 :                 *len = spprintf(&id, 0, ZEND_LONG_FMT, (zend_long) H->pgoid);
     370             :         } else {
     371             :                 PGresult *res;
     372             :                 ExecStatusType status;
     373             :                 const char *q[1];
     374           0 :                 q[0] = name;
     375           0 :                 res = PQexecParams(H->server, "SELECT CURRVAL($1)", 1, NULL, q, NULL, NULL, 0);
     376           0 :                 status = PQresultStatus(res);
     377             : 
     378           0 :                 if (res && (status == PGRES_TUPLES_OK)) {
     379           0 :                         id = estrdup((char *)PQgetvalue(res, 0, 0));
     380           0 :                         *len = PQgetlength(res, 0, 0);
     381             :                 } else {
     382           0 :                         pdo_pgsql_error(dbh, status, pdo_pgsql_sqlstate(res));
     383             :                 }
     384             : 
     385           0 :                 if (res) {
     386           0 :                         PQclear(res);
     387             :                 }
     388             :         }
     389           0 :         return id;
     390             : }
     391             : 
     392          22 : static int pdo_pgsql_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value)
     393             : {
     394          22 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     395             : 
     396          22 :         switch (attr) {
     397             :                 case PDO_ATTR_EMULATE_PREPARES:
     398           3 :                         ZVAL_BOOL(return_value, H->emulate_prepares);
     399           3 :                         break;
     400             : 
     401             :                 case PDO_PGSQL_ATTR_DISABLE_PREPARES:
     402           3 :                         ZVAL_BOOL(return_value, H->disable_prepares);
     403           3 :                         break;
     404             : 
     405             :                 case PDO_ATTR_CLIENT_VERSION:
     406           4 :                         ZVAL_STRING(return_value, PG_VERSION);
     407           2 :                         break;
     408             : 
     409             :                 case PDO_ATTR_SERVER_VERSION:
     410           3 :                         if (PQprotocolVersion(H->server) >= 3) { /* PostgreSQL 7.4 or later */
     411           6 :                                 ZVAL_STRING(return_value, (char*)PQparameterStatus(H->server, "server_version"));
     412             :                         } else /* emulate above via a query */
     413             :                         {
     414           0 :                                 PGresult *res = PQexec(H->server, "SELECT VERSION()");
     415           0 :                                 if (res && PQresultStatus(res) == PGRES_TUPLES_OK) {
     416           0 :                                         ZVAL_STRING(return_value, (char *)PQgetvalue(res, 0, 0));
     417             :                                 }
     418             : 
     419           0 :                                 if (res) {
     420           0 :                                         PQclear(res);
     421             :                                 }
     422             :                         }
     423           3 :                         break;
     424             : 
     425             :                 case PDO_ATTR_CONNECTION_STATUS:
     426           1 :                         switch (PQstatus(H->server)) {
     427             :                                 case CONNECTION_STARTED:
     428           0 :                                         ZVAL_STRINGL(return_value, "Waiting for connection to be made.", sizeof("Waiting for connection to be made.")-1);
     429           0 :                                         break;
     430             : 
     431             :                                 case CONNECTION_MADE:
     432             :                                 case CONNECTION_OK:
     433           2 :                                         ZVAL_STRINGL(return_value, "Connection OK; waiting to send.", sizeof("Connection OK; waiting to send.")-1);
     434           1 :                                         break;
     435             : 
     436             :                                 case CONNECTION_AWAITING_RESPONSE:
     437           0 :                                         ZVAL_STRINGL(return_value, "Waiting for a response from the server.", sizeof("Waiting for a response from the server.")-1);
     438           0 :                                         break;
     439             : 
     440             :                                 case CONNECTION_AUTH_OK:
     441           0 :                                         ZVAL_STRINGL(return_value, "Received authentication; waiting for backend start-up to finish.", sizeof("Received authentication; waiting for backend start-up to finish.")-1);
     442           0 :                                         break;
     443             : #ifdef CONNECTION_SSL_STARTUP
     444             :                                 case CONNECTION_SSL_STARTUP:
     445             :                                         ZVAL_STRINGL(return_value, "Negotiating SSL encryption.", sizeof("Negotiating SSL encryption.")-1);
     446             :                                         break;
     447             : #endif
     448             :                                 case CONNECTION_SETENV:
     449           0 :                                         ZVAL_STRINGL(return_value, "Negotiating environment-driven parameter settings.", sizeof("Negotiating environment-driven parameter settings.")-1);
     450           0 :                                         break;
     451             : 
     452             :                                 case CONNECTION_BAD:
     453             :                                 default:
     454           0 :                                         ZVAL_STRINGL(return_value, "Bad connection.", sizeof("Bad connection.")-1);
     455             :                                         break;
     456             :                         }
     457           1 :                         break;
     458             : 
     459             :                 case PDO_ATTR_SERVER_INFO: {
     460           1 :                         int spid = PQbackendPID(H->server);
     461             : 
     462             : 
     463             :                         zend_string *str_info =
     464           4 :                                 strpprintf(0,
     465             :                                         "PID: %d; Client Encoding: %s; Is Superuser: %s; Session Authorization: %s; Date Style: %s",
     466             :                                         spid,
     467           1 :                                         (char*)PQparameterStatus(H->server, "client_encoding"),
     468           1 :                                         (char*)PQparameterStatus(H->server, "is_superuser"),
     469           1 :                                         (char*)PQparameterStatus(H->server, "session_authorization"),
     470           2 :                                         (char*)PQparameterStatus(H->server, "DateStyle"));
     471             : 
     472           1 :                         ZVAL_STR(return_value, str_info);
     473             :                 }
     474           1 :                         break;
     475             : 
     476             :                 default:
     477           9 :                         return 0;
     478             :         }
     479             : 
     480          13 :         return 1;
     481             : }
     482             : 
     483             : /* {{{ */
     484           0 : static int pdo_pgsql_check_liveness(pdo_dbh_t *dbh)
     485             : {
     486           0 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     487           0 :         if (PQstatus(H->server) == CONNECTION_BAD) {
     488           0 :                 PQreset(H->server);
     489             :         }
     490           0 :         return (PQstatus(H->server) == CONNECTION_OK) ? SUCCESS : FAILURE;
     491             : }
     492             : /* }}} */
     493             : 
     494           5 : static int pgsql_handle_in_transaction(pdo_dbh_t *dbh)
     495             : {
     496             :         pdo_pgsql_db_handle *H;
     497             : 
     498           5 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     499             : 
     500           5 :         return PQtransactionStatus(H->server) > PQTRANS_IDLE;
     501             : }
     502             : 
     503          44 : static int pdo_pgsql_transaction_cmd(const char *cmd, pdo_dbh_t *dbh)
     504             : {
     505          44 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     506             :         PGresult *res;
     507          44 :         int ret = 1;
     508             : 
     509          44 :         res = PQexec(H->server, cmd);
     510             : 
     511          44 :         if (PQresultStatus(res) != PGRES_COMMAND_OK) {
     512           0 :                 pdo_pgsql_error(dbh, PQresultStatus(res), pdo_pgsql_sqlstate(res));
     513           0 :                 ret = 0;
     514             :         }
     515             : 
     516          44 :         PQclear(res);
     517          44 :         return ret;
     518             : }
     519             : 
     520          22 : static int pgsql_handle_begin(pdo_dbh_t *dbh)
     521             : {
     522          22 :         return pdo_pgsql_transaction_cmd("BEGIN", dbh);
     523             : }
     524             : 
     525           4 : static int pgsql_handle_commit(pdo_dbh_t *dbh)
     526             : {
     527           4 :         int ret = pdo_pgsql_transaction_cmd("COMMIT", dbh);
     528             : 
     529             :         /* When deferred constraints are used the commit could
     530             :            fail, and a ROLLBACK implicitly ran. See bug #67462 */
     531           4 :         if (!ret) {
     532           0 :                 dbh->in_txn = pgsql_handle_in_transaction(dbh);
     533             :         }
     534             : 
     535           4 :         return ret;
     536             : }
     537             : 
     538          18 : static int pgsql_handle_rollback(pdo_dbh_t *dbh)
     539             : {
     540          18 :         return pdo_pgsql_transaction_cmd("ROLLBACK", dbh);
     541             : }
     542             : 
     543             : /* {{{ proto string PDO::pgsqlCopyFromArray(string $table_name , array $rows [, string $delimiter [, string $null_as ] [, string $fields])
     544             :    Returns true if the copy worked fine or false if error */
     545           4 : static PHP_METHOD(PDO, pgsqlCopyFromArray)
     546             : {
     547             :         pdo_dbh_t *dbh;
     548             :         pdo_pgsql_db_handle *H;
     549             : 
     550             :         zval *pg_rows;
     551             : 
     552           4 :         char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
     553           4 :         size_t table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
     554             :         char *query;
     555             : 
     556             :         PGresult *pgsql_result;
     557             :         ExecStatusType status;
     558             : 
     559           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s/a|sss",
     560             :                                         &table_name, &table_name_len, &pg_rows,
     561             :                                         &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
     562           0 :                 return;
     563             :         }
     564             : 
     565           4 :         if (!zend_hash_num_elements(Z_ARRVAL_P(pg_rows))) {
     566           0 :                 php_error_docref(NULL, E_WARNING, "Cannot copy from an empty array");
     567           0 :                 RETURN_FALSE;
     568             :         }
     569             : 
     570           4 :         dbh = Z_PDO_DBH_P(getThis());
     571           4 :         PDO_CONSTRUCT_CHECK;
     572           4 :         PDO_DBH_CLEAR_ERR();
     573             : 
     574             :         /* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
     575           4 :         if (pg_fields) {
     576           2 :                 spprintf(&query, 0, "COPY %s (%s) FROM STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
     577             :         } else {
     578           2 :                 spprintf(&query, 0, "COPY %s FROM STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
     579             :         }
     580             : 
     581             :         /* Obtain db Handle */
     582           4 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     583             : 
     584           8 :         while ((pgsql_result = PQgetResult(H->server))) {
     585           0 :                 PQclear(pgsql_result);
     586             :         }
     587           4 :         pgsql_result = PQexec(H->server, query);
     588             : 
     589           4 :         efree(query);
     590           4 :         query = NULL;
     591             : 
     592           4 :         if (pgsql_result) {
     593           4 :                 status = PQresultStatus(pgsql_result);
     594             :         } else {
     595           0 :                 status = (ExecStatusType) PQstatus(H->server);
     596             :         }
     597             : 
     598           4 :         if (status == PGRES_COPY_IN && pgsql_result) {
     599           3 :                 int command_failed = 0;
     600           3 :                 size_t buffer_len = 0;
     601             :                 zval *tmp;
     602             : 
     603           3 :                 PQclear(pgsql_result);
     604          21 :                 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pg_rows), tmp) {
     605             :                         size_t query_len;
     606           9 :                         convert_to_string_ex(tmp);
     607             : 
     608           9 :                         if (buffer_len < Z_STRLEN_P(tmp)) {
     609           3 :                                 buffer_len = Z_STRLEN_P(tmp);
     610           3 :                                 query = erealloc(query, buffer_len + 2); /* room for \n\0 */
     611             :                         }
     612           9 :                         memcpy(query, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
     613           9 :                         query_len = Z_STRLEN_P(tmp);
     614           9 :                         if (query[query_len - 1] != '\n') {
     615           9 :                                 query[query_len++] = '\n';
     616             :                         }
     617           9 :                         query[query_len] = '\0';
     618           9 :                         if (PQputCopyData(H->server, query, query_len) != 1) {
     619           0 :                                 efree(query);
     620           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
     621           0 :                                 PDO_HANDLE_DBH_ERR();
     622           0 :                                 RETURN_FALSE;
     623             :                         }
     624             :                 } ZEND_HASH_FOREACH_END();
     625           3 :                 if (query) {
     626           3 :                         efree(query);
     627             :                 }
     628             : 
     629           3 :                 if (PQputCopyEnd(H->server, NULL) != 1) {
     630           0 :                         pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
     631           0 :                         PDO_HANDLE_DBH_ERR();
     632           0 :                         RETURN_FALSE;
     633             :                 }
     634             : 
     635           9 :                 while ((pgsql_result = PQgetResult(H->server))) {
     636           3 :                         if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
     637           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
     638           0 :                                 command_failed = 1;
     639             :                         }
     640           3 :                         PQclear(pgsql_result);
     641             :                 }
     642             : 
     643           3 :                 PDO_HANDLE_DBH_ERR();
     644           3 :                 RETURN_BOOL(!command_failed);
     645             :         } else {
     646           1 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
     647           1 :                 PQclear(pgsql_result);
     648           1 :                 PDO_HANDLE_DBH_ERR();
     649           1 :                 RETURN_FALSE;
     650             :         }
     651             : }
     652             : /* }}} */
     653             : 
     654             : /* {{{ proto string PDO::pgsqlCopyFromFile(string $table_name , string $filename [, string $delimiter [, string $null_as ] [, string $fields])
     655             :    Returns true if the copy worked fine or false if error */
     656           5 : static PHP_METHOD(PDO, pgsqlCopyFromFile)
     657             : {
     658             :         pdo_dbh_t *dbh;
     659             :         pdo_pgsql_db_handle *H;
     660             : 
     661           5 :         char *table_name, *filename, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
     662           5 :         size_t  table_name_len, filename_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
     663             :         char *query;
     664             :         PGresult *pgsql_result;
     665             :         ExecStatusType status;
     666             :         php_stream *stream;
     667             : 
     668           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "sp|sss",
     669             :                                 &table_name, &table_name_len, &filename, &filename_len,
     670             :                                 &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
     671           0 :                 return;
     672             :         }
     673             : 
     674             :         /* Obtain db Handler */
     675           5 :         dbh = Z_PDO_DBH_P(getThis());
     676           5 :         PDO_CONSTRUCT_CHECK;
     677           5 :         PDO_DBH_CLEAR_ERR();
     678             : 
     679           5 :         stream = php_stream_open_wrapper_ex(filename, "rb", 0, NULL, FG(default_context));
     680           5 :         if (!stream) {
     681           1 :                 pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to open the file");
     682           1 :                 PDO_HANDLE_DBH_ERR();
     683           1 :                 RETURN_FALSE;
     684             :         }
     685             : 
     686             :         /* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
     687           4 :         if (pg_fields) {
     688           2 :                 spprintf(&query, 0, "COPY %s (%s) FROM STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
     689             :         } else {
     690           2 :                 spprintf(&query, 0, "COPY %s FROM STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
     691             :         }
     692             : 
     693           4 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     694             : 
     695           8 :         while ((pgsql_result = PQgetResult(H->server))) {
     696           0 :                 PQclear(pgsql_result);
     697             :         }
     698           4 :         pgsql_result = PQexec(H->server, query);
     699             : 
     700           4 :         efree(query);
     701             : 
     702           4 :         if (pgsql_result) {
     703           4 :                 status = PQresultStatus(pgsql_result);
     704             :         } else {
     705           0 :                 status = (ExecStatusType) PQstatus(H->server);
     706             :         }
     707             : 
     708           4 :         if (status == PGRES_COPY_IN && pgsql_result) {
     709             :                 char *buf;
     710           3 :                 int command_failed = 0;
     711           3 :                 size_t line_len = 0;
     712             : 
     713           3 :                 PQclear(pgsql_result);
     714          15 :                 while ((buf = php_stream_get_line(stream, NULL, 0, &line_len)) != NULL) {
     715           9 :                         if (PQputCopyData(H->server, buf, line_len) != 1) {
     716           0 :                                 efree(buf);
     717           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
     718           0 :                                 php_stream_close(stream);
     719           0 :                                 PDO_HANDLE_DBH_ERR();
     720           0 :                                 RETURN_FALSE;
     721             :                         }
     722           9 :                         efree(buf);
     723             :                 }
     724           3 :                 php_stream_close(stream);
     725             : 
     726           3 :                 if (PQputCopyEnd(H->server, NULL) != 1) {
     727           0 :                         pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
     728           0 :                         PDO_HANDLE_DBH_ERR();
     729           0 :                         RETURN_FALSE;
     730             :                 }
     731             : 
     732           9 :                 while ((pgsql_result = PQgetResult(H->server))) {
     733           3 :                         if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
     734           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
     735           0 :                                 command_failed = 1;
     736             :                         }
     737           3 :                         PQclear(pgsql_result);
     738             :                 }
     739             : 
     740           3 :                 PDO_HANDLE_DBH_ERR();
     741           3 :                 RETURN_BOOL(!command_failed);
     742             :         } else {
     743           1 :                 php_stream_close(stream);
     744           1 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
     745           1 :                 PQclear(pgsql_result);
     746           1 :                 PDO_HANDLE_DBH_ERR();
     747           1 :                 RETURN_FALSE;
     748             :         }
     749             : }
     750             : /* }}} */
     751             : 
     752             : 
     753             : /* {{{ proto string PDO::pgsqlCopyToFile(string $table_name , string $filename, [string $delimiter [, string $null_as [, string $fields]]])
     754             :    Returns true if the copy worked fine or false if error */
     755           5 : static PHP_METHOD(PDO, pgsqlCopyToFile)
     756             : {
     757             :         pdo_dbh_t *dbh;
     758             :         pdo_pgsql_db_handle *H;
     759             : 
     760           5 :         char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL, *filename = NULL;
     761           5 :         size_t table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len, filename_len;
     762             :         char *query;
     763             : 
     764             :         PGresult *pgsql_result;
     765             :         ExecStatusType status;
     766             : 
     767             :         php_stream *stream;
     768             : 
     769           5 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "sp|sss",
     770             :                                         &table_name, &table_name_len, &filename, &filename_len,
     771             :                                         &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
     772           0 :                 return;
     773             :         }
     774             : 
     775           5 :         dbh = Z_PDO_DBH_P(getThis());
     776           5 :         PDO_CONSTRUCT_CHECK;
     777           5 :         PDO_DBH_CLEAR_ERR();
     778             : 
     779           5 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     780             : 
     781           5 :         stream = php_stream_open_wrapper_ex(filename, "wb", 0, NULL, FG(default_context));
     782           5 :         if (!stream) {
     783           1 :                 pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to open the file for writing");
     784           1 :                 PDO_HANDLE_DBH_ERR();
     785           1 :                 RETURN_FALSE;
     786             :         }
     787             : 
     788           8 :         while ((pgsql_result = PQgetResult(H->server))) {
     789           0 :                 PQclear(pgsql_result);
     790             :         }
     791             : 
     792             :         /* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
     793           4 :         if (pg_fields) {
     794           1 :                 spprintf(&query, 0, "COPY %s (%s) TO STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
     795             :         } else {
     796           3 :                 spprintf(&query, 0, "COPY %s TO STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
     797             :         }
     798           4 :         pgsql_result = PQexec(H->server, query);
     799           4 :         efree(query);
     800             : 
     801           4 :         if (pgsql_result) {
     802           4 :                 status = PQresultStatus(pgsql_result);
     803             :         } else {
     804           0 :                 status = (ExecStatusType) PQstatus(H->server);
     805             :         }
     806             : 
     807           4 :         if (status == PGRES_COPY_OUT && pgsql_result) {
     808           3 :                 PQclear(pgsql_result);
     809             :                 while (1) {
     810          12 :                         char *csv = NULL;
     811          12 :                         int ret = PQgetCopyData(H->server, &csv, 0);
     812             : 
     813          12 :                         if (ret == -1) {
     814           3 :                                 break; /* done */
     815           9 :                         } else if (ret > 0) {
     816           9 :                                 if (php_stream_write(stream, csv, ret) != ret) {
     817           0 :                                         pdo_pgsql_error_msg(dbh, PGRES_FATAL_ERROR, "Unable to write to file");
     818           0 :                                         PQfreemem(csv);
     819           0 :                                         php_stream_close(stream);
     820           0 :                                         PDO_HANDLE_DBH_ERR();
     821           0 :                                         RETURN_FALSE;
     822             :                                 } else {
     823           9 :                                         PQfreemem(csv);
     824             :                                 }
     825             :                         } else {
     826           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
     827           0 :                                 php_stream_close(stream);
     828           0 :                                 PDO_HANDLE_DBH_ERR();
     829           0 :                                 RETURN_FALSE;
     830             :                         }
     831           9 :                 }
     832           3 :                 php_stream_close(stream);
     833             : 
     834           9 :                 while ((pgsql_result = PQgetResult(H->server))) {
     835           3 :                         PQclear(pgsql_result);
     836             :                 }
     837           3 :                 RETURN_TRUE;
     838             :         } else {
     839           1 :                 php_stream_close(stream);
     840           1 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
     841           1 :                 PQclear(pgsql_result);
     842           1 :                 PDO_HANDLE_DBH_ERR();
     843           1 :                 RETURN_FALSE;
     844             :         }
     845             : }
     846             : /* }}} */
     847             : 
     848             : /* {{{ proto string PDO::pgsqlCopyToArray(string $table_name , [string $delimiter [, string $null_as [, string $fields]]])
     849             :    Returns true if the copy worked fine or false if error */
     850           4 : static PHP_METHOD(PDO, pgsqlCopyToArray)
     851             : {
     852             :         pdo_dbh_t *dbh;
     853             :         pdo_pgsql_db_handle *H;
     854             : 
     855           4 :         char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
     856           4 :         size_t table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
     857             :         char *query;
     858             : 
     859             :         PGresult *pgsql_result;
     860             :         ExecStatusType status;
     861             : 
     862           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|sss",
     863             :                 &table_name, &table_name_len,
     864             :                 &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
     865           0 :                 return;
     866             :         }
     867             : 
     868           4 :         dbh = Z_PDO_DBH_P(getThis());
     869           4 :         PDO_CONSTRUCT_CHECK;
     870           4 :         PDO_DBH_CLEAR_ERR();
     871             : 
     872           4 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     873             : 
     874           8 :         while ((pgsql_result = PQgetResult(H->server))) {
     875           0 :                 PQclear(pgsql_result);
     876             :         }
     877             : 
     878             :         /* using pre-9.0 syntax as PDO_pgsql is 7.4+ compatible */
     879           4 :         if (pg_fields) {
     880           1 :                 spprintf(&query, 0, "COPY %s (%s) TO STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
     881             :         } else {
     882           3 :                 spprintf(&query, 0, "COPY %s TO STDIN WITH DELIMITER E'%c' NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
     883             :         }
     884           4 :         pgsql_result = PQexec(H->server, query);
     885           4 :         efree(query);
     886             : 
     887           4 :         if (pgsql_result) {
     888           4 :                 status = PQresultStatus(pgsql_result);
     889             :         } else {
     890           0 :                 status = (ExecStatusType) PQstatus(H->server);
     891             :         }
     892             : 
     893           7 :         if (status == PGRES_COPY_OUT && pgsql_result) {
     894           3 :                 PQclear(pgsql_result);
     895           3 :                 array_init(return_value);
     896             : 
     897             :                 while (1) {
     898          12 :                         char *csv = NULL;
     899          12 :                         int ret = PQgetCopyData(H->server, &csv, 0);
     900          12 :                         if (ret == -1) {
     901           3 :                                 break; /* copy done */
     902           9 :                         } else if (ret > 0) {
     903           9 :                                 add_next_index_stringl(return_value, csv, ret);
     904           9 :                                 PQfreemem(csv);
     905             :                         } else {
     906           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
     907           0 :                                 PDO_HANDLE_DBH_ERR();
     908           0 :                                 RETURN_FALSE;
     909             :                         }
     910           9 :                 }
     911             : 
     912           9 :                 while ((pgsql_result = PQgetResult(H->server))) {
     913           3 :                         PQclear(pgsql_result);
     914             :                 }
     915             :         } else {
     916           1 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, pdo_pgsql_sqlstate(pgsql_result));
     917           1 :                 PQclear(pgsql_result);
     918           1 :                 PDO_HANDLE_DBH_ERR();
     919           1 :                 RETURN_FALSE;
     920             :         }
     921             : }
     922             : /* }}} */
     923             : 
     924             : 
     925             : /* {{{ proto string PDO::pgsqlLOBCreate()
     926             :    Creates a new large object, returning its identifier.  Must be called inside a transaction. */
     927           1 : static PHP_METHOD(PDO, pgsqlLOBCreate)
     928             : {
     929             :         pdo_dbh_t *dbh;
     930             :         pdo_pgsql_db_handle *H;
     931             :         Oid lfd;
     932             : 
     933           1 :         dbh = Z_PDO_DBH_P(getThis());
     934           1 :         PDO_CONSTRUCT_CHECK;
     935           1 :         PDO_DBH_CLEAR_ERR();
     936             : 
     937           1 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     938           1 :         lfd = lo_creat(H->server, INV_READ|INV_WRITE);
     939             : 
     940           1 :         if (lfd != InvalidOid) {
     941           1 :                 zend_string *buf = strpprintf(0, ZEND_ULONG_FMT, (zend_long) lfd);
     942             : 
     943           1 :                 RETURN_STR(buf);
     944             :         }
     945             : 
     946           0 :         pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
     947           0 :         PDO_HANDLE_DBH_ERR();
     948           0 :         RETURN_FALSE;
     949             : }
     950             : /* }}} */
     951             : 
     952             : /* {{{ proto resource PDO::pgsqlLOBOpen(string oid [, string mode = 'rb'])
     953             :    Opens an existing large object stream.  Must be called inside a transaction. */
     954           1 : static PHP_METHOD(PDO, pgsqlLOBOpen)
     955             : {
     956             :         pdo_dbh_t *dbh;
     957             :         pdo_pgsql_db_handle *H;
     958             :         Oid oid;
     959             :         int lfd;
     960             :         char *oidstr;
     961             :         size_t oidstrlen;
     962           1 :         char *modestr = "rb";
     963             :         size_t modestrlen;
     964           1 :         int mode = INV_READ;
     965             :         char *end_ptr;
     966             : 
     967           1 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|s",
     968             :                                 &oidstr, &oidstrlen, &modestr, &modestrlen)) {
     969           0 :                 RETURN_FALSE;
     970             :         }
     971             : 
     972           1 :         oid = (Oid)strtoul(oidstr, &end_ptr, 10);
     973           1 :         if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
     974           0 :                 RETURN_FALSE;
     975             :         }
     976             : 
     977           1 :         if (strpbrk(modestr, "+w")) {
     978           1 :                 mode = INV_READ|INV_WRITE;
     979             :         }
     980             : 
     981           1 :         dbh = Z_PDO_DBH_P(getThis());
     982           1 :         PDO_CONSTRUCT_CHECK;
     983           1 :         PDO_DBH_CLEAR_ERR();
     984             : 
     985           1 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     986             : 
     987           1 :         lfd = lo_open(H->server, oid, mode);
     988             : 
     989           1 :         if (lfd >= 0) {
     990           1 :                 php_stream *stream = pdo_pgsql_create_lob_stream(getThis(), lfd, oid);
     991           1 :                 if (stream) {
     992           1 :                         php_stream_to_zval(stream, return_value);
     993           1 :                         return;
     994             :                 }
     995             :         } else {
     996           0 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
     997             :         }
     998             : 
     999           0 :         PDO_HANDLE_DBH_ERR();
    1000           0 :         RETURN_FALSE;
    1001             : }
    1002             : /* }}} */
    1003             : 
    1004             : /* {{{ proto bool PDO::pgsqlLOBUnlink(string oid)
    1005             :    Deletes the large object identified by oid.  Must be called inside a transaction. */
    1006           1 : static PHP_METHOD(PDO, pgsqlLOBUnlink)
    1007             : {
    1008             :         pdo_dbh_t *dbh;
    1009             :         pdo_pgsql_db_handle *H;
    1010             :         Oid oid;
    1011             :         char *oidstr, *end_ptr;
    1012             :         size_t oidlen;
    1013             : 
    1014           1 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s",
    1015             :                                 &oidstr, &oidlen)) {
    1016           0 :                 RETURN_FALSE;
    1017             :         }
    1018             : 
    1019           1 :         oid = (Oid)strtoul(oidstr, &end_ptr, 10);
    1020           1 :         if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
    1021           0 :                 RETURN_FALSE;
    1022             :         }
    1023             : 
    1024           1 :         dbh = Z_PDO_DBH_P(getThis());
    1025           1 :         PDO_CONSTRUCT_CHECK;
    1026           1 :         PDO_DBH_CLEAR_ERR();
    1027             : 
    1028           1 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
    1029             : 
    1030           1 :         if (1 == lo_unlink(H->server, oid)) {
    1031           1 :                 RETURN_TRUE;
    1032             :         }
    1033             : 
    1034           0 :         pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
    1035           0 :         PDO_HANDLE_DBH_ERR();
    1036           0 :         RETURN_FALSE;
    1037             : }
    1038             : /* }}} */
    1039             : 
    1040             : /* {{{ proto mixed PDO::pgsqlGetNotify([ int $result_type = PDO::FETCH_USE_DEFAULT] [, int $ms_timeout = 0 ]])
    1041             :    Get asyncronous notification */
    1042          10 : static PHP_METHOD(PDO, pgsqlGetNotify)
    1043             : {
    1044             :         pdo_dbh_t *dbh;
    1045             :         pdo_pgsql_db_handle *H;
    1046          10 :         zend_long result_type = PDO_FETCH_USE_DEFAULT;
    1047          10 :         zend_long ms_timeout = 0;
    1048             :         PGnotify *pgsql_notify;
    1049             : 
    1050          10 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "|ll",
    1051             :                                 &result_type, &ms_timeout)) {
    1052           0 :                 RETURN_FALSE;
    1053             :         }
    1054             : 
    1055          10 :         dbh = Z_PDO_DBH_P(getThis());
    1056          10 :         PDO_CONSTRUCT_CHECK;
    1057             : 
    1058          10 :         if (result_type == PDO_FETCH_USE_DEFAULT) {
    1059           5 :                 result_type = dbh->default_fetch_type;
    1060             :         }
    1061             : 
    1062          10 :         if (result_type != PDO_FETCH_BOTH && result_type != PDO_FETCH_ASSOC && result_type != PDO_FETCH_NUM) {
    1063           0 :                 php_error_docref(NULL, E_WARNING, "Invalid result type");
    1064           0 :                 RETURN_FALSE;
    1065             :         }
    1066             : 
    1067          10 :         if (ms_timeout < 0) {
    1068           0 :                 php_error_docref(NULL, E_WARNING, "Invalid timeout");
    1069           0 :                 RETURN_FALSE;
    1070             : #if ZEND_ENABLE_ZVAL_LONG64
    1071          10 :         } else if (ms_timeout > INT_MAX) {
    1072           0 :                 php_error_docref(NULL, E_WARNING, "timeout was shrinked to %d", INT_MAX);
    1073           0 :                 ms_timeout = INT_MAX;
    1074             : #endif
    1075             :         }
    1076             : 
    1077          10 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
    1078             : 
    1079          10 :         PQconsumeInput(H->server);
    1080          10 :         pgsql_notify = PQnotifies(H->server);
    1081             : 
    1082          10 :         if (ms_timeout && !pgsql_notify) {
    1083           1 :                 php_pollfd_for_ms(PQsocket(H->server), PHP_POLLREADABLE, (int)ms_timeout);
    1084             : 
    1085           1 :                 PQconsumeInput(H->server);
    1086           1 :                 pgsql_notify = PQnotifies(H->server);
    1087             :         }
    1088             : 
    1089          10 :         if (!pgsql_notify) {
    1090           4 :                 RETURN_FALSE;
    1091             :         }
    1092             : 
    1093           6 :         array_init(return_value);
    1094           6 :         if (result_type == PDO_FETCH_NUM || result_type == PDO_FETCH_BOTH) {
    1095           3 :                 add_index_string(return_value, 0, pgsql_notify->relname);
    1096           3 :                 add_index_long(return_value, 1, pgsql_notify->be_pid);
    1097           3 :                 if (pgsql_notify->extra && pgsql_notify->extra[0]) {
    1098           0 :                         add_index_string(return_value, 2, pgsql_notify->extra);
    1099             :                 }
    1100             :         }
    1101           6 :         if (result_type == PDO_FETCH_ASSOC || result_type == PDO_FETCH_BOTH) {
    1102           4 :                 add_assoc_string(return_value, "message", pgsql_notify->relname);
    1103           4 :                 add_assoc_long(return_value, "pid", pgsql_notify->be_pid);
    1104           4 :                 if (pgsql_notify->extra && pgsql_notify->extra[0]) {
    1105           0 :                         add_assoc_string(return_value, "payload", pgsql_notify->extra);
    1106             :                 }
    1107             :         }
    1108             : 
    1109           6 :         PQfreemem(pgsql_notify);
    1110             : }
    1111             : /* }}} */
    1112             : 
    1113             : /* {{{ proto int PDO::pgsqlGetPid()
    1114             :    Get backend(server) pid */
    1115           1 : static PHP_METHOD(PDO, pgsqlGetPid)
    1116             : {
    1117             :         pdo_dbh_t *dbh;
    1118             :         pdo_pgsql_db_handle *H;
    1119             : 
    1120           1 :         dbh = Z_PDO_DBH_P(getThis());
    1121           1 :         PDO_CONSTRUCT_CHECK;
    1122             : 
    1123           1 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
    1124             : 
    1125           1 :         RETURN_LONG(PQbackendPID(H->server));
    1126             : }
    1127             : /* }}} */
    1128             : 
    1129             : 
    1130             : static const zend_function_entry dbh_methods[] = {
    1131             :         PHP_ME(PDO, pgsqlLOBCreate, NULL, ZEND_ACC_PUBLIC)
    1132             :         PHP_ME(PDO, pgsqlLOBOpen, NULL, ZEND_ACC_PUBLIC)
    1133             :         PHP_ME(PDO, pgsqlLOBUnlink, NULL, ZEND_ACC_PUBLIC)
    1134             :         PHP_ME(PDO, pgsqlCopyFromArray, NULL, ZEND_ACC_PUBLIC)
    1135             :         PHP_ME(PDO, pgsqlCopyFromFile, NULL, ZEND_ACC_PUBLIC)
    1136             :         PHP_ME(PDO, pgsqlCopyToArray, NULL, ZEND_ACC_PUBLIC)
    1137             :         PHP_ME(PDO, pgsqlCopyToFile, NULL, ZEND_ACC_PUBLIC)
    1138             :     PHP_ME(PDO, pgsqlGetNotify, NULL, ZEND_ACC_PUBLIC)
    1139             :     PHP_ME(PDO, pgsqlGetPid, NULL, ZEND_ACC_PUBLIC)
    1140             :         PHP_FE_END
    1141             : };
    1142             : 
    1143           4 : static const zend_function_entry *pdo_pgsql_get_driver_methods(pdo_dbh_t *dbh, int kind)
    1144             : {
    1145           4 :         switch (kind) {
    1146             :                 case PDO_DBH_DRIVER_METHOD_KIND_DBH:
    1147           4 :                         return dbh_methods;
    1148             :                 default:
    1149           0 :                         return NULL;
    1150             :         }
    1151             : }
    1152             : 
    1153          14 : static int pdo_pgsql_set_attr(pdo_dbh_t *dbh, zend_long attr, zval *val)
    1154             : {
    1155          14 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
    1156             : 
    1157          14 :         switch (attr) {
    1158             :                 case PDO_ATTR_EMULATE_PREPARES:
    1159          11 :                         convert_to_long(val);
    1160          11 :                         H->emulate_prepares = 0 != Z_LVAL_P(val);
    1161          11 :                         return 1;
    1162             :                 case PDO_PGSQL_ATTR_DISABLE_PREPARES:
    1163           2 :                         convert_to_long(val);
    1164           2 :                         H->disable_prepares = 0 != Z_LVAL_P(val);
    1165           2 :                         return 1;
    1166             :                 default:
    1167           1 :                         return 0;
    1168             :         }
    1169             : }
    1170             : 
    1171             : static struct pdo_dbh_methods pgsql_methods = {
    1172             :         pgsql_handle_closer,
    1173             :         pgsql_handle_preparer,
    1174             :         pgsql_handle_doer,
    1175             :         pgsql_handle_quoter,
    1176             :         pgsql_handle_begin,
    1177             :         pgsql_handle_commit,
    1178             :         pgsql_handle_rollback,
    1179             :         pdo_pgsql_set_attr,
    1180             :         pdo_pgsql_last_insert_id,
    1181             :         pdo_pgsql_fetch_error_func,
    1182             :         pdo_pgsql_get_attribute,
    1183             :         pdo_pgsql_check_liveness,       /* check_liveness */
    1184             :         pdo_pgsql_get_driver_methods,  /* get_driver_methods */
    1185             :         NULL,
    1186             :         pgsql_handle_in_transaction,
    1187             : };
    1188             : 
    1189         162 : static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ */
    1190             : {
    1191             :         pdo_pgsql_db_handle *H;
    1192         162 :         int ret = 0;
    1193             :         char *conn_str, *p, *e;
    1194             :         zend_string *tmp_user, *tmp_pass;
    1195         162 :         zend_long connect_timeout = 30;
    1196             : 
    1197         163 :         H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent);
    1198         162 :         dbh->driver_data = H;
    1199             : 
    1200         162 :         H->einfo.errcode = 0;
    1201         162 :         H->einfo.errmsg = NULL;
    1202             : 
    1203             :         /* PostgreSQL wants params in the connect string to be separated by spaces,
    1204             :          * if the PDO standard semicolons are used, we convert them to spaces
    1205             :          */
    1206         162 :         e = (char *) dbh->data_source + strlen(dbh->data_source);
    1207         162 :         p = (char *) dbh->data_source;
    1208         324 :         while ((p = memchr(p, ';', (e - p)))) {
    1209           0 :                 *p = ' ';
    1210             :         }
    1211             : 
    1212         162 :         if (driver_options) {
    1213           2 :                 connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
    1214             :         }
    1215             : 
    1216             :         /* escape username and password, if provided */
    1217         162 :         tmp_user = _pdo_pgsql_escape_credentials(dbh->username);
    1218         162 :         tmp_pass = _pdo_pgsql_escape_credentials(dbh->password);
    1219             : 
    1220             :         /* support both full connection string & connection string + login and/or password */
    1221         162 :         if (tmp_user && tmp_pass) {
    1222           0 :                 spprintf(&conn_str, 0, "%s user='%s' password='%s' connect_timeout=%pd", (char *) dbh->data_source, ZSTR_VAL(tmp_user), ZSTR_VAL(tmp_pass), connect_timeout);
    1223         162 :         } else if (tmp_user) {
    1224           0 :                 spprintf(&conn_str, 0, "%s user='%s' connect_timeout=%pd", (char *) dbh->data_source, ZSTR_VAL(tmp_user), connect_timeout);
    1225         162 :         } else if (tmp_pass) {
    1226           0 :                 spprintf(&conn_str, 0, "%s password='%s' connect_timeout=%pd", (char *) dbh->data_source, ZSTR_VAL(tmp_pass), connect_timeout);
    1227             :         } else {
    1228         162 :                 spprintf(&conn_str, 0, "%s connect_timeout=%pd", (char *) dbh->data_source, connect_timeout);
    1229             :         }
    1230             : 
    1231         162 :         H->server = PQconnectdb(conn_str);
    1232             : 
    1233         162 :         if (tmp_user) {
    1234             :                 zend_string_release(tmp_user);
    1235             :         }
    1236         162 :         if (tmp_pass) {
    1237             :                 zend_string_release(tmp_pass);
    1238             :         }
    1239             : 
    1240         162 :         efree(conn_str);
    1241             : 
    1242         162 :         if (PQstatus(H->server) != CONNECTION_OK) {
    1243           0 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, PHP_PDO_PGSQL_CONNECTION_FAILURE_SQLSTATE);
    1244           0 :                 goto cleanup;
    1245             :         }
    1246             : 
    1247         162 :         PQsetNoticeProcessor(H->server, (void(*)(void*,const char*))_pdo_pgsql_notice, (void *)&dbh);
    1248             : 
    1249         162 :         H->attached = 1;
    1250         162 :         H->pgoid = -1;
    1251             : 
    1252         162 :         dbh->methods = &pgsql_methods;
    1253         162 :         dbh->alloc_own_columns = 1;
    1254         162 :         dbh->max_escaped_char_length = 2;
    1255             : 
    1256         162 :         ret = 1;
    1257             : 
    1258             : cleanup:
    1259         162 :         dbh->methods = &pgsql_methods;
    1260         162 :         if (!ret) {
    1261           0 :                 pgsql_handle_closer(dbh);
    1262             :         }
    1263             : 
    1264         162 :         return ret;
    1265             : }
    1266             : /* }}} */
    1267             : 
    1268             : pdo_driver_t pdo_pgsql_driver = {
    1269             :         PDO_DRIVER_HEADER(pgsql),
    1270             :         pdo_pgsql_handle_factory
    1271             : };
    1272             : 
    1273             : /*
    1274             :  * Local variables:
    1275             :  * tab-width: 4
    1276             :  * c-basic-offset: 4
    1277             :  * End:
    1278             :  * vim600: noet sw=4 ts=4 fdm=marker
    1279             :  * vim<600: noet sw=4 ts=4
    1280             :  */

Generated by: LCOV version 1.10

Generated at Sat, 29 Aug 2015 10:22:20 +0000 (4 days ago)

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