Back to index

php5  5.3.10
oci_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: Wez Furlong <wez@php.net>                                    |
00016   +----------------------------------------------------------------------+
00017 */
00018 
00019 /* $Id: oci_driver.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_oci.h"
00031 #include "php_pdo_oci_int.h"
00032 #include "Zend/zend_exceptions.h"
00033 
00034 static inline ub4 pdo_oci_sanitize_prefetch(long prefetch);
00035 
00036 static int pdo_oci_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC) /* {{{ */
00037 {
00038        pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
00039        pdo_oci_error_info *einfo;
00040 
00041        einfo = &H->einfo;
00042 
00043        if (stmt) {
00044               pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
00045 
00046               if (S->einfo.errmsg) {
00047                      einfo = &S->einfo;
00048               }
00049        }
00050 
00051        if (einfo->errcode) {
00052               add_next_index_long(info, einfo->errcode);
00053               add_next_index_string(info, einfo->errmsg, 1);
00054        }
00055 
00056        return 1;
00057 }
00058 /* }}} */
00059 
00060 ub4 _oci_error(OCIError *err, pdo_dbh_t *dbh, pdo_stmt_t *stmt, char *what, sword status, int isinit, const char *file, int line TSRMLS_DC) /* {{{ */
00061 {
00062        text errbuf[1024] = "<<Unknown>>";
00063        char tmp_buf[2048];
00064        pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
00065        pdo_oci_error_info *einfo;
00066        pdo_oci_stmt *S = NULL;
00067        pdo_error_type *pdo_err = &dbh->error_code;
00068 
00069        if (stmt) {
00070               S = (pdo_oci_stmt*)stmt->driver_data;
00071               einfo = &S->einfo;
00072               pdo_err = &stmt->error_code;
00073        }
00074        else {
00075               einfo = &H->einfo;
00076        }
00077 
00078        if (einfo->errmsg) {
00079               pefree(einfo->errmsg, dbh->is_persistent);
00080        }
00081 
00082        einfo->errmsg = NULL;
00083        einfo->errcode = 0;
00084        einfo->file = file;
00085        einfo->line = line;
00086 
00087        if (isinit) { /* Initialization error */
00088               strcpy(*pdo_err, "HY000");
00089               slprintf(tmp_buf, sizeof(tmp_buf), "%s (%s:%d)", what, file, line);
00090               einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
00091        }
00092        else {
00093               switch (status) {
00094                      case OCI_SUCCESS:
00095                             strcpy(*pdo_err, "00000");
00096                             break;
00097                      case OCI_ERROR:
00098                             OCIErrorGet(err, (ub4)1, NULL, &einfo->errcode, errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
00099                             slprintf(tmp_buf, sizeof(tmp_buf), "%s: %s (%s:%d)", what, errbuf, file, line);
00100                             einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
00101                             break;
00102                      case OCI_SUCCESS_WITH_INFO:
00103                             OCIErrorGet(err, (ub4)1, NULL, &einfo->errcode, errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
00104                             slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_SUCCESS_WITH_INFO: %s (%s:%d)", what, errbuf, file, line);
00105                             einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
00106                             break;
00107                      case OCI_NEED_DATA:
00108                             slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_NEED_DATA (%s:%d)", what, file, line);
00109                             einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
00110                             break;
00111                      case OCI_NO_DATA:
00112                             slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_NO_DATA (%s:%d)", what, file, line);
00113                             einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
00114                             break;
00115                      case OCI_INVALID_HANDLE:
00116                             slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_INVALID_HANDLE (%s:%d)", what, file, line);
00117                             einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
00118                             break;
00119                      case OCI_STILL_EXECUTING:
00120                             slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_STILL_EXECUTING (%s:%d)", what, file, line);
00121                             einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
00122                             break;
00123                      case OCI_CONTINUE:
00124                             slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_CONTINUE (%s:%d)", what, file, line);
00125                             einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
00126                             break;
00127               }
00128 
00129               if (einfo->errcode) {
00130                      switch (einfo->errcode) {
00131                             case 1013:    /* user requested cancel of current operation */
00132                                    zend_bailout();
00133                                    break;
00134 
00135 #if 0
00136                             case 955:     /* ORA-00955: name is already used by an existing object */
00137                                    *pdo_err = PDO_ERR_ALREADY_EXISTS;
00138                                    break;
00139 #endif
00140 
00141                             case 12154:   /* ORA-12154: TNS:could not resolve service name */
00142                                    strcpy(*pdo_err, "42S02");
00143                                    break;
00144                             
00145                             case   22:    /* ORA-00022: invalid session id */
00146                             case   378:
00147                             case   602:
00148                             case   603:
00149                             case   604:
00150                             case   609:
00151                             case  1012:   /* ORA-01012: */
00152                             case  1033:
00153                             case  1041:
00154                             case  1043:
00155                             case  1089:
00156                             case  1090:
00157                             case  1092:
00158                             case  3113:   /* ORA-03133: end of file on communication channel */
00159                             case  3114:
00160                             case  3122:
00161                             case  3135:
00162                             case 12153:
00163                             case 27146:
00164                             case 28511:
00165                                    /* consider the connection closed */
00166                                    dbh->is_closed = 1;
00167                                    H->attached = 0;
00168                                    strcpy(*pdo_err, "01002"); /* FIXME */
00169                                    break;
00170 
00171                             default:
00172                                    strcpy(*pdo_err, "HY000");
00173                      }
00174               }
00175 
00176               if (stmt) {
00177                      /* always propogate the error code back up to the dbh,
00178                       * so that we can catch the error information when execute
00179                       * is called via query.  See Bug #33707 */
00180                      if (H->einfo.errmsg) {
00181                             pefree(H->einfo.errmsg, dbh->is_persistent);
00182                      }
00183                      H->einfo = *einfo;
00184                      H->einfo.errmsg = einfo->errmsg ? pestrdup(einfo->errmsg, dbh->is_persistent) : NULL;
00185                      strcpy(dbh->error_code, stmt->error_code);
00186               }
00187        }
00188 
00189        /* little mini hack so that we can use this code from the dbh ctor */
00190        if (!dbh->methods) {
00191               zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode TSRMLS_CC, "SQLSTATE[%s]: %s", *pdo_err, einfo->errmsg);
00192        }
00193 
00194        return einfo->errcode;
00195 }
00196 /* }}} */
00197 
00198 static int oci_handle_closer(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
00199 {
00200        pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
00201 
00202        if (H->svc) {
00203               /* rollback any outstanding work */
00204               OCITransRollback(H->svc, H->err, 0);
00205        }
00206 
00207        if (H->session) {
00208               OCIHandleFree(H->session, OCI_HTYPE_SESSION);
00209               H->session = NULL;
00210        }
00211 
00212        if (H->svc) {
00213               OCIHandleFree(H->svc, OCI_HTYPE_SVCCTX);
00214               H->svc = NULL;
00215        }
00216 
00217        if (H->server && H->attached) {
00218               H->last_err = OCIServerDetach(H->server, H->err, OCI_DEFAULT);
00219               if (H->last_err) {
00220                      oci_drv_error("OCIServerDetach");
00221               }
00222               H->attached = 0;
00223        }
00224 
00225        if (H->server) {
00226               OCIHandleFree(H->server, OCI_HTYPE_SERVER);
00227               H->server = NULL;
00228        }
00229 
00230        OCIHandleFree(H->err, OCI_HTYPE_ERROR);
00231        H->err = NULL;
00232 
00233        if (H->charset && H->env) {
00234               OCIHandleFree(H->env, OCI_HTYPE_ENV);
00235               H->env = NULL;
00236        }
00237 
00238        if (H->einfo.errmsg) {
00239               pefree(H->einfo.errmsg, dbh->is_persistent);
00240               H->einfo.errmsg = NULL;
00241        }
00242 
00243        pefree(H, dbh->is_persistent);
00244 
00245        return 0;
00246 }
00247 /* }}} */
00248 
00249 static int oci_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC) /* {{{ */
00250 {
00251        pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
00252        pdo_oci_stmt *S = ecalloc(1, sizeof(*S));
00253        ub4 prefetch;
00254        char *nsql = NULL;
00255        int nsql_len = 0;
00256        int ret;
00257 
00258 #if HAVE_OCISTMTFETCH2
00259        S->exec_type = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR,
00260               PDO_CURSOR_FWDONLY TSRMLS_CC) == PDO_CURSOR_SCROLL ?
00261               OCI_STMT_SCROLLABLE_READONLY : OCI_DEFAULT;
00262 #else
00263        S->exec_type = OCI_DEFAULT;
00264 #endif
00265 
00266        S->H = H;
00267        stmt->supports_placeholders = PDO_PLACEHOLDER_NAMED;
00268        ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len TSRMLS_CC);
00269 
00270        if (ret == 1) {
00271               /* query was re-written */
00272               sql = nsql;
00273               sql_len = nsql_len;
00274        } else if (ret == -1) {
00275               /* couldn't grok it */
00276               strcpy(dbh->error_code, stmt->error_code);
00277               efree(S);
00278               return 0;
00279        }
00280 
00281        /* create an OCI statement handle */
00282        OCIHandleAlloc(H->env, (dvoid*)&S->stmt, OCI_HTYPE_STMT, 0, NULL);
00283 
00284        /* and our own private error handle */
00285        OCIHandleAlloc(H->env, (dvoid*)&S->err, OCI_HTYPE_ERROR, 0, NULL);
00286 
00287        if (sql_len) {
00288               H->last_err = OCIStmtPrepare(S->stmt, H->err, (text*)sql, sql_len, OCI_NTV_SYNTAX, OCI_DEFAULT);
00289               if (nsql) {
00290                      efree(nsql);
00291                      nsql = NULL;
00292               }
00293               if (H->last_err) {
00294                      H->last_err = oci_drv_error("OCIStmtPrepare");
00295                      OCIHandleFree(S->stmt, OCI_HTYPE_STMT);
00296                      OCIHandleFree(S->err, OCI_HTYPE_ERROR);
00297                      efree(S);
00298                      return 0;
00299               }
00300 
00301        }
00302 
00303        prefetch = pdo_oci_sanitize_prefetch(pdo_attr_lval(driver_options, PDO_ATTR_PREFETCH, PDO_OCI_PREFETCH_DEFAULT TSRMLS_CC));
00304        if (prefetch) {
00305               H->last_err = OCIAttrSet(S->stmt, OCI_HTYPE_STMT, &prefetch, 0,
00306                      OCI_ATTR_PREFETCH_ROWS, H->err);
00307               if (!H->last_err) {
00308                      prefetch *= PDO_OCI_PREFETCH_ROWSIZE;
00309                      H->last_err = OCIAttrSet(S->stmt, OCI_HTYPE_STMT, &prefetch, 0,
00310                             OCI_ATTR_PREFETCH_MEMORY, H->err);
00311               }
00312        }
00313 
00314        stmt->driver_data = S;
00315        stmt->methods = &oci_stmt_methods;
00316        if (nsql) {
00317               efree(nsql);
00318               nsql = NULL;
00319        }
00320 
00321        return 1;
00322 }
00323 /* }}} */
00324 
00325 static long oci_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC) /* {{{ */
00326 {
00327        pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
00328        OCIStmt              *stmt;
00329        ub2 stmt_type;
00330        ub4 rowcount;
00331        int ret = -1;
00332 
00333        OCIHandleAlloc(H->env, (dvoid*)&stmt, OCI_HTYPE_STMT, 0, NULL);
00334 
00335        H->last_err = OCIStmtPrepare(stmt, H->err, (text*)sql, sql_len, OCI_NTV_SYNTAX, OCI_DEFAULT);
00336        if (H->last_err) {
00337               H->last_err = oci_drv_error("OCIStmtPrepare");
00338               OCIHandleFree(stmt, OCI_HTYPE_STMT);
00339               return -1;
00340        }
00341 
00342        H->last_err = OCIAttrGet(stmt, OCI_HTYPE_STMT, &stmt_type, 0, OCI_ATTR_STMT_TYPE, H->err);
00343 
00344        if (stmt_type == OCI_STMT_SELECT) {
00345               /* invalid usage; cancel it */
00346               OCIHandleFree(stmt, OCI_HTYPE_STMT);
00347               php_error_docref(NULL TSRMLS_CC, E_WARNING, "issuing a SELECT query here is invalid");
00348               return -1;
00349        }
00350 
00351        /* now we are good to go */
00352        H->last_err = OCIStmtExecute(H->svc, stmt, H->err, 1, 0, NULL, NULL,
00353                      (dbh->auto_commit && !dbh->in_txn) ? OCI_COMMIT_ON_SUCCESS : OCI_DEFAULT);
00354 
00355        if (H->last_err) {
00356               H->last_err = oci_drv_error("OCIStmtExecute");
00357        } else {
00358               /* return the number of affected rows */
00359               H->last_err = OCIAttrGet(stmt, OCI_HTYPE_STMT, &rowcount, 0, OCI_ATTR_ROW_COUNT, H->err);
00360               ret = rowcount;
00361        }
00362 
00363        OCIHandleFree(stmt, OCI_HTYPE_STMT);
00364 
00365        return ret;
00366 }
00367 /* }}} */
00368 
00369 static int oci_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, int unquotedlen, char **quoted, int *quotedlen, enum pdo_param_type paramtype  TSRMLS_DC) /* {{{ */
00370 {
00371        int qcount = 0;
00372        char const *cu, *l, *r;
00373        char *c;
00374 
00375        if (!unquotedlen) {
00376               *quotedlen = 2;
00377               *quoted = emalloc(*quotedlen+1);
00378               strcpy(*quoted, "''");
00379               return 1;
00380        }
00381 
00382        /* count single quotes */
00383        for (cu = unquoted; (cu = strchr(cu,'\'')); qcount++, cu++)
00384               ; /* empty loop */
00385 
00386        *quotedlen = unquotedlen + qcount + 2;
00387        *quoted = c = emalloc(*quotedlen+1);
00388        *c++ = '\'';
00389        
00390        /* foreach (chunk that ends in a quote) */
00391        for (l = unquoted; (r = strchr(l,'\'')); l = r+1) {
00392               strncpy(c, l, r-l+1);
00393               c += (r-l+1);        
00394               *c++ = '\'';                /* add second quote */
00395        }
00396 
00397     /* Copy remainder and add enclosing quote */ 
00398        strncpy(c, l, *quotedlen-(c-*quoted)-1);
00399        (*quoted)[*quotedlen-1] = '\''; 
00400        (*quoted)[*quotedlen]   = '\0';
00401        
00402        return 1;
00403 }
00404 /* }}} */
00405 
00406 static int oci_handle_begin(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
00407 {
00408        /* with Oracle, there is nothing special to be done */
00409        return 1;
00410 }
00411 /* }}} */
00412 
00413 static int oci_handle_commit(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
00414 {
00415        pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
00416 
00417        H->last_err = OCITransCommit(H->svc, H->err, 0);
00418 
00419        if (H->last_err) {
00420               H->last_err = oci_drv_error("OCITransCommit");
00421               return 0;
00422        }
00423        return 1;
00424 }
00425 /* }}} */
00426 
00427 static int oci_handle_rollback(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
00428 {
00429        pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
00430 
00431        H->last_err = OCITransRollback(H->svc, H->err, 0);
00432 
00433        if (H->last_err) {
00434               H->last_err = oci_drv_error("OCITransRollback");
00435               return 0;
00436        }
00437        return 1;
00438 }
00439 /* }}} */
00440 
00441 static int oci_handle_set_attribute(pdo_dbh_t *dbh, long attr, zval *val TSRMLS_DC) /* {{{ */
00442 {
00443        pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
00444 
00445        if (attr == PDO_ATTR_AUTOCOMMIT) {
00446               if (dbh->in_txn) {
00447                      /* Assume they want to commit whatever is outstanding */
00448                      H->last_err = OCITransCommit(H->svc, H->err, 0);
00449 
00450                      if (H->last_err) {
00451                             H->last_err = oci_drv_error("OCITransCommit");
00452                             return 0;
00453                      }
00454                      dbh->in_txn = 0;
00455               }
00456 
00457               convert_to_long(val);
00458 
00459               dbh->auto_commit = Z_LVAL_P(val);
00460               return 1;
00461        } else {
00462               return 0;
00463        }
00464        
00465 }
00466 /* }}} */
00467 
00468 static int oci_handle_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC)  /* {{{ */
00469 {
00470        pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
00471 
00472        switch (attr) {
00473               case PDO_ATTR_SERVER_VERSION:
00474               case PDO_ATTR_SERVER_INFO:
00475               {
00476                      text infostr[512];
00477                      char verstr[15];
00478                      ub4  vernum;
00479                      
00480                      if (OCIServerRelease(H->svc, H->err, infostr, (ub4)sizeof(infostr), (ub1)OCI_HTYPE_SVCCTX, &vernum))
00481                      {
00482                             ZVAL_STRING(return_value, "<<Unknown>>", 1);
00483                      } else {
00484                             if (attr == PDO_ATTR_SERVER_INFO) {
00485                                    ZVAL_STRING(return_value, (char *)infostr, 1);
00486                             } else {
00487                                    slprintf(verstr, sizeof(verstr), "%d.%d.%d.%d.%d", 
00488                                                   (int)((vernum>>24) & 0xFF),  /* version number */
00489                                                   (int)((vernum>>20) & 0x0F),  /* release number*/
00490                                                   (int)((vernum>>12) & 0xFF),  /* update number */
00491                                                   (int)((vernum>>8)  & 0x0F),  /* port release number */
00492                                                   (int)((vernum>>0)  & 0xFF)); /* port update number */
00493                                    
00494                                    ZVAL_STRING(return_value, verstr, 1);
00495                             }
00496                      }
00497                      return TRUE;
00498               }
00499 
00500               case PDO_ATTR_CLIENT_VERSION:
00501               {
00502 #if OCI_MAJOR_VERSION > 10 || (OCI_MAJOR_VERSION == 10 && OCI_MINOR_VERSION >= 2)
00503                      /* Run time client version */
00504                      sword major, minor, update, patch, port_update;
00505                      char verstr[15];
00506 
00507                      OCIClientVersion(&major, &minor, &update, &patch, &port_update);
00508                      slprintf(verstr, sizeof(verstr), "%d.%d.%d.%d.%d", major, minor, update, patch, port_update);
00509                      ZVAL_STRING(return_value, verstr, 1);
00510 #elif defined(PHP_PDO_OCI_CLIENT_VERSION)
00511                      /* Compile time client version */
00512                      ZVAL_STRING(return_value, PHP_PDO_OCI_CLIENT_VERSION, 1);
00513 #else
00514                      return FALSE;
00515 
00516 #endif /* Check for OCIClientVersion() support */
00517 
00518                      return TRUE;
00519               }
00520 
00521               case PDO_ATTR_AUTOCOMMIT:
00522                      ZVAL_BOOL(return_value, dbh->auto_commit);
00523                      return TRUE;
00524 
00525               default:
00526                      return FALSE;
00527 
00528        }
00529        return FALSE;
00530 
00531 }
00532 /* }}} */
00533 
00534 static int pdo_oci_check_liveness(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
00535 {
00536        pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
00537        sb4 error_code = 0;
00538        char version[256];
00539 
00540        /* TODO move attached check to PDO level */
00541        if (H->attached == 0) {
00542               return FAILURE;
00543        }
00544        /* TODO add persistent_timeout check at PDO level */
00545 
00546 
00547        /* Use OCIPing instead of OCIServerVersion. If OCIPing returns ORA-1010 (invalid OCI operation)
00548         * such as from Pre-10.1 servers, the error is still from the server and we would have
00549         * successfully performed a roundtrip and validated the connection. Use OCIServerVersion for
00550         * Pre-10.2 clients
00551         */    
00552 #if ((OCI_MAJOR_VERSION > 10) || ((OCI_MAJOR_VERSION == 10) && (OCI_MINOR_VERSION >= 2)))  /* OCIPing available 10.2 onwards */
00553        H->last_err = OCIPing (H->svc, H->err, OCI_DEFAULT);
00554 #else
00555        /* use good old OCIServerVersion() */
00556        H->last_err = OCIServerVersion (H->svc, H->err, (text *)version, sizeof(version), OCI_HTYPE_SVCCTX);
00557 #endif
00558        if (H->last_err == OCI_SUCCESS) { 
00559               return SUCCESS;
00560        }
00561 
00562        OCIErrorGet (H->err, (ub4)1, NULL, &error_code, NULL, 0, OCI_HTYPE_ERROR);
00563 
00564        if (error_code == 1010) {
00565               return SUCCESS;
00566        }
00567        return FAILURE;
00568 }
00569 /* }}} */
00570 
00571 static struct pdo_dbh_methods oci_methods = {
00572        oci_handle_closer,
00573        oci_handle_preparer,
00574        oci_handle_doer,
00575        oci_handle_quoter,
00576        oci_handle_begin,
00577        oci_handle_commit,
00578        oci_handle_rollback,
00579        oci_handle_set_attribute,
00580        NULL,
00581        pdo_oci_fetch_error_func,
00582        oci_handle_get_attribute,
00583        pdo_oci_check_liveness,     /* check_liveness */
00584        NULL   /* get_driver_methods */
00585 };
00586 
00587 static int pdo_oci_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */
00588 {
00589        pdo_oci_db_handle *H;
00590        int i, ret = 0;
00591        struct pdo_data_src_parser vars[] = {
00592               { "charset",  NULL,  0 },
00593               { "dbname",   "",    0 }
00594        };
00595 
00596        php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 2);
00597 
00598        H = pecalloc(1, sizeof(*H), dbh->is_persistent);
00599        dbh->driver_data = H;
00600 
00601        /* allocate an environment */
00602 #if HAVE_OCIENVNLSCREATE
00603        if (vars[0].optval) {
00604               H->charset = OCINlsCharSetNameToId(pdo_oci_Env, (const oratext *)vars[0].optval);
00605               if (!H->charset) {
00606                      oci_init_error("OCINlsCharSetNameToId: unknown character set name");
00607                      goto cleanup;
00608               } else {
00609                      if (OCIEnvNlsCreate(&H->env, PDO_OCI_INIT_MODE, 0, NULL, NULL, NULL, 0, NULL, H->charset, H->charset) != OCI_SUCCESS) {
00610                             oci_init_error("OCIEnvNlsCreate: Check the character set is valid and that PHP has access to Oracle libraries and NLS data");
00611                             goto cleanup;
00612                      }
00613               }
00614        }
00615 #endif
00616        if (H->env == NULL) {
00617               /* use the global environment */
00618               H->env = pdo_oci_Env;
00619        }
00620 
00621        /* something to hold errors */
00622        OCIHandleAlloc(H->env, (dvoid **)&H->err, OCI_HTYPE_ERROR, 0, NULL);
00623 
00624        /* handle for the server */
00625        OCIHandleAlloc(H->env, (dvoid **)&H->server, OCI_HTYPE_SERVER, 0, NULL);
00626 
00627        H->last_err = OCIServerAttach(H->server, H->err, (text*)vars[1].optval,
00628                      strlen(vars[1].optval), OCI_DEFAULT);
00629 
00630        if (H->last_err) {
00631               oci_drv_error("pdo_oci_handle_factory");
00632               goto cleanup;
00633        }
00634 
00635        H->attached = 1;
00636 
00637        /* create a service context */
00638        H->last_err = OCIHandleAlloc(H->env, (dvoid**)&H->svc, OCI_HTYPE_SVCCTX, 0, NULL);
00639        if (H->last_err) {
00640               oci_drv_error("OCIHandleAlloc: OCI_HTYPE_SVCCTX");
00641               goto cleanup;
00642        }
00643 
00644        H->last_err = OCIHandleAlloc(H->env, (dvoid**)&H->session, OCI_HTYPE_SESSION, 0, NULL);
00645        if (H->last_err) {
00646               oci_drv_error("OCIHandleAlloc: OCI_HTYPE_SESSION");
00647               goto cleanup;
00648        }
00649 
00650        /* set server handle into service handle */
00651        H->last_err = OCIAttrSet(H->svc, OCI_HTYPE_SVCCTX, H->server, 0, OCI_ATTR_SERVER, H->err);
00652        if (H->last_err) {
00653               oci_drv_error("OCIAttrSet: OCI_ATTR_SERVER");
00654               goto cleanup;
00655        }
00656 
00657        /* username */
00658        if (dbh->username) {
00659               H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
00660                             dbh->username, strlen(dbh->username),
00661                             OCI_ATTR_USERNAME, H->err);
00662               if (H->last_err) {
00663                      oci_drv_error("OCIAttrSet: OCI_ATTR_USERNAME");
00664                      goto cleanup;
00665               }
00666        }
00667 
00668        /* password */
00669        if (dbh->password) {
00670               H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
00671                             dbh->password, strlen(dbh->password),
00672                             OCI_ATTR_PASSWORD, H->err);
00673               if (H->last_err) {
00674                      oci_drv_error("OCIAttrSet: OCI_ATTR_PASSWORD");
00675                      goto cleanup;
00676               }
00677        }
00678 
00679        /* Now fire up the session */
00680        H->last_err = OCISessionBegin(H->svc, H->err, H->session, OCI_CRED_RDBMS, OCI_DEFAULT);
00681        if (H->last_err) {
00682               oci_drv_error("OCISessionBegin");
00683               goto cleanup;
00684        }
00685 
00686        /* set the server handle into service handle */
00687        H->last_err = OCIAttrSet(H->svc, OCI_HTYPE_SVCCTX, H->session, 0, OCI_ATTR_SESSION, H->err);
00688        if (H->last_err) {
00689               oci_drv_error("OCIAttrSet: OCI_ATTR_SESSION");
00690               goto cleanup;
00691        }
00692 
00693        dbh->methods = &oci_methods;
00694        dbh->alloc_own_columns = 1;
00695        dbh->native_case = PDO_CASE_UPPER;
00696 
00697        ret = 1;
00698 
00699 cleanup:
00700        for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
00701               if (vars[i].freeme) {
00702                      efree(vars[i].optval);
00703               }
00704        }
00705 
00706        if (!ret) {
00707               oci_handle_closer(dbh TSRMLS_CC);
00708        }
00709 
00710        return ret;
00711 }
00712 /* }}} */
00713 
00714 pdo_driver_t pdo_oci_driver = {
00715        PDO_DRIVER_HEADER(oci),
00716        pdo_oci_handle_factory
00717 };
00718 
00719 static inline ub4 pdo_oci_sanitize_prefetch(long prefetch) /* {{{ */
00720 {
00721        if (prefetch < 0) {
00722               prefetch = 0;
00723        } else if (prefetch > UB4MAXVAL / PDO_OCI_PREFETCH_ROWSIZE) {
00724               prefetch = PDO_OCI_PREFETCH_DEFAULT;
00725        }
00726        return ((ub4)prefetch);
00727 }
00728 /* }}} */
00729 
00730 
00731 /*
00732  * Local variables:
00733  * tab-width: 4
00734  * c-basic-offset: 4
00735  * End:
00736  * vim600: noet sw=4 ts=4 fdm=marker
00737  * vim<600: noet sw=4 ts=4
00738  */