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: 265 312 84.9 %
Date: 2014-10-30 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10

Generated at Thu, 30 Oct 2014 07:41:36 +0000 (20 hours ago)

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