Back to index

php5  5.3.10
firebird_statement.c
Go to the documentation of this file.
00001 /*
00002   +----------------------------------------------------------------------+
00003   | PHP Version 5                                                        |
00004   +----------------------------------------------------------------------+
00005   | Copyright (c) 1997-2012 The PHP Group                                |
00006   +----------------------------------------------------------------------+
00007   | This source file is subject to version 3.01 of the PHP license,      |
00008   | that is bundled with this package in the file LICENSE, and is        |
00009   | available through the world-wide-web at the following url:           |
00010   | http://www.php.net/license/3_01.txt                                  |
00011   | If you did not receive a copy of the PHP license and are unable to   |
00012   | obtain it through the world-wide-web, please send a note to          |
00013   | license@php.net so we can mail you a copy immediately.               |
00014   +----------------------------------------------------------------------+
00015   | Author: Ard Biesheuvel <abies@php.net>                               |
00016   +----------------------------------------------------------------------+
00017 */
00018 
00019 /* $Id: firebird_statement.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include "config.h"
00023 #endif
00024 
00025 #include "php.h"
00026 #include "php_ini.h"
00027 #include "ext/standard/info.h"
00028 #include "pdo/php_pdo.h"
00029 #include "pdo/php_pdo_driver.h"
00030 #include "php_pdo_firebird.h"
00031 #include "php_pdo_firebird_int.h"
00032 
00033 #include <time.h>
00034        
00035 #define RECORD_ERROR(stmt) _firebird_error(NULL, stmt,  __FILE__, __LINE__ TSRMLS_CC)
00036 
00037 /* free the allocated space for passing field values to the db and back */
00038 static void free_sqlda(XSQLDA const *sqlda) /* {{{ */
00039 {
00040        int i;
00041        
00042        for (i = 0; i < sqlda->sqld; ++i) {
00043               XSQLVAR const *var = &sqlda->sqlvar[i];
00044               
00045               if (var->sqlind) {
00046                      efree(var->sqlind);
00047               }
00048        }
00049 }
00050 /* }}} */
00051        
00052 /* called by PDO to clean up a statement handle */
00053 static int firebird_stmt_dtor(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
00054 {
00055        pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
00056        int result = 1, i;
00057        
00058        /* release the statement */
00059        if (isc_dsql_free_statement(S->H->isc_status, &S->stmt, DSQL_drop)) {
00060               RECORD_ERROR(stmt);
00061               result = 0;
00062        }
00063 
00064        /* clean up the fetch buffers if they have been used */
00065        for (i = 0; i < S->out_sqlda.sqld; ++i) {
00066               if (S->fetch_buf[i]) {
00067                      efree(S->fetch_buf[i]);
00068               }
00069        }
00070        efree(S->fetch_buf);
00071        
00072        zend_hash_destroy(S->named_params);
00073        FREE_HASHTABLE(S->named_params);
00074        
00075        /* clean up the input descriptor */
00076        if (S->in_sqlda) {
00077               free_sqlda(S->in_sqlda);
00078               efree(S->in_sqlda);
00079        }
00080 
00081        free_sqlda(&S->out_sqlda);
00082        efree(S);
00083        
00084        return result;
00085 }
00086 /* }}} */
00087 
00088 /* called by PDO to execute a prepared query */
00089 static int firebird_stmt_execute(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
00090 {
00091        pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
00092        pdo_firebird_db_handle *H = S->H;
00093        unsigned long affected_rows = 0;
00094        static char info_count[] = {isc_info_sql_records};
00095        char result[64];
00096 
00097        do {
00098               /* named or open cursors should be closed first */
00099               if ((*S->name || S->cursor_open) && isc_dsql_free_statement(H->isc_status, &S->stmt, DSQL_close)) {
00100                      break;
00101               }
00102               S->cursor_open = 0;
00103               /* assume all params have been bound */
00104        
00105               if (isc_dsql_execute(H->isc_status, &H->tr, &S->stmt, PDO_FB_SQLDA_VERSION, S->in_sqlda)) {
00106                      break;
00107               }
00108               
00109               /* Determine how many rows have changed. In this case we are
00110                * only interested in rows changed, not rows retrieved. That
00111                * should be handled by the client when fetching. */
00112               stmt->row_count = affected_rows;
00113               
00114               switch (S->statement_type) {
00115                      case isc_info_sql_stmt_insert:
00116                      case isc_info_sql_stmt_update:
00117                      case isc_info_sql_stmt_delete:
00118                      case isc_info_sql_stmt_exec_procedure:
00119                             if (isc_dsql_sql_info(H->isc_status, &S->stmt, sizeof ( info_count),
00120                                    info_count, sizeof(result), result)) {
00121                                    break;
00122                             }
00123                             if (result[0] == isc_info_sql_records) {
00124                                    unsigned i = 3, result_size = isc_vax_integer(&result[1], 2);
00125                                    while (result[i] != isc_info_end && i < result_size) {
00126                                           short len = (short) isc_vax_integer(&result[i + 1], 2);
00127                                           if (result[i] != isc_info_req_select_count) {
00128                                                  affected_rows += isc_vax_integer(&result[i + 3], len);
00129                                           }
00130                                           i += len + 3;
00131                                    }
00132                                    stmt->row_count = affected_rows;
00133                             }
00134                      default:
00135                             ;
00136               }
00137 
00138               /* commit? */
00139               if (stmt->dbh->auto_commit && isc_commit_retaining(H->isc_status, &H->tr)) {
00140                      break;
00141               }
00142        
00143               *S->name = 0;
00144               S->cursor_open = (S->out_sqlda.sqln > 0); /* A cursor is opened, when more than zero columns returned */
00145               S->exhausted = !S->cursor_open;
00146               
00147               return 1;
00148        } while (0);
00149 
00150        RECORD_ERROR(stmt);  
00151 
00152        return 0;
00153 }
00154 /* }}} */
00155 
00156 /* called by PDO to fetch the next row from a statement */
00157 static int firebird_stmt_fetch(pdo_stmt_t *stmt, /* {{{ */
00158        enum pdo_fetch_orientation ori, long offset TSRMLS_DC)
00159 {
00160        pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
00161        pdo_firebird_db_handle *H = S->H;
00162 
00163        if (!stmt->executed) {
00164               strcpy(stmt->error_code, "HY000");
00165               H->last_app_error = "Cannot fetch from a closed cursor";
00166        } else if (!S->exhausted) {
00167               if (isc_dsql_fetch(H->isc_status, &S->stmt, PDO_FB_SQLDA_VERSION, &S->out_sqlda)) {
00168                      if (H->isc_status[0] && H->isc_status[1]) {
00169                             RECORD_ERROR(stmt);
00170                      }
00171                      S->exhausted = 1;
00172                      return 0;
00173               }
00174               if (S->statement_type == isc_info_sql_stmt_exec_procedure) {
00175                      S->exhausted = 1;
00176               }
00177               stmt->row_count++;
00178               return 1;
00179        }
00180        return 0;
00181 }
00182 /* }}} */
00183 
00184 /* called by PDO to retrieve information about the fields being returned */
00185 static int firebird_stmt_describe(pdo_stmt_t *stmt, int colno TSRMLS_DC) /* {{{ */
00186 {
00187        pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
00188        struct pdo_column_data *col = &stmt->columns[colno];
00189        XSQLVAR *var = &S->out_sqlda.sqlvar[colno];
00190        int colname_len;
00191        char *cp;
00192        
00193        /* allocate storage for the column */
00194        var->sqlind = (void*)ecalloc(1, var->sqllen + 2*sizeof(short));
00195        var->sqldata = &((char*)var->sqlind)[sizeof(short)];
00196 
00197        colname_len = (S->H->fetch_table_names && var->relname_length)
00198                                    ? (var->aliasname_length + var->relname_length + 1)
00199                                    : (var->aliasname_length);
00200        col->precision = -var->sqlscale;
00201        col->maxlen = var->sqllen;
00202        col->namelen = colname_len;
00203        col->name = cp = emalloc(colname_len + 1);
00204        if (colname_len > var->aliasname_length) {
00205               memmove(cp, var->relname, var->relname_length);
00206               cp += var->relname_length;
00207               *cp++ = '.';
00208        }
00209        memmove(cp, var->aliasname, var->aliasname_length);
00210        *(cp+var->aliasname_length) = '\0';
00211        col->param_type = PDO_PARAM_STR;
00212 
00213        return 1;
00214 }
00215 /* }}} */
00216 
00217 #define FETCH_BUF(buf,type,len,lenvar) ((buf) = (buf) ? (buf) : \
00218        emalloc((len) ? (len * sizeof(type)) : ((*(unsigned long*)lenvar) = sizeof(type))))
00219 
00220 #define CHAR_BUF_LEN 24
00221 
00222 /* fetch a blob into a fetch buffer */
00223 static int firebird_fetch_blob(pdo_stmt_t *stmt, int colno, char **ptr, /* {{{ */
00224        unsigned long *len, ISC_QUAD *blob_id TSRMLS_DC)
00225 {
00226        pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
00227        pdo_firebird_db_handle *H = S->H;
00228        isc_blob_handle blobh = NULL;
00229        char const bl_item = isc_info_blob_total_length;
00230        char bl_info[20];
00231        unsigned short i;
00232        int result = *len = 0;
00233 
00234        if (isc_open_blob(H->isc_status, &H->db, &H->tr, &blobh, blob_id)) {
00235               RECORD_ERROR(stmt);
00236               return 0;
00237        }
00238 
00239        if (isc_blob_info(H->isc_status, &blobh, 1, const_cast(&bl_item),
00240                      sizeof(bl_info), bl_info)) {
00241               RECORD_ERROR(stmt);
00242               goto fetch_blob_end;
00243        }
00244 
00245        /* find total length of blob's data */
00246        for (i = 0; i < sizeof(bl_info); ) {
00247               unsigned short item_len;
00248               char item = bl_info[i++];
00249 
00250               if (item == isc_info_end || item == isc_info_truncated || item == isc_info_error
00251                             || i >= sizeof(bl_info)) {
00252                      H->last_app_error = "Couldn't determine BLOB size";
00253                      goto fetch_blob_end;
00254               }                                                       
00255 
00256               item_len = (unsigned short) isc_vax_integer(&bl_info[i], 2);
00257 
00258               if (item == isc_info_blob_total_length) {
00259                      *len = isc_vax_integer(&bl_info[i+2], item_len);
00260                      break;
00261               }
00262               i += item_len+2;
00263        }
00264 
00265        /* we've found the blob's length, now fetch! */
00266        
00267        if (*len) {
00268               unsigned long cur_len;
00269               unsigned short seg_len;
00270               ISC_STATUS stat;
00271 
00272               *ptr = S->fetch_buf[colno] = erealloc(*ptr, *len+1);
00273        
00274               for (cur_len = stat = 0; (!stat || stat == isc_segment) && cur_len < *len; cur_len += seg_len) {
00275        
00276                      unsigned short chunk_size = (*len-cur_len) > USHRT_MAX ? USHRT_MAX
00277                             : (unsigned short)(*len-cur_len);
00278        
00279                      stat = isc_get_segment(H->isc_status, &blobh, &seg_len, chunk_size, &(*ptr)[cur_len]);
00280               }
00281        
00282               (*ptr)[*len++] = '\0';
00283        
00284               if (H->isc_status[0] == 1 && (stat != 0 && stat != isc_segstr_eof && stat != isc_segment)) {
00285                      H->last_app_error = "Error reading from BLOB";
00286                      goto fetch_blob_end;
00287               }
00288        }
00289        result = 1;
00290 
00291 fetch_blob_end:
00292        if (isc_close_blob(H->isc_status, &blobh)) {
00293               RECORD_ERROR(stmt);
00294               return 0;
00295        }
00296        return result;
00297 }
00298 /* }}} */
00299 
00300 static int firebird_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr,  /* {{{ */
00301        unsigned long *len, int *caller_frees TSRMLS_DC)
00302 {
00303        pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
00304        XSQLVAR const *var = &S->out_sqlda.sqlvar[colno];
00305 
00306        if (*var->sqlind == -1) {
00307               /* A NULL value */
00308               *ptr = NULL;
00309               *len = 0;
00310        } else {
00311               if (var->sqlscale < 0) {
00312                      static ISC_INT64 const scales[] = { 1, 10, 100, 1000, 
00313                             10000, 
00314                             100000, 
00315                             1000000, 
00316                             10000000,
00317                             100000000, 
00318                             1000000000, 
00319                             LL_LIT(10000000000), 
00320                             LL_LIT(100000000000),
00321                             LL_LIT(1000000000000), 
00322                             LL_LIT(10000000000000), 
00323                             LL_LIT(100000000000000),
00324                             LL_LIT(1000000000000000),
00325                             LL_LIT(10000000000000000), 
00326                             LL_LIT(100000000000000000), 
00327                             LL_LIT(1000000000000000000)
00328                      };
00329                      ISC_INT64 n, f = scales[-var->sqlscale];
00330 
00331                      switch (var->sqltype & ~1) {
00332                             case SQL_SHORT:
00333                                    n = *(short*)var->sqldata;
00334                                    break;
00335                             case SQL_LONG:
00336                                    n = *(ISC_LONG*)var->sqldata;
00337                                    break;
00338                             case SQL_INT64:
00339                                    n = *(ISC_INT64*)var->sqldata;
00340                      }
00341                             
00342                      *ptr = FETCH_BUF(S->fetch_buf[colno], char, CHAR_BUF_LEN, NULL);
00343                      
00344                      if (n >= 0) {
00345                             *len = slprintf(*ptr, CHAR_BUF_LEN, "%" LL_MASK "d.%0*" LL_MASK "d", 
00346                                    n / f, -var->sqlscale, n % f);
00347                      } else if (n < -f) {
00348                             *len = slprintf(*ptr, CHAR_BUF_LEN, "%" LL_MASK "d.%0*" LL_MASK "d",
00349                                    n / f, -var->sqlscale, -n % f);                         
00350                       } else {
00351                             *len = slprintf(*ptr, CHAR_BUF_LEN, "-0.%0*" LL_MASK "d", -var->sqlscale, -n % f);
00352                      }
00353               } else {
00354                      switch (var->sqltype & ~1) {
00355                             struct tm t;
00356                             char *fmt;                         
00357 
00358                             case SQL_VARYING:
00359                                    *ptr = &var->sqldata[2];
00360                                    *len = *(short*)var->sqldata;
00361                                    break;
00362                             case SQL_TEXT:
00363                                    *ptr = var->sqldata;
00364                                    *len = var->sqllen;
00365                                    break;
00366                             case SQL_SHORT:
00367                                 *ptr = FETCH_BUF(S->fetch_buf[colno], char, CHAR_BUF_LEN, NULL);
00368                                    *len = slprintf(*ptr, CHAR_BUF_LEN, "%d", *(short*)var->sqldata);
00369                                    break;
00370                             case SQL_LONG:
00371                                    *ptr = FETCH_BUF(S->fetch_buf[colno], char, CHAR_BUF_LEN, NULL);
00372                                    *len = slprintf(*ptr, CHAR_BUF_LEN, "%ld", *(ISC_LONG*)var->sqldata);
00373                                    break;
00374                             case SQL_INT64:
00375                                    *ptr = FETCH_BUF(S->fetch_buf[colno], char, CHAR_BUF_LEN, NULL);
00376                                    *len = slprintf(*ptr, CHAR_BUF_LEN, "%" LL_MASK "d", *(ISC_INT64*)var->sqldata);
00377                                    break;
00378                             case SQL_FLOAT:
00379                                    *ptr = FETCH_BUF(S->fetch_buf[colno], char, CHAR_BUF_LEN, NULL);
00380                                    *len = slprintf(*ptr, CHAR_BUF_LEN, "%F", *(float*)var->sqldata);
00381                                    break;
00382                             case SQL_DOUBLE:
00383                                    *ptr = FETCH_BUF(S->fetch_buf[colno], char, CHAR_BUF_LEN, NULL);
00384                                    *len = slprintf(*ptr, CHAR_BUF_LEN, "%F" , *(double*)var->sqldata);
00385                                    break;
00386                             case SQL_TYPE_DATE:
00387                                    isc_decode_sql_date((ISC_DATE*)var->sqldata, &t);
00388                                    fmt = S->H->date_format ? S->H->date_format : PDO_FB_DEF_DATE_FMT;
00389                                    if (0) {
00390                             case SQL_TYPE_TIME:
00391                                           isc_decode_sql_time((ISC_TIME*)var->sqldata, &t);
00392                                           fmt = S->H->time_format ? S->H->time_format : PDO_FB_DEF_TIME_FMT;
00393                                    } else if (0) {
00394                             case SQL_TIMESTAMP:
00395                                           isc_decode_timestamp((ISC_TIMESTAMP*)var->sqldata, &t);
00396                                           fmt = S->H->timestamp_format ? S->H->timestamp_format : PDO_FB_DEF_TIMESTAMP_FMT;
00397                                    }
00398                                    /* convert the timestamp into a string */
00399                                    *len = 80;
00400                                    *ptr = FETCH_BUF(S->fetch_buf[colno], char, *len, NULL);
00401                                    *len = strftime(*ptr, *len, fmt, &t);
00402                                    break;
00403                             case SQL_BLOB:
00404                                    return firebird_fetch_blob(stmt,colno,ptr,len,
00405                                           (ISC_QUAD*)var->sqldata TSRMLS_CC);
00406                      }
00407               }
00408        }
00409        return 1;
00410 }
00411 /* }}} */
00412 
00413 static int firebird_bind_blob(pdo_stmt_t *stmt, ISC_QUAD *blob_id, zval *param TSRMLS_DC)
00414 {
00415        pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
00416        pdo_firebird_db_handle *H = S->H;
00417        isc_blob_handle h = NULL;
00418        unsigned long put_cnt = 0, rem_cnt;
00419        unsigned short chunk_size;
00420        int result = 1;
00421        
00422        if (isc_create_blob(H->isc_status, &H->db, &H->tr, &h, blob_id)) {
00423               RECORD_ERROR(stmt);
00424               return 0;
00425        }
00426 
00427        SEPARATE_ZVAL(&param);
00428 
00429        convert_to_string_ex(&param);
00430        
00431        for (rem_cnt = Z_STRLEN_P(param); rem_cnt > 0; rem_cnt -= chunk_size)  {
00432 
00433               chunk_size = rem_cnt > USHRT_MAX ? USHRT_MAX : (unsigned short)rem_cnt;
00434 
00435               if (isc_put_segment(H->isc_status, &h, chunk_size, &Z_STRVAL_P(param)[put_cnt])) {
00436                      RECORD_ERROR(stmt);
00437                      result = 0;
00438                      break;
00439               }
00440               put_cnt += chunk_size;
00441        }
00442        
00443        zval_dtor(param);
00444 
00445        if (isc_close_blob(H->isc_status, &h)) {
00446               RECORD_ERROR(stmt);
00447               return 0;
00448        }
00449        return result;
00450 }      
00451 
00452 static int firebird_stmt_param_hook(pdo_stmt_t *stmt, struct pdo_bound_param_data *param, /* {{{ */
00453        enum pdo_param_event event_type TSRMLS_DC)
00454 {
00455        pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
00456        XSQLDA *sqlda = param->is_param ? S->in_sqlda : &S->out_sqlda;
00457        XSQLVAR *var;
00458 
00459        if (event_type == PDO_PARAM_EVT_FREE) { /* not used */
00460               return 1;
00461        }
00462 
00463        if (!sqlda || param->paramno >= sqlda->sqld) {
00464               strcpy(stmt->error_code, "HY093");
00465               S->H->last_app_error = "Invalid parameter index";
00466               return 0;
00467        }
00468        if (param->is_param && param->paramno == -1) {
00469               long *index;
00470 
00471               /* try to determine the index by looking in the named_params hash */
00472               if (SUCCESS == zend_hash_find(S->named_params, param->name, param->namelen+1, (void*)&index)) {
00473                      param->paramno = *index;
00474               } else {
00475                      /* ... or by looking in the input descriptor */
00476                      int i;
00477 
00478                      for (i = 0; i < sqlda->sqld; ++i) {
00479                             XSQLVAR *var = &sqlda->sqlvar[i];
00480 
00481                             if ((var->aliasname_length && !strncasecmp(param->name, var->aliasname, 
00482                                           min(param->namelen, var->aliasname_length))) 
00483                                           || (var->sqlname_length && !strncasecmp(param->name, var->sqlname,
00484                                           min(param->namelen, var->sqlname_length)))) {
00485                                    param->paramno = i;
00486                                    break;
00487                             }
00488                      }
00489                      if (i >= sqlda->sqld) {
00490                             strcpy(stmt->error_code, "HY093");
00491                             S->H->last_app_error = "Invalid parameter name";
00492                             return 0;
00493                      }
00494               }
00495        }
00496 
00497        var = &sqlda->sqlvar[param->paramno];
00498        
00499        switch (event_type) {
00500               char *value;
00501               unsigned long value_len;
00502               int caller_frees;
00503                      
00504               case PDO_PARAM_EVT_ALLOC:
00505                      if (param->is_param) {
00506                             /* allocate the parameter */
00507                             if (var->sqlind) {
00508                                    efree(var->sqlind);
00509                             }
00510                             var->sqlind = (void*)emalloc(var->sqllen + 2*sizeof(short));
00511                             var->sqldata = &((char*)var->sqlind)[sizeof(short)];
00512                      }
00513                      break;
00514                      
00515               case PDO_PARAM_EVT_EXEC_PRE:
00516                      if (!param->is_param) {
00517                             break;
00518                      }
00519 
00520                      *var->sqlind = 0;
00521 
00522                      switch (var->sqltype & ~1) {
00523                             case SQL_ARRAY:
00524                                    strcpy(stmt->error_code, "HY000");
00525                                    S->H->last_app_error = "Cannot bind to array field";
00526                                    return 0;
00527        
00528                             case SQL_BLOB:
00529                                    return firebird_bind_blob(stmt, (ISC_QUAD*)var->sqldata,
00530                                           param->parameter TSRMLS_CC);
00531                      }
00532                                                  
00533                      /* check if a NULL should be inserted */
00534                      switch (Z_TYPE_P(param->parameter)) {
00535                             int force_null;
00536                             
00537                             case IS_LONG:
00538                                    var->sqltype = sizeof(long) == 8 ? SQL_INT64 : SQL_LONG;
00539                                    var->sqldata = (void*)&Z_LVAL_P(param->parameter);
00540                                    var->sqllen = sizeof(long);
00541                                    break;
00542                             case IS_DOUBLE:
00543                                    var->sqltype = SQL_DOUBLE;
00544                                    var->sqldata = (void*)&Z_DVAL_P(param->parameter);
00545                                    var->sqllen = sizeof(double);
00546                                    break;
00547                             case IS_STRING:
00548                                    force_null = 0;
00549        
00550                                    /* for these types, an empty string can be handled like a NULL value */
00551                                    switch (var->sqltype & ~1) {
00552                                           case SQL_SHORT:
00553                                           case SQL_LONG:
00554                                           case SQL_INT64:
00555                                           case SQL_FLOAT:
00556                                           case SQL_DOUBLE:
00557                                           case SQL_TIMESTAMP:
00558                                           case SQL_TYPE_DATE:
00559                                           case SQL_TYPE_TIME:
00560                                                  force_null = (Z_STRLEN_P(param->parameter) == 0);
00561                                    }
00562                                    if (!force_null) {
00563                                           var->sqltype = SQL_TEXT;
00564                                           var->sqldata = Z_STRVAL_P(param->parameter);
00565                                           var->sqllen    = Z_STRLEN_P(param->parameter);
00566                                           break;
00567                                    }
00568                             case IS_NULL:
00569                                    /* complain if this field doesn't allow NULL values */
00570                                    if (~var->sqltype & 1) {
00571                                           strcpy(stmt->error_code, "HY105");
00572                                           S->H->last_app_error = "Parameter requires non-null value";
00573                                           return 0;
00574                                    }
00575                                    *var->sqlind = -1;
00576                                    break;
00577                             default:
00578                                    strcpy(stmt->error_code, "HY105");
00579                                    S->H->last_app_error = "Binding arrays/objects is not supported";
00580                                    return 0;
00581                      }
00582                      break;
00583 
00584               case PDO_PARAM_EVT_FETCH_POST:
00585                         if (param->paramno == -1) {
00586                             return 0;
00587                         }
00588                      if (param->is_param) {
00589                             break;
00590                      }
00591                      value = NULL;
00592                      value_len = 0;
00593                      caller_frees = 0;
00594                      
00595                      if (firebird_stmt_get_col(stmt, param->paramno, &value, &value_len, &caller_frees TSRMLS_CC)) {
00596                             switch (PDO_PARAM_TYPE(param->param_type)) {
00597                                    case PDO_PARAM_STR:
00598                                           if (value) {
00599                                                  ZVAL_STRINGL(param->parameter, value, value_len, 1);
00600                                                  break;
00601                                           }
00602                                    case PDO_PARAM_INT:
00603                                           if (value) {
00604                                                  ZVAL_LONG(param->parameter, *(long*)value);
00605                                                  break;
00606                                           }
00607                                         case PDO_PARAM_EVT_NORMALIZE:
00608                                                  if (!param->is_param) {
00609                                                       char *s = param->name;
00610                                                       while (*s != '\0') {
00611                                                            *s = toupper(*s);
00612                                                             s++;
00613                                                       }
00614                                                  }
00615                                                         break;
00616                                    default:
00617                                           ZVAL_NULL(param->parameter);
00618                             }
00619                             if (value && caller_frees) {
00620                                    efree(value);
00621                             }
00622                             return 1;
00623                      }
00624                      return 0;
00625               default:
00626                      ;
00627        }             
00628        return 1;
00629 }
00630 /* }}} */
00631 
00632 static int firebird_stmt_set_attribute(pdo_stmt_t *stmt, long attr, zval *val TSRMLS_DC) /* {{{ */
00633 {
00634        pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
00635        
00636        switch (attr) {
00637               default:
00638                      return 0;
00639               case PDO_ATTR_CURSOR_NAME:
00640                      convert_to_string(val);
00641                      
00642                      if (isc_dsql_set_cursor_name(S->H->isc_status, &S->stmt, Z_STRVAL_P(val),0)) {
00643                             RECORD_ERROR(stmt);
00644                             return 0;
00645                      }
00646                      strlcpy(S->name, Z_STRVAL_P(val), sizeof(S->name));
00647                      break;
00648        }
00649        return 1;
00650 }
00651 /* }}} */
00652 
00653 static int firebird_stmt_get_attribute(pdo_stmt_t *stmt, long attr, zval *val TSRMLS_DC) /* {{{ */
00654 {
00655        pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
00656        
00657        switch (attr) {
00658               default:
00659                      return 0;
00660               case PDO_ATTR_CURSOR_NAME:
00661                      if (*S->name) {
00662                             ZVAL_STRING(val,S->name,1);
00663                      } else {
00664                             ZVAL_NULL(val);
00665                      }
00666                      break;
00667        }
00668        return 1;
00669 }
00670 /* }}} */
00671 
00672 static int firebird_stmt_cursor_closer(pdo_stmt_t *stmt TSRMLS_DC) /* {{{ */
00673 {
00674        pdo_firebird_stmt *S = (pdo_firebird_stmt*)stmt->driver_data;
00675        
00676        /* close the statement handle */
00677        if ((*S->name || S->cursor_open) && isc_dsql_free_statement(S->H->isc_status, &S->stmt, DSQL_close)) {
00678               RECORD_ERROR(stmt);
00679               return 0;
00680        }
00681        *S->name = 0;
00682        S->cursor_open = 0;
00683        return 1;
00684 }
00685 /* }}} */
00686 
00687 
00688 struct pdo_stmt_methods firebird_stmt_methods = { /* {{{ */
00689        firebird_stmt_dtor,
00690        firebird_stmt_execute,
00691        firebird_stmt_fetch,
00692        firebird_stmt_describe,
00693        firebird_stmt_get_col,
00694        firebird_stmt_param_hook,
00695        firebird_stmt_set_attribute,
00696        firebird_stmt_get_attribute,
00697        NULL, /* get_column_meta_func */
00698        NULL, /* next_rowset_func */
00699        firebird_stmt_cursor_closer
00700 };
00701 /* }}} */
00702 
00703 /*
00704  * Local variables:
00705  * tab-width: 4
00706  * c-basic-offset: 4
00707  * End:
00708  * vim600: noet sw=4 ts=4 fdm=marker
00709  * vim<600: noet sw=4 ts=4
00710  */