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: 438 598 73.2 %
Date: 2014-07-21 Functions: 31 34 91.2 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Tue, 22 Jul 2014 01:33:15 +0000 (8 days ago)

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