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: 322 355 90.7 %
Date: 2016-08-24 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   +----------------------------------------------------------------------+
       3             :   | PHP Version 7                                                        |
       4             :   +----------------------------------------------------------------------+
       5             :   | Copyright (c) 1997-2016 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 BOOLLABEL   "bool"
      40             : #define BOOLOID     16
      41             : #define BYTEALABEL  "bytea"
      42             : #define BYTEAOID    17
      43             : #define DATELABEL   "date"
      44             : #define DATEOID     1082
      45             : #define INT2LABEL   "int2"
      46             : #define INT2OID     21
      47             : #define INT4LABEL   "int4"
      48             : #define INT4OID     23
      49             : #define INT8LABEL   "int8"
      50             : #define INT8OID     20
      51             : #define OIDOID      26
      52             : #define TEXTLABEL   "text"
      53             : #define TEXTOID     25
      54             : #define TIMESTAMPLABEL "timestamp"
      55             : #define TIMESTAMPOID   1114
      56             : #define VARCHARLABEL "varchar"
      57             : #define VARCHAROID   1043
      58             : 
      59             : 
      60             : 
      61         498 : static int pgsql_stmt_dtor(pdo_stmt_t *stmt)
      62             : {
      63         498 :         pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
      64         993 :         zend_bool server_obj_usable = IS_OBJ_VALID(EG(objects_store).object_buckets[Z_OBJ_HANDLE(stmt->database_object_handle)])
      65         993 :                 && !(GC_FLAGS(Z_OBJ(stmt->database_object_handle)) & IS_OBJ_FREE_CALLED);
      66             : 
      67         498 :         if (S->result) {
      68             :                 /* free the resource */
      69         484 :                 PQclear(S->result);
      70         484 :                 S->result = NULL;
      71             :         }
      72             : 
      73         498 :         if (S->stmt_name) {
      74         482 :                 if (S->is_prepared && server_obj_usable) {
      75         462 :                         pdo_pgsql_db_handle *H = S->H;
      76         462 :                         char *q = NULL;
      77             :                         PGresult *res;
      78             : 
      79         462 :                         spprintf(&q, 0, "DEALLOCATE %s", S->stmt_name);
      80         462 :                         res = PQexec(H->server, q);
      81         462 :                         efree(q);
      82         462 :                         if (res) {
      83         462 :                                 PQclear(res);
      84             :                         }
      85             :                 }
      86         482 :                 efree(S->stmt_name);
      87         482 :                 S->stmt_name = NULL;
      88             :         }
      89         498 :         if (S->param_lengths) {
      90          46 :                 efree(S->param_lengths);
      91          46 :                 S->param_lengths = NULL;
      92             :         }
      93         498 :         if (S->param_values) {
      94          46 :                 efree(S->param_values);
      95          46 :                 S->param_values = NULL;
      96             :         }
      97         498 :         if (S->param_formats) {
      98          46 :                 efree(S->param_formats);
      99          46 :                 S->param_formats = NULL;
     100             :         }
     101         498 :         if (S->param_types) {
     102          46 :                 efree(S->param_types);
     103          46 :                 S->param_types = NULL;
     104             :         }
     105         498 :         if (S->query) {
     106         483 :                 efree(S->query);
     107         483 :                 S->query = NULL;
     108             :         }
     109             : 
     110         498 :         if (S->cursor_name) {
     111           3 :                 if (server_obj_usable) {
     112           3 :                         pdo_pgsql_db_handle *H = S->H;
     113           3 :                         char *q = NULL;
     114             :                         PGresult *res;
     115             : 
     116           3 :                         spprintf(&q, 0, "CLOSE %s", S->cursor_name);
     117           3 :                         res = PQexec(H->server, q);
     118           3 :                         efree(q);
     119           3 :                         if (res) PQclear(res);
     120             :                 }
     121           3 :                 efree(S->cursor_name);
     122           3 :                 S->cursor_name = NULL;
     123             :         }
     124             : 
     125         498 :         if(S->cols) {
     126         458 :                 efree(S->cols);
     127         458 :                 S->cols = NULL;
     128             :         }
     129         498 :         efree(S);
     130         498 :         stmt->driver_data = NULL;
     131         498 :         return 1;
     132             : }
     133             : 
     134        1582 : static int pgsql_stmt_execute(pdo_stmt_t *stmt)
     135             : {
     136        1582 :         pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
     137        1582 :         pdo_pgsql_db_handle *H = S->H;
     138             :         ExecStatusType status;
     139             : 
     140             :         /* ensure that we free any previous unfetched results */
     141        1582 :         if(S->result) {
     142        1098 :                 PQclear(S->result);
     143        1098 :                 S->result = NULL;
     144             :         }
     145             : 
     146        1582 :         S->current_row = 0;
     147             : 
     148        1582 :         if (S->cursor_name) {
     149           4 :                 char *q = NULL;
     150             : 
     151           4 :                 if (S->is_prepared) {
     152           1 :                         spprintf(&q, 0, "CLOSE %s", S->cursor_name);
     153           1 :                         S->result = PQexec(H->server, q);
     154           1 :                         efree(q);
     155             :                 }
     156             : 
     157           4 :                 spprintf(&q, 0, "DECLARE %s SCROLL CURSOR WITH HOLD FOR %s", S->cursor_name, stmt->active_query_string);
     158           4 :                 S->result = PQexec(H->server, q);
     159           4 :                 efree(q);
     160             : 
     161             :                 /* check if declare failed */
     162           4 :                 status = PQresultStatus(S->result);
     163           4 :                 if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
     164           1 :                         pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
     165           1 :                         return 0;
     166             :                 }
     167             : 
     168             :                 /* the cursor was declared correctly */
     169           3 :                 S->is_prepared = 1;
     170             : 
     171             :                 /* fetch to be able to get the number of tuples later, but don't advance the cursor pointer */
     172           3 :                 spprintf(&q, 0, "FETCH FORWARD 0 FROM %s", S->cursor_name);
     173           3 :                 S->result = PQexec(H->server, q);
     174           3 :                 efree(q);
     175        1578 :         } else if (S->stmt_name) {
     176             :                 /* using a prepared statement */
     177             : 
     178        1558 :                 if (!S->is_prepared) {
     179             : stmt_retry:
     180             :                         /* we deferred the prepare until now, because we didn't
     181             :                          * know anything about the parameter types; now we do */
     182         518 :                         S->result = PQprepare(H->server, S->stmt_name, S->query,
     183         518 :                                                 stmt->bound_params ? zend_hash_num_elements(stmt->bound_params) : 0,
     184         471 :                                                 S->param_types);
     185         471 :                         status = PQresultStatus(S->result);
     186         471 :                         switch (status) {
     187             :                                 case PGRES_COMMAND_OK:
     188             :                                 case PGRES_TUPLES_OK:
     189             :                                         /* it worked */
     190         464 :                                         S->is_prepared = 1;
     191         464 :                                         PQclear(S->result);
     192         464 :                                         break;
     193             :                                 default: {
     194           7 :                                         char *sqlstate = pdo_pgsql_sqlstate(S->result);
     195             :                                         /* 42P05 means that the prepared statement already existed. this can happen if you use
     196             :                                          * a connection pooling software line pgpool which doesn't close the db-connection once
     197             :                                          * php disconnects. if php dies (no chance to run RSHUTDOWN) during execution it has no
     198             :                                          * chance to DEALLOCATE the prepared statements it has created. so, if we hit a 42P05 we
     199             :                                          * deallocate it and retry ONCE (thies 2005.12.15)
     200             :                                          */
     201           7 :                                         if (sqlstate && !strcmp(sqlstate, "42P05")) {
     202             :                                                 char buf[100]; /* stmt_name == "pdo_crsr_%08x" */
     203             :                                                 PGresult *res;
     204           0 :                                                 snprintf(buf, sizeof(buf), "DEALLOCATE %s", S->stmt_name);
     205           0 :                                                 res = PQexec(H->server, buf);
     206           0 :                                                 if (res) {
     207           0 :                                                         PQclear(res);
     208             :                                                 }
     209           0 :                                                 goto stmt_retry;
     210             :                                         } else {
     211           7 :                                                 pdo_pgsql_error_stmt(stmt, status, sqlstate);
     212           7 :                                                 return 0;
     213             :                                         }
     214             :                                 }
     215             :                         }
     216             :                 }
     217        4206 :                 S->result = PQexecPrepared(H->server, S->stmt_name,
     218        1551 :                                 stmt->bound_params ?
     219        1104 :                                         zend_hash_num_elements(stmt->bound_params) :
     220             :                                         0,
     221             :                                 (const char**)S->param_values,
     222        1551 :                                 S->param_lengths,
     223        1551 :                                 S->param_formats,
     224             :                                 0);
     225          20 :         } else if (stmt->supports_placeholders == PDO_PLACEHOLDER_NAMED) {
     226             :                 /* execute query with parameters */
     227           6 :                 S->result = PQexecParams(H->server, S->query,
     228           4 :                                 stmt->bound_params ? zend_hash_num_elements(stmt->bound_params) : 0,
     229           2 :                                 S->param_types,
     230             :                                 (const char**)S->param_values,
     231           2 :                                 S->param_lengths,
     232           2 :                                 S->param_formats,
     233             :                                 0);
     234             :         } else {
     235             :                 /* execute plain query (with embedded parameters) */
     236          18 :                 S->result = PQexec(H->server, stmt->active_query_string);
     237             :         }
     238        1574 :         status = PQresultStatus(S->result);
     239             : 
     240        1574 :         if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
     241          12 :                 pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
     242          12 :                 return 0;
     243             :         }
     244             : 
     245        1562 :         if (!stmt->executed && (!stmt->column_count || S->cols == NULL)) {
     246        1485 :                 stmt->column_count = (int) PQnfields(S->result);
     247        1485 :                 S->cols = ecalloc(stmt->column_count, sizeof(pdo_pgsql_column));
     248             :         }
     249             : 
     250        1562 :         if (status == PGRES_COMMAND_OK) {
     251        1081 :                 ZEND_ATOL(stmt->row_count, PQcmdTuples(S->result));
     252        1081 :                 H->pgoid = PQoidValue(S->result);
     253             :         } else {
     254         481 :                 stmt->row_count = (zend_long)PQntuples(S->result);
     255             :         }
     256             : 
     257        1562 :         return 1;
     258             : }
     259             : 
     260       23485 : static int pgsql_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param,
     261             :                 enum pdo_param_event event_type)
     262             : {
     263       23485 :         pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
     264             : 
     265       36735 :         if (stmt->supports_placeholders == PDO_PLACEHOLDER_NAMED && param->is_param) {
     266       22088 :                 switch (event_type) {
     267             :                         case PDO_PARAM_EVT_FREE:
     268        4523 :                                 if (param->driver_data) {
     269           1 :                                         efree(param->driver_data);
     270             :                                 }
     271        4523 :                                 break;
     272             : 
     273             :                         case PDO_PARAM_EVT_NORMALIZE:
     274             :                                 /* decode name from $1, $2 into 0, 1 etc. */
     275        4525 :                                 if (param->name) {
     276        4067 :                                         if (ZSTR_VAL(param->name)[0] == '$') {
     277           0 :                                                 ZEND_ATOL(param->paramno, ZSTR_VAL(param->name) + 1);
     278             :                                         } else {
     279             :                                                 /* resolve parameter name to rewritten name */
     280             :                                                 char *namevar;
     281             : 
     282       12198 :                                                 if (stmt->bound_param_map && (namevar = zend_hash_find_ptr(stmt->bound_param_map,
     283             :                                                                 param->name)) != NULL) {
     284        4065 :                                                         ZEND_ATOL(param->paramno, namevar + 1);
     285        4065 :                                                         param->paramno--;
     286             :                                                 } else {
     287           2 :                                                         pdo_raise_impl_error(stmt->dbh, stmt, "HY093", ZSTR_VAL(param->name));
     288           2 :                                                         return 0;
     289             :                                                 }
     290             :                                         }
     291             :                                 }
     292        4523 :                                 break;
     293             : 
     294             :                         case PDO_PARAM_EVT_ALLOC:
     295        4523 :                                 if (!stmt->bound_param_map) {
     296           2 :                                         return 1;
     297             :                                 }
     298        4521 :                                 if (!zend_hash_index_exists(stmt->bound_param_map, param->paramno)) {
     299           4 :                                         pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");
     300           4 :                                         return 0;
     301             :                                 }
     302             :                         case PDO_PARAM_EVT_EXEC_POST:
     303             :                         case PDO_PARAM_EVT_FETCH_PRE:
     304             :                         case PDO_PARAM_EVT_FETCH_POST:
     305             :                                 /* work is handled by EVT_NORMALIZE */
     306        8827 :                                 return 1;
     307             : 
     308             :                         case PDO_PARAM_EVT_EXEC_PRE:
     309        4207 :                                 if (!stmt->bound_param_map) {
     310           2 :                                         return 1;
     311             :                                 }
     312        4205 :                                 if (!S->param_values) {
     313          46 :                                         S->param_values = ecalloc(
     314             :                                                         zend_hash_num_elements(stmt->bound_param_map),
     315             :                                                         sizeof(char*));
     316          46 :                                         S->param_lengths = ecalloc(
     317             :                                                         zend_hash_num_elements(stmt->bound_param_map),
     318             :                                                         sizeof(int));
     319          46 :                                         S->param_formats = ecalloc(
     320             :                                                         zend_hash_num_elements(stmt->bound_param_map),
     321             :                                                         sizeof(int));
     322          46 :                                         S->param_types = ecalloc(
     323             :                                                         zend_hash_num_elements(stmt->bound_param_map),
     324             :                                                         sizeof(Oid));
     325             :                                 }
     326        4205 :                                 if (param->paramno >= 0) {
     327             :                                         zval *parameter;
     328             : 
     329             :                                         /*
     330             :                                         if (param->paramno >= zend_hash_num_elements(stmt->bound_params)) {
     331             :                                                 pdo_raise_impl_error(stmt->dbh, stmt, "HY093", "parameter was not defined");
     332             :                                                 return 0;
     333             :                                         }
     334             :                                         */
     335             : 
     336        8410 :                                         if (Z_ISREF(param->parameter)) {
     337          50 :                                                 parameter = Z_REFVAL(param->parameter);
     338             :                                         } else {
     339        4155 :                                                 parameter = &param->parameter;
     340             :                                         }
     341             : 
     342        4215 :                                         if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB &&
     343             :                                                         Z_TYPE_P(parameter) == IS_RESOURCE) {
     344             :                                                 php_stream *stm;
     345           6 :                                                 php_stream_from_zval_no_verify(stm, parameter);
     346           6 :                                                 if (stm) {
     347           6 :                                                         if (php_stream_is(stm, &pdo_pgsql_lob_stream_ops)) {
     348           1 :                                                                 struct pdo_pgsql_lob_self *self = (struct pdo_pgsql_lob_self*)stm->abstract;
     349           1 :                                                                 pdo_pgsql_bound_param *P = param->driver_data;
     350             : 
     351           1 :                                                                 if (P == NULL) {
     352           1 :                                                                         P = ecalloc(1, sizeof(*P));
     353           1 :                                                                         param->driver_data = P;
     354             :                                                                 }
     355           1 :                                                                 P->oid = htonl(self->oid);
     356           1 :                                                                 S->param_values[param->paramno] = (char*)&P->oid;
     357           1 :                                                                 S->param_lengths[param->paramno] = sizeof(P->oid);
     358           1 :                                                                 S->param_formats[param->paramno] = 1;
     359           1 :                                                                 S->param_types[param->paramno] = OIDOID;
     360           1 :                                                                 return 1;
     361             :                                                         } else {
     362           5 :                                                                 zend_string *str = php_stream_copy_to_mem(stm, PHP_STREAM_COPY_ALL, 0);
     363           5 :                                                                 if (str != NULL) {
     364             :                                                                         //??SEPARATE_ZVAL_IF_NOT_REF(&param->parameter);
     365           3 :                                                                         ZVAL_STR(parameter, str);
     366             :                                                                 } else {
     367           2 :                                                                         ZVAL_EMPTY_STRING(parameter);
     368             :                                                                 }
     369             :                                                         }
     370             :                                                 } else {
     371             :                                                         /* expected a stream resource */
     372           0 :                                                         pdo_pgsql_error_stmt(stmt, PGRES_FATAL_ERROR, "HY105");
     373           0 :                                                         return 0;
     374             :                                                 }
     375             :                                         }
     376             : 
     377        8415 :                                         if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_NULL ||
     378             :                                                         Z_TYPE_P(parameter) == IS_NULL) {
     379           7 :                                                 S->param_values[param->paramno] = NULL;
     380           7 :                                                 S->param_lengths[param->paramno] = 0;
     381        8395 :                                         } else if (Z_TYPE_P(parameter) == IS_FALSE || Z_TYPE_P(parameter) == IS_TRUE) {
     382           4 :                                                 S->param_values[param->paramno] = Z_TYPE_P(parameter) == IS_TRUE ? "t" : "f";
     383           2 :                                                 S->param_lengths[param->paramno] = 1;
     384           2 :                                                 S->param_formats[param->paramno] = 0;
     385             :                                         } else {
     386             :                                                 //SEPARATE_ZVAL_IF_NOT_REF(&param->parameter);
     387        4217 :                                                 convert_to_string_ex(parameter);
     388        4195 :                                                 S->param_values[param->paramno] = Z_STRVAL_P(parameter);
     389        4195 :                                                 S->param_lengths[param->paramno] = Z_STRLEN_P(parameter);
     390        4195 :                                                 S->param_formats[param->paramno] = 0;
     391             :                                         }
     392             : 
     393        4204 :                                         if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
     394           9 :                                                 S->param_types[param->paramno] = 0;
     395           9 :                                                 S->param_formats[param->paramno] = 1;
     396             :                                         } else {
     397        4195 :                                                 S->param_types[param->paramno] = 0;
     398             :                                         }
     399             :                                 }
     400             :                                 break;
     401             :                 }
     402        1397 :         } else if (param->is_param) {
     403             :                 /* We need to manually convert to a pg native boolean value */
     404        1129 :                 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_BOOL &&
     405           7 :                         ((param->param_type & PDO_PARAM_INPUT_OUTPUT) != PDO_PARAM_INPUT_OUTPUT)) {
     406           7 :                         const char *s = zend_is_true(&param->parameter) ? "t" : "f";
     407           7 :                         param->param_type = PDO_PARAM_STR;
     408           7 :                         zval_ptr_dtor(&param->parameter);
     409          14 :                         ZVAL_STRINGL(&param->parameter, s, 1);
     410             :                 }
     411             :         }
     412       14647 :         return 1;
     413             : }
     414             : 
     415         428 : static int pgsql_stmt_fetch(pdo_stmt_t *stmt,
     416             :         enum pdo_fetch_orientation ori, zend_long offset)
     417             : {
     418         428 :         pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
     419             : 
     420         428 :         if (S->cursor_name) {
     421          15 :                 char *ori_str = NULL;
     422          15 :                 char *q = NULL;
     423             :                 ExecStatusType status;
     424             : 
     425          15 :                 switch (ori) {
     426          10 :                         case PDO_FETCH_ORI_NEXT:        spprintf(&ori_str, 0, "NEXT"); break;
     427           1 :                         case PDO_FETCH_ORI_PRIOR:       spprintf(&ori_str, 0, "BACKWARD"); break;
     428           1 :                         case PDO_FETCH_ORI_FIRST:       spprintf(&ori_str, 0, "FIRST"); break;
     429           1 :                         case PDO_FETCH_ORI_LAST:        spprintf(&ori_str, 0, "LAST"); break;
     430           1 :                         case PDO_FETCH_ORI_ABS:         spprintf(&ori_str, 0, "ABSOLUTE " ZEND_LONG_FMT, offset); break;
     431           1 :                         case PDO_FETCH_ORI_REL:         spprintf(&ori_str, 0, "RELATIVE " ZEND_LONG_FMT, offset); break;
     432             :                         default:
     433           0 :                                 return 0;
     434             :                 }
     435             : 
     436          15 :                 spprintf(&q, 0, "FETCH %s FROM %s", ori_str, S->cursor_name);
     437          15 :                 efree(ori_str);
     438          15 :                 S->result = PQexec(S->H->server, q);
     439          15 :                 efree(q);
     440          15 :                 status = PQresultStatus(S->result);
     441             : 
     442          15 :                 if (status != PGRES_COMMAND_OK && status != PGRES_TUPLES_OK) {
     443           0 :                         pdo_pgsql_error_stmt(stmt, status, pdo_pgsql_sqlstate(S->result));
     444           0 :                         return 0;
     445             :                 }
     446             : 
     447          15 :                 if (PQntuples(S->result)) {
     448          12 :                         S->current_row = 1;
     449          12 :                         return 1;
     450             :                 } else {
     451           3 :                         return 0;
     452             :                 }
     453             :         } else {
     454         413 :                 if (S->current_row < stmt->row_count) {
     455         315 :                         S->current_row++;
     456         315 :                         return 1;
     457             :                 } else {
     458          98 :                         return 0;
     459             :                 }
     460             :         }
     461             : }
     462             : 
     463         516 : static int pgsql_stmt_describe(pdo_stmt_t *stmt, int colno)
     464             : {
     465         516 :         pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
     466         516 :         struct pdo_column_data *cols = stmt->columns;
     467             :         struct pdo_bound_param_data *param;
     468             :         char *str;
     469             : 
     470         516 :         if (!S->result) {
     471           0 :                 return 0;
     472             :         }
     473             : 
     474         516 :         str = PQfname(S->result, colno);
     475        1032 :         cols[colno].name = zend_string_init(str, strlen(str), 0);
     476         516 :         cols[colno].maxlen = PQfsize(S->result, colno);
     477         516 :         cols[colno].precision = PQfmod(S->result, colno);
     478         516 :         S->cols[colno].pgsql_type = PQftype(S->result, colno);
     479             : 
     480         516 :         switch (S->cols[colno].pgsql_type) {
     481             : 
     482             :                 case BOOLOID:
     483           2 :                         cols[colno].param_type = PDO_PARAM_BOOL;
     484           2 :                         break;
     485             : 
     486             :                 case OIDOID:
     487             :                         /* did the user bind the column as a LOB ? */
     488           5 :                         if (stmt->bound_columns && (
     489           1 :                                         (param = zend_hash_index_find_ptr(stmt->bound_columns, colno)) != NULL ||
     490           1 :                                         (param = zend_hash_find_ptr(stmt->bound_columns, cols[colno].name)) != NULL)) {
     491             : 
     492           1 :                                 if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_LOB) {
     493           1 :                                         cols[colno].param_type = PDO_PARAM_LOB;
     494           1 :                                         break;
     495             :                                 }
     496             :                         }
     497           2 :                         cols[colno].param_type = PDO_PARAM_INT;
     498           2 :                         break;
     499             : 
     500             :                 case INT2OID:
     501             :                 case INT4OID:
     502         375 :                         cols[colno].param_type = PDO_PARAM_INT;
     503         375 :                         break;
     504             : 
     505             :                 case INT8OID:
     506             :                         if (sizeof(zend_long)>=8) {
     507          13 :                                 cols[colno].param_type = PDO_PARAM_INT;
     508             :                         } else {
     509             :                                 cols[colno].param_type = PDO_PARAM_STR;
     510             :                         }
     511          13 :                         break;
     512             : 
     513             :                 case BYTEAOID:
     514           3 :                         cols[colno].param_type = PDO_PARAM_LOB;
     515           3 :                         break;
     516             : 
     517             :                 default:
     518         120 :                         cols[colno].param_type = PDO_PARAM_STR;
     519             :         }
     520             : 
     521         516 :         return 1;
     522             : }
     523             : 
     524         629 : static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, zend_ulong *len, int *caller_frees )
     525             : {
     526         629 :         pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
     527         629 :         struct pdo_column_data *cols = stmt->columns;
     528             :         size_t tmp_len;
     529             : 
     530         629 :         if (!S->result) {
     531           0 :                 return 0;
     532             :         }
     533             : 
     534             :         /* We have already increased count by 1 in pgsql_stmt_fetch() */
     535         629 :         if (PQgetisnull(S->result, S->current_row - 1, colno)) { /* Check if we got NULL */
     536          33 :                 *ptr = NULL;
     537          33 :                 *len = 0;
     538             :         } else {
     539         596 :                 *ptr = PQgetvalue(S->result, S->current_row - 1, colno);
     540         596 :                 *len = PQgetlength(S->result, S->current_row - 1, colno);
     541             : 
     542         596 :                 switch (cols[colno].param_type) {
     543             : 
     544             :                         case PDO_PARAM_INT:
     545         232 :                                 ZEND_ATOL(S->cols[colno].intval, *ptr);
     546         232 :                                 *ptr = (char *) &(S->cols[colno].intval);
     547         232 :                                 *len = sizeof(zend_long);
     548         232 :                                 break;
     549             : 
     550             :                         case PDO_PARAM_BOOL:
     551           2 :                                 S->cols[colno].boolval = **ptr == 't' ? 1: 0;
     552           2 :                                 *ptr = (char *) &(S->cols[colno].boolval);
     553           2 :                                 *len = sizeof(zend_bool);
     554           2 :                                 break;
     555             : 
     556             :                         case PDO_PARAM_LOB:
     557           8 :                                 if (S->cols[colno].pgsql_type == OIDOID) {
     558             :                                         /* ooo, a real large object */
     559             :                                         char *end_ptr;
     560           2 :                                         Oid oid = (Oid)strtoul(*ptr, &end_ptr, 10);
     561           2 :                                         int loid = lo_open(S->H->server, oid, INV_READ);
     562           2 :                                         if (loid >= 0) {
     563           2 :                                                 *ptr = (char*)pdo_pgsql_create_lob_stream(&stmt->database_object_handle, loid, oid);
     564           2 :                                                 *len = 0;
     565           2 :                                                 return *ptr ? 1 : 0;
     566             :                                         }
     567           0 :                                         *ptr = NULL;
     568           0 :                                         *len = 0;
     569           0 :                                         return 0;
     570             :                                 } else {
     571           6 :                                         char *tmp_ptr = (char *)PQunescapeBytea((unsigned char *)*ptr, &tmp_len);
     572           6 :                                         if (!tmp_ptr) {
     573             :                                                 /* PQunescapeBytea returned an error */
     574           0 :                                                 *len = 0;
     575           0 :                                                 return 0;
     576             :                                         }
     577           6 :                                         if (!tmp_len) {
     578             :                                                 /* Empty string, return as empty stream */
     579           4 :                                                 *ptr = (char *)php_stream_memory_open(TEMP_STREAM_READONLY, "", 0);
     580           4 :                                                 PQfreemem(tmp_ptr);
     581           4 :                                                 *len = 0;
     582             :                                         } else {
     583           2 :                                                 *ptr = estrndup(tmp_ptr, tmp_len);
     584           2 :                                                 PQfreemem(tmp_ptr);
     585           2 :                                                 *len = tmp_len;
     586           2 :                                                 *caller_frees = 1;
     587             :                                         }
     588             :                                 }
     589             :                                 break;
     590             :                         case PDO_PARAM_NULL:
     591             :                         case PDO_PARAM_STR:
     592             :                         case PDO_PARAM_STMT:
     593             :                         case PDO_PARAM_INPUT_OUTPUT:
     594             :                         case PDO_PARAM_ZVAL:
     595             :                         default:
     596             :                                 break;
     597             :                 }
     598             :         }
     599             : 
     600         627 :         return 1;
     601             : }
     602             : 
     603             : static zend_always_inline char * pdo_pgsql_translate_oid_to_table(Oid oid, PGconn *conn)
     604             : {
     605           9 :         char *table_name = NULL;
     606             :         PGresult *tmp_res;
     607           9 :         char *querystr = NULL;
     608             : 
     609           9 :         spprintf(&querystr, 0, "SELECT RELNAME FROM PG_CLASS WHERE OID=%d", oid);
     610             : 
     611           9 :         if ((tmp_res = PQexec(conn, querystr)) == NULL || PQresultStatus(tmp_res) != PGRES_TUPLES_OK) {
     612           0 :                 if (tmp_res) {
     613           0 :                         PQclear(tmp_res);
     614             :                 }
     615           0 :                 efree(querystr);
     616           0 :                 return 0;
     617             :         }
     618           9 :         efree(querystr);
     619             : 
     620           9 :         if ((table_name = PQgetvalue(tmp_res, 0, 0)) == NULL) {
     621           0 :                 PQclear(tmp_res);
     622           0 :                 return 0;
     623             :         }
     624             : 
     625           9 :         PQclear(tmp_res);
     626           9 :         return table_name;
     627             : }
     628             : 
     629           9 : static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, zend_long colno, zval *return_value)
     630             : {
     631           9 :         pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
     632             :         PGresult *res;
     633           9 :         char *q=NULL;
     634             :         ExecStatusType status;
     635             :         Oid table_oid;
     636           9 :         char *table_name=NULL;
     637             : 
     638           9 :         if (!S->result) {
     639           0 :                 return FAILURE;
     640             :         }
     641             : 
     642           9 :         if (colno >= stmt->column_count) {
     643           0 :                 return FAILURE;
     644             :         }
     645             : 
     646           9 :         array_init(return_value);
     647           9 :         add_assoc_long(return_value, "pgsql:oid", S->cols[colno].pgsql_type);
     648             : 
     649           9 :         table_oid = PQftable(S->result, colno);
     650           9 :         add_assoc_long(return_value, "pgsql:table_oid", table_oid);
     651          18 :         table_name = pdo_pgsql_translate_oid_to_table(table_oid, S->H->server);
     652           9 :         if (table_name) {
     653           9 :                 add_assoc_string(return_value, "table", table_name);
     654             :         }
     655             : 
     656           9 :         switch (S->cols[colno].pgsql_type) {
     657             :                 case BOOLOID:
     658           1 :                         add_assoc_string(return_value, "native_type", BOOLLABEL);
     659           1 :                         break;
     660             :                 case BYTEAOID:
     661           1 :                         add_assoc_string(return_value, "native_type", BYTEALABEL);
     662           1 :                         break;
     663             :                 case INT8OID:
     664           1 :                         add_assoc_string(return_value, "native_type", INT8LABEL);
     665           1 :                         break;
     666             :                 case INT2OID:
     667           1 :                         add_assoc_string(return_value, "native_type", INT2LABEL);
     668           1 :                         break;
     669             :                 case INT4OID:
     670           1 :                         add_assoc_string(return_value, "native_type", INT4LABEL);
     671           1 :                         break;
     672             :                 case TEXTOID:
     673           1 :                         add_assoc_string(return_value, "native_type", TEXTLABEL);
     674           1 :                         break;
     675             :                 case VARCHAROID:
     676           1 :                         add_assoc_string(return_value, "native_type", VARCHARLABEL);
     677           1 :                         break;
     678             :                 case DATEOID:
     679           1 :                         add_assoc_string(return_value, "native_type", DATELABEL);
     680           1 :                         break;
     681             :                 case TIMESTAMPOID:
     682           1 :                         add_assoc_string(return_value, "native_type", TIMESTAMPLABEL);
     683           1 :                         break;
     684             :                 default:
     685             :                         /* Fetch metadata from Postgres system catalogue */
     686           0 :                         spprintf(&q, 0, "SELECT TYPNAME FROM PG_TYPE WHERE OID=%u", S->cols[colno].pgsql_type);
     687           0 :                         res = PQexec(S->H->server, q);
     688           0 :                         efree(q);
     689           0 :                         status = PQresultStatus(res);
     690           0 :                         if (status == PGRES_TUPLES_OK && 1 == PQntuples(res)) {
     691           0 :                                 add_assoc_string(return_value, "native_type", PQgetvalue(res, 0, 0));
     692             :                         }
     693           0 :                         PQclear(res);
     694             :         }
     695           9 :         return 1;
     696             : }
     697             : 
     698        1027 : static int pdo_pgsql_stmt_cursor_closer(pdo_stmt_t *stmt)
     699             : {
     700        1027 :         pdo_pgsql_stmt *S = (pdo_pgsql_stmt*)stmt->driver_data;
     701             : 
     702        1027 :         if (S->cols != NULL){
     703        1027 :                 efree(S->cols);
     704        1027 :                 S->cols = NULL;
     705             :         }
     706        1027 :         return 1;
     707             : }
     708             : 
     709             : struct pdo_stmt_methods pgsql_stmt_methods = {
     710             :         pgsql_stmt_dtor,
     711             :         pgsql_stmt_execute,
     712             :         pgsql_stmt_fetch,
     713             :         pgsql_stmt_describe,
     714             :         pgsql_stmt_get_col,
     715             :         pgsql_stmt_param_hook,
     716             :         NULL, /* set_attr */
     717             :         NULL, /* get_attr */
     718             :         pgsql_stmt_get_column_meta,
     719             :         NULL,  /* next_rowset */
     720             :         pdo_pgsql_stmt_cursor_closer
     721             : };
     722             : 
     723             : /*
     724             :  * Local variables:
     725             :  * tab-width: 4
     726             :  * c-basic-offset: 4
     727             :  * End:
     728             :  * vim600: noet sw=4 ts=4 fdm=marker
     729             :  * vim<600: noet sw=4 ts=4
     730             :  */

Generated by: LCOV version 1.10

Generated at Wed, 24 Aug 2016 12:20:27 +0000 (3 days ago)

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