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: 377 524 71.9 %
Date: 2014-10-16 Functions: 29 32 90.6 %
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 "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         396 : static char * _pdo_pgsql_trim_message(const char *message, int persistent)
      45             : {
      46         396 :         register int i = strlen(message)-1;
      47             :         char *tmp;
      48             : 
      49         396 :         if (i>1 && (message[i-1] == '\r' || message[i-1] == '\n') && message[i] == '.') {
      50           0 :                 --i;
      51             :         }
      52        1188 :         while (i>0 && (message[i] == '\r' || message[i] == '\n')) {
      53         396 :                 --i;
      54             :         }
      55         396 :         ++i;
      56         396 :         tmp = pemalloc(i + 1, persistent);
      57         396 :         memcpy(tmp, message, i);
      58         396 :         tmp[i] = '\0';
      59             :         
      60         396 :         return tmp;
      61             : }
      62             : 
      63         396 : 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         396 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
      66         396 :         pdo_error_type *pdo_err = stmt ? &stmt->error_code : &dbh->error_code;
      67         396 :         pdo_pgsql_error_info *einfo = &H->einfo;
      68         396 :         char *errmsg = PQerrorMessage(H->server);
      69             : 
      70         396 :         einfo->errcode = errcode;
      71         396 :         einfo->file = file;
      72         396 :         einfo->line = line;
      73             : 
      74         396 :         if (einfo->errmsg) {
      75         250 :                 pefree(einfo->errmsg, dbh->is_persistent);
      76         250 :                 einfo->errmsg = NULL;
      77             :         }
      78             : 
      79         400 :         if (sqlstate == NULL || strlen(sqlstate) >= sizeof(pdo_error_type)) {
      80           4 :                 strcpy(*pdo_err, "HY000");
      81             :         }
      82             :         else {
      83         392 :                 strcpy(*pdo_err, sqlstate);
      84             :         }
      85             : 
      86         396 :         if (errmsg) {
      87         396 :                 einfo->errmsg = _pdo_pgsql_trim_message(errmsg, dbh->is_persistent);
      88             :         }
      89             : 
      90         396 :         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         396 :         return errcode;
      96             : }
      97             : /* }}} */
      98             : 
      99          51 : 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          51 : }
     103             : /* }}} */
     104             : 
     105          26 : static int pdo_pgsql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC) /* {{{ */
     106             : {
     107          26 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     108          26 :         pdo_pgsql_error_info *einfo = &H->einfo;
     109             : 
     110          26 :         if (einfo->errcode) {
     111          26 :                 add_next_index_long(info, einfo->errcode);
     112          26 :                 add_next_index_string(info, einfo->errmsg, 1);
     113             :         }
     114             : 
     115          26 :         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         146 : static int pgsql_handle_closer(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
     195             : {
     196         146 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     197         146 :         if (H) {
     198         146 :                 if (H->server) {
     199         146 :                         PQfinish(H->server);
     200         146 :                         H->server = NULL;
     201             :                 }
     202         146 :                 if (H->einfo.errmsg) {
     203         145 :                         pefree(H->einfo.errmsg, dbh->is_persistent);
     204         145 :                         H->einfo.errmsg = NULL;
     205             :                 }
     206         146 :                 pefree(H, dbh->is_persistent);
     207         146 :                 dbh->driver_data = NULL;
     208             :         }
     209         146 :         return 0;
     210             : }
     211             : /* }}} */
     212             : 
     213         171 : 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         171 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     216         171 :         pdo_pgsql_stmt *S = ecalloc(1, sizeof(pdo_pgsql_stmt));
     217             :         int scrollable;
     218             : #if HAVE_PQPREPARE
     219             :         int ret;
     220         171 :         char *nsql = NULL;
     221         171 :         int nsql_len = 0;
     222         171 :         int emulate = 0;
     223             : #endif
     224             : 
     225         171 :         S->H = H;
     226         171 :         stmt->driver_data = S;
     227         171 :         stmt->methods = &pgsql_stmt_methods;
     228             : 
     229         171 :         scrollable = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR,
     230             :                 PDO_CURSOR_FWDONLY TSRMLS_CC) == PDO_CURSOR_SCROLL;
     231             : 
     232         171 :         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         169 :         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         141 :                 emulate = H->disable_native_prepares || H->emulate_prepares;
     250             :         }
     251             : 
     252         171 :         if (!emulate && PQprotocolVersion(H->server) > 2) {
     253         154 :                 stmt->supports_placeholders = PDO_PLACEHOLDER_NAMED;
     254         154 :                 stmt->named_rewrite_template = "$%d";
     255         154 :                 ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len TSRMLS_CC);
     256             : 
     257         154 :                 if (ret == 1) {
     258             :                         /* query was re-written */
     259          44 :                         sql = nsql;
     260         110 :                 } 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         154 :                 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         154 :                 if (nsql) {
     270          44 :                         S->query = nsql;
     271             :                 } else {
     272         110 :                         S->query = estrdup(sql);
     273             :                 }
     274             : 
     275         154 :                 return 1;
     276             :         }
     277             : #endif
     278             : 
     279          17 :         stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
     280          17 :         return 1;
     281             : }
     282             : 
     283         592 : static long pgsql_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC)
     284             : {
     285         592 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     286             :         PGresult *res;
     287         592 :         long ret = 1;
     288             :         ExecStatusType qs;
     289             :         
     290         592 :         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         592 :         qs = PQresultStatus(res);
     296         592 :         if (qs != PGRES_COMMAND_OK && qs != PGRES_TUPLES_OK) {
     297         375 :                 pdo_pgsql_error(dbh, qs, pdo_pgsql_sqlstate(res));
     298         375 :                 PQclear(res);
     299         375 :                 return -1;
     300             :         }
     301         217 :         H->pgoid = PQoidValue(res);
     302         217 :         ret = (qs == PGRES_COMMAND_OK) ? atol(PQcmdTuples(res)) : 0L;
     303         217 :         PQclear(res);
     304             : 
     305         217 :         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, (unsigned char *)unquoted, (size_t)unquotedlen, &tmp_len);
     319             : #else
     320             :                         escaped = PQescapeBytea((unsigned char *)unquoted, (size_t)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, (size_t)unquotedlen);
     335             : #else
     336          21 :                         *quotedlen = PQescapeStringConn(H->server, *quoted + 1, unquoted, (size_t)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          36 : static int pdo_pgsql_transaction_cmd(const char *cmd, pdo_dbh_t *dbh TSRMLS_DC)
     469             : {
     470          36 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
     471             :         PGresult *res;
     472          36 :         int ret = 1;
     473             : 
     474          36 :         res = PQexec(H->server, cmd);
     475             : 
     476          36 :         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          36 :         PQclear(res);
     482          36 :         return ret;
     483             : }
     484             : 
     485          18 : static int pgsql_handle_begin(pdo_dbh_t *dbh TSRMLS_DC)
     486             : {
     487          18 :         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          14 : static int pgsql_handle_rollback(pdo_dbh_t *dbh TSRMLS_DC)
     496             : {
     497          14 :         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             : 
     539           4 :         if (pg_fields) {
     540           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"));
     541             :         } else {
     542           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"));
     543             :         }
     544             : 
     545             :         /* Obtain db Handle */
     546           4 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     547             : 
     548           8 :         while ((pgsql_result = PQgetResult(H->server))) {
     549           0 :                 PQclear(pgsql_result);
     550             :         }
     551           4 :         pgsql_result = PQexec(H->server, query);
     552             : 
     553           4 :         efree(query);
     554           4 :         query = NULL;
     555             : 
     556           4 :         if (pgsql_result) {
     557           4 :                 status = PQresultStatus(pgsql_result);
     558             :         } else {
     559           0 :                 status = (ExecStatusType) PQstatus(H->server);
     560             :         }
     561             : 
     562           4 :         if (status == PGRES_COPY_IN && pgsql_result) {
     563           3 :                 int command_failed = 0;
     564           3 :                 int buffer_len = 0;
     565             :                 zval **tmp;
     566             :                 HashPosition pos;
     567             : 
     568           3 :                 PQclear(pgsql_result);
     569           3 :                 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(pg_rows), &pos);
     570          15 :                 while (zend_hash_get_current_data_ex(Z_ARRVAL_P(pg_rows), (void **) &tmp, &pos) == SUCCESS) {
     571             :                         int query_len;
     572           9 :                         convert_to_string_ex(tmp);
     573             :                 
     574           9 :                         if (buffer_len < Z_STRLEN_PP(tmp)) {
     575           3 :                                 buffer_len = Z_STRLEN_PP(tmp);
     576           3 :                                 query = erealloc(query, buffer_len + 2); /* room for \n\0 */
     577             :                         }
     578           9 :                         memcpy(query, Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
     579           9 :                         query_len = Z_STRLEN_PP(tmp);
     580           9 :                         if (query[query_len - 1] != '\n') {
     581           9 :                                 query[query_len++] = '\n';
     582             :                         }
     583           9 :                         query[query_len] = '\0';
     584           9 :                         if (PQputCopyData(H->server, query, query_len) != 1) {
     585           0 :                                 efree(query);
     586           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "copy failed");
     587           0 :                                 RETURN_FALSE;
     588             :                         }
     589           9 :                         zend_hash_move_forward_ex(Z_ARRVAL_P(pg_rows), &pos);
     590             :                 }
     591           3 :                 if (query) {
     592           3 :                         efree(query);
     593             :                 }
     594             : 
     595           3 :                 if (PQputCopyEnd(H->server, NULL) != 1) {
     596           0 :                         pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "putcopyend failed");
     597           0 :                         RETURN_FALSE;
     598             :                 }
     599             : 
     600           9 :                 while ((pgsql_result = PQgetResult(H->server))) {
     601           3 :                         if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
     602           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed");
     603           0 :                                 command_failed = 1;
     604             :                         }
     605           3 :                         PQclear(pgsql_result);
     606             :                 }
     607             : 
     608           3 :                 RETURN_BOOL(!command_failed);
     609             :         } else {
     610           1 :                 PQclear(pgsql_result);
     611           1 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed");
     612           1 :                 RETURN_FALSE;
     613             :         }
     614             : }
     615             : /* }}} */
     616             : 
     617             : /* {{{ proto string PDO::pgsqlCopyFromFile(string $table_name , string $filename [, string $delimiter [, string $null_as ] [, string $fields])
     618             :    Returns true if the copy worked fine or false if error */
     619           4 : static PHP_METHOD(PDO, pgsqlCopyFromFile)
     620             : {
     621             :         pdo_dbh_t *dbh;
     622             :         pdo_pgsql_db_handle *H;
     623             : 
     624           4 :         char *table_name, *filename, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
     625           4 :         int  table_name_len, filename_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
     626             :         char *query;
     627             :         PGresult *pgsql_result;
     628             :         ExecStatusType status;
     629             :         php_stream *stream;
     630             : 
     631           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sp|sss",
     632             :                                 &table_name, &table_name_len, &filename, &filename_len,
     633             :                                 &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
     634           0 :                 return;
     635             :         }
     636             : 
     637             :         /* Obtain db Handler */
     638           4 :         dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
     639           4 :         PDO_CONSTRUCT_CHECK;
     640             : 
     641           4 :         stream = php_stream_open_wrapper_ex(filename, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, FG(default_context));
     642           4 :         if (!stream) {
     643           0 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Unable to open the file");
     644           0 :                 RETURN_FALSE;
     645             :         }
     646             : 
     647           4 :         if (pg_fields) {
     648           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"));
     649             :         } else {
     650           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"));
     651             :         }
     652             : 
     653           4 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     654             : 
     655           8 :         while ((pgsql_result = PQgetResult(H->server))) {
     656           0 :                 PQclear(pgsql_result);
     657             :         }
     658           4 :         pgsql_result = PQexec(H->server, query);
     659             : 
     660           4 :         efree(query);
     661             : 
     662           4 :         if (pgsql_result) {
     663           4 :                 status = PQresultStatus(pgsql_result);
     664             :         } else {
     665           0 :                 status = (ExecStatusType) PQstatus(H->server);
     666             :         }
     667             : 
     668           4 :         if (status == PGRES_COPY_IN && pgsql_result) {
     669             :                 char *buf;
     670           3 :                 int command_failed = 0;
     671           3 :                 size_t line_len = 0;
     672             : 
     673           3 :                 PQclear(pgsql_result);
     674          15 :                 while ((buf = php_stream_get_line(stream, NULL, 0, &line_len)) != NULL) {
     675           9 :                         if (PQputCopyData(H->server, buf, line_len) != 1) {
     676           0 :                                 efree(buf);
     677           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "copy failed");
     678           0 :                                 php_stream_close(stream);
     679           0 :                                 RETURN_FALSE;
     680             :                         }
     681           9 :                         efree(buf);
     682             :                 }
     683           3 :                 php_stream_close(stream);
     684             : 
     685           3 :                 if (PQputCopyEnd(H->server, NULL) != 1) {
     686           0 :                         pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "putcopyend failed");
     687           0 :                         RETURN_FALSE;
     688             :                 }
     689             : 
     690           9 :                 while ((pgsql_result = PQgetResult(H->server))) {
     691           3 :                         if (PGRES_COMMAND_OK != PQresultStatus(pgsql_result)) {
     692           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed");
     693           0 :                                 command_failed = 1;
     694             :                         }
     695           3 :                         PQclear(pgsql_result);
     696             :                 }
     697             : 
     698           3 :                 RETURN_BOOL(!command_failed);
     699             :         } else {
     700           1 :                 PQclear(pgsql_result);
     701           1 :                 php_stream_close(stream);
     702           1 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed");
     703           1 :                 RETURN_FALSE;
     704             :         }
     705             : }
     706             : /* }}} */
     707             : 
     708             : 
     709             : /* {{{ proto string PDO::pgsqlCopyToFile(string $table_name , $filename, [string $delimiter [, string $null_as [, string $fields]]])
     710             :    Returns true if the copy worked fine or false if error */
     711           4 : static PHP_METHOD(PDO, pgsqlCopyToFile)
     712             : {
     713             :         pdo_dbh_t *dbh;
     714             :         pdo_pgsql_db_handle *H;
     715             : 
     716           4 :         char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL, *filename = NULL;
     717           4 :         int table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len, filename_len;
     718             :         char *query;
     719             : 
     720             :         PGresult *pgsql_result;
     721             :         ExecStatusType status;
     722             : 
     723             :         php_stream *stream;
     724             : 
     725           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sp|sss",
     726             :                                         &table_name, &table_name_len, &filename, &filename_len,
     727             :                                         &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
     728           0 :                 return;
     729             :         }
     730             : 
     731           4 :         dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
     732           4 :         PDO_CONSTRUCT_CHECK;
     733             : 
     734           4 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     735             : 
     736           4 :         stream = php_stream_open_wrapper_ex(filename, "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, FG(default_context));
     737           4 :         if (!stream) {
     738           0 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Unable to open the file for writing");
     739           0 :                 RETURN_FALSE;
     740             :         }
     741             : 
     742           8 :         while ((pgsql_result = PQgetResult(H->server))) {
     743           0 :                 PQclear(pgsql_result);
     744             :         }
     745             : 
     746           4 :         if (pg_fields) {
     747           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"));
     748             :         } else {
     749           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"));
     750             :         }
     751           4 :         pgsql_result = PQexec(H->server, query);
     752           4 :         efree(query);
     753             : 
     754           4 :         if (pgsql_result) {
     755           4 :                 status = PQresultStatus(pgsql_result);
     756             :         } else {
     757           0 :                 status = (ExecStatusType) PQstatus(H->server);
     758             :         }
     759             : 
     760           4 :         if (status == PGRES_COPY_OUT && pgsql_result) {
     761           3 :                 PQclear(pgsql_result);
     762             :                 while (1) {
     763          12 :                         char *csv = NULL;
     764          12 :                         int ret = PQgetCopyData(H->server, &csv, 0);
     765             : 
     766          12 :                         if (ret == -1) {
     767           3 :                                 break; /* done */
     768           9 :                         } else if (ret > 0) {
     769           9 :                                 if (php_stream_write(stream, csv, ret) != ret) {
     770           0 :                                         pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Unable to write to file");
     771           0 :                                         PQfreemem(csv);
     772           0 :                                         php_stream_close(stream);
     773           0 :                                         RETURN_FALSE;
     774             :                                 } else {
     775           9 :                                         PQfreemem(csv);
     776             :                                 }
     777             :                         } else {
     778           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed: getline failed");
     779           0 :                                 php_stream_close(stream);
     780           0 :                                 RETURN_FALSE;
     781             :                         }
     782           9 :                 }
     783           3 :                 php_stream_close(stream);
     784             : 
     785           9 :                 while ((pgsql_result = PQgetResult(H->server))) {
     786           3 :                         PQclear(pgsql_result);
     787             :                 }
     788           3 :                 RETURN_TRUE;
     789             :         } else {
     790           1 :                 php_stream_close(stream);
     791           1 :                 PQclear(pgsql_result);
     792           1 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed");
     793           1 :                 RETURN_FALSE;
     794             :         }
     795             : }
     796             : /* }}} */
     797             : 
     798             : /* {{{ proto string PDO::pgsqlCopyToArray(string $table_name , [string $delimiter [, string $null_as [, string $fields]]])
     799             :    Returns true if the copy worked fine or false if error */
     800           4 : static PHP_METHOD(PDO, pgsqlCopyToArray)
     801             : {
     802             :         pdo_dbh_t *dbh;
     803             :         pdo_pgsql_db_handle *H;
     804             : 
     805           4 :         char *table_name, *pg_delim = NULL, *pg_null_as = NULL, *pg_fields = NULL;
     806           4 :         int table_name_len, pg_delim_len = 0, pg_null_as_len = 0, pg_fields_len;
     807             :         char *query;
     808             : 
     809             :         PGresult *pgsql_result;
     810             :         ExecStatusType status;
     811             : 
     812           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sss",
     813             :                 &table_name, &table_name_len,
     814             :                 &pg_delim, &pg_delim_len, &pg_null_as, &pg_null_as_len, &pg_fields, &pg_fields_len) == FAILURE) {
     815           0 :                 return;
     816             :         }
     817             : 
     818           4 :         dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
     819           4 :         PDO_CONSTRUCT_CHECK;
     820             : 
     821           4 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     822             : 
     823           8 :         while ((pgsql_result = PQgetResult(H->server))) {
     824           0 :                 PQclear(pgsql_result);
     825             :         }
     826             : 
     827           4 :         if (pg_fields) {
     828           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"));
     829             :         } else {
     830           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"));
     831             :         }
     832           4 :         pgsql_result = PQexec(H->server, query);
     833           4 :         efree(query);
     834             : 
     835           4 :         if (pgsql_result) {
     836           4 :                 status = PQresultStatus(pgsql_result);
     837             :         } else {
     838           0 :                 status = (ExecStatusType) PQstatus(H->server);
     839             :         }
     840             : 
     841           7 :         if (status == PGRES_COPY_OUT && pgsql_result) {
     842           3 :                 PQclear(pgsql_result);
     843           3 :                 array_init(return_value);
     844             : 
     845             :                 while (1) {
     846          12 :                         char *csv = NULL;
     847          12 :                         int ret = PQgetCopyData(H->server, &csv, 0);
     848          12 :                         if (ret == -1) {
     849           3 :                                 break; /* copy done */
     850           9 :                         } else if (ret > 0) { 
     851           9 :                                 add_next_index_stringl(return_value, csv, ret, 1);
     852           9 :                                 PQfreemem(csv);
     853             :                         } else {
     854           0 :                                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed: getline failed");
     855           0 :                                 RETURN_FALSE;
     856             :                         }
     857           9 :                 }
     858             : 
     859           9 :                 while ((pgsql_result = PQgetResult(H->server))) {
     860           3 :                         PQclear(pgsql_result);
     861             :                 }
     862             :         } else {
     863           1 :                 PQclear(pgsql_result);
     864           1 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "Copy command failed");
     865           1 :                 RETURN_FALSE;
     866             :         }
     867             : }
     868             : /* }}} */
     869             : 
     870             : 
     871             : /* {{{ proto string PDO::pgsqlLOBCreate()
     872             :    Creates a new large object, returning its identifier.  Must be called inside a transaction. */
     873           1 : static PHP_METHOD(PDO, pgsqlLOBCreate)
     874             : {
     875             :         pdo_dbh_t *dbh;
     876             :         pdo_pgsql_db_handle *H;
     877             :         Oid lfd;
     878             : 
     879           1 :         dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
     880           1 :         PDO_CONSTRUCT_CHECK;
     881             : 
     882           1 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     883           1 :         lfd = lo_creat(H->server, INV_READ|INV_WRITE);
     884             : 
     885           1 :         if (lfd != InvalidOid) {
     886             :                 char *buf;
     887           1 :                 spprintf(&buf, 0, "%lu", (long) lfd);
     888           1 :                 RETURN_STRING(buf, 0);
     889             :         }
     890             :         
     891           0 :         pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "HY000");
     892           0 :         RETURN_FALSE;
     893             : }
     894             : /* }}} */
     895             : 
     896             : /* {{{ proto resource PDO::pgsqlLOBOpen(string oid [, string mode = 'rb'])
     897             :    Opens an existing large object stream.  Must be called inside a transaction. */
     898           1 : static PHP_METHOD(PDO, pgsqlLOBOpen)
     899             : {
     900             :         pdo_dbh_t *dbh;
     901             :         pdo_pgsql_db_handle *H;
     902             :         Oid oid;
     903             :         int lfd;
     904             :         char *oidstr;
     905             :         int oidstrlen;
     906           1 :         char *modestr = "rb";
     907             :         int modestrlen;
     908           1 :         int mode = INV_READ;
     909             :         char *end_ptr;
     910             : 
     911           1 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s",
     912             :                                 &oidstr, &oidstrlen, &modestr, &modestrlen)) {
     913           0 :                 RETURN_FALSE;
     914             :         }
     915             : 
     916           1 :         oid = (Oid)strtoul(oidstr, &end_ptr, 10);
     917           1 :         if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
     918           0 :                 RETURN_FALSE;
     919             :         }
     920             : 
     921           1 :         if (strpbrk(modestr, "+w")) {
     922           1 :                 mode = INV_READ|INV_WRITE;
     923             :         }
     924             :         
     925           1 :         dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
     926           1 :         PDO_CONSTRUCT_CHECK;
     927             : 
     928           1 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     929             : 
     930           1 :         lfd = lo_open(H->server, oid, mode);
     931             : 
     932           1 :         if (lfd >= 0) {
     933           1 :                 php_stream *stream = pdo_pgsql_create_lob_stream(dbh, lfd, oid TSRMLS_CC);
     934           1 :                 if (stream) {
     935           1 :                         php_stream_to_zval(stream, return_value);
     936           1 :                         return;
     937             :                 }
     938             :         } else {
     939           0 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "HY000");
     940             :         }
     941           0 :         RETURN_FALSE;
     942             : }
     943             : /* }}} */
     944             : 
     945             : /* {{{ proto bool PDO::pgsqlLOBUnlink(string oid)
     946             :    Deletes the large object identified by oid.  Must be called inside a transaction. */
     947           1 : static PHP_METHOD(PDO, pgsqlLOBUnlink)
     948             : {
     949             :         pdo_dbh_t *dbh;
     950             :         pdo_pgsql_db_handle *H;
     951             :         Oid oid;
     952             :         char *oidstr, *end_ptr;
     953             :         int oidlen;
     954             : 
     955           1 :         if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s",
     956             :                                 &oidstr, &oidlen)) {
     957           0 :                 RETURN_FALSE;
     958             :         }
     959             : 
     960           1 :         oid = (Oid)strtoul(oidstr, &end_ptr, 10);
     961           1 :         if (oid == 0 && (errno == ERANGE || errno == EINVAL)) {
     962           0 :                 RETURN_FALSE;
     963             :         }
     964             : 
     965           1 :         dbh = zend_object_store_get_object(getThis() TSRMLS_CC);
     966           1 :         PDO_CONSTRUCT_CHECK;
     967             : 
     968           1 :         H = (pdo_pgsql_db_handle *)dbh->driver_data;
     969             :         
     970           1 :         if (1 == lo_unlink(H->server, oid)) {
     971           1 :                 RETURN_TRUE;
     972             :         }
     973           0 :         pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, "HY000");
     974           0 :         RETURN_FALSE;
     975             : }
     976             : /* }}} */
     977             : 
     978             : 
     979             : static const zend_function_entry dbh_methods[] = {
     980             :         PHP_ME(PDO, pgsqlLOBCreate, NULL, ZEND_ACC_PUBLIC)
     981             :         PHP_ME(PDO, pgsqlLOBOpen, NULL, ZEND_ACC_PUBLIC)
     982             :         PHP_ME(PDO, pgsqlLOBUnlink, NULL, ZEND_ACC_PUBLIC)
     983             :         PHP_ME(PDO, pgsqlCopyFromArray, NULL, ZEND_ACC_PUBLIC)
     984             :         PHP_ME(PDO, pgsqlCopyFromFile, NULL, ZEND_ACC_PUBLIC)
     985             :         PHP_ME(PDO, pgsqlCopyToArray, NULL, ZEND_ACC_PUBLIC)
     986             :         PHP_ME(PDO, pgsqlCopyToFile, NULL, ZEND_ACC_PUBLIC)
     987             :         PHP_FE_END
     988             : };
     989             : 
     990           3 : static const zend_function_entry *pdo_pgsql_get_driver_methods(pdo_dbh_t *dbh, int kind TSRMLS_DC)
     991             : {
     992           3 :         switch (kind) {
     993             :                 case PDO_DBH_DRIVER_METHOD_KIND_DBH:
     994           3 :                         return dbh_methods;
     995             :                 default:
     996           0 :                         return NULL;
     997             :         }
     998             : }
     999             : 
    1000          12 : static int pdo_pgsql_set_attr(pdo_dbh_t *dbh, long attr, zval *val TSRMLS_DC)
    1001             : {
    1002          12 :         pdo_pgsql_db_handle *H = (pdo_pgsql_db_handle *)dbh->driver_data;
    1003             : 
    1004          12 :         switch (attr) {
    1005             : #if HAVE_PQPREPARE
    1006             :                 case PDO_ATTR_EMULATE_PREPARES:
    1007           7 :                         H->emulate_prepares = Z_LVAL_P(val);
    1008           7 :                         return 1;
    1009             :                 case PDO_PGSQL_ATTR_DISABLE_NATIVE_PREPARED_STATEMENT:
    1010           4 :                         H->disable_native_prepares = Z_LVAL_P(val);
    1011           4 :                         return 1;
    1012             : #endif
    1013             : 
    1014             :                 default:
    1015           1 :                         return 0;
    1016             :         }
    1017             : }
    1018             : 
    1019             : static struct pdo_dbh_methods pgsql_methods = {
    1020             :         pgsql_handle_closer,
    1021             :         pgsql_handle_preparer,
    1022             :         pgsql_handle_doer,
    1023             :         pgsql_handle_quoter,
    1024             :         pgsql_handle_begin,
    1025             :         pgsql_handle_commit,
    1026             :         pgsql_handle_rollback,
    1027             :         pdo_pgsql_set_attr,
    1028             :         pdo_pgsql_last_insert_id,
    1029             :         pdo_pgsql_fetch_error_func,
    1030             :         pdo_pgsql_get_attribute,
    1031             :         pdo_pgsql_check_liveness,       /* check_liveness */
    1032             :         pdo_pgsql_get_driver_methods,  /* get_driver_methods */
    1033             :         NULL,
    1034             :         pgsql_handle_in_transaction,
    1035             : };
    1036             : 
    1037         147 : static int pdo_pgsql_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */
    1038             : {
    1039             :         pdo_pgsql_db_handle *H;
    1040         147 :         int ret = 0;
    1041             :         char *conn_str, *p, *e;
    1042             :         char *tmp_pass;
    1043         147 :         long connect_timeout = 30;
    1044             : 
    1045         147 :         H = pecalloc(1, sizeof(pdo_pgsql_db_handle), dbh->is_persistent);
    1046         147 :         dbh->driver_data = H;
    1047             : 
    1048         147 :         H->einfo.errcode = 0;
    1049         147 :         H->einfo.errmsg = NULL;
    1050             :         
    1051             :         /* PostgreSQL wants params in the connect string to be separated by spaces,
    1052             :          * if the PDO standard semicolons are used, we convert them to spaces
    1053             :          */
    1054         147 :         e = (char *) dbh->data_source + strlen(dbh->data_source);
    1055         147 :         p = (char *) dbh->data_source;
    1056         294 :         while ((p = memchr(p, ';', (e - p)))) {
    1057           0 :                 *p = ' ';
    1058             :         }
    1059             : 
    1060         147 :         if (driver_options) {
    1061           4 :                 connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30 TSRMLS_CC);
    1062             :         }
    1063             : 
    1064         147 :         if (dbh->password) {
    1065           0 :                 if (dbh->password[0] != '\'' && dbh->password[strlen(dbh->password) - 1] != '\'') {
    1066           0 :                         char *pwd = dbh->password;
    1067           0 :                         int pos = 1;
    1068             : 
    1069           0 :                         tmp_pass = safe_emalloc(2, strlen(dbh->password), 3);
    1070           0 :                         tmp_pass[0] = '\'';
    1071             : 
    1072           0 :                         while (*pwd != '\0') {
    1073           0 :                                 if (*pwd == '\\' || *pwd == '\'') {
    1074           0 :                                         tmp_pass[pos++] = '\\';
    1075             :                                 }
    1076             : 
    1077           0 :                                 tmp_pass[pos++] = *pwd++;
    1078             :                         }
    1079             : 
    1080           0 :                         tmp_pass[pos++] = '\'';
    1081           0 :                         tmp_pass[pos] = '\0';
    1082             :                 } else {
    1083           0 :                         tmp_pass = dbh->password;
    1084             :                 }
    1085             :         }
    1086             : 
    1087             :         /* support both full connection string & connection string + login and/or password */
    1088         147 :         if (dbh->username && dbh->password) {
    1089           0 :                 spprintf(&conn_str, 0, "%s user=%s password=%s connect_timeout=%ld", dbh->data_source, dbh->username, tmp_pass, connect_timeout);
    1090         147 :         } else if (dbh->username) {
    1091           0 :                 spprintf(&conn_str, 0, "%s user=%s connect_timeout=%ld", dbh->data_source, dbh->username, connect_timeout);
    1092         147 :         } else if (dbh->password) {
    1093           0 :                 spprintf(&conn_str, 0, "%s password=%s connect_timeout=%ld", dbh->data_source, tmp_pass, connect_timeout);
    1094             :         } else {
    1095         147 :                 spprintf(&conn_str, 0, "%s connect_timeout=%ld", (char *) dbh->data_source, connect_timeout);
    1096             :         }
    1097             : 
    1098         147 :         H->server = PQconnectdb(conn_str);
    1099         147 :         if (dbh->password && tmp_pass != dbh->password) {
    1100           0 :                 efree(tmp_pass);
    1101             :         }
    1102             : 
    1103         147 :         efree(conn_str);
    1104             : 
    1105         147 :         if (PQstatus(H->server) != CONNECTION_OK) {
    1106           0 :                 pdo_pgsql_error(dbh, PGRES_FATAL_ERROR, PHP_PDO_PGSQL_CONNECTION_FAILURE_SQLSTATE);
    1107           0 :                 goto cleanup;
    1108             :         }
    1109             : 
    1110         147 :         PQsetNoticeProcessor(H->server, (void(*)(void*,const char*))_pdo_pgsql_notice, (void *)&dbh);
    1111             : 
    1112         147 :         H->attached = 1;
    1113         147 :         H->pgoid = -1;
    1114             : 
    1115         147 :         dbh->methods = &pgsql_methods;
    1116         147 :         dbh->alloc_own_columns = 1;
    1117         147 :         dbh->max_escaped_char_length = 2;
    1118             : 
    1119         147 :         ret = 1;
    1120             :         
    1121             : cleanup:
    1122         147 :         dbh->methods = &pgsql_methods;
    1123         147 :         if (!ret) {
    1124           0 :                 pgsql_handle_closer(dbh TSRMLS_CC);
    1125             :         }
    1126             : 
    1127         147 :         return ret;
    1128             : }
    1129             : /* }}} */
    1130             : 
    1131             : pdo_driver_t pdo_pgsql_driver = {
    1132             :         PDO_DRIVER_HEADER(pgsql),
    1133             :         pdo_pgsql_handle_factory
    1134             : };
    1135             : 
    1136             : /*
    1137             :  * Local variables:
    1138             :  * tab-width: 4
    1139             :  * c-basic-offset: 4
    1140             :  * End:
    1141             :  * vim600: noet sw=4 ts=4 fdm=marker
    1142             :  * vim<600: noet sw=4 ts=4
    1143             :  */

Generated by: LCOV version 1.10

Generated at Thu, 16 Oct 2014 05:27:07 +0000 (6 days ago)

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