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: 371 506 73.3 %
Date: 2014-04-18 Functions: 28 31 90.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 5                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 1997-2013 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 "pdo/php_pdo.h"
      31             : #include "pdo/php_pdo_driver.h"
      32             : #include "ext/standard/file.h"
      33             : 
      34             : #undef PACKAGE_BUGREPORT
      35             : #undef PACKAGE_NAME
      36             : #undef PACKAGE_STRING
      37             : #undef PACKAGE_TARNAME
      38             : #undef PACKAGE_VERSION
      39             : #include "pg_config.h" /* needed for PG_VERSION */
      40             : #include "php_pdo_pgsql.h"
      41             : #include "php_pdo_pgsql_int.h"
      42             : #include "zend_exceptions.h"
      43             : 
      44         367 : static char * _pdo_pgsql_trim_message(const char *message, int persistent)
      45             : {
      46         367 :         register int i = strlen(message)-1;
      47             :         char *tmp;
      48             : 
      49         367 :         if (i>1 && (message[i-1] == '\r' || message[i-1] == '\n') && message[i] == '.') {
      50           0 :                 --i;
      51             :         }
      52        1101 :         while (i>0 && (message[i] == '\r' || message[i] == '\n')) {
      53         367 :                 --i;
      54             :         }
      55         367 :         ++i;
      56         367 :         tmp = pemalloc(i + 1, persistent);
      57         367 :         memcpy(tmp, message, i);
      58         367 :         tmp[i] = '\0';
      59             :         
      60         367 :         return tmp;
      61             : }
      62             : 
      63         367 : int _pdo_pgsql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, int errcode, const char *sqlstate, const char *file, int line TSRMLS_DC) /* {{{ */
      64             : {
      65         367 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
      66         367 :         pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code;
      67         367 :         pdo_pgsql_error_info *einfo = &H->einfo;
      68         367 :         char *errmsg = PQerrorMessage(H->server);
      69             : 
      70         367 :         einfo->errcode = errcode;
      71         367 :         einfo->file = file;
      72         367 :         einfo->line = line;
      73             : 
      74         367 :         if (einfo->errmsg) {
      75         230 :                 pefree(einfo->errmsg, dbh->is_persistent);
      76         230 :                 einfo->errmsg = NULL;
      77             :         }
      78             : 
      79         371 :         if (sqlstate == NULL || strlen(sqlstate) >= sizeof(pdo_error_type)) {
      80           4 :                 strcpy(*pdo_err, "HY000");
      81             :         }
      82             :         else {
      83         363 :                 strcpy(*pdo_err, sqlstate);
      84             :         }
      85             : 
      86         367 :         if (errmsg) {
      87         367 :                 einfo->errmsg = _pdo_pgsql_trim_message(errmsg, dbh->is_persistent);
      88             :         }
      89             : 
      90         367 :         if (!dbh->methods) {
      91           0 :                 zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
      92             :                                 *pdo_err, einfo->errcode, einfo->errmsg);
      93             :         }
      94             :         
      95         367 :         return errcode;
      96             : }
      97             : /* }}} */
      98             : 
      99          50 : static void _pdo_pgsql_notice(pdo_dbh_t *dbh, const char *message) /* {{{ */
     100             : {
     101             : /*      pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data; */
     102          50 : }
     103             : /* }}} */
     104             : 
     105          22 : static int pdo_pgsql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC) /* {{{ */
     106             : {
     107          22 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     108          22 :         pdo_pgsql_error_info *einfo = &H->einfo;
     109             : 
     110          22 :         if (einfo->errcode) {
     111          22 :                 add_next_index_long(info, einfo->errcode);
     112          22 :                 add_next_index_string(info, einfo->errmsg, 1);
     113             :         }
     114             : 
     115          22 :         return 1;
     116             : }
     117             : /* }}} */
     118             : 
     119             : /* {{{ pdo_pgsql_create_lob_stream */
     120           1 : static size_t pgsql_lob_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
     121             : {
     122           1 :         struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
     123           1 :         return lo_write(self->conn, self->lfd, (char*)buf, count);
     124             : }
     125             : 
     126           2 : static size_t pgsql_lob_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
     127             : {
     128           2 :         struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
     129           2 :         return lo_read(self->conn, self->lfd, buf, count);
     130             : }
     131             : 
     132           3 : static int pgsql_lob_close(php_stream *stream, int close_handle TSRMLS_DC)
     133             : {
     134           3 :         struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
     135           3 :         pdo_dbh_t *dbh = self->dbh;
     136             : 
     137           3 :         if (close_handle) {
     138           3 :                 lo_close(self->conn, self->lfd);
     139             :         }
     140           3 :         efree(self);
     141           3 :         php_pdo_dbh_delref(dbh TSRMLS_CC);
     142           3 :         return 0;
     143             : }
     144             : 
     145           3 : static int pgsql_lob_flush(php_stream *stream TSRMLS_DC)
     146             : {
     147           3 :         return 0;
     148             : }
     149             : 
     150           0 : static int pgsql_lob_seek(php_stream *stream, off_t offset, int whence,
     151             :                 off_t *newoffset TSRMLS_DC)
     152             : {
     153           0 :         struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stream->abstract;
     154           0 :         int pos = lo_lseek(self->conn, self->lfd, offset, whence);
     155           0 :         *newoffset = pos;
     156           0 :         return pos >= 0 ? 0 : -1;
     157             : }
     158             : 
     159             : php_stream_ops pdo_pgsql_lob_stream_ops = {
     160             :         pgsql_lob_write,
     161             :         pgsql_lob_read,
     162             :         pgsql_lob_close,
     163             :         pgsql_lob_flush,
     164             :         "pdo_pgsql lob stream",
     165             :         pgsql_lob_seek,
     166             :         NULL,
     167             :         NULL,
     168             :         NULL
     169             : };
     170             : 
     171           3 : php_stream *pdo_pgsql_create_lob_stream(pdo_dbh_t *dbh, int lfd, Oid oid TSRMLS_DC)
     172             : {
     173             :         php_stream *stm;
     174           3 :         struct pdo_pgsql_lob_self *self = ecalloc(1, sizeof(*self));
     175           3 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     176             : 
     177           3 :         self->dbh = dbh;
     178           3 :         self->lfd = lfd;
     179           3 :         self->oid = oid;
     180           3 :         self->conn = H->server;
     181             : 
     182           3 :         stm = php_stream_alloc(&pdo_pgsql_lob_stream_ops, self, 0, "r+b");
     183             : 
     184           3 :         if (stm) {
     185           3 :                 php_pdo_dbh_addref(dbh TSRMLS_CC);
     186           3 :                 return stm;
     187             :         }
     188             : 
     189           0 :         efree(self);
     190           0 :         return NULL;
     191             : }
     192             : /* }}} */
     193             : 
     194         136 : static int pgsql_handle_closer(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
     195             : {
     196         136 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     197         136 :         if (H) {
     198         136 :                 if (H->server) {
     199         136 :                         PQfinish(H->server);
     200         136 :                         H->server = NULL;
     201             :                 }
     202         136 :                 if (H->einfo.errmsg) {
     203         136 :                         pefree(H->einfo.errmsg, dbh->is_persistent);
     204         136 :                         H->einfo.errmsg = NULL;
     205             :                 }
     206         136 :                 pefree(H, dbh->is_persistent);
     207         136 :                 dbh->driver_data = NULL;
     208             :         }
     209         136 :         return 0;
     210             : }
     211             : /* }}} */
     212             : 
     213         162 : static int pgsql_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC)
     214             : {
     215         162 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     216         162 :         pdo_pgsql_stmt *S = ecalloc(1, sizeof(pdo_pgsql_stmt));
     217             :         int scrollable;
     218             : #if HAVE_PQPREPARE
     219             :         int ret;
     220         162 :         char *nsql = NULL;
     221         162 :         int nsql_len = 0;
     222         162 :         int emulate = 0;
     223             : #endif
     224             : 
     225         162 :         S->H = H;
     226         162 :         stmt->driver_data = S;
     227         162 :         stmt->methods = &pgsql_stmt_methods;
     228             : 
     229         162 :         scrollable = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR,
     230             :                 PDO_CURSOR_FWDONLY TSRMLS_CC) == PDO_CURSOR_SCROLL;
     231             : 
     232         162 :         if (scrollable) {
     233           2 :                 if (S->cursor_name) {
     234           0 :                         efree(S->cursor_name);
     235             :                 }
     236           2 :                 spprintf(&S->cursor_name, 0, "pdo_crsr_%08x", ++H->stmt_counter);
     237             : #if HAVE_PQPREPARE
     238           2 :                 emulate = 1;
     239             : #endif
     240             :         }
     241             : 
     242             : #if HAVE_PQPREPARE
     243         160 :         else if (driver_options) {
     244          49 :                 if (pdo_attr_lval(driver_options, PDO_PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT, H->disable_native_prepares TSRMLS_CC) == 1 ||
     245          21 :                         pdo_attr_lval(driver_options, PDO_ATTR_EMULATE_PREPARES, H->emulate_prepares TSRMLS_CC) == 1) {
     246          12 :                         emulate = 1;
     247             :                 }
     248             :         } else {
     249         132 :                 emulate = H->disable_native_prepares || H->emulate_prepares;
     250             :         }
     251             : 
     252         162 :         if (!emulate && PQprotocolVersion(H->server) > 2) {
     253         146 :                 stmt->supports_placeholders = PDO_PLACEHOLDER_NAMED;
     254         146 :                 stmt->named_rewrite_template = "$%d";
     255         146 :                 ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len TSRMLS_CC);
     256             : 
     257         146 :                 if (ret == 1) {
     258             :                         /* query was re-written */
     259          39 :                         sql = nsql;
     260         107 :                 } else if (ret == -1) {
     261             :                         /* couldn't grok it */
     262           0 :                         strcpy(dbh->error_code, stmt->error_code);
     263           0 :                         return 0;
     264             :                 }
     265             : 
     266         146 :                 spprintf(&S->stmt_name, 0, "pdo_stmt_%08x", ++H->stmt_counter);
     267             :                 /* that's all for now; we'll defer the actual prepare until the first execute call */
     268             :         
     269         146 :                 if (nsql) {
     270          39 :                         S->query = nsql;
     271             :                 } else {
     272         107 :                         S->query = estrdup(sql);
     273             :                 }
     274             : 
     275         146 :                 return 1;
     276             :         }
     277             : #endif
     278             : 
     279          16 :         stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
     280          16 :         return 1;
     281             : }
     282             : 
     283         561 : static long pgsql_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC)
     284             : {
     285         561 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     286             :         PGresult *res;
     287         561 :         long ret = 1;
     288             :         ExecStatusType qs;
     289             :         
     290         561 :         if (!(res = PQexec(H->server, sql))) {
     291             :                 /* fatal error */
     292           0 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, NULL);
     293           0 :                 return -1;
     294             :         }
     295         561 :         qs = PQresultStatus(res);
     296         561 :         if (qs != PGRES_COMMAND_OK && qs != PGRES_TUPLES_OK) {
     297         349 :                 pdo_pgsql_error(dbh, qs, pdo_pgsql_sqlstate(res));
     298         349 :                 PQclear(res);
     299         349 :                 return -1;
     300             :         }
     301         212 :         H->pgoid = PQoidValue(res);
     302         212 :         ret = (qs == PGRES_COMMAND_OK) ? atol(PQcmdTuples(res)) : 0L;
     303         212 :         PQclear(res);
     304             : 
     305         212 :         return ret;
     306             : }
     307             : 
     308          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)
     309             : {
     310             :         unsigned char *escaped;
     311          21 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     312             :         size_t tmp_len;
     313             :         
     314          21 :         switch (paramtype) {
     315             :                 case PDO_PARAM_LOB:
     316             :                         /* escapedlen returned by PQescapeBytea() accounts for trailing 0 */
     317             : #ifdef HAVE_PQESCAPE_BYTEA_CONN
     318           0 :                         escaped = PQescapeByteaConn(H->server, unquoted, unquotedlen, &tmp_len);
     319             : #else
     320             :                         escaped = PQescapeBytea(unquoted, unquotedlen, &tmp_len);
     321             : #endif
     322           0 :                         *quotedlen = (int)tmp_len + 1;
     323           0 :                         *quoted = emalloc(*quotedlen + 1);
     324           0 :                         memcpy((*quoted)+1, escaped, *quotedlen-2);
     325           0 :                         (*quoted)[0] = '\'';
     326           0 :                         (*quoted)[*quotedlen-1] = '\'';
     327           0 :                         (*quoted)[*quotedlen] = '\0';
     328           0 :                         PQfreemem(escaped);
     329           0 :                         break;
     330             :                 default:
     331          21 :                         *quoted = safe_emalloc(2, unquotedlen, 3);
     332          21 :                         (*quoted)[0] = '\'';
     333             : #ifndef HAVE_PQESCAPE_CONN
     334             :                         *quotedlen = PQescapeString(*quoted + 1, unquoted, unquotedlen);
     335             : #else
     336          21 :                         *quotedlen = PQescapeStringConn(H->server, *quoted + 1, unquoted, unquotedlen, NULL);
     337             : #endif
     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          32 : static int pdo_pgsql_transaction_cmd(const char *cmd, pdo_dbh_t *dbh TSRMLS_DC)
     469             : {
     470          32 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     471             :         PGresult *res;
     472          32 :         int ret = 1;
     473             : 
     474          32 :         res = PQexec(H->server, cmd);
     475             : 
     476          32 :         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          32 :         PQclear(res);
     482          32 :         return ret;
     483             : }
     484             : 
     485          16 : static int pgsql_handle_begin(pdo_dbh_t *dbh TSRMLS_DC)
     486             : {
     487          16 :         return pdo_pgsql_transaction_cmd("BEGIN", dbh TSRMLS_CC);
     488             : }
     489             : 
     490           3 : static int pgsql_handle_commit(pdo_dbh_t *dbh TSRMLS_DC)
     491             : {
     492           3 :         return pdo_pgsql_transaction_cmd("COMMIT", dbh TSRMLS_CC);
     493             : }
     494             : 
     495          13 : static int pgsql_handle_rollback(pdo_dbh_t *dbh TSRMLS_DC)
     496             : {
     497          13 :         return pdo_pgsql_transaction_cmd("ROLLBACK", dbh TSRMLS_CC);
     498             : }
     499             : 
     500             : /* {{{ proto string PDO::pgsqlCopyFromArray(string $table_name , array $rows [, string $delimiter [, string $null_as ] [, string $fields])
     501             :    Returns true if the copy worked fine or false if error */
     502           4 : static PHP_METHOD(PDO, pgsqlCopyFromArray)
     503             : {
     504             :         pdo_dbh_t *dbh;
     505             :         pdo_pgsql_db_handle *H;
     506             : 
     507             :         zval *pg_rows;
     508             : 
     509           4 :         char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
     510           4 :         int table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
     511             :         char *query;
     512             : 
     513             :         PGresult *pgsql_result;
     514             :         ExecStatusType status;
     515             : 
     516           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s/a|sss",
     517             :                                         &table_name, &table_name_len, &pg_rows,
     518             :                                         &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
     519           0 :                 return;
     520             :         }
     521             : 
     522           4 :         if (!zend_hash_num_elements(Z_ARRVAL_P(pg_rows))) {
     523           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot copy from an empty array");
     524           0 :                 RETURN_FALSE;
     525             :         }
     526             : 
     527           4 :         dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
     528           4 :         PDO_CONSTRUCT_CHECK;
     529             : 
     530           4 :         if (pg_fields) {
     531           2 :                 spprintf(&query, 0, "COPY %s (%s) FROM STDIN DELIMITERS E'%c' WITH NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
     532             :         } else {
     533           2 :                 spprintf(&query, 0, "COPY %s FROM STDIN DELIMITERS E'%c' WITH NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
     534             :         }
     535             : 
     536             :         /* Obtain db Handle */
     537           4 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     538             : 
     539           8 :         while ((pgsql_result = PQgetResult(H->server))) {
     540           0 :                 PQclear(pgsql_result);
     541             :         }
     542           4 :         pgsql_result = PQexec(H->server, query);
     543             : 
     544           4 :         efree(query);
     545           4 :         query = NULL;
     546             : 
     547           4 :         if (pgsql_result) {
     548           4 :                 status = PQresultStatus(pgsql_result);
     549             :         } else {
     550           0 :                 status = (ExecStatusType) PQstatus(H->server);
     551             :         }
     552             : 
     553           4 :         if (status == PGRES_COPY_IN && pgsql_result) {
     554           3 :                 int command_failed = 0;
     555           3 :                 int buffer_len = 0;
     556             :                 zval **tmp;
     557             :                 HashPosition pos;
     558             : 
     559           3 :                 PQclear(pgsql_result);
     560           3 :                 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(pg_rows), &pos);
     561          15 :                 while (zend_hash_get_current_data_ex(Z_ARRVAL_P(pg_rows), (void **) &tmp, &pos) == SUCCESS) {
     562             :                         int query_len;
     563           9 :                         convert_to_string_ex(tmp);
     564             :                 
     565           9 :                         if (buffer_len < Z_STRLEN_PP(tmp)) {
     566           3 :                                 buffer_len = Z_STRLEN_PP(tmp);
     567           3 :                                 query = erealloc(query, buffer_len + 2); /* room for \n\0 */
     568             :                         }
     569           9 :                         memcpy(query, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
     570           9 :                         query_len = Z_STRLEN_PP(tmp);
     571           9 :                         if (query[query_len - 1] != '\n') {
     572           9 :                                 query[query_len++] = '\n';
     573             :                         }
     574           9 :                         query[query_len] = '\0';
     575           9 :                         if (PQputCopyData(H->server, query, query_len) != 1) {
     576           0 :                                 efree(query);
     577           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "copy failed");
     578           0 :                                 RETURN_FALSE;
     579             :                         }
     580           9 :                         zend_hash_move_forward_ex(Z_ARRVAL_P(pg_rows), &pos);
     581             :                 }
     582           3 :                 if (query) {
     583           3 :                         efree(query);
     584             :                 }
     585             : 
     586           3 :                 if (PQputCopyEnd(H->server, NULL) != 1) {
     587           0 :                         pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "putcopyend failed");
     588           0 :                         RETURN_FALSE;
     589             :                 }
     590             : 
     591           9 :                 while ((pgsql_result = PQgetResult(H->server))) {
     592           3 :                         if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
     593           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed");
     594           0 :                                 command_failed = 1;
     595             :                         }
     596           3 :                         PQclear(pgsql_result);
     597             :                 }
     598             : 
     599           3 :                 RETURN_BOOL(!command_failed);
     600             :         } else {
     601           1 :                 PQclear(pgsql_result);
     602           1 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed");
     603           1 :                 RETURN_FALSE;
     604             :         }
     605             : }
     606             : /* }}} */
     607             : 
     608             : /* {{{ proto string PDO::pgsqlCopyFromFile(string $table_name , string $filename [, string $delimiter [, string $null_as ] [, string $fields])
     609             :    Returns true if the copy worked fine or false if error */
     610           4 : static PHP_METHOD(PDO, pgsqlCopyFromFile)
     611             : {
     612             :         pdo_dbh_t *dbh;
     613             :         pdo_pgsql_db_handle *H;
     614             : 
     615           4 :         char *table_name, *filename, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
     616           4 :         int  table_name_len, filename_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
     617             :         char *query;
     618             :         PGresult *pgsql_result;
     619             :         ExecStatusType status;
     620             :         php_stream *stream;
     621             : 
     622           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|sss",
     623             :                                 &table_name, &table_name_len, &filename, &filename_len,
     624             :                                 &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
     625           0 :                 return;
     626             :         }
     627             : 
     628             :         /* Obtain db Handler */
     629           4 :         dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
     630           4 :         PDO_CONSTRUCT_CHECK;
     631             : 
     632           4 :         stream = php_stream_open_wrapper_ex(filename, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, FG(default_context));
     633           4 :         if (!stream) {
     634           0 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Unable to open the file");
     635           0 :                 RETURN_FALSE;
     636             :         }
     637             : 
     638           4 :         if (pg_fields) {
     639           2 :                 spprintf(&query, 0, "COPY %s (%s) FROM STDIN DELIMITERS E'%c' WITH NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
     640             :         } else {
     641           2 :                 spprintf(&query, 0, "COPY %s FROM STDIN DELIMITERS E'%c' WITH NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
     642             :         }
     643             : 
     644           4 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     645             : 
     646           8 :         while ((pgsql_result = PQgetResult(H->server))) {
     647           0 :                 PQclear(pgsql_result);
     648             :         }
     649           4 :         pgsql_result = PQexec(H->server, query);
     650             : 
     651           4 :         efree(query);
     652             : 
     653           4 :         if (pgsql_result) {
     654           4 :                 status = PQresultStatus(pgsql_result);
     655             :         } else {
     656           0 :                 status = (ExecStatusType) PQstatus(H->server);
     657             :         }
     658             : 
     659           4 :         if (status == PGRES_COPY_IN && pgsql_result) {
     660             :                 char *buf;
     661           3 :                 int command_failed = 0;
     662           3 :                 size_t line_len = 0;
     663             : 
     664           3 :                 PQclear(pgsql_result);
     665          15 :                 while ((buf = php_stream_get_line(stream, NULL, 0, &line_len)) != NULL) {
     666           9 :                         if (PQputCopyData(H->server, buf, line_len) != 1) {
     667           0 :                                 efree(buf);
     668           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "copy failed");
     669           0 :                                 php_stream_close(stream);
     670           0 :                                 RETURN_FALSE;
     671             :                         }
     672           9 :                         efree(buf);
     673             :                 }
     674           3 :                 php_stream_close(stream);
     675             : 
     676           3 :                 if (PQputCopyEnd(H->server, NULL) != 1) {
     677           0 :                         pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "putcopyend failed");
     678           0 :                         RETURN_FALSE;
     679             :                 }
     680             : 
     681           9 :                 while ((pgsql_result = PQgetResult(H->server))) {
     682           3 :                         if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
     683           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed");
     684           0 :                                 command_failed = 1;
     685             :                         }
     686           3 :                         PQclear(pgsql_result);
     687             :                 }
     688             : 
     689           3 :                 RETURN_BOOL(!command_failed);
     690             :         } else {
     691           1 :                 PQclear(pgsql_result);
     692           1 :                 php_stream_close(stream);
     693           1 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed");
     694           1 :                 RETURN_FALSE;
     695             :         }
     696             : }
     697             : /* }}} */
     698             : 
     699             : 
     700             : /* {{{ proto string PDO::pgsqlCopyToFile(string $table_name , $filename, [string $delimiter [, string $null_as [, string $fields]]])
     701             :    Returns true if the copy worked fine or false if error */
     702           4 : static PHP_METHOD(PDO, pgsqlCopyToFile)
     703             : {
     704             :         pdo_dbh_t *dbh;
     705             :         pdo_pgsql_db_handle *H;
     706             : 
     707           4 :         char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL, *filename = NULL;
     708           4 :         int table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len, filename_len;
     709             :         char *query;
     710             : 
     711             :         PGresult *pgsql_result;
     712             :         ExecStatusType status;
     713             : 
     714             :         php_stream *stream;
     715             : 
     716           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|sss",
     717             :                                         &table_name, &table_name_len, &filename, &filename_len,
     718             :                                         &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
     719           0 :                 return;
     720             :         }
     721             : 
     722           4 :         dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
     723           4 :         PDO_CONSTRUCT_CHECK;
     724             : 
     725           4 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     726             : 
     727           4 :         stream = php_stream_open_wrapper_ex(filename, "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, FG(default_context));
     728           4 :         if (!stream) {
     729           0 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Unable to open the file for writing");
     730           0 :                 RETURN_FALSE;
     731             :         }
     732             : 
     733           8 :         while ((pgsql_result = PQgetResult(H->server))) {
     734           0 :                 PQclear(pgsql_result);
     735             :         }
     736             : 
     737           4 :         if (pg_fields) {
     738           1 :                 spprintf(&query, 0, "COPY %s (%s) TO STDIN DELIMITERS E'%c' WITH NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
     739             :         } else {
     740           3 :                 spprintf(&query, 0, "COPY %s TO STDIN DELIMITERS E'%c' WITH NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
     741             :         }
     742           4 :         pgsql_result = PQexec(H->server, query);
     743           4 :         efree(query);
     744             : 
     745           4 :         if (pgsql_result) {
     746           4 :                 status = PQresultStatus(pgsql_result);
     747             :         } else {
     748           0 :                 status = (ExecStatusType) PQstatus(H->server);
     749             :         }
     750             : 
     751           4 :         if (status == PGRES_COPY_OUT && pgsql_result) {
     752           3 :                 PQclear(pgsql_result);
     753             :                 while (1) {
     754          12 :                         char *csv = NULL;
     755          12 :                         int ret = PQgetCopyData(H->server, &csv, 0);
     756             : 
     757          12 :                         if (ret == -1) {
     758           3 :                                 break; /* done */
     759           9 :                         } else if (ret > 0) {
     760           9 :                                 if (php_stream_write(stream, csv, ret) != ret) {
     761           0 :                                         pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Unable to write to file");
     762           0 :                                         PQfreemem(csv);
     763           0 :                                         php_stream_close(stream);
     764           0 :                                         RETURN_FALSE;
     765             :                                 } else {
     766           9 :                                         PQfreemem(csv);
     767             :                                 }
     768             :                         } else {
     769           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed: getline failed");
     770           0 :                                 php_stream_close(stream);
     771           0 :                                 RETURN_FALSE;
     772             :                         }
     773           9 :                 }
     774           3 :                 php_stream_close(stream);
     775             : 
     776           9 :                 while ((pgsql_result = PQgetResult(H->server))) {
     777           3 :                         PQclear(pgsql_result);
     778             :                 }
     779           3 :                 RETURN_TRUE;
     780             :         } else {
     781           1 :                 php_stream_close(stream);
     782           1 :                 PQclear(pgsql_result);
     783           1 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed");
     784           1 :                 RETURN_FALSE;
     785             :         }
     786             : }
     787             : /* }}} */
     788             : 
     789             : /* {{{ proto string PDO::pgsqlCopyToArray(string $table_name , [string $delimiter [, string $null_as [, string $fields]]])
     790             :    Returns true if the copy worked fine or false if error */
     791           4 : static PHP_METHOD(PDO, pgsqlCopyToArray)
     792             : {
     793             :         pdo_dbh_t *dbh;
     794             :         pdo_pgsql_db_handle *H;
     795             : 
     796           4 :         char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
     797           4 :         int table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
     798             :         char *query;
     799             : 
     800             :         PGresult *pgsql_result;
     801             :         ExecStatusType status;
     802             : 
     803           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sss",
     804             :                 &table_name, &table_name_len,
     805             :                 &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
     806           0 :                 return;
     807             :         }
     808             : 
     809           4 :         dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
     810           4 :         PDO_CONSTRUCT_CHECK;
     811             : 
     812           4 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     813             : 
     814           8 :         while ((pgsql_result = PQgetResult(H->server))) {
     815           0 :                 PQclear(pgsql_result);
     816             :         }
     817             : 
     818           4 :         if (pg_fields) {
     819           1 :                 spprintf(&query, 0, "COPY %s (%s) TO STDIN DELIMITERS E'%c' WITH NULL AS E'%s'", table_name, pg_fields, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
     820             :         } else {
     821           3 :                 spprintf(&query, 0, "COPY %s TO STDIN DELIMITERS E'%c' WITH NULL AS E'%s'", table_name, (pg_delim_len ? *pg_delim : '\t'), (pg_null_as_len ? pg_null_as : "\\\\N"));
     822             :         }
     823           4 :         pgsql_result = PQexec(H->server, query);
     824           4 :         efree(query);
     825             : 
     826           4 :         if (pgsql_result) {
     827           4 :                 status = PQresultStatus(pgsql_result);
     828             :         } else {
     829           0 :                 status = (ExecStatusType) PQstatus(H->server);
     830             :         }
     831             : 
     832           7 :         if (status == PGRES_COPY_OUT && pgsql_result) {
     833           3 :                 PQclear(pgsql_result);
     834           3 :                 array_init(return_value);
     835             : 
     836             :                 while (1) {
     837          12 :                         char *csv = NULL;
     838          12 :                         int ret = PQgetCopyData(H->server, &csv, 0);
     839          12 :                         if (ret == -1) {
     840           3 :                                 break; /* copy done */
     841           9 :                         } else if (ret > 0) { 
     842           9 :                                 add_next_index_stringl(return_value, csv, ret, 1);
     843           9 :                                 PQfreemem(csv);
     844             :                         } else {
     845           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed: getline failed");
     846           0 :                                 RETURN_FALSE;
     847             :                         }
     848           9 :                 }
     849             : 
     850           9 :                 while ((pgsql_result = PQgetResult(H->server))) {
     851           3 :                         PQclear(pgsql_result);
     852             :                 }
     853             :         } else {
     854           1 :                 PQclear(pgsql_result);
     855           1 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed");
     856           1 :                 RETURN_FALSE;
     857             :         }
     858             : }
     859             : /* }}} */
     860             : 
     861             : 
     862             : /* {{{ proto string PDO::pgsqlLOBCreate()
     863             :    Creates a new large object, returning its identifier.  Must be called inside a transaction. */
     864           1 : static PHP_METHOD(PDO, pgsqlLOBCreate)
     865             : {
     866             :         pdo_dbh_t *dbh;
     867             :         pdo_pgsql_db_handle *H;
     868             :         Oid lfd;
     869             : 
     870           1 :         dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
     871           1 :         PDO_CONSTRUCT_CHECK;
     872             : 
     873           1 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     874           1 :         lfd = lo_creat(H->server, INV_READ|INV_WRITE);
     875             : 
     876           1 :         if (lfd != InvalidOid) {
     877             :                 char *buf;
     878           1 :                 spprintf(&buf, 0, "%lu", (long) lfd);
     879           1 :                 RETURN_STRING(buf, 0);
     880             :         }
     881             :         
     882           0 :         pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "HY000");
     883           0 :         RETURN_FALSE;
     884             : }
     885             : /* }}} */
     886             : 
     887             : /* {{{ proto resource PDO::pgsqlLOBOpen(string oid [, string mode = 'rb'])
     888             :    Opens an existing large object stream.  Must be called inside a transaction. */
     889           1 : static PHP_METHOD(PDO, pgsqlLOBOpen)
     890             : {
     891             :         pdo_dbh_t *dbh;
     892             :         pdo_pgsql_db_handle *H;
     893             :         Oid oid;
     894             :         int lfd;
     895             :         char *oidstr;
     896             :         int oidstrlen;
     897           1 :         char *modestr = "rb";
     898             :         int modestrlen;
     899           1 :         int mode = INV_READ;
     900             :         char *end_ptr;
     901             : 
     902           1 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s",
     903             :                                 &oidstr, &oidstrlen, &modestr, &modestrlen)) {
     904           0 :                 RETURN_FALSE;
     905             :         }
     906             : 
     907           1 :         oid = (Oid)strtoul(oidstr, &end_ptr, 10);
     908           1 :         if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
     909           0 :                 RETURN_FALSE;
     910             :         }
     911             : 
     912           1 :         if (strpbrk(modestr, "+w")) {
     913           1 :                 mode = INV_READ|INV_WRITE;
     914             :         }
     915             :         
     916           1 :         dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
     917           1 :         PDO_CONSTRUCT_CHECK;
     918             : 
     919           1 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     920             : 
     921           1 :         lfd = lo_open(H->server, oid, mode);
     922             : 
     923           1 :         if (lfd >= 0) {
     924           1 :                 php_stream *stream = pdo_pgsql_create_lob_stream(dbh, lfd, oid TSRMLS_CC);
     925           1 :                 if (stream) {
     926           1 :                         php_stream_to_zval(stream, return_value);
     927           1 :                         return;
     928             :                 }
     929             :         } else {
     930           0 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "HY000");
     931             :         }
     932           0 :         RETURN_FALSE;
     933             : }
     934             : /* }}} */
     935             : 
     936             : /* {{{ proto bool PDO::pgsqlLOBUnlink(string oid)
     937             :    Deletes the large object identified by oid.  Must be called inside a transaction. */
     938           1 : static PHP_METHOD(PDO, pgsqlLOBUnlink)
     939             : {
     940             :         pdo_dbh_t *dbh;
     941             :         pdo_pgsql_db_handle *H;
     942             :         Oid oid;
     943             :         char *oidstr, *end_ptr;
     944             :         int oidlen;
     945             : 
     946           1 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
     947             :                                 &oidstr, &oidlen)) {
     948           0 :                 RETURN_FALSE;
     949             :         }
     950             : 
     951           1 :         oid = (Oid)strtoul(oidstr, &end_ptr, 10);
     952           1 :         if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
     953           0 :                 RETURN_FALSE;
     954             :         }
     955             : 
     956           1 :         dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
     957           1 :         PDO_CONSTRUCT_CHECK;
     958             : 
     959           1 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     960             :         
     961           1 :         if (1 == lo_unlink(H->server, oid)) {
     962           1 :                 RETURN_TRUE;
     963             :         }
     964           0 :         pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "HY000");
     965           0 :         RETURN_FALSE;
     966             : }
     967             : /* }}} */
     968             : 
     969             : 
     970             : static const zend_function_entry dbh_methods[] = {
     971             :         PHP_ME(PDO, pgsqlLOBCreate, NULL, ZEND_ACC_PUBLIC)
     972             :         PHP_ME(PDO, pgsqlLOBOpen, NULL, ZEND_ACC_PUBLIC)
     973             :         PHP_ME(PDO, pgsqlLOBUnlink, NULL, ZEND_ACC_PUBLIC)
     974             :         PHP_ME(PDO, pgsqlCopyFromArray, NULL, ZEND_ACC_PUBLIC)
     975             :         PHP_ME(PDO, pgsqlCopyFromFile, NULL, ZEND_ACC_PUBLIC)
     976             :         PHP_ME(PDO, pgsqlCopyToArray, NULL, ZEND_ACC_PUBLIC)
     977             :         PHP_ME(PDO, pgsqlCopyToFile, NULL, ZEND_ACC_PUBLIC)
     978             :         PHP_FE_END
     979             : };
     980             : 
     981           3 : static const zend_function_entry *pdo_pgsql_get_driver_methods(pdo_dbh_t *dbh, int kind TSRMLS_DC)
     982             : {
     983           3 :         switch (kind) {
     984             :                 case PDO_DBH_DRIVER_METHOD_KIND_DBH:
     985           3 :                         return dbh_methods;
     986             :                 default:
     987           0 :                         return NULL;
     988             :         }
     989             : }
     990             : 
     991          10 : static int pdo_pgsql_set_attr(pdo_dbh_t *dbh, long attr, zval *val TSRMLS_DC)
     992             : {
     993          10 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     994             : 
     995          10 :         switch (attr) {
     996             : #if HAVE_PQPREPARE
     997             :                 case PDO_ATTR_EMULATE_PREPARES:
     998           6 :                         H->emulate_prepares = Z_LVAL_P(val);
     999           6 :                         return 1;
    1000             :                 case PDO_PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT:
    1001           4 :                         H->disable_native_prepares = Z_LVAL_P(val);
    1002           4 :                         return 1;
    1003             : #endif
    1004             : 
    1005             :                 default:
    1006           0 :                         return 0;
    1007             :         }
    1008             : }
    1009             : 
    1010             : static struct pdo_dbh_methods pgsql_methods = {
    1011             :         pgsql_handle_closer,
    1012             :         pgsql_handle_preparer,
    1013             :         pgsql_handle_doer,
    1014             :         pgsql_handle_quoter,
    1015             :         pgsql_handle_begin,
    1016             :         pgsql_handle_commit,
    1017             :         pgsql_handle_rollback,
    1018             :         pdo_pgsql_set_attr,
    1019             :         pdo_pgsql_last_insert_id,
    1020             :         pdo_pgsql_fetch_error_func,
    1021             :         pdo_pgsql_get_attribute,
    1022             :         pdo_pgsql_check_liveness,       /* check_liveness */
    1023             :         pdo_pgsql_get_driver_methods,  /* get_driver_methods */
    1024             :         NULL,
    1025             : };
    1026             : 
    1027         137 : static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */
    1028             : {
    1029             :         pdo_pgsql_db_handle *H;
    1030         137 :         int ret = 0;
    1031             :         char *conn_str, *p, *e;
    1032         137 :         long connect_timeout = 30;
    1033             : 
    1034         137 :         H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent);
    1035         137 :         dbh->driver_data = H;
    1036             : 
    1037         137 :         H->einfo.errcode = 0;
    1038         137 :         H->einfo.errmsg = NULL;
    1039             :         
    1040             :         /* PostgreSQL wants params in the connect string to be separated by spaces,
    1041             :          * if the PDO standard semicolons are used, we convert them to spaces
    1042             :          */
    1043         137 :         e = (char *) dbh->data_source + strlen(dbh->data_source);
    1044         137 :         p = (char *) dbh->data_source;
    1045         274 :         while ((p = memchr(p, ';', (e - p)))) {
    1046           0 :                 *p = ' ';
    1047             :         }
    1048             : 
    1049         137 :         if (driver_options) {
    1050           3 :                 connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30 TSRMLS_CC);
    1051             :         }
    1052             : 
    1053             :         /* support both full connection string & connection string + login and/or password */
    1054         137 :         if (dbh->username && dbh->password) {
    1055           0 :                 spprintf(&conn_str, 0, "%s user=%s password=%s connect_timeout=%ld", dbh->data_source, dbh->username, dbh->password, connect_timeout);
    1056         137 :         } else if (dbh->username) {
    1057           0 :                 spprintf(&conn_str, 0, "%s user=%s connect_timeout=%ld", dbh->data_source, dbh->username, connect_timeout);
    1058         137 :         } else if (dbh->password) {
    1059           0 :                 spprintf(&conn_str, 0, "%s password=%s connect_timeout=%ld", dbh->data_source, dbh->password, connect_timeout);
    1060             :         } else {
    1061         137 :                 spprintf(&conn_str, 0, "%s connect_timeout=%ld", (char *) dbh->data_source, connect_timeout);
    1062             :         }
    1063             : 
    1064         137 :         H->server = PQconnectdb(conn_str);
    1065             : 
    1066         137 :         efree(conn_str);
    1067             : 
    1068         137 :         if (PQstatus(H->server) != CONNECTION_OK) {
    1069           0 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, PHP_PDO_PGSQL_CONNECTION_FAILURE_SQLSTATE);
    1070           0 :                 goto cleanup;
    1071             :         }
    1072             : 
    1073         137 :         PQsetNoticeProcessor(H->server, (void(*)(void*,const char*))_pdo_pgsql_notice, (void *)&dbh);
    1074             : 
    1075         137 :         H->attached = 1;
    1076         137 :         H->pgoid = -1;
    1077             : 
    1078         137 :         dbh->methods = &pgsql_methods;
    1079         137 :         dbh->alloc_own_columns = 1;
    1080         137 :         dbh->max_escaped_char_length = 2;
    1081             : 
    1082         137 :         ret = 1;
    1083             :         
    1084             : cleanup:
    1085         137 :         dbh->methods = &pgsql_methods;
    1086         137 :         if (!ret) {
    1087           0 :                 pgsql_handle_closer(dbh TSRMLS_CC);
    1088             :         }
    1089             : 
    1090         137 :         return ret;
    1091             : }
    1092             : /* }}} */
    1093             : 
    1094             : pdo_driver_t pdo_pgsql_driver = {
    1095             :         PDO_DRIVER_HEADER(pgsql),
    1096             :         pdo_pgsql_handle_factory
    1097             : };
    1098             : 
    1099             : /*
    1100             :  * Local variables:
    1101             :  * tab-width: 4
    1102             :  * c-basic-offset: 4
    1103             :  * End:
    1104             :  * vim600: noet sw=4 ts=4 fdm=marker
    1105             :  * vim<600: noet sw=4 ts=4
    1106             :  */

Generated by: LCOV version 1.10

Generated at Fri, 18 Apr 2014 07:01:32 +0000 (6 days ago)

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