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

Generated by: LCOV version 1.10

Generated at Tue, 14 Apr 2015 11:48:48 +0000 (13 days ago)

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