1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Author: Wez Furlong <wez@php.net> |
16 : +----------------------------------------------------------------------+
17 : */
18 :
19 : /* $Id: oci_driver.c 280402 2009-05-12 21:52:54Z mbeccati $ */
20 :
21 : #ifdef HAVE_CONFIG_H
22 : #include "config.h"
23 : #endif
24 :
25 : #include "php.h"
26 : #include "php_ini.h"
27 : #include "ext/standard/info.h"
28 : #include "pdo/php_pdo.h"
29 : #include "pdo/php_pdo_driver.h"
30 : #include "php_pdo_oci.h"
31 : #include "php_pdo_oci_int.h"
32 : #include "Zend/zend_exceptions.h"
33 :
34 : static inline ub4 pdo_oci_sanitize_prefetch(long prefetch);
35 :
36 : static int pdo_oci_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info TSRMLS_DC) /* {{{ */
37 19 : {
38 19 : pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
39 : pdo_oci_error_info *einfo;
40 :
41 19 : einfo = &H->einfo;
42 :
43 19 : if (stmt) {
44 11 : pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
45 :
46 11 : if (S->einfo.errmsg) {
47 6 : einfo = &S->einfo;
48 : }
49 : }
50 :
51 19 : if (einfo->errcode) {
52 12 : add_next_index_long(info, einfo->errcode);
53 12 : add_next_index_string(info, einfo->errmsg, 1);
54 : }
55 :
56 19 : return 1;
57 : }
58 : /* }}} */
59 :
60 : 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) /* {{{ */
61 1705 : {
62 1705 : text errbuf[1024] = "<<Unknown>>";
63 : char tmp_buf[2048];
64 1705 : pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
65 : pdo_oci_error_info *einfo;
66 1705 : pdo_oci_stmt *S = NULL;
67 1705 : pdo_error_type *pdo_err = &dbh->error_code;
68 :
69 1705 : if (stmt) {
70 1445 : S = (pdo_oci_stmt*)stmt->driver_data;
71 1445 : einfo = &S->einfo;
72 1445 : pdo_err = &stmt->error_code;
73 : }
74 : else {
75 260 : einfo = &H->einfo;
76 : }
77 :
78 1705 : if (einfo->errmsg) {
79 168 : pefree(einfo->errmsg, dbh->is_persistent);
80 : }
81 :
82 1705 : einfo->errmsg = NULL;
83 1705 : einfo->errcode = 0;
84 1705 : einfo->file = file;
85 1705 : einfo->line = line;
86 :
87 1705 : if (isinit) { /* Initialization error */
88 1 : strcpy(*pdo_err, "HY000");
89 1 : slprintf(tmp_buf, sizeof(tmp_buf), "%s (%s:%d)", what, file, line);
90 1 : einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
91 : }
92 : else {
93 1704 : switch (status) {
94 : case OCI_SUCCESS:
95 1428 : strcpy(*pdo_err, "00000");
96 1428 : break;
97 : case OCI_ERROR:
98 276 : OCIErrorGet(err, (ub4)1, NULL, &einfo->errcode, errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
99 276 : slprintf(tmp_buf, sizeof(tmp_buf), "%s: %s (%s:%d)", what, errbuf, file, line);
100 276 : einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
101 276 : break;
102 : case OCI_SUCCESS_WITH_INFO:
103 0 : OCIErrorGet(err, (ub4)1, NULL, &einfo->errcode, errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
104 0 : slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_SUCCESS_WITH_INFO: %s (%s:%d)", what, errbuf, file, line);
105 0 : einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
106 0 : break;
107 : case OCI_NEED_DATA:
108 0 : slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_NEED_DATA (%s:%d)", what, file, line);
109 0 : einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
110 0 : break;
111 : case OCI_NO_DATA:
112 0 : slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_NO_DATA (%s:%d)", what, file, line);
113 0 : einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
114 0 : break;
115 : case OCI_INVALID_HANDLE:
116 0 : slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_INVALID_HANDLE (%s:%d)", what, file, line);
117 0 : einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
118 0 : break;
119 : case OCI_STILL_EXECUTING:
120 0 : slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_STILL_EXECUTING (%s:%d)", what, file, line);
121 0 : einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
122 0 : break;
123 : case OCI_CONTINUE:
124 0 : slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_CONTINUE (%s:%d)", what, file, line);
125 0 : einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
126 : break;
127 : }
128 :
129 1704 : if (einfo->errcode) {
130 276 : switch (einfo->errcode) {
131 : case 1013: /* user requested cancel of current operation */
132 0 : zend_bailout();
133 0 : break;
134 :
135 : #if 0
136 : case 955: /* ORA-00955: name is already used by an existing object */
137 : *pdo_err = PDO_ERR_ALREADY_EXISTS;
138 : break;
139 : #endif
140 :
141 : case 12154: /* ORA-12154: TNS:could not resolve service name */
142 0 : strcpy(*pdo_err, "42S02");
143 0 : break;
144 :
145 : case 22: /* ORA-00022: invalid session id */
146 : case 1012: /* ORA-01012: */
147 : case 3113: /* ORA-03133: end of file on communication channel */
148 : case 604:
149 : case 1041:
150 : /* consider the connection closed */
151 0 : dbh->is_closed = 1;
152 0 : H->attached = 0;
153 0 : strcpy(*pdo_err, "01002"); /* FIXME */
154 0 : break;
155 :
156 : default:
157 276 : strcpy(*pdo_err, "HY000");
158 : }
159 : }
160 :
161 1704 : if (stmt) {
162 : /* always propogate the error code back up to the dbh,
163 : * so that we can catch the error information when execute
164 : * is called via query. See Bug #33707 */
165 1445 : if (H->einfo.errmsg) {
166 57 : pefree(H->einfo.errmsg, dbh->is_persistent);
167 : }
168 1445 : H->einfo = *einfo;
169 1445 : H->einfo.errmsg = einfo->errmsg ? pestrdup(einfo->errmsg, dbh->is_persistent) : NULL;
170 1445 : strcpy(dbh->error_code, stmt->error_code);
171 : }
172 : }
173 :
174 : /* little mini hack so that we can use this code from the dbh ctor */
175 1705 : if (!dbh->methods) {
176 1 : zend_throw_exception_ex(php_pdo_get_exception(), 0 TSRMLS_CC, "SQLSTATE[%s]: %s", *pdo_err, einfo->errmsg);
177 : }
178 :
179 1705 : return einfo->errcode;
180 : }
181 : /* }}} */
182 :
183 : static int oci_handle_closer(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
184 97 : {
185 97 : pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
186 :
187 97 : if (H->svc) {
188 : /* rollback any outstanding work */
189 96 : OCITransRollback(H->svc, H->err, 0);
190 : }
191 :
192 97 : if (H->session) {
193 96 : OCIHandleFree(H->session, OCI_HTYPE_SESSION);
194 96 : H->session = NULL;
195 : }
196 :
197 97 : if (H->svc) {
198 96 : OCIHandleFree(H->svc, OCI_HTYPE_SVCCTX);
199 96 : H->svc = NULL;
200 : }
201 :
202 97 : if (H->server && H->attached) {
203 96 : H->last_err = OCIServerDetach(H->server, H->err, OCI_DEFAULT);
204 96 : if (H->last_err) {
205 0 : oci_drv_error("OCIServerDetach");
206 : }
207 96 : H->attached = 0;
208 : }
209 :
210 97 : if (H->server) {
211 96 : OCIHandleFree(H->server, OCI_HTYPE_SERVER);
212 96 : H->server = NULL;
213 : }
214 :
215 97 : OCIHandleFree(H->err, OCI_HTYPE_ERROR);
216 97 : H->err = NULL;
217 :
218 97 : if (H->charset && H->env) {
219 0 : OCIHandleFree(H->env, OCI_HTYPE_ENV);
220 0 : H->env = NULL;
221 : }
222 :
223 97 : if (H->einfo.errmsg) {
224 60 : pefree(H->einfo.errmsg, dbh->is_persistent);
225 60 : H->einfo.errmsg = NULL;
226 : }
227 :
228 97 : pefree(H, dbh->is_persistent);
229 :
230 97 : return 0;
231 : }
232 : /* }}} */
233 :
234 : static int oci_handle_preparer(pdo_dbh_t *dbh, const char *sql, long sql_len, pdo_stmt_t *stmt, zval *driver_options TSRMLS_DC) /* {{{ */
235 97 : {
236 97 : pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
237 97 : pdo_oci_stmt *S = ecalloc(1, sizeof(*S));
238 : ub4 prefetch;
239 97 : char *nsql = NULL;
240 97 : int nsql_len = 0;
241 : int ret;
242 :
243 : #if HAVE_OCISTMTFETCH2
244 97 : S->exec_type = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR,
245 : PDO_CURSOR_FWDONLY TSRMLS_CC) == PDO_CURSOR_SCROLL ?
246 : OCI_STMT_SCROLLABLE_READONLY : OCI_DEFAULT;
247 : #else
248 : S->exec_type = OCI_DEFAULT;
249 : #endif
250 :
251 97 : S->H = H;
252 97 : stmt->supports_placeholders = PDO_PLACEHOLDER_NAMED;
253 97 : ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len TSRMLS_CC);
254 :
255 97 : if (ret == 1) {
256 : /* query was re-written */
257 9 : sql = nsql;
258 9 : sql_len = nsql_len;
259 88 : } else if (ret == -1) {
260 : /* couldn't grok it */
261 0 : strcpy(dbh->error_code, stmt->error_code);
262 0 : efree(S);
263 0 : return 0;
264 : }
265 :
266 : /* create an OCI statement handle */
267 97 : OCIHandleAlloc(H->env, (dvoid*)&S->stmt, OCI_HTYPE_STMT, 0, NULL);
268 :
269 : /* and our own private error handle */
270 97 : OCIHandleAlloc(H->env, (dvoid*)&S->err, OCI_HTYPE_ERROR, 0, NULL);
271 :
272 97 : if (sql_len) {
273 97 : H->last_err = OCIStmtPrepare(S->stmt, H->err, (text*)sql, sql_len, OCI_NTV_SYNTAX, OCI_DEFAULT);
274 97 : if (nsql) {
275 9 : efree(nsql);
276 9 : nsql = NULL;
277 : }
278 97 : if (H->last_err) {
279 0 : H->last_err = oci_drv_error("OCIStmtPrepare");
280 0 : OCIHandleFree(S->stmt, OCI_HTYPE_STMT);
281 0 : OCIHandleFree(S->err, OCI_HTYPE_ERROR);
282 0 : efree(S);
283 0 : return 0;
284 : }
285 :
286 : }
287 :
288 97 : prefetch = pdo_oci_sanitize_prefetch(pdo_attr_lval(driver_options, PDO_ATTR_PREFETCH, PDO_OCI_PREFETCH_DEFAULT TSRMLS_CC));
289 97 : if (prefetch) {
290 97 : H->last_err = OCIAttrSet(S->stmt, OCI_HTYPE_STMT, &prefetch, 0,
291 : OCI_ATTR_PREFETCH_ROWS, H->err);
292 97 : if (!H->last_err) {
293 97 : prefetch *= PDO_OCI_PREFETCH_ROWSIZE;
294 97 : H->last_err = OCIAttrSet(S->stmt, OCI_HTYPE_STMT, &prefetch, 0,
295 : OCI_ATTR_PREFETCH_MEMORY, H->err);
296 : }
297 : }
298 :
299 97 : stmt->driver_data = S;
300 97 : stmt->methods = &oci_stmt_methods;
301 97 : if (nsql) {
302 0 : efree(nsql);
303 0 : nsql = NULL;
304 : }
305 :
306 97 : return 1;
307 : }
308 : /* }}} */
309 :
310 : static long oci_handle_doer(pdo_dbh_t *dbh, const char *sql, long sql_len TSRMLS_DC) /* {{{ */
311 429 : {
312 429 : pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
313 : OCIStmt *stmt;
314 : ub2 stmt_type;
315 : ub4 rowcount;
316 429 : int ret = -1;
317 :
318 429 : OCIHandleAlloc(H->env, (dvoid*)&stmt, OCI_HTYPE_STMT, 0, NULL);
319 :
320 429 : H->last_err = OCIStmtPrepare(stmt, H->err, (text*)sql, sql_len, OCI_NTV_SYNTAX, OCI_DEFAULT);
321 429 : if (H->last_err) {
322 0 : H->last_err = oci_drv_error("OCIStmtPrepare");
323 0 : OCIHandleFree(stmt, OCI_HTYPE_STMT);
324 0 : return -1;
325 : }
326 :
327 429 : H->last_err = OCIAttrGet(stmt, OCI_HTYPE_STMT, &stmt_type, 0, OCI_ATTR_STMT_TYPE, H->err);
328 :
329 429 : if (stmt_type == OCI_STMT_SELECT) {
330 : /* invalid usage; cancel it */
331 0 : OCIHandleFree(stmt, OCI_HTYPE_STMT);
332 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "issuing a SELECT query here is invalid");
333 0 : return -1;
334 : }
335 :
336 : /* now we are good to go */
337 429 : H->last_err = OCIStmtExecute(H->svc, stmt, H->err, 1, 0, NULL, NULL,
338 : (dbh->auto_commit && !dbh->in_txn) ? OCI_COMMIT_ON_SUCCESS : OCI_DEFAULT);
339 :
340 429 : if (H->last_err) {
341 259 : H->last_err = oci_drv_error("OCIStmtExecute");
342 : } else {
343 : /* return the number of affected rows */
344 170 : H->last_err = OCIAttrGet(stmt, OCI_HTYPE_STMT, &rowcount, 0, OCI_ATTR_ROW_COUNT, H->err);
345 170 : ret = rowcount;
346 : }
347 :
348 429 : OCIHandleFree(stmt, OCI_HTYPE_STMT);
349 :
350 429 : return ret;
351 : }
352 : /* }}} */
353 :
354 : 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) /* {{{ */
355 1 : {
356 1 : int qcount = 0;
357 : char const *cu, *l, *r;
358 : char *c;
359 :
360 1 : if (!unquotedlen) {
361 0 : *quotedlen = 2;
362 0 : *quoted = emalloc(*quotedlen+1);
363 0 : strcpy(*quoted, "''");
364 0 : return 1;
365 : }
366 :
367 : /* count single quotes */
368 1 : for (cu = unquoted; (cu = strchr(cu,'\'')); qcount++, cu++)
369 : ; /* empty loop */
370 :
371 1 : *quotedlen = unquotedlen + qcount + 2;
372 1 : *quoted = c = emalloc(*quotedlen+1);
373 1 : *c++ = '\'';
374 :
375 : /* foreach (chunk that ends in a quote) */
376 2 : for (l = unquoted; (r = strchr(l,'\'')); l = r+1) {
377 1 : strncpy(c, l, r-l+1);
378 1 : c += (r-l+1);
379 1 : *c++ = '\''; /* add second quote */
380 : }
381 :
382 : /* Copy remainder and add enclosing quote */
383 1 : strncpy(c, l, *quotedlen-(c-*quoted)-1);
384 1 : (*quoted)[*quotedlen-1] = '\'';
385 1 : (*quoted)[*quotedlen] = '\0';
386 :
387 1 : return 1;
388 : }
389 : /* }}} */
390 :
391 : static int oci_handle_begin(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
392 5 : {
393 : /* with Oracle, there is nothing special to be done */
394 5 : return 1;
395 : }
396 : /* }}} */
397 :
398 : static int oci_handle_commit(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
399 1 : {
400 1 : pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
401 :
402 1 : H->last_err = OCITransCommit(H->svc, H->err, 0);
403 :
404 1 : if (H->last_err) {
405 0 : H->last_err = oci_drv_error("OCITransCommit");
406 0 : return 0;
407 : }
408 1 : return 1;
409 : }
410 : /* }}} */
411 :
412 : static int oci_handle_rollback(pdo_dbh_t *dbh TSRMLS_DC) /* {{{ */
413 2 : {
414 2 : pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
415 :
416 2 : H->last_err = OCITransRollback(H->svc, H->err, 0);
417 :
418 2 : if (H->last_err) {
419 0 : H->last_err = oci_drv_error("OCITransRollback");
420 0 : return 0;
421 : }
422 2 : return 1;
423 : }
424 : /* }}} */
425 :
426 : static int oci_handle_set_attribute(pdo_dbh_t *dbh, long attr, zval *val TSRMLS_DC) /* {{{ */
427 1 : {
428 1 : pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
429 :
430 1 : if (attr == PDO_ATTR_AUTOCOMMIT) {
431 0 : if (dbh->in_txn) {
432 : /* Assume they want to commit whatever is outstanding */
433 0 : H->last_err = OCITransCommit(H->svc, H->err, 0);
434 :
435 0 : if (H->last_err) {
436 0 : H->last_err = oci_drv_error("OCITransCommit");
437 0 : return 0;
438 : }
439 0 : dbh->in_txn = 0;
440 : }
441 :
442 0 : convert_to_long(val);
443 :
444 0 : dbh->auto_commit = Z_LVAL_P(val);
445 0 : return 1;
446 : } else {
447 1 : return 0;
448 : }
449 :
450 : }
451 : /* }}} */
452 :
453 : static int oci_handle_get_attribute(pdo_dbh_t *dbh, long attr, zval *return_value TSRMLS_DC) /* {{{ */
454 0 : {
455 0 : pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
456 :
457 0 : switch (attr) {
458 : case PDO_ATTR_SERVER_VERSION:
459 : case PDO_ATTR_SERVER_INFO:
460 : {
461 : text infostr[512];
462 : char verstr[15];
463 : ub4 vernum;
464 :
465 0 : if (OCIServerRelease(H->svc, H->err, infostr, (ub4)sizeof(infostr), (ub1)OCI_HTYPE_SVCCTX, &vernum))
466 : {
467 0 : ZVAL_STRING(return_value, "<<Unknown>>", 1);
468 : } else {
469 0 : if (attr == PDO_ATTR_SERVER_INFO) {
470 0 : ZVAL_STRING(return_value, (char *)infostr, 1);
471 : } else {
472 0 : slprintf(verstr, sizeof(verstr), "%d.%d.%d.%d.%d",
473 : (int)((vernum>>24) & 0xFF), /* version number */
474 : (int)((vernum>>20) & 0x0F), /* release number*/
475 : (int)((vernum>>12) & 0xFF), /* update number */
476 : (int)((vernum>>8) & 0x0F), /* port release number */
477 : (int)((vernum>>0) & 0xFF)); /* port update number */
478 :
479 0 : ZVAL_STRING(return_value, verstr, 1);
480 : }
481 : }
482 0 : return TRUE;
483 : }
484 :
485 : case PDO_ATTR_CLIENT_VERSION:
486 : {
487 : #if OCI_MAJOR_VERSION > 10 || (OCI_MAJOR_VERSION == 10 && OCI_MINOR_VERSION >= 2)
488 : /* Run time client version */
489 : sword major, minor, update, patch, port_update;
490 : char verstr[15];
491 :
492 0 : OCIClientVersion(&major, &minor, &update, &patch, &port_update);
493 0 : slprintf(verstr, sizeof(verstr), "%d.%d.%d.%d.%d", major, minor, update, patch, port_update);
494 0 : ZVAL_STRING(return_value, verstr, 1);
495 : #elif defined(PHP_PDO_OCI_CLIENT_VERSION)
496 : /* Compile time client version */
497 : ZVAL_STRING(return_value, PHP_PDO_OCI_CLIENT_VERSION, 1);
498 : #else
499 : return FALSE;
500 :
501 : #endif /* Check for OCIClientVersion() support */
502 :
503 0 : return TRUE;
504 : }
505 :
506 : case PDO_ATTR_AUTOCOMMIT:
507 0 : ZVAL_BOOL(return_value, dbh->auto_commit);
508 0 : return TRUE;
509 :
510 : default:
511 0 : return FALSE;
512 :
513 : }
514 : return FALSE;
515 :
516 : }
517 : /* }}} */
518 :
519 : static struct pdo_dbh_methods oci_methods = {
520 : oci_handle_closer,
521 : oci_handle_preparer,
522 : oci_handle_doer,
523 : oci_handle_quoter,
524 : oci_handle_begin,
525 : oci_handle_commit,
526 : oci_handle_rollback,
527 : oci_handle_set_attribute,
528 : NULL,
529 : pdo_oci_fetch_error_func,
530 : oci_handle_get_attribute,
531 : NULL, /* check_liveness */
532 : NULL /* get_driver_methods */
533 : };
534 :
535 : static int pdo_oci_handle_factory(pdo_dbh_t *dbh, zval *driver_options TSRMLS_DC) /* {{{ */
536 100 : {
537 : pdo_oci_db_handle *H;
538 100 : int i, ret = 0;
539 : struct pdo_data_src_parser vars[] = {
540 : { "charset", NULL, 0 },
541 : { "dbname", "", 0 }
542 100 : };
543 :
544 100 : php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 2);
545 :
546 100 : H = pecalloc(1, sizeof(*H), dbh->is_persistent);
547 100 : dbh->driver_data = H;
548 :
549 : /* allocate an environment */
550 : #if HAVE_OCIENVNLSCREATE
551 100 : if (vars[0].optval) {
552 1 : H->charset = OCINlsCharSetNameToId(pdo_oci_Env, (const oratext *)vars[0].optval);
553 1 : if (!H->charset) {
554 1 : oci_init_error("OCINlsCharSetNameToId: unknown character set name");
555 1 : goto cleanup;
556 : } else {
557 0 : if (OCIEnvNlsCreate(&H->env, PDO_OCI_INIT_MODE, 0, NULL, NULL, NULL, 0, NULL, H->charset, H->charset) != OCI_SUCCESS) {
558 0 : oci_init_error("OCIEnvNlsCreate: Check the character set is valid and that PHP has access to Oracle libraries and NLS data");
559 0 : goto cleanup;
560 : }
561 : }
562 : }
563 : #endif
564 99 : if (H->env == NULL) {
565 : /* use the global environment */
566 99 : H->env = pdo_oci_Env;
567 : }
568 :
569 : /* something to hold errors */
570 99 : OCIHandleAlloc(H->env, (dvoid **)&H->err, OCI_HTYPE_ERROR, 0, NULL);
571 :
572 : /* handle for the server */
573 99 : OCIHandleAlloc(H->env, (dvoid **)&H->server, OCI_HTYPE_SERVER, 0, NULL);
574 :
575 99 : H->last_err = OCIServerAttach(H->server, H->err, (text*)vars[1].optval,
576 : strlen(vars[1].optval), OCI_DEFAULT);
577 :
578 99 : if (H->last_err) {
579 0 : oci_drv_error("pdo_oci_handle_factory");
580 0 : goto cleanup;
581 : }
582 :
583 99 : H->attached = 1;
584 :
585 : /* create a service context */
586 99 : H->last_err = OCIHandleAlloc(H->env, (dvoid**)&H->svc, OCI_HTYPE_SVCCTX, 0, NULL);
587 99 : if (H->last_err) {
588 0 : oci_drv_error("OCIHandleAlloc: OCI_HTYPE_SVCCTX");
589 0 : goto cleanup;
590 : }
591 :
592 99 : H->last_err = OCIHandleAlloc(H->env, (dvoid**)&H->session, OCI_HTYPE_SESSION, 0, NULL);
593 99 : if (H->last_err) {
594 0 : oci_drv_error("OCIHandleAlloc: OCI_HTYPE_SESSION");
595 0 : goto cleanup;
596 : }
597 :
598 : /* set server handle into service handle */
599 99 : H->last_err = OCIAttrSet(H->svc, OCI_HTYPE_SVCCTX, H->server, 0, OCI_ATTR_SERVER, H->err);
600 99 : if (H->last_err) {
601 0 : oci_drv_error("OCIAttrSet: OCI_ATTR_SERVER");
602 0 : goto cleanup;
603 : }
604 :
605 : /* username */
606 99 : if (dbh->username) {
607 99 : H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
608 : dbh->username, strlen(dbh->username),
609 : OCI_ATTR_USERNAME, H->err);
610 99 : if (H->last_err) {
611 0 : oci_drv_error("OCIAttrSet: OCI_ATTR_USERNAME");
612 0 : goto cleanup;
613 : }
614 : }
615 :
616 : /* password */
617 99 : if (dbh->password) {
618 99 : H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
619 : dbh->password, strlen(dbh->password),
620 : OCI_ATTR_PASSWORD, H->err);
621 99 : if (H->last_err) {
622 0 : oci_drv_error("OCIAttrSet: OCI_ATTR_PASSWORD");
623 0 : goto cleanup;
624 : }
625 : }
626 :
627 : /* Now fire up the session */
628 99 : H->last_err = OCISessionBegin(H->svc, H->err, H->session, OCI_CRED_RDBMS, OCI_DEFAULT);
629 99 : if (H->last_err) {
630 0 : oci_drv_error("OCISessionBegin");
631 0 : goto cleanup;
632 : }
633 :
634 : /* set the server handle into service handle */
635 99 : H->last_err = OCIAttrSet(H->svc, OCI_HTYPE_SVCCTX, H->session, 0, OCI_ATTR_SESSION, H->err);
636 99 : if (H->last_err) {
637 0 : oci_drv_error("OCIAttrSet: OCI_ATTR_SESSION");
638 0 : goto cleanup;
639 : }
640 :
641 99 : dbh->methods = &oci_methods;
642 99 : dbh->alloc_own_columns = 1;
643 99 : dbh->native_case = PDO_CASE_UPPER;
644 :
645 99 : ret = 1;
646 :
647 100 : cleanup:
648 300 : for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
649 200 : if (vars[i].freeme) {
650 101 : efree(vars[i].optval);
651 : }
652 : }
653 :
654 100 : if (!ret) {
655 1 : oci_handle_closer(dbh TSRMLS_CC);
656 : }
657 :
658 100 : return ret;
659 : }
660 : /* }}} */
661 :
662 : pdo_driver_t pdo_oci_driver = {
663 : PDO_DRIVER_HEADER(oci),
664 : pdo_oci_handle_factory
665 : };
666 :
667 : static inline ub4 pdo_oci_sanitize_prefetch(long prefetch) /* {{{ */
668 97 : {
669 97 : if (prefetch < 0) {
670 0 : prefetch = 0;
671 97 : } else if (prefetch > UB4MAXVAL / PDO_OCI_PREFETCH_ROWSIZE) {
672 0 : prefetch = PDO_OCI_PREFETCH_DEFAULT;
673 : }
674 97 : return ((ub4)prefetch);
675 : }
676 : /* }}} */
677 :
678 : /*
679 : * Local variables:
680 : * tab-width: 4
681 : * c-basic-offset: 4
682 : * End:
683 : * vim600: noet sw=4 ts=4 fdm=marker
684 : * vim<600: noet sw=4 ts=4
685 : */
|