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