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

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:54 +0000 (28 hours ago)

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