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

Generated by: LCOV version 1.10

Generated at Sun, 18 Sep 2016 08:20:13 +0000 (7 days ago)

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