xref: /PHP-7.3/ext/pdo_oci/oci_driver.c (revision 44ade0e8)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2018 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 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include "php.h"
24 #include "php_ini.h"
25 #include "ext/standard/info.h"
26 #include "pdo/php_pdo.h"
27 #include "pdo/php_pdo_driver.h"
28 #include "php_pdo_oci.h"
29 #include "php_pdo_oci_int.h"
30 #include "Zend/zend_exceptions.h"
31 
32 static inline ub4 pdo_oci_sanitize_prefetch(long prefetch);
33 
pdo_oci_fetch_error_func(pdo_dbh_t * dbh,pdo_stmt_t * stmt,zval * info)34 static int pdo_oci_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info) /* {{{ */
35 {
36 	pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
37 	pdo_oci_error_info *einfo;
38 
39 	einfo = &H->einfo;
40 
41 	if (stmt) {
42 		pdo_oci_stmt *S = (pdo_oci_stmt*)stmt->driver_data;
43 
44 		if (S->einfo.errmsg) {
45 			einfo = &S->einfo;
46 		}
47 	}
48 
49 	if (einfo->errcode) {
50 		add_next_index_long(info, einfo->errcode);
51 		add_next_index_string(info, einfo->errmsg);
52 	}
53 
54 	return 1;
55 }
56 /* }}} */
57 
_oci_error(OCIError * err,pdo_dbh_t * dbh,pdo_stmt_t * stmt,char * what,sword status,int isinit,const char * file,int line)58 ub4 _oci_error(OCIError *err, pdo_dbh_t *dbh, pdo_stmt_t *stmt, char *what, sword status, int isinit, const char *file, int line) /* {{{ */
59 {
60 	text errbuf[1024] = "<<Unknown>>";
61 	char tmp_buf[2048];
62 	pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
63 	pdo_oci_error_info *einfo;
64 	pdo_oci_stmt *S = NULL;
65 	pdo_error_type *pdo_err = &dbh->error_code;
66 
67 	if (stmt) {
68 		S = (pdo_oci_stmt*)stmt->driver_data;
69 		einfo = &S->einfo;
70 		pdo_err = &stmt->error_code;
71 	}
72 	else {
73 		einfo = &H->einfo;
74 	}
75 
76 	if (einfo->errmsg) {
77 		pefree(einfo->errmsg, dbh->is_persistent);
78 	}
79 
80 	einfo->errmsg = NULL;
81 	einfo->errcode = 0;
82 	einfo->file = file;
83 	einfo->line = line;
84 
85 	if (isinit) { /* Initialization error */
86 		strcpy(*pdo_err, "HY000");
87 		slprintf(tmp_buf, sizeof(tmp_buf), "%s (%s:%d)", what, file, line);
88 		einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
89 	}
90 	else {
91 		switch (status) {
92 			case OCI_SUCCESS:
93 				strcpy(*pdo_err, "00000");
94 				break;
95 			case OCI_ERROR:
96 				OCIErrorGet(err, (ub4)1, NULL, &einfo->errcode, errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
97 				slprintf(tmp_buf, sizeof(tmp_buf), "%s: %s (%s:%d)", what, errbuf, file, line);
98 				einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
99 				break;
100 			case OCI_SUCCESS_WITH_INFO:
101 				OCIErrorGet(err, (ub4)1, NULL, &einfo->errcode, errbuf, (ub4)sizeof(errbuf), OCI_HTYPE_ERROR);
102 				slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_SUCCESS_WITH_INFO: %s (%s:%d)", what, errbuf, file, line);
103 				einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
104 				break;
105 			case OCI_NEED_DATA:
106 				slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_NEED_DATA (%s:%d)", what, file, line);
107 				einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
108 				break;
109 			case OCI_NO_DATA:
110 				slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_NO_DATA (%s:%d)", what, file, line);
111 				einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
112 				break;
113 			case OCI_INVALID_HANDLE:
114 				slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_INVALID_HANDLE (%s:%d)", what, file, line);
115 				einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
116 				break;
117 			case OCI_STILL_EXECUTING:
118 				slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_STILL_EXECUTING (%s:%d)", what, file, line);
119 				einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
120 				break;
121 			case OCI_CONTINUE:
122 				slprintf(tmp_buf, sizeof(tmp_buf), "%s: OCI_CONTINUE (%s:%d)", what, file, line);
123 				einfo->errmsg = pestrdup(tmp_buf, dbh->is_persistent);
124 				break;
125 		}
126 
127 		if (einfo->errcode) {
128 			switch (einfo->errcode) {
129 				case 1013:	/* user requested cancel of current operation */
130 					zend_bailout();
131 					break;
132 
133 				case 12154:	/* ORA-12154: TNS:could not resolve service name */
134 					strcpy(*pdo_err, "42S02");
135 					break;
136 
137 				case	22:	/* ORA-00022: invalid session id */
138 				case   378:
139 				case   602:
140 				case   603:
141 				case   604:
142 				case   609:
143 				case  1012:	/* ORA-01012: */
144 				case  1033:
145 				case  1041:
146 				case  1043:
147 				case  1089:
148 				case  1090:
149 				case  1092:
150 				case  3113:	/* ORA-03133: end of file on communication channel */
151 				case  3114:
152 				case  3122:
153 				case  3135:
154 				case 12153:
155 				case 27146:
156 				case 28511:
157 					/* consider the connection closed */
158 					dbh->is_closed = 1;
159 					H->attached = 0;
160 					strcpy(*pdo_err, "01002"); /* FIXME */
161 					break;
162 
163 				default:
164 					strcpy(*pdo_err, "HY000");
165 			}
166 		}
167 
168 		if (stmt) {
169 			/* always propagate the error code back up to the dbh,
170 			 * so that we can catch the error information when execute
171 			 * is called via query.  See Bug #33707 */
172 			if (H->einfo.errmsg) {
173 				pefree(H->einfo.errmsg, dbh->is_persistent);
174 			}
175 			H->einfo = *einfo;
176 			H->einfo.errmsg = einfo->errmsg ? pestrdup(einfo->errmsg, dbh->is_persistent) : NULL;
177 			strcpy(dbh->error_code, stmt->error_code);
178 		}
179 	}
180 
181 	/* little mini hack so that we can use this code from the dbh ctor */
182 	if (!dbh->methods) {
183 		zend_throw_exception_ex(php_pdo_get_exception(), einfo->errcode, "SQLSTATE[%s]: %s", *pdo_err, einfo->errmsg);
184 	}
185 
186 	return einfo->errcode;
187 }
188 /* }}} */
189 
oci_handle_closer(pdo_dbh_t * dbh)190 static int oci_handle_closer(pdo_dbh_t *dbh) /* {{{ */
191 {
192 	pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
193 
194 	if (H->svc) {
195 		/* rollback any outstanding work */
196 		OCITransRollback(H->svc, H->err, 0);
197 	}
198 
199 	if (H->session) {
200 		OCIHandleFree(H->session, OCI_HTYPE_SESSION);
201 		H->session = NULL;
202 	}
203 
204 	if (H->svc) {
205 		OCIHandleFree(H->svc, OCI_HTYPE_SVCCTX);
206 		H->svc = NULL;
207 	}
208 
209 	if (H->server && H->attached) {
210 		H->last_err = OCIServerDetach(H->server, H->err, OCI_DEFAULT);
211 		if (H->last_err) {
212 			oci_drv_error("OCIServerDetach");
213 		}
214 		H->attached = 0;
215 	}
216 
217 	if (H->server) {
218 		OCIHandleFree(H->server, OCI_HTYPE_SERVER);
219 		H->server = NULL;
220 	}
221 
222 	if (H->err) {
223 		OCIHandleFree(H->err, OCI_HTYPE_ERROR);
224 		H->err = NULL;
225 	}
226 
227 	if (H->charset && H->env) {
228 		OCIHandleFree(H->env, OCI_HTYPE_ENV);
229 		H->env = NULL;
230 	}
231 
232 	if (H->einfo.errmsg) {
233 		pefree(H->einfo.errmsg, dbh->is_persistent);
234 		H->einfo.errmsg = NULL;
235 	}
236 
237 	pefree(H, dbh->is_persistent);
238 
239 	return 0;
240 }
241 /* }}} */
242 
oci_handle_preparer(pdo_dbh_t * dbh,const char * sql,size_t sql_len,pdo_stmt_t * stmt,zval * driver_options)243 static int oci_handle_preparer(pdo_dbh_t *dbh, const char *sql, size_t sql_len, pdo_stmt_t *stmt, zval *driver_options) /* {{{ */
244 {
245 	pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
246 	pdo_oci_stmt *S = ecalloc(1, sizeof(*S));
247 	ub4 prefetch;
248 	char *nsql = NULL;
249 	size_t nsql_len = 0;
250 	int ret;
251 
252 #if HAVE_OCISTMTFETCH2
253 	S->exec_type = pdo_attr_lval(driver_options, PDO_ATTR_CURSOR,
254 		PDO_CURSOR_FWDONLY) == PDO_CURSOR_SCROLL ?
255 		OCI_STMT_SCROLLABLE_READONLY : OCI_DEFAULT;
256 #else
257 	S->exec_type = OCI_DEFAULT;
258 #endif
259 
260 	S->H = H;
261 	stmt->supports_placeholders = PDO_PLACEHOLDER_NAMED;
262 	ret = pdo_parse_params(stmt, (char*)sql, sql_len, &nsql, &nsql_len);
263 
264 	if (ret == 1) {
265 		/* query was re-written */
266 		sql = nsql;
267 		sql_len = nsql_len;
268 	} else if (ret == -1) {
269 		/* couldn't grok it */
270 		strcpy(dbh->error_code, stmt->error_code);
271 		efree(S);
272 		return 0;
273 	}
274 
275 	/* create an OCI statement handle */
276 	OCIHandleAlloc(H->env, (dvoid*)&S->stmt, OCI_HTYPE_STMT, 0, NULL);
277 
278 	/* and our own private error handle */
279 	OCIHandleAlloc(H->env, (dvoid*)&S->err, OCI_HTYPE_ERROR, 0, NULL);
280 
281 	if (sql_len) {
282 		H->last_err = OCIStmtPrepare(S->stmt, H->err, (text*)sql, (ub4) sql_len, OCI_NTV_SYNTAX, OCI_DEFAULT);
283 		if (nsql) {
284 			efree(nsql);
285 			nsql = NULL;
286 		}
287 		if (H->last_err) {
288 			H->last_err = oci_drv_error("OCIStmtPrepare");
289 			OCIHandleFree(S->stmt, OCI_HTYPE_STMT);
290 			OCIHandleFree(S->err, OCI_HTYPE_ERROR);
291 			efree(S);
292 			return 0;
293 		}
294 
295 	}
296 
297 	prefetch = H->prefetch;  /* Note 0 is allowed so in future REF CURSORs can be used & then passed with no row loss*/
298 	H->last_err = OCIAttrSet(S->stmt, OCI_HTYPE_STMT, &prefetch, 0,
299 							 OCI_ATTR_PREFETCH_ROWS, H->err);
300 	if (!H->last_err) {
301 		prefetch *= PDO_OCI_PREFETCH_ROWSIZE;
302 		H->last_err = OCIAttrSet(S->stmt, OCI_HTYPE_STMT, &prefetch, 0,
303 								 OCI_ATTR_PREFETCH_MEMORY, H->err);
304 	}
305 
306 	stmt->driver_data = S;
307 	stmt->methods = &oci_stmt_methods;
308 	if (nsql) {
309 		efree(nsql);
310 		nsql = NULL;
311 	}
312 
313 	return 1;
314 }
315 /* }}} */
316 
oci_handle_doer(pdo_dbh_t * dbh,const char * sql,size_t sql_len)317 static zend_long oci_handle_doer(pdo_dbh_t *dbh, const char *sql, size_t sql_len) /* {{{ */
318 {
319 	pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
320 	OCIStmt		*stmt;
321 	ub2 stmt_type;
322 	ub4 rowcount;
323 	int ret = -1;
324 
325 	OCIHandleAlloc(H->env, (dvoid*)&stmt, OCI_HTYPE_STMT, 0, NULL);
326 
327 	H->last_err = OCIStmtPrepare(stmt, H->err, (text*)sql, (ub4) sql_len, OCI_NTV_SYNTAX, OCI_DEFAULT);
328 	if (H->last_err) {
329 		H->last_err = oci_drv_error("OCIStmtPrepare");
330 		OCIHandleFree(stmt, OCI_HTYPE_STMT);
331 		return -1;
332 	}
333 
334 	H->last_err = OCIAttrGet(stmt, OCI_HTYPE_STMT, &stmt_type, 0, OCI_ATTR_STMT_TYPE, H->err);
335 
336 	if (stmt_type == OCI_STMT_SELECT) {
337 		/* invalid usage; cancel it */
338 		OCIHandleFree(stmt, OCI_HTYPE_STMT);
339 		php_error_docref(NULL, E_WARNING, "issuing a SELECT query here is invalid");
340 		return -1;
341 	}
342 
343 	/* now we are good to go */
344 	H->last_err = OCIStmtExecute(H->svc, stmt, H->err, 1, 0, NULL, NULL,
345 			(dbh->auto_commit && !dbh->in_txn) ? OCI_COMMIT_ON_SUCCESS : OCI_DEFAULT);
346 
347 	if (H->last_err) {
348 		H->last_err = oci_drv_error("OCIStmtExecute");
349 	} else {
350 		/* return the number of affected rows */
351 		H->last_err = OCIAttrGet(stmt, OCI_HTYPE_STMT, &rowcount, 0, OCI_ATTR_ROW_COUNT, H->err);
352 		ret = rowcount;
353 	}
354 
355 	OCIHandleFree(stmt, OCI_HTYPE_STMT);
356 
357 	return ret;
358 }
359 /* }}} */
360 
oci_handle_quoter(pdo_dbh_t * dbh,const char * unquoted,size_t unquotedlen,char ** quoted,size_t * quotedlen,enum pdo_param_type paramtype)361 static int oci_handle_quoter(pdo_dbh_t *dbh, const char *unquoted, size_t unquotedlen, char **quoted, size_t *quotedlen, enum pdo_param_type paramtype ) /* {{{ */
362 {
363 	int qcount = 0;
364 	char const *cu, *l, *r;
365 	char *c;
366 
367 	if (!unquotedlen) {
368 		*quotedlen = 2;
369 		*quoted = emalloc(*quotedlen+1);
370 		strcpy(*quoted, "''");
371 		return 1;
372 	}
373 
374 	/* count single quotes */
375 	for (cu = unquoted; (cu = strchr(cu,'\'')); qcount++, cu++)
376 		; /* empty loop */
377 
378 	*quotedlen = unquotedlen + qcount + 2;
379 	*quoted = c = emalloc(*quotedlen+1);
380 	*c++ = '\'';
381 
382 	/* foreach (chunk that ends in a quote) */
383 	for (l = unquoted; (r = strchr(l,'\'')); l = r+1) {
384 		strncpy(c, l, r-l+1);
385 		c += (r-l+1);
386 		*c++ = '\'';			/* add second quote */
387 	}
388 
389     /* Copy remainder and add enclosing quote */
390 	strncpy(c, l, *quotedlen-(c-*quoted)-1);
391 	(*quoted)[*quotedlen-1] = '\'';
392 	(*quoted)[*quotedlen]   = '\0';
393 
394 	return 1;
395 }
396 /* }}} */
397 
oci_handle_begin(pdo_dbh_t * dbh)398 static int oci_handle_begin(pdo_dbh_t *dbh) /* {{{ */
399 {
400 	/* with Oracle, there is nothing special to be done */
401 	return 1;
402 }
403 /* }}} */
404 
oci_handle_commit(pdo_dbh_t * dbh)405 static int oci_handle_commit(pdo_dbh_t *dbh) /* {{{ */
406 {
407 	pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
408 
409 	H->last_err = OCITransCommit(H->svc, H->err, 0);
410 
411 	if (H->last_err) {
412 		H->last_err = oci_drv_error("OCITransCommit");
413 		return 0;
414 	}
415 	return 1;
416 }
417 /* }}} */
418 
oci_handle_rollback(pdo_dbh_t * dbh)419 static int oci_handle_rollback(pdo_dbh_t *dbh) /* {{{ */
420 {
421 	pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
422 
423 	H->last_err = OCITransRollback(H->svc, H->err, 0);
424 
425 	if (H->last_err) {
426 		H->last_err = oci_drv_error("OCITransRollback");
427 		return 0;
428 	}
429 	return 1;
430 }
431 /* }}} */
432 
oci_handle_set_attribute(pdo_dbh_t * dbh,zend_long attr,zval * val)433 static int oci_handle_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val) /* {{{ */
434 {
435 	zend_long lval = zval_get_long(val);
436 	pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
437 
438 	switch (attr) {
439 		case PDO_ATTR_AUTOCOMMIT:
440 		{
441 			if (dbh->in_txn) {
442 				/* Assume they want to commit whatever is outstanding */
443 				H->last_err = OCITransCommit(H->svc, H->err, 0);
444 
445 				if (H->last_err) {
446 					H->last_err = oci_drv_error("OCITransCommit");
447 					return 0;
448 				}
449 				dbh->in_txn = 0;
450 			}
451 
452 			dbh->auto_commit = (unsigned int)lval? 1 : 0;
453 			return 1;
454 		}
455 		case PDO_ATTR_PREFETCH:
456 		{
457 			H->prefetch = pdo_oci_sanitize_prefetch(lval);
458 			return 1;
459 		}
460 		case PDO_OCI_ATTR_ACTION:
461 		{
462 #if (OCI_MAJOR_VERSION >= 10)
463 			zend_string *action = zval_get_string(val);
464 
465 			H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
466 				(dvoid *) ZSTR_VAL(action), (ub4) ZSTR_LEN(action),
467 				OCI_ATTR_ACTION, H->err);
468 			if (H->last_err) {
469 				oci_drv_error("OCIAttrSet: OCI_ATTR_ACTION");
470 				return 0;
471 			}
472 			return 1;
473 #else
474 			oci_drv_error("Unsupported attribute type");
475 			return 0;
476 #endif
477 		}
478 		case PDO_OCI_ATTR_CLIENT_INFO:
479 		{
480 #if (OCI_MAJOR_VERSION >= 10)
481 			zend_string *client_info = zval_get_string(val);
482 
483 			H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
484 				(dvoid *) ZSTR_VAL(client_info), (ub4) ZSTR_LEN(client_info),
485 				OCI_ATTR_CLIENT_INFO, H->err);
486 			if (H->last_err) {
487 				oci_drv_error("OCIAttrSet: OCI_ATTR_CLIENT_INFO");
488 				return 0;
489 			}
490 			return 1;
491 #else
492 			oci_drv_error("Unsupported attribute type");
493 			return 0;
494 #endif
495 		}
496 		case PDO_OCI_ATTR_CLIENT_IDENTIFIER:
497 		{
498 #if (OCI_MAJOR_VERSION >= 10)
499 			zend_string *identifier = zval_get_string(val);
500 
501 			H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
502 				(dvoid *) ZSTR_VAL(identifier), (ub4) ZSTR_LEN(identifier),
503 				OCI_ATTR_CLIENT_IDENTIFIER, H->err);
504 			if (H->last_err) {
505 				oci_drv_error("OCIAttrSet: OCI_ATTR_CLIENT_IDENTIFIER");
506 				return 0;
507 			}
508 			return 1;
509 #else
510 			oci_drv_error("Unsupported attribute type");
511 			return 0;
512 #endif
513 		}
514 		case PDO_OCI_ATTR_MODULE:
515 		{
516 #if (OCI_MAJOR_VERSION >= 10)
517 			zend_string *module = zval_get_string(val);
518 
519 			H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
520 				(dvoid *) ZSTR_VAL(module), (ub4) ZSTR_LEN(module),
521 				OCI_ATTR_MODULE, H->err);
522 			if (H->last_err) {
523 				oci_drv_error("OCIAttrSet: OCI_ATTR_MODULE");
524 				return 0;
525 			}
526 			return 1;
527 #else
528 			oci_drv_error("Unsupported attribute type");
529 			return 0;
530 #endif
531 		}
532 		default:
533 			return 0;
534 	}
535 
536 }
537 /* }}} */
538 
oci_handle_get_attribute(pdo_dbh_t * dbh,zend_long attr,zval * return_value)539 static int oci_handle_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value)  /* {{{ */
540 {
541 	pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
542 
543 	switch (attr) {
544 		case PDO_ATTR_SERVER_VERSION:
545 		case PDO_ATTR_SERVER_INFO:
546 		{
547 			text infostr[512];
548 			char verstr[15];
549 			ub4  vernum;
550 
551 			if (OCIServerRelease(H->svc, H->err, infostr, (ub4)sizeof(infostr), (ub1)OCI_HTYPE_SVCCTX, &vernum))
552 			{
553 				ZVAL_STRING(return_value, "<<Unknown>>");
554 			} else {
555 				if (attr == PDO_ATTR_SERVER_INFO) {
556 					ZVAL_STRING(return_value, (char *)infostr);
557 				} else {
558 					slprintf(verstr, sizeof(verstr), "%d.%d.%d.%d.%d",
559 							 (int)((vernum>>24) & 0xFF),  /* version number */
560 							 (int)((vernum>>20) & 0x0F),  /* release number*/
561 							 (int)((vernum>>12) & 0xFF),  /* update number */
562 							 (int)((vernum>>8)  & 0x0F),  /* port release number */
563 							 (int)((vernum>>0)  & 0xFF)); /* port update number */
564 
565 					ZVAL_STRING(return_value, verstr);
566 				}
567 			}
568 			return TRUE;
569 		}
570 
571 		case PDO_ATTR_CLIENT_VERSION:
572 		{
573 #if OCI_MAJOR_VERSION > 10 || (OCI_MAJOR_VERSION == 10 && OCI_MINOR_VERSION >= 2)
574 			/* Run time client version */
575 			sword major, minor, update, patch, port_update;
576 			char verstr[15];
577 
578 			OCIClientVersion(&major, &minor, &update, &patch, &port_update);
579 			slprintf(verstr, sizeof(verstr), "%d.%d.%d.%d.%d", major, minor, update, patch, port_update);
580 			ZVAL_STRING(return_value, verstr);
581 #elif defined(PHP_PDO_OCI_CLIENT_VERSION)
582 			/* Compile time client version */
583 			ZVAL_STRING(return_value, PHP_PDO_OCI_CLIENT_VERSION);
584 #else
585 			return FALSE;
586 
587 #endif /* Check for OCIClientVersion() support */
588 
589 			return TRUE;
590 		}
591 
592 		case PDO_ATTR_AUTOCOMMIT:
593 			ZVAL_BOOL(return_value, dbh->auto_commit);
594 			return TRUE;
595 
596 		case PDO_ATTR_PREFETCH:
597 			ZVAL_LONG(return_value, H->prefetch);
598 			return TRUE;
599 		default:
600 			return FALSE;
601 
602 	}
603 	return FALSE;
604 
605 }
606 /* }}} */
607 
pdo_oci_check_liveness(pdo_dbh_t * dbh)608 static int pdo_oci_check_liveness(pdo_dbh_t *dbh) /* {{{ */
609 {
610 	pdo_oci_db_handle *H = (pdo_oci_db_handle *)dbh->driver_data;
611 	sb4 error_code = 0;
612 #if (!((OCI_MAJOR_VERSION > 10) || ((OCI_MAJOR_VERSION == 10) && (OCI_MINOR_VERSION >= 2))))
613 	char version[256];
614 #endif
615 
616 	/* TODO move attached check to PDO level */
617 	if (H->attached == 0) {
618 		return FAILURE;
619 	}
620 	/* TODO add persistent_timeout check at PDO level */
621 
622 
623 	/* Use OCIPing instead of OCIServerVersion. If OCIPing returns ORA-1010 (invalid OCI operation)
624 	 * such as from Pre-10.1 servers, the error is still from the server and we would have
625 	 * successfully performed a roundtrip and validated the connection. Use OCIServerVersion for
626 	 * Pre-10.2 clients
627 	 */
628 #if ((OCI_MAJOR_VERSION > 10) || ((OCI_MAJOR_VERSION == 10) && (OCI_MINOR_VERSION >= 2)))	/* OCIPing available 10.2 onwards */
629 	H->last_err = OCIPing (H->svc, H->err, OCI_DEFAULT);
630 #else
631 	/* use good old OCIServerVersion() */
632 	H->last_err = OCIServerVersion (H->svc, H->err, (text *)version, sizeof(version), OCI_HTYPE_SVCCTX);
633 #endif
634 	if (H->last_err == OCI_SUCCESS) {
635 		return SUCCESS;
636 	}
637 
638 	OCIErrorGet (H->err, (ub4)1, NULL, &error_code, NULL, 0, OCI_HTYPE_ERROR);
639 
640 	if (error_code == 1010) {
641 		return SUCCESS;
642 	}
643 	return FAILURE;
644 }
645 /* }}} */
646 
647 static const struct pdo_dbh_methods oci_methods = {
648 	oci_handle_closer,
649 	oci_handle_preparer,
650 	oci_handle_doer,
651 	oci_handle_quoter,
652 	oci_handle_begin,
653 	oci_handle_commit,
654 	oci_handle_rollback,
655 	oci_handle_set_attribute,
656 	NULL,
657 	pdo_oci_fetch_error_func,
658 	oci_handle_get_attribute,
659 	pdo_oci_check_liveness,	/* check_liveness */
660 	NULL	/* get_driver_methods */
661 };
662 
pdo_oci_handle_factory(pdo_dbh_t * dbh,zval * driver_options)663 static int pdo_oci_handle_factory(pdo_dbh_t *dbh, zval *driver_options) /* {{{ */
664 {
665 	pdo_oci_db_handle *H;
666 	int i, ret = 0;
667 	struct pdo_data_src_parser vars[] = {
668 		{ "charset",  NULL,	0 },
669 		{ "dbname",   "",	0 }
670 	};
671 
672 	php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 2);
673 
674 	H = pecalloc(1, sizeof(*H), dbh->is_persistent);
675 	dbh->driver_data = H;
676 
677 	dbh->skip_param_evt =
678 		1 << PDO_PARAM_EVT_FETCH_PRE |
679 		1 << PDO_PARAM_EVT_FETCH_POST |
680 		1 << PDO_PARAM_EVT_NORMALIZE;
681 
682 	H->prefetch = PDO_OCI_PREFETCH_DEFAULT;
683 
684 	/* allocate an environment */
685 #if HAVE_OCIENVNLSCREATE
686 	if (vars[0].optval) {
687 		H->charset = OCINlsCharSetNameToId(pdo_oci_Env, (const oratext *)vars[0].optval);
688 		if (!H->charset) {
689 			oci_init_error("OCINlsCharSetNameToId: unknown character set name");
690 			goto cleanup;
691 		} else {
692 			if (OCIEnvNlsCreate(&H->env, PDO_OCI_INIT_MODE, 0, NULL, NULL, NULL, 0, NULL, H->charset, H->charset) != OCI_SUCCESS) {
693 				oci_init_error("OCIEnvNlsCreate: Check the character set is valid and that PHP has access to Oracle libraries and NLS data");
694 				goto cleanup;
695 			}
696 		}
697 	}
698 #endif
699 	if (H->env == NULL) {
700 		/* use the global environment */
701 		H->env = pdo_oci_Env;
702 	}
703 
704 	/* something to hold errors */
705 	OCIHandleAlloc(H->env, (dvoid **)&H->err, OCI_HTYPE_ERROR, 0, NULL);
706 
707 	/* handle for the server */
708 	OCIHandleAlloc(H->env, (dvoid **)&H->server, OCI_HTYPE_SERVER, 0, NULL);
709 
710 	H->last_err = OCIServerAttach(H->server, H->err, (text*)vars[1].optval,
711 		   	(sb4) strlen(vars[1].optval), OCI_DEFAULT);
712 
713 	if (H->last_err) {
714 		oci_drv_error("pdo_oci_handle_factory");
715 		goto cleanup;
716 	}
717 
718 	H->attached = 1;
719 
720 	/* create a service context */
721 	H->last_err = OCIHandleAlloc(H->env, (dvoid**)&H->svc, OCI_HTYPE_SVCCTX, 0, NULL);
722 	if (H->last_err) {
723 		oci_drv_error("OCIHandleAlloc: OCI_HTYPE_SVCCTX");
724 		goto cleanup;
725 	}
726 
727 	H->last_err = OCIHandleAlloc(H->env, (dvoid**)&H->session, OCI_HTYPE_SESSION, 0, NULL);
728 	if (H->last_err) {
729 		oci_drv_error("OCIHandleAlloc: OCI_HTYPE_SESSION");
730 		goto cleanup;
731 	}
732 
733 	/* set server handle into service handle */
734 	H->last_err = OCIAttrSet(H->svc, OCI_HTYPE_SVCCTX, H->server, 0, OCI_ATTR_SERVER, H->err);
735 	if (H->last_err) {
736 		oci_drv_error("OCIAttrSet: OCI_ATTR_SERVER");
737 		goto cleanup;
738 	}
739 
740 	/* username */
741 	if (dbh->username) {
742 		H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
743 			   	dbh->username, (ub4) strlen(dbh->username),
744 				OCI_ATTR_USERNAME, H->err);
745 		if (H->last_err) {
746 			oci_drv_error("OCIAttrSet: OCI_ATTR_USERNAME");
747 			goto cleanup;
748 		}
749 	}
750 
751 	/* password */
752 	if (dbh->password) {
753 		H->last_err = OCIAttrSet(H->session, OCI_HTYPE_SESSION,
754 			   	dbh->password, (ub4) strlen(dbh->password),
755 				OCI_ATTR_PASSWORD, H->err);
756 		if (H->last_err) {
757 			oci_drv_error("OCIAttrSet: OCI_ATTR_PASSWORD");
758 			goto cleanup;
759 		}
760 	}
761 
762 	/* Now fire up the session */
763 	H->last_err = OCISessionBegin(H->svc, H->err, H->session, OCI_CRED_RDBMS, OCI_DEFAULT);
764 	if (H->last_err) {
765 		oci_drv_error("OCISessionBegin");
766 		goto cleanup;
767 	}
768 
769 	/* set the server handle into service handle */
770 	H->last_err = OCIAttrSet(H->svc, OCI_HTYPE_SVCCTX, H->session, 0, OCI_ATTR_SESSION, H->err);
771 	if (H->last_err) {
772 		oci_drv_error("OCIAttrSet: OCI_ATTR_SESSION");
773 		goto cleanup;
774 	}
775 
776 	/* Get max character width */
777  	H->last_err = OCINlsNumericInfoGet(H->env, H->err, &H->max_char_width, OCI_NLS_CHARSET_MAXBYTESZ);
778  	if (H->last_err) {
779  		oci_drv_error("OCINlsNumericInfoGet: OCI_NLS_CHARSET_MAXBYTESZ");
780  		goto cleanup;
781  	}
782 
783 	dbh->methods = &oci_methods;
784 	dbh->alloc_own_columns = 1;
785 	dbh->native_case = PDO_CASE_UPPER;
786 
787 	ret = 1;
788 
789 cleanup:
790 	for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
791 		if (vars[i].freeme) {
792 			efree(vars[i].optval);
793 		}
794 	}
795 
796 	if (!ret) {
797 		oci_handle_closer(dbh);
798 	}
799 
800 	return ret;
801 }
802 /* }}} */
803 
804 const pdo_driver_t pdo_oci_driver = {
805 	PDO_DRIVER_HEADER(oci),
806 	pdo_oci_handle_factory
807 };
808 
pdo_oci_sanitize_prefetch(long prefetch)809 static inline ub4 pdo_oci_sanitize_prefetch(long prefetch) /* {{{ */
810 {
811 	if (prefetch < 0) {
812 		prefetch = 0;
813 	} else if (prefetch > UB4MAXVAL / PDO_OCI_PREFETCH_ROWSIZE) {
814 		prefetch = PDO_OCI_PREFETCH_DEFAULT;
815 	}
816 	return ((ub4)prefetch);
817 }
818 /* }}} */
819 
820 
821 /*
822  * Local variables:
823  * tab-width: 4
824  * c-basic-offset: 4
825  * End:
826  * vim600: noet sw=4 ts=4 fdm=marker
827  * vim<600: noet sw=4 ts=4
828  */
829