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_statement.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 259 306 84.6 %
Date: 2014-07-23 Functions: 7 8 87.5 %
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 "php_pdo_pgsql.h"
      33             : #include "php_pdo_pgsql_int.h"
      34             : #if HAVE_NETINET_IN_H
      35             : #include <netinet/in.h>
      36             : #endif
      37             : 
      38             : /* from postgresql/src/include/catalog/pg_type.h */
      39             : #define BOOLOID     16
      40             : #define BYTEAOID    17
      41             : #define INT8OID     20
      42             : #define INT2OID     21
      43             : #define INT4OID     23
      44             : #define TEXTOID     25
      45             : #define OIDOID      26
      46             : 
      47         161 : static int pgsql_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC)
      48             : {
      49         161 :         pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
      50             : 
      51         161 :         if (S->result) {
      52             :                 /* free the resource */
      53         154 :                 PQclear(S->result);
      54         154 :                 S->result = NULL;
      55             :         }
      56             : 
      57             : #if HAVE_PQPREPARE
      58         161 :         if (S->stmt_name) {
      59         145 :                 pdo_pgsql_db_handle *H = S->H;
      60         145 :                 char *q = NULL;
      61             :                 PGresult *res;
      62             : 
      63         145 :                 if (S->is_prepared) {
      64         129 :                         spprintf(&q, 0, "DEALLOCATE %s", S->stmt_name);
      65         129 :                         res = PQexec(H->server, q);
      66         129 :                         efree(q);
      67         129 :                         if (res) {
      68         129 :                                 PQclear(res);
      69             :                         }
      70             :                 }
      71         145 :                 efree(S->stmt_name);
      72         145 :                 S->stmt_name = NULL;
      73             :         }
      74         161 :         if (S->param_lengths) {
      75          39 :                 efree(S->param_lengths);
      76          39 :                 S->param_lengths = NULL;
      77             :         }
      78         161 :         if (S->param_values) {
      79          39 :                 efree(S->param_values);
      80          39 :                 S->param_values = NULL;
      81             :         }
      82         161 :         if (S->param_formats) {
      83          39 :                 efree(S->param_formats);
      84          39 :                 S->param_formats = NULL;
      85             :         }
      86         161 :         if (S->param_types) {
      87          39 :                 efree(S->param_types);
      88          39 :                 S->param_types = NULL;
      89             :         }
      90         161 :         if (S->query) {
      91         145 :                 efree(S->query);
      92         145 :                 S->query = NULL;
      93             :         }
      94             : #endif
      95             : 
      96         161 :         if (S->cursor_name) {
      97           2 :                 pdo_pgsql_db_handle *H = S->H;
      98           2 :                 char *q = NULL;
      99             :                 PGresult *res;
     100             : 
     101           2 :                 spprintf(&q, 0, "CLOSE %s", S->cursor_name);
     102           2 :                 res = PQexec(H->server, q);
     103           2 :                 efree(q);
     104           2 :                 if (res) PQclear(res);
     105           2 :                 efree(S->cursor_name);
     106           2 :                 S->cursor_name = NULL;
     107             :         }
     108             :         
     109         161 :         if(S->cols) {
     110         142 :                 efree(S->cols);
     111         142 :                 S->cols = NULL;
     112             :         }
     113         161 :         efree(S);
     114         161 :         stmt->driver_data = NULL;
     115         161 :         return 1;
     116             : }
     117             : 
     118         248 : static int pgsql_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC)
     119             : {
     120         248 :         pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
     121         248 :         pdo_pgsql_db_handle *H = S->H;
     122             :         ExecStatusType status;
     123             : 
     124             :         /* ensure that we free any previous unfetched results */
     125         248 :         if(S->result) {
     126          93 :                 PQclear(S->result);
     127          93 :                 S->result = NULL;
     128             :         }
     129             :         
     130         248 :         S->current_row = 0;
     131             : 
     132         248 :         if (S->cursor_name) {
     133           3 :                 char *q = NULL;
     134             : 
     135           3 :                 if (S->is_prepared) {
     136           1 :                         spprintf(&q, 0, "CLOSE %s", S->cursor_name);
     137           1 :                         S->result = PQexec(H->server, q);
     138           1 :                         efree(q);
     139             :                 }
     140             : 
     141           3 :                 spprintf(&q, 0, "DECLARE %s SCROLL CURSOR WITH HOLD FOR %s", S->cursor_name, stmt->active_query_string);
     142           3 :                 S->result = PQexec(H->server, q);
     143           3 :                 efree(q);
     144             : 
     145             :                 /* check if declare failed */
     146           3 :                 status = PQresultStatus(S->result);
     147           3 :                 if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
     148           0 :                         pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
     149           0 :                         return 0;
     150             :                 }
     151             : 
     152             :                 /* the cursor was declared correctly */
     153           3 :                 S->is_prepared = 1;
     154             : 
     155             :                 /* fetch to be able to get the number of tuples later, but don't advance the cursor pointer */
     156           3 :                 spprintf(&q, 0, "FETCH FORWARD 0 FROM %s", S->cursor_name);
     157           3 :                 S->result = PQexec(H->server, q);
     158           3 :                 efree(q);
     159             :         } else
     160             : #if HAVE_PQPREPARE
     161         245 :         if (S->stmt_name) {
     162             :                 /* using a prepared statement */
     163             : 
     164         226 :                 if (!S->is_prepared) {
     165             : stmt_retry:
     166             :                         /* we deferred the prepare until now, because we didn't
     167             :                          * know anything about the parameter types; now we do */
     168         178 :                         S->result = PQprepare(H->server, S->stmt_name, S->query, 
     169         178 :                                                 stmt->bound_params ? zend_hash_num_elements(stmt->bound_params) : 0,
     170         139 :                                                 S->param_types);
     171         139 :                         status = PQresultStatus(S->result);
     172         139 :                         switch (status) {
     173             :                                 case PGRES_COMMAND_OK:
     174             :                                 case PGRES_TUPLES_OK:
     175             :                                         /* it worked */
     176         130 :                                         S->is_prepared = 1;
     177         130 :                                         PQclear(S->result);
     178         130 :                                         break;
     179             :                                 default: {
     180           9 :                                         char *sqlstate = pdo_pgsql_sqlstate(S->result);
     181             :                                         /* 42P05 means that the prepared statement already existed. this can happen if you use 
     182             :                                          * a connection pooling software line pgpool which doesn't close the db-connection once 
     183             :                                          * php disconnects. if php dies (no chance to run RSHUTDOWN) during execution it has no 
     184             :                                          * chance to DEALLOCATE the prepared statements it has created. so, if we hit a 42P05 we 
     185             :                                          * deallocate it and retry ONCE (thies 2005.12.15)
     186             :                                          */
     187           9 :                                         if (sqlstate && !strcmp(sqlstate, "42P05")) {
     188             :                                                 char buf[100]; /* stmt_name == "pdo_crsr_%08x" */
     189             :                                                 PGresult *res;
     190           0 :                                                 snprintf(buf, sizeof(buf), "DEALLOCATE %s", S->stmt_name);
     191           0 :                                                 res = PQexec(H->server, buf);
     192           0 :                                                 if (res) {
     193           0 :                                                         PQclear(res);
     194             :                                                 }
     195           0 :                                                 goto stmt_retry;
     196             :                                         } else {
     197           9 :                                                 pdo_pgsql_error_stmt(stmt, status, sqlstate);
     198           9 :                                                 return 0;
     199             :                                         }
     200             :                                 }
     201             :                         }
     202             :                 }
     203         527 :                 S->result = PQexecPrepared(H->server, S->stmt_name,
     204         217 :                                 stmt->bound_params ?
     205          93 :                                         zend_hash_num_elements(stmt->bound_params) :
     206             :                                         0,
     207             :                                 (const char**)S->param_values,
     208         217 :                                 S->param_lengths,
     209         217 :                                 S->param_formats,
     210             :                                 0);
     211             :         } else
     212             : #endif
     213             :         {
     214          19 :                 S->result = PQexec(H->server, stmt->active_query_string);
     215             :         }
     216         239 :         status = PQresultStatus(S->result);
     217             : 
     218         239 :         if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
     219           5 :                 pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
     220           5 :                 return 0;
     221             :         }
     222             : 
     223         234 :         if (!stmt->executed && !stmt->column_count) {
     224         143 :                 stmt->column_count = (int) PQnfields(S->result);
     225         143 :                 S->cols = ecalloc(stmt->column_count, sizeof(pdo_pgsql_column));
     226             :         }
     227             : 
     228         234 :         if (status == PGRES_COMMAND_OK) {
     229          71 :                 stmt->row_count = (long)atoi(PQcmdTuples(S->result));
     230          71 :                 H->pgoid = PQoidValue(S->result);
     231             :         } else {
     232         163 :                 stmt->row_count = (long)PQntuples(S->result);
     233             :         }
     234             : 
     235         234 :         return 1;
     236             : }
     237             : 
     238        1300 : static int pgsql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
     239             :                 enum pdo_param_event event_type TSRMLS_DC)
     240             : {
     241             : #if HAVE_PQPREPARE
     242        1300 :         pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
     243             : 
     244        1800 :         if (S->stmt_name && param->is_param) {
     245         932 :                 switch (event_type) {
     246             :                         case PDO_PARAM_EVT_FREE:
     247         157 :                                 if (param->driver_data) {
     248           1 :                                         efree(param->driver_data);
     249             :                                 }
     250         157 :                                 break;
     251             : 
     252             :                         case PDO_PARAM_EVT_NORMALIZE:
     253             :                                 /* decode name from $1, $2 into 0, 1 etc. */
     254         159 :                                 if (param->name) {
     255          56 :                                         if (param->name[0] == '$') {
     256           0 :                                                 param->paramno = atoi(param->name + 1);
     257             :                                         } else {
     258             :                                                 /* resolve parameter name to rewritten name */
     259             :                                                 char *nameptr;
     260         220 :                                                 if (stmt->bound_param_map && SUCCESS == zend_hash_find(stmt->bound_param_map,
     261         110 :                                                                 param->name, param->namelen + 1, (void**)&nameptr)) {
     262          54 :                                                         param->paramno = atoi(nameptr + 1) - 1;
     263             :                                                 } else {
     264           2 :                                                         pdo_raise_impl_error(stmt->dbh, stmt, "HY093", param->name TSRMLS_CC);
     265           2 :                                                         return 0;
     266             :                                                 }
     267             :                                         }
     268             :                                 }
     269         157 :                                 break;
     270             : 
     271             :                         case PDO_PARAM_EVT_ALLOC:
     272             :                         case PDO_PARAM_EVT_EXEC_POST:
     273             :                         case PDO_PARAM_EVT_FETCH_PRE:
     274             :                         case PDO_PARAM_EVT_FETCH_POST:
     275             :                                 /* work is handled by EVT_NORMALIZE */
     276         429 :                                 return 1;
     277             : 
     278             :                         case PDO_PARAM_EVT_EXEC_PRE:
     279         187 :                                 if (!stmt->bound_param_map) {
     280           0 :                                         return 0;
     281             :                                 }
     282         187 :                                 if (!S->param_values) {
     283          39 :                                         S->param_values = ecalloc(
     284             :                                                         zend_hash_num_elements(stmt->bound_param_map),
     285             :                                                         sizeof(char*));
     286          39 :                                         S->param_lengths = ecalloc(
     287             :                                                         zend_hash_num_elements(stmt->bound_param_map),
     288             :                                                         sizeof(int));
     289          39 :                                         S->param_formats = ecalloc(
     290             :                                                         zend_hash_num_elements(stmt->bound_param_map),
     291             :                                                         sizeof(int));
     292          39 :                                         S->param_types = ecalloc(
     293             :                                                         zend_hash_num_elements(stmt->bound_param_map),
     294             :                                                         sizeof(Oid));
     295             :                                 }
     296         187 :                                 if (param->paramno >= 0) {
     297         187 :                                         if (param->paramno > zend_hash_num_elements(stmt->bound_param_map)) {
     298           0 :                                                 pdo_pgsql_error_stmt(stmt, PGRES_FATAL_ERROR, "HY105");
     299           0 :                                                 return 0;
     300             :                                         }
     301             : 
     302         197 :                                         if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB &&
     303          10 :                                                         Z_TYPE_P(param->parameter) == IS_RESOURCE) {
     304             :                                                 php_stream *stm;
     305           6 :                                                 php_stream_from_zval_no_verify(stm, &param->parameter);
     306           6 :                                                 if (stm) {
     307           6 :                                                         if (php_stream_is(stm, &pdo_pgsql_lob_stream_ops)) {
     308           1 :                                                                 struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stm->abstract;
     309           1 :                                                                 pdo_pgsql_bound_param *P = param->driver_data;
     310             : 
     311           1 :                                                                 if (P == NULL) {
     312           1 :                                                                         P = ecalloc(1, sizeof(*P));
     313           1 :                                                                         param->driver_data = P;
     314             :                                                                 }
     315           1 :                                                                 P->oid = htonl(self->oid);
     316           1 :                                                                 S->param_values[param->paramno] = (char*)&P->oid;
     317           1 :                                                                 S->param_lengths[param->paramno] = sizeof(P->oid);
     318           1 :                                                                 S->param_formats[param->paramno] = 1;
     319           1 :                                                                 S->param_types[param->paramno] = OIDOID;
     320           1 :                                                                 return 1;
     321             :                                                         } else {
     322             :                                                                 int len;
     323             :                                                                 
     324          11 :                                                                 SEPARATE_ZVAL_IF_NOT_REF(&param->parameter);
     325           5 :                                                                 Z_TYPE_P(param->parameter) = IS_STRING;
     326             :                                                                 
     327           5 :                                                                 if ((len = php_stream_copy_to_mem(stm, &Z_STRVAL_P(param->parameter), PHP_STREAM_COPY_ALL, 0)) > 0) {
     328           3 :                                                                         Z_STRLEN_P(param->parameter) = len;
     329             :                                                                 } else {
     330           2 :                                                                         ZVAL_EMPTY_STRING(param->parameter);
     331             :                                                                 }
     332             :                                                         }
     333             :                                                 } else {
     334             :                                                         /* expected a stream resource */
     335           0 :                                                         pdo_pgsql_error_stmt(stmt, PGRES_FATAL_ERROR, "HY105");
     336           0 :                                                         return 0;
     337             :                                                 }
     338             :                                         }
     339             : 
     340         379 :                                         if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL ||
     341         186 :                                                         Z_TYPE_P(param->parameter) == IS_NULL) {
     342           7 :                                                 S->param_values[param->paramno] = NULL;
     343           7 :                                                 S->param_lengths[param->paramno] = 0;
     344         179 :                                         } else if (Z_TYPE_P(param->parameter) == IS_BOOL) {
     345           2 :                                                 S->param_values[param->paramno] = Z_BVAL_P(param->parameter) ? "t" : "f";
     346           2 :                                                 S->param_lengths[param->paramno] = 1;
     347           2 :                                                 S->param_formats[param->paramno] = 0;
     348             :                                         } else {
     349         486 :                                                 SEPARATE_ZVAL_IF_NOT_REF(&param->parameter);
     350         177 :                                                 convert_to_string(param->parameter);
     351         177 :                                                 S->param_values[param->paramno] = Z_STRVAL_P(param->parameter);
     352         177 :                                                 S->param_lengths[param->paramno] = Z_STRLEN_P(param->parameter);
     353         177 :                                                 S->param_formats[param->paramno] = 0;
     354             :                                         }
     355             : 
     356         186 :                                         if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
     357           9 :                                                 S->param_types[param->paramno] = 0;
     358           9 :                                                 S->param_formats[param->paramno] = 1;
     359             :                                         } else {
     360         177 :                                                 S->param_types[param->paramno] = 0;
     361             :                                         }
     362             :                                 }
     363             :                                 break;
     364             :                 }
     365             :         } else {
     366             : #endif
     367         368 :         if (param->is_param) {
     368             :         /* We need to manually convert to a pg native boolean value */
     369          98 :         if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_BOOL &&
     370           5 :             ((param->param_type & PDO_PARAM_INPUT_OUTPUT) != PDO_PARAM_INPUT_OUTPUT)) {
     371          20 :             SEPARATE_ZVAL(&param->parameter);
     372           5 :             param->param_type = PDO_PARAM_STR;
     373           5 :             ZVAL_STRINGL(param->parameter, Z_BVAL_P(param->parameter) ? "t" : "f", 1, 1);
     374             :         }
     375             :     }
     376             : #if HAVE_PQPREPARE
     377             :         }
     378             : #endif
     379         868 :         return 1;
     380             : }
     381             : 
     382         399 : static int pgsql_stmt_fetch(pdo_stmt_t *stmt,
     383             :         enum pdo_fetch_orientation ori, long offset TSRMLS_DC)
     384             : {
     385         399 :         pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
     386             : 
     387         399 :         if (S->cursor_name) {
     388          15 :                 char *ori_str = NULL;
     389          15 :                 char *q = NULL;
     390             :                 ExecStatusType status;
     391             : 
     392          15 :                 switch (ori) {
     393          10 :                         case PDO_FETCH_ORI_NEXT:        spprintf(&ori_str, 0, "NEXT"); break;
     394           1 :                         case PDO_FETCH_ORI_PRIOR:       spprintf(&ori_str, 0, "BACKWARD"); break;
     395           1 :                         case PDO_FETCH_ORI_FIRST:       spprintf(&ori_str, 0, "FIRST"); break;
     396           1 :                         case PDO_FETCH_ORI_LAST:        spprintf(&ori_str, 0, "LAST"); break;
     397           1 :                         case PDO_FETCH_ORI_ABS:         spprintf(&ori_str, 0, "ABSOLUTE %ld", offset); break;
     398           1 :                         case PDO_FETCH_ORI_REL:         spprintf(&ori_str, 0, "RELATIVE %ld", offset); break;
     399             :                         default:
     400           0 :                                 return 0;
     401             :                 }
     402             :                 
     403          15 :                 spprintf(&q, 0, "FETCH %s FROM %s", ori_str, S->cursor_name);
     404          15 :                 efree(ori_str);
     405          15 :                 S->result = PQexec(S->H->server, q);
     406          15 :                 efree(q);
     407          15 :                 status = PQresultStatus(S->result);
     408             : 
     409          15 :                 if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
     410           0 :                         pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
     411           0 :                         return 0;
     412             :                 }
     413             : 
     414          15 :                 if (PQntuples(S->result)) {
     415          12 :                         S->current_row = 1;
     416          12 :                         return 1;
     417             :                 } else {
     418           3 :                         return 0;
     419             :                 }
     420             :         } else {
     421         384 :                 if (S->current_row < stmt->row_count) {
     422         292 :                         S->current_row++;
     423         292 :                         return 1;
     424             :                 } else {
     425          92 :                         return 0;
     426             :                 }
     427             :         }
     428             : }
     429             : 
     430         196 : static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC)
     431             : {
     432         196 :         pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
     433         196 :         struct pdo_column_data *cols = stmt->columns;
     434             :         struct pdo_bound_param_data *param;
     435             :         
     436         196 :         if (!S->result) {
     437           0 :                 return 0;
     438             :         }
     439             : 
     440         196 :         cols[colno].name = estrdup(PQfname(S->result, colno));
     441         196 :         cols[colno].namelen = strlen(cols[colno].name);
     442         196 :         cols[colno].maxlen = PQfsize(S->result, colno);
     443         196 :         cols[colno].precision = PQfmod(S->result, colno);
     444         196 :         S->cols[colno].pgsql_type = PQftype(S->result, colno);
     445             :         
     446         196 :         switch(S->cols[colno].pgsql_type) {
     447             : 
     448             :                 case BOOLOID:
     449           1 :                         cols[colno].param_type = PDO_PARAM_BOOL;
     450           1 :                         break;
     451             :         
     452             :                 case OIDOID:
     453             :                         /* did the user bind the column as a LOB ? */
     454           5 :                         if (stmt->bound_columns && (
     455           1 :                                         SUCCESS == zend_hash_index_find(stmt->bound_columns,
     456           2 :                                                 colno, (void**)&param) ||
     457           3 :                                         SUCCESS == zend_hash_find(stmt->bound_columns,
     458           2 :                                                 cols[colno].name, cols[colno].namelen,
     459             :                                                 (void**)&param))) {
     460           1 :                                 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
     461           1 :                                         cols[colno].param_type = PDO_PARAM_LOB;
     462           1 :                                         break;
     463             :                                 }
     464             :                         }
     465           2 :                         cols[colno].param_type = PDO_PARAM_INT;
     466           2 :                         break;
     467             : 
     468             :                 case INT2OID:
     469             :                 case INT4OID:
     470          63 :                         cols[colno].param_type = PDO_PARAM_INT;
     471          63 :                         break;
     472             : 
     473             :                 case INT8OID:
     474             :                         if (sizeof(long)>=8) {
     475          12 :                                 cols[colno].param_type = PDO_PARAM_INT;
     476             :                         } else {
     477             :                                 cols[colno].param_type = PDO_PARAM_STR;
     478             :                         }
     479          12 :                         break;
     480             : 
     481             :                 case BYTEAOID:
     482           2 :                         cols[colno].param_type = PDO_PARAM_LOB;
     483           2 :                         break;
     484             : 
     485             :                 default:
     486         115 :                         cols[colno].param_type = PDO_PARAM_STR;
     487             :         }
     488             : 
     489         196 :         return 1;
     490             : }
     491             : 
     492         604 : static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, unsigned long *len, int *caller_frees  TSRMLS_DC)
     493             : {
     494         604 :         pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
     495         604 :         struct pdo_column_data *cols = stmt->columns;
     496             :         size_t tmp_len;
     497             : 
     498         604 :         if (!S->result) {
     499           0 :                 return 0;
     500             :         }
     501             : 
     502             :         /* We have already increased count by 1 in pgsql_stmt_fetch() */
     503         604 :         if (PQgetisnull(S->result, S->current_row - 1, colno)) { /* Check if we got NULL */
     504          33 :                 *ptr = NULL;
     505          33 :                 *len = 0;
     506             :         } else {
     507         571 :                 *ptr = PQgetvalue(S->result, S->current_row - 1, colno);
     508         571 :                 *len = PQgetlength(S->result, S->current_row - 1, colno);
     509             :                 
     510         571 :                 switch(cols[colno].param_type) {
     511             : 
     512             :                         case PDO_PARAM_INT:
     513         221 :                                 S->cols[colno].intval = atol(*ptr);
     514         221 :                                 *ptr = (char *) &(S->cols[colno].intval);
     515         221 :                                 *len = sizeof(long);
     516         221 :                                 break;
     517             : 
     518             :                         case PDO_PARAM_BOOL:
     519           0 :                                 S->cols[colno].boolval = **ptr == 't' ? 1: 0;
     520           0 :                                 *ptr = (char *) &(S->cols[colno].boolval);
     521           0 :                                 *len = sizeof(zend_bool);
     522           0 :                                 break;
     523             :                                 
     524             :                         case PDO_PARAM_LOB:
     525           8 :                                 if (S->cols[colno].pgsql_type == OIDOID) {
     526             :                                         /* ooo, a real large object */
     527             :                                         char *end_ptr;
     528           2 :                                         Oid oid = (Oid)strtoul(*ptr, &end_ptr, 10);
     529           2 :                                         int loid = lo_open(S->H->server, oid, INV_READ);
     530           2 :                                         if (loid >= 0) {
     531           2 :                                                 *ptr = (char*)pdo_pgsql_create_lob_stream(stmt->dbh, loid, oid TSRMLS_CC);
     532           2 :                                                 *len = 0;
     533           2 :                                                 return *ptr ? 1 : 0;
     534             :                                         }
     535           0 :                                         *ptr = NULL;
     536           0 :                                         *len = 0;
     537           0 :                                         return 0;
     538             :                                 } else {
     539           6 :                                         char *tmp_ptr = PQunescapeBytea(*ptr, &tmp_len);
     540           6 :                                         if (!tmp_ptr) {
     541             :                                                 /* PQunescapeBytea returned an error */
     542           0 :                                                 *len = 0;
     543           0 :                                                 return 0;
     544             :                                         }
     545           6 :                                         if (!tmp_len) {
     546             :                                                 /* Empty string, return as empty stream */
     547           4 :                                                 *ptr = (char *)php_stream_memory_open(TEMP_STREAM_READONLY, "", 0);
     548           4 :                                                 PQfreemem(tmp_ptr);
     549           4 :                                                 *len = 0;
     550             :                                         } else {
     551           2 :                                                 *ptr = estrndup(tmp_ptr, tmp_len);
     552           2 :                                                 PQfreemem(tmp_ptr);
     553           2 :                                                 *len = tmp_len;
     554           2 :                                                 *caller_frees = 1;
     555             :                                         }
     556             :                                 }
     557             :                                 break;
     558             :                         case PDO_PARAM_NULL:
     559             :                         case PDO_PARAM_STR:
     560             :                         case PDO_PARAM_STMT:
     561             :                         case PDO_PARAM_INPUT_OUTPUT:
     562             :                         case PDO_PARAM_ZVAL:
     563             :                         default:
     564             :                                 break;
     565             :                 }
     566             :         }
     567             : 
     568         602 :         return 1;
     569             : }
     570             : 
     571           0 : static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, long colno, zval *return_value TSRMLS_DC)
     572             : {
     573           0 :         pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
     574             :         PGresult *res;
     575           0 :         char *q=NULL;
     576             :         ExecStatusType status;
     577             :         
     578           0 :         if (!S->result) {
     579           0 :                 return FAILURE;
     580             :         }
     581             :         
     582           0 :         if (colno >= stmt->column_count) {
     583           0 :                 return FAILURE;
     584             :         }
     585             :         
     586           0 :         array_init(return_value);
     587           0 :         add_assoc_long(return_value, "pgsql:oid", S->cols[colno].pgsql_type);
     588             : 
     589             :         /* Fetch metadata from Postgres system catalogue */
     590           0 :         spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%d", S->cols[colno].pgsql_type);
     591           0 :         res = PQexec(S->H->server, q);
     592           0 :         efree(q);
     593             :         
     594           0 :         status = PQresultStatus(res);
     595             :         
     596           0 :         if (status != PGRES_TUPLES_OK) {
     597             :                 /* Failed to get system catalogue, but return success
     598             :                  * with the data we have collected so far
     599             :                  */
     600           0 :                 goto done;
     601             :         }
     602             : 
     603             :         /* We want exactly one row returned */
     604           0 :         if (1 != PQntuples(res)) {
     605           0 :                 goto done;
     606             :         }
     607             : 
     608           0 :         add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0), 1);
     609             : done:
     610           0 :         PQclear(res);           
     611           0 :         return 1;
     612             : }
     613             : 
     614          27 : static int pdo_pgsql_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC)
     615             : {
     616          27 :         return 1;
     617             : }
     618             : 
     619             : struct pdo_stmt_methods pgsql_stmt_methods = {
     620             :         pgsql_stmt_dtor,
     621             :         pgsql_stmt_execute,
     622             :         pgsql_stmt_fetch,
     623             :         pgsql_stmt_describe,
     624             :         pgsql_stmt_get_col,
     625             :         pgsql_stmt_param_hook,
     626             :         NULL, /* set_attr */
     627             :         NULL, /* get_attr */
     628             :         pgsql_stmt_get_column_meta,
     629             :         NULL,  /* next_rowset */
     630             :         pdo_pgsql_stmt_cursor_closer
     631             : };
     632             : 
     633             : /*
     634             :  * Local variables:
     635             :  * tab-width: 4
     636             :  * c-basic-offset: 4
     637             :  * End:
     638             :  * vim600: noet sw=4 ts=4 fdm=marker
     639             :  * vim<600: noet sw=4 ts=4
     640             :  */

Generated by: LCOV version 1.10

Generated at Wed, 23 Jul 2014 19:58:36 +0000 (18 hours ago)

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