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

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:59:00 +0000 (6 days ago)

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