Back to index

php5  5.3.10
firebird_driver.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_driver.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include "config.h"
00023 #endif
00024 
00025 #define _GNU_SOURCE
00026 
00027 #include "php.h"
00028 #ifdef ZEND_ENGINE_2
00029 # include "zend_exceptions.h"
00030 #endif
00031 #include "php_ini.h"
00032 #include "ext/standard/info.h"
00033 #include "pdo/php_pdo.h"
00034 #include "pdo/php_pdo_driver.h"
00035 #include "php_pdo_firebird.h"
00036 #include "php_pdo_firebird_int.h"
00037 
00038 static int firebird_alloc_prepare_stmt(pdo_dbh_t*, const char*, long, XSQLDA*, isc_stmt_handle*,
00039        HashTable* TSRMLS_DC);
00040 
00041 /* map driver specific error message to PDO error */
00042 void _firebird_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, char const *file, long line TSRMLS_DC) /* {{{ */
00043 {
00044 #if 0
00045        pdo_firebird_db_handle *H = stmt ? ((pdo_firebird_stmt *)stmt->driver_data)->H 
00046               : (pdo_firebird_db_handle *)dbh->driver_data;
00047 #endif
00048        pdo_error_type *const error_code = stmt ? &stmt->error_code : &dbh->error_code;
00049        
00050 #if 0
00051        switch (isc_sqlcode(H->isc_status)) {
00052 
00053               case 0:
00054                      *error_code = PDO_ERR_NONE;
00055                      break;
00056               default:
00057                      *error_code = PDO_ERR_CANT_MAP;
00058                      break;
00059               case -104:
00060                      *error_code = PDO_ERR_SYNTAX;
00061                      break;
00062               case -530:
00063               case -803:
00064                      *error_code = PDO_ERR_CONSTRAINT;
00065                      break;
00066               case -204:
00067               case -205:
00068               case -206:
00069               case -829:
00070                      *error_code = PDO_ERR_NOT_FOUND;
00071                      break;
00072 
00073                      *error_code = PDO_ERR_ALREADY_EXISTS;
00074                      break;
00075               
00076                      *error_code = PDO_ERR_NOT_IMPLEMENTED;
00077                      break;
00078               case -313:
00079               case -804:
00080                      *error_code = PDO_ERR_MISMATCH;
00081                      break;
00082               case -303:
00083               case -314:    
00084               case -413:
00085                      *error_code = PDO_ERR_TRUNCATED;
00086                      break;
00087                      
00088                      *error_code = PDO_ERR_DISCONNECTED;
00089                      break;
00090        }
00091 #else
00092        strcpy(*error_code, "HY000");
00093 #endif
00094 }
00095 /* }}} */
00096 
00097 #define RECORD_ERROR(dbh) _firebird_error(dbh, NULL, __FILE__, __LINE__ TSRMLS_CC)
00098 
00099 /* called by PDO to close a db handle */
00100 static int firebird_handle_closer(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
00101 {
00102        pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
00103        
00104        if (dbh->in_txn) {
00105               if (dbh->auto_commit) {
00106                      if (isc_commit_transaction(H->isc_status, &H->tr)) {
00107                             RECORD_ERROR(dbh);
00108                      }
00109               } else {
00110                      if (isc_rollback_transaction(H->isc_status, &H->tr)) {
00111                             RECORD_ERROR(dbh);
00112                      }
00113               }
00114        }
00115        
00116        if (isc_detach_database(H->isc_status, &H->db)) {
00117               RECORD_ERROR(dbh);
00118        }
00119 
00120        if (H->date_format) {
00121               efree(H->date_format);
00122        }
00123        if (H->time_format) {
00124               efree(H->time_format);
00125        }
00126        if (H->timestamp_format) {
00127               efree(H->timestamp_format);
00128        }
00129        
00130        pefree(H, dbh->is_persistent);
00131 
00132        return 0;
00133 }
00134 /* }}} */
00135 
00136 /* called by PDO to prepare an SQL query */
00137 static int firebird_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len, /* {{{ */
00138        pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC)
00139 {
00140        pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
00141        pdo_firebird_stmt *S = NULL;
00142        HashTable *np;
00143 
00144        do {
00145               isc_stmt_handle s = NULL;
00146               XSQLDA num_sqlda;
00147               static char const info[] = { isc_info_sql_stmt_type };
00148               char result[8];
00149 
00150               num_sqlda.version = PDO_FB_SQLDA_VERSION;
00151               num_sqlda.sqln = 1;
00152 
00153               ALLOC_HASHTABLE(np);
00154               zend_hash_init(np, 8, NULL, NULL, 0);
00155               
00156               /* allocate and prepare statement */
00157               if (!firebird_alloc_prepare_stmt(dbh, sql, sql_len, &num_sqlda, &s, np TSRMLS_CC)) {
00158                      break;
00159               }
00160        
00161               /* allocate a statement handle struct of the right size (struct out_sqlda is inlined) */
00162               S = ecalloc(1, sizeof(*S)-sizeof(XSQLDA) + XSQLDA_LENGTH(num_sqlda.sqld));
00163               S->H = H;
00164               S->stmt = s;
00165               S->fetch_buf = ecalloc(1,sizeof(char*) * num_sqlda.sqld);
00166               S->out_sqlda.version = PDO_FB_SQLDA_VERSION;
00167               S->out_sqlda.sqln = stmt->column_count = num_sqlda.sqld;
00168               S->named_params = np;
00169 
00170               /* determine the statement type */
00171               if (isc_dsql_sql_info(H->isc_status, &s, sizeof(info), const_cast(info), sizeof(result),
00172                             result)) {
00173                      break;
00174               }
00175               S->statement_type = result[3];     
00176               
00177               /* fill the output sqlda with information about the prepared query */
00178               if (isc_dsql_describe(H->isc_status, &s, PDO_FB_SQLDA_VERSION, &S->out_sqlda)) {
00179                      RECORD_ERROR(dbh);
00180                      break;
00181               }
00182               
00183               /* allocate the input descriptors */
00184               if (isc_dsql_describe_bind(H->isc_status, &s, PDO_FB_SQLDA_VERSION, &num_sqlda)) {
00185                      break;
00186               }
00187               
00188               if (num_sqlda.sqld) {
00189                      S->in_sqlda = ecalloc(1,XSQLDA_LENGTH(num_sqlda.sqld));
00190                      S->in_sqlda->version = PDO_FB_SQLDA_VERSION;
00191                      S->in_sqlda->sqln = num_sqlda.sqld;
00192               
00193                      if (isc_dsql_describe_bind(H->isc_status, &s, PDO_FB_SQLDA_VERSION, S->in_sqlda)) {
00194                             break;
00195                      }
00196               }
00197        
00198               stmt->driver_data = S;
00199               stmt->methods = &firebird_stmt_methods;
00200               stmt->supports_placeholders = PDO_PLACEHOLDER_POSITIONAL;
00201        
00202               return 1;
00203 
00204        } while (0);
00205 
00206        RECORD_ERROR(dbh);
00207        
00208        zend_hash_destroy(np);
00209        FREE_HASHTABLE(np);
00210        
00211        if (S) {
00212               if (S->in_sqlda) {
00213                      efree(S->in_sqlda);
00214               }
00215               efree(S);
00216        }
00217        
00218        return 0;
00219 }
00220 /* }}} */
00221 
00222 /* called by PDO to execute a statement that doesn't produce a result set */
00223 static long firebird_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC) /* {{{ */
00224 {
00225        pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
00226        isc_stmt_handle stmt = NULL;
00227        static char const info_count[] = { isc_info_sql_records };
00228        char result[64];
00229        int ret = 0;
00230        XSQLDA in_sqlda, out_sqlda;
00231               
00232        /* TODO no placeholders in exec() for now */
00233        in_sqlda.version = out_sqlda.version = PDO_FB_SQLDA_VERSION;
00234        in_sqlda.sqld = out_sqlda.sqld = 0;
00235        out_sqlda.sqln = 1;
00236        
00237        /* allocate and prepare statement */
00238        if (!firebird_alloc_prepare_stmt(dbh, sql, sql_len, &out_sqlda, &stmt, 0 TSRMLS_CC)) {
00239               return -1;
00240        }
00241 
00242        /* execute the statement */
00243        if (isc_dsql_execute2(H->isc_status, &H->tr, &stmt, PDO_FB_SQLDA_VERSION, &in_sqlda, &out_sqlda)) {
00244               RECORD_ERROR(dbh);
00245               return -1;
00246        }
00247        
00248        /* find out how many rows were affected */
00249        if (isc_dsql_sql_info(H->isc_status, &stmt, sizeof(info_count), const_cast(info_count),
00250                      sizeof(result),      result)) {
00251               RECORD_ERROR(dbh);
00252               return -1;
00253        }
00254 
00255        if (result[0] == isc_info_sql_records) {
00256               unsigned i = 3, result_size = isc_vax_integer(&result[1],2);
00257 
00258               while (result[i] != isc_info_end && i < result_size) {
00259                      short len = (short)isc_vax_integer(&result[i+1],2);
00260                      if (result[i] != isc_info_req_select_count) {
00261                             ret += isc_vax_integer(&result[i+3],len);
00262                      }
00263                      i += len+3;
00264               }
00265        }
00266        
00267        /* commit if we're in auto_commit mode */
00268        if (dbh->auto_commit && isc_commit_retaining(H->isc_status, &H->tr)) {
00269               RECORD_ERROR(dbh);
00270        }
00271 
00272        return ret;
00273 }
00274 /* }}} */
00275 
00276 /* called by the PDO SQL parser to add quotes to values that are copied into SQL */
00277 static int firebird_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, int unquotedlen, /* {{{ */
00278        char **quoted, int *quotedlen, enum pdo_param_type paramtype TSRMLS_DC)
00279 {
00280        int qcount = 0;
00281        char const *co, *l, *r;
00282        char *c;
00283        
00284        if (!unquotedlen) {
00285               *quotedlen = 2;
00286               *quoted = emalloc(*quotedlen+1);
00287               strcpy(*quoted, "''");
00288               return 1;
00289        }
00290                      
00291        /* Firebird only requires single quotes to be doubled if string lengths are used */
00292        /* count the number of ' characters */
00293        for (co = unquoted; (co = strchr(co,'\'')); qcount++, co++);
00294        
00295        *quotedlen = unquotedlen + qcount + 2;
00296        *quoted = c = emalloc(*quotedlen+1);             
00297        *c++ = '\'';
00298        
00299        /* foreach (chunk that ends in a quote) */
00300        for (l = unquoted; (r = strchr(l,'\'')); l = r+1) {                   
00301               strncpy(c, l, r-l+1);
00302               c += (r-l+1);               
00303               /* add the second quote */
00304               *c++ = '\'';
00305        }
00306               
00307        /* copy the remainder */
00308        strncpy(c, l, *quotedlen-(c-*quoted)-1);
00309        (*quoted)[*quotedlen-1] = '\''; 
00310        (*quoted)[*quotedlen]   = '\0';
00311        
00312        return 1;
00313 }
00314 /* }}} */
00315 
00316 /* called by PDO to start a transaction */
00317 static int firebird_handle_begin(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
00318 {
00319        pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
00320        char tpb[8] = { isc_tpb_version3 }, *ptpb = tpb+1;
00321 #if abies_0   
00322        if (dbh->transaction_flags & PDO_TRANS_ISOLATION_LEVEL) {
00323               if (dbh->transaction_flags & PDO_TRANS_READ_UNCOMMITTED) {
00324                      /* this is a poor fit, but it's all we have */
00325                      *ptpb++ = isc_tpb_read_committed;
00326                      *ptpb++ = isc_tpb_rec_version;
00327                      dbh->transaction_flags &= ~(PDO_TRANS_ISOLATION_LEVEL^PDO_TRANS_READ_UNCOMMITTED);
00328               } else if (dbh->transaction_flags & PDO_TRANS_READ_COMMITTED) {
00329                      *ptpb++ = isc_tpb_read_committed;
00330                      *ptpb++ = isc_tpb_no_rec_version;
00331                      dbh->transaction_flags &= ~(PDO_TRANS_ISOLATION_LEVEL^PDO_TRANS_READ_COMMITTED);
00332               } else if (dbh->transaction_flags & PDO_TRANS_REPEATABLE_READ) {
00333                      *ptpb++ = isc_tpb_concurrency;
00334                      dbh->transaction_flags &= ~(PDO_TRANS_ISOLATION_LEVEL^PDO_TRANS_REPEATABLE_READ);
00335               } else {
00336                      *ptpb++ = isc_tpb_consistency;
00337                      dbh->transaction_flags &= ~(PDO_TRANS_ISOLATION_LEVEL^PDO_TRANS_SERIALIZABLE);
00338               }
00339        }
00340               
00341        if (dbh->transaction_flags & PDO_TRANS_ACCESS_MODE) {
00342               if (dbh->transaction_flags & PDO_TRANS_READONLY) {
00343                      *ptpb++ = isc_tpb_read;
00344                      dbh->transaction_flags &= ~(PDO_TRANS_ACCESS_MODE^PDO_TRANS_READONLY);
00345               } else {
00346                      *ptpb++ = isc_tpb_write;
00347                      dbh->transaction_flags &= ~(PDO_TRANS_ACCESS_MODE^PDO_TRANS_READWRITE);
00348               }
00349        }
00350 
00351        if (dbh->transaction_flags & PDO_TRANS_CONFLICT_RESOLUTION) {
00352               if (dbh->transaction_flags & PDO_TRANS_RETRY) {
00353                      *ptpb++ = isc_tpb_wait;
00354                      dbh->transaction_flags &= ~(PDO_TRANS_CONFLICT_RESOLUTION^PDO_TRANS_RETRY);
00355               } else {
00356                      *ptpb++ = isc_tpb_nowait;
00357                      dbh->transaction_flags &= ~(PDO_TRANS_CONFLICT_RESOLUTION^PDO_TRANS_ABORT);
00358               }
00359        }
00360 #endif
00361        if (isc_start_transaction(H->isc_status, &H->tr, 1, &H->db, (unsigned short)(ptpb-tpb), tpb)) {
00362               RECORD_ERROR(dbh);
00363               return 0;
00364        }
00365        return 1;
00366 }
00367 /* }}} */
00368 
00369 /* called by PDO to commit a transaction */
00370 static int firebird_handle_commit(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
00371 {
00372        pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
00373 
00374        if (isc_commit_transaction(H->isc_status, &H->tr)) {
00375               RECORD_ERROR(dbh);
00376               return 0;
00377        }
00378        return 1;
00379 }
00380 /* }}} */
00381 
00382 /* called by PDO to rollback a transaction */
00383 static int firebird_handle_rollback(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
00384 {
00385        pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
00386 
00387        if (isc_rollback_transaction(H->isc_status, &H->tr)) {
00388               RECORD_ERROR(dbh);
00389               return 0;
00390        }
00391        return 1;
00392 }
00393 /* }}} */
00394 
00395 /* used by prepare and exec to allocate a statement handle and prepare the SQL */
00396 static int firebird_alloc_prepare_stmt(pdo_dbh_t *dbh, const char *sql, long sql_len, /* {{{ */
00397        XSQLDA *out_sqlda, isc_stmt_handle *s, HashTable *named_params TSRMLS_DC)
00398 {
00399        pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
00400        char *c, *new_sql, in_quote, in_param, pname[64], *ppname;
00401        long l, pindex = -1;
00402               
00403        /* Firebird allows SQL statements up to 64k, so bail if it doesn't fit */
00404        if (sql_len > SHORT_MAX) {
00405               strcpy(dbh->error_code, "01004");
00406               return 0;
00407        }
00408        
00409        /* start a new transaction implicitly if auto_commit is enabled and no transaction is open */
00410        if (dbh->auto_commit && !dbh->in_txn) {
00411               /* dbh->transaction_flags = PDO_TRANS_READ_UNCOMMITTED; */
00412 
00413               if (!firebird_handle_begin(dbh TSRMLS_CC)) {
00414                      return 0;
00415               }
00416               dbh->in_txn = 1;
00417        }
00418        
00419        /* allocate the statement */
00420        if (isc_dsql_allocate_statement(H->isc_status, &H->db, s)) {
00421               RECORD_ERROR(dbh);
00422               return 0;
00423        }
00424        
00425        /* in order to support named params, which Firebird itself doesn't, 
00426           we need to replace :foo by ?, and store the name we just replaced */
00427        new_sql = c = emalloc(sql_len+1);
00428        
00429        for (l = in_quote = in_param = 0; l <= sql_len; ++l) {
00430               if ( !(in_quote ^= (sql[l] == '\''))) {
00431                      if (!in_param) {
00432                             switch (sql[l]) {
00433                                    case ':':
00434                                           in_param = 1;
00435                                           ppname = pname;
00436                                           *ppname++ = sql[l];
00437                                    case '?':
00438                                           *c++ = '?';
00439                                           ++pindex;
00440                                    continue;
00441                             }
00442                      } else {
00443                                 if ((in_param &= ((sql[l] >= 'A' && sql[l] <= 'Z') || (sql[l] >= 'a' && sql[l] <= 'z')
00444                                         || (sql[l] >= '0' && sql[l] <= '9') || sql[l] == '_' || sql[l] == '-'))) { 
00445 
00446                                    
00447                                    *ppname++ = sql[l];
00448                                    continue;
00449                             } else {
00450                                    *ppname++ = 0;
00451                                    if (named_params) {
00452                                           zend_hash_update(named_params, pname, (unsigned int)(ppname-pname),
00453                                                  (void*)&pindex, sizeof(long)+1,NULL);
00454                                    }
00455                             }
00456                      }
00457               }
00458               *c++ = sql[l];
00459        }
00460 
00461        /* prepare the statement */
00462        if (isc_dsql_prepare(H->isc_status, &H->tr, s, 0, new_sql, PDO_FB_DIALECT, out_sqlda)) {
00463               RECORD_ERROR(dbh);
00464               efree(new_sql);
00465               return 0;
00466        }
00467        
00468        efree(new_sql);
00469        return 1;
00470 }
00471 /* }}} */
00472        
00473 /* called by PDO to set a driver-specific dbh attribute */
00474 static int firebird_handle_set_attribute(pdo_dbh_t *dbh, long attr, zval *val TSRMLS_DC) /* {{{ */
00475 {
00476        pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
00477 
00478        switch (attr) {
00479               case PDO_ATTR_AUTOCOMMIT:
00480 
00481                      convert_to_boolean(val);
00482        
00483                      /* ignore if the new value equals the old one */               
00484                      if (dbh->auto_commit ^ Z_BVAL_P(val)) {
00485                             if (dbh->in_txn) {
00486                                    if (Z_BVAL_P(val)) {
00487                                           /* turning on auto_commit with an open transaction is illegal, because
00488                                              we won't know what to do with it */
00489                                           H->last_app_error = "Cannot enable auto-commit while a transaction is already open";
00490                                           return 0;
00491                                    } else {
00492                                           /* close the transaction */
00493                                           if (!firebird_handle_commit(dbh TSRMLS_CC)) {
00494                                                  break;
00495                                           }
00496                                           dbh->in_txn = 0;
00497                                    }
00498                             }
00499                             dbh->auto_commit = Z_BVAL_P(val);
00500                      }
00501                      return 1;
00502 
00503               case PDO_ATTR_FETCH_TABLE_NAMES:
00504                      convert_to_boolean(val);
00505                      H->fetch_table_names = Z_BVAL_P(val);
00506                      return 1;
00507 
00508               case PDO_FB_ATTR_DATE_FORMAT:
00509                      convert_to_string(val);
00510                      if (H->date_format) {
00511                             efree(H->date_format);
00512                      }
00513                      spprintf(&H->date_format, 0, "%s", Z_STRVAL_P(val)); 
00514                      return 1;
00515 
00516               case PDO_FB_ATTR_TIME_FORMAT:
00517                      convert_to_string(val);
00518                      if (H->time_format) {
00519                             efree(H->time_format);
00520                      }
00521                      spprintf(&H->time_format, 0, "%s", Z_STRVAL_P(val)); 
00522                      return 1;
00523 
00524               case PDO_FB_ATTR_TIMESTAMP_FORMAT:
00525                      convert_to_string(val);
00526                      if (H->timestamp_format) {
00527                             efree(H->timestamp_format);
00528                      }
00529                      spprintf(&H->timestamp_format, 0, "%s", Z_STRVAL_P(val)); 
00530                      return 1;
00531        }
00532        return 0;
00533 }
00534 /* }}} */
00535 
00536 /* callback to used to report database server info */
00537 static void firebird_info_cb(void *arg, char const *s) /* {{{ */
00538 {
00539        if (arg) {
00540               if (*(char*)arg) { /* second call */
00541                      strcat(arg, " ");
00542               }
00543               strcat(arg, s);
00544        }
00545 }
00546 /* }}} */
00547 
00548 /* called by PDO to get a driver-specific dbh attribute */
00549 static int firebird_handle_get_attribute(pdo_dbh_t *dbh, long attr, zval *val TSRMLS_DC) /* {{{ */
00550 {
00551        pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
00552 
00553        switch (attr) {
00554               char tmp[512];
00555               
00556               case PDO_ATTR_AUTOCOMMIT:
00557                      ZVAL_LONG(val,dbh->auto_commit);
00558                      return 1;
00559 
00560               case PDO_ATTR_CONNECTION_STATUS:
00561                      ZVAL_BOOL(val, !isc_version(&H->db, firebird_info_cb, NULL));
00562                      return 1;
00563 
00564               case PDO_ATTR_CLIENT_VERSION: {
00565 #if defined(__GNUC__) || defined(PHP_WIN32)
00566                      info_func_t info_func = NULL;
00567 #ifdef __GNUC__
00568                      info_func = (info_func_t)dlsym(RTLD_DEFAULT, "isc_get_client_version");
00569 #else
00570                      HMODULE l = GetModuleHandle("fbclient");
00571 
00572                      if (!l && !(l = GetModuleHandle("gds32"))) {
00573                             break;
00574                      }
00575                      info_func = (info_func_t)GetProcAddress(l, "isc_get_client_version");
00576 #endif
00577                      if (info_func) {
00578                             info_func(tmp);
00579                             ZVAL_STRING(val,tmp,1);
00580                      } else {
00581                             ZVAL_STRING(val,"Firebird 1.0/Interbase 6",1);
00582                      }
00583 #else
00584                      ZVAL_NULL(val);
00585 #endif
00586                      }
00587                      return 1;
00588                      
00589               case PDO_ATTR_SERVER_VERSION:
00590               case PDO_ATTR_SERVER_INFO:
00591                      *tmp = 0;
00592                      
00593                      if (!isc_version(&H->db, firebird_info_cb, (void*)tmp)) {
00594                             ZVAL_STRING(val,tmp,1);
00595                             return 1;
00596                      }
00597                      
00598               case PDO_ATTR_FETCH_TABLE_NAMES:
00599                      ZVAL_BOOL(val, H->fetch_table_names);
00600                      return 1;
00601        }
00602        return 0;
00603 }       
00604 /* }}} */
00605         
00606 /* called by PDO to retrieve driver-specific information about an error that has occurred */
00607 static int pdo_firebird_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC) /* {{{ */
00608 {       
00609        pdo_firebird_db_handle *H = (pdo_firebird_db_handle *)dbh->driver_data;
00610        ISC_STATUS *s = H->isc_status;
00611        char buf[400];
00612        long i = 0, l, sqlcode = isc_sqlcode(s);
00613 
00614        if (sqlcode) {
00615               add_next_index_long(info, sqlcode);
00616 
00617               while ((l = isc_interprete(&buf[i],&s))) {
00618                      i += l;
00619                      strcpy(&buf[i++], " ");
00620               }
00621               add_next_index_string(info, buf, 1);
00622        } else if (H->last_app_error) {
00623               add_next_index_long(info, -999);
00624               add_next_index_string(info, const_cast(H->last_app_error),1);
00625        }
00626        return 1;
00627 }
00628 /* }}} */
00629 
00630 static struct pdo_dbh_methods firebird_methods = { /* {{{ */
00631        firebird_handle_closer,
00632        firebird_handle_preparer,
00633        firebird_handle_doer,
00634        firebird_handle_quoter,
00635        firebird_handle_begin,
00636        firebird_handle_commit,
00637        firebird_handle_rollback,
00638        firebird_handle_set_attribute,
00639        NULL, /* last_id not supported */
00640        pdo_firebird_fetch_error_func,
00641        firebird_handle_get_attribute,
00642        NULL /* check_liveness */
00643 };
00644 /* }}} */
00645 
00646 /* the driver-specific PDO handle constructor */
00647 static int pdo_firebird_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */
00648 {
00649        struct pdo_data_src_parser vars[] = {
00650               { "dbname", NULL, 0 },
00651               { "charset",  NULL,  0 },
00652               { "role", NULL,      0 }
00653        };
00654        int i, ret = 0;
00655        short buf_len = 256, dpb_len;
00656 
00657        pdo_firebird_db_handle *H = dbh->driver_data = pecalloc(1,sizeof(*H),dbh->is_persistent);
00658 
00659        php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 3);
00660        
00661        do {
00662               static char const dpb_flags[] = { 
00663                      isc_dpb_user_name, isc_dpb_password, isc_dpb_lc_ctype, isc_dpb_sql_role_name };
00664               char const *dpb_values[] = { dbh->username, dbh->password, vars[1].optval, vars[2].optval };
00665               char dpb_buffer[256] = { isc_dpb_version1 }, *dpb;
00666               
00667               dpb = dpb_buffer + 1; 
00668 
00669               /* loop through all the provided arguments and set dpb fields accordingly */
00670               for (i = 0; i < sizeof(dpb_flags); ++i) {
00671                      if (dpb_values[i] && buf_len > 0) {
00672                             dpb_len = slprintf(dpb, buf_len, "%c%c%s", dpb_flags[i], (unsigned char)strlen(dpb_values[i]),
00673                                    dpb_values[i]);
00674                             dpb += dpb_len;
00675                             buf_len -= dpb_len;
00676                      }
00677               }
00678               
00679               /* fire it up baby! */
00680               if (isc_attach_database(H->isc_status, 0, vars[0].optval, &H->db,(short)(dpb-dpb_buffer), dpb_buffer)) {
00681                      break;
00682               }
00683               
00684               dbh->methods = &firebird_methods;
00685               dbh->native_case = PDO_CASE_UPPER;
00686               dbh->alloc_own_columns = 1;
00687               
00688               ret = 1;
00689               
00690        } while (0);
00691               
00692        for (i = 0; i < sizeof(vars)/sizeof(vars[0]); ++i) {
00693               if (vars[i].freeme) {
00694                      efree(vars[i].optval);
00695               }
00696        }
00697 
00698        if (!dbh->methods) {
00699               char errmsg[512];
00700               ISC_STATUS *s = H->isc_status;
00701               isc_interprete(errmsg, &s);
00702               zend_throw_exception_ex(php_pdo_get_exception(), H->isc_status[1] TSRMLS_CC, "SQLSTATE[%s] [%d] %s",
00703                             "HY000", H->isc_status[1], errmsg);
00704        }
00705 
00706        if (!ret) {
00707               firebird_handle_closer(dbh TSRMLS_CC);
00708        }
00709 
00710        return ret;
00711 }
00712 /* }}} */
00713 
00714 
00715 pdo_driver_t pdo_firebird_driver = { /* {{{ */
00716        PDO_DRIVER_HEADER(firebird),
00717        pdo_firebird_handle_factory
00718 };
00719 /* }}} */
00720 
00721 /*
00722  * Local variables:
00723  * tab-width: 4
00724  * c-basic-offset: 4
00725  * End:
00726  * vim600: noet sw=4 ts=4 fdm=marker
00727  * vim<600: noet sw=4 ts=4
00728  */