xref: /PHP-7.1/ext/oci8/oci8_statement.c (revision 91954c24)
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    | Authors: Stig Sæther Bakken <ssb@php.net>                            |
16    |          Thies C. Arntzen <thies@thieso.net>                         |
17    |                                                                      |
18    | Collection support by Andy Sautins <asautins@veripost.net>           |
19    | Temporary LOB support by David Benson <dbenson@mancala.com>          |
20    | ZTS per process OCIPLogon by Harald Radi <harald.radi@nme.at>        |
21    |                                                                      |
22    | Redesigned by: Antony Dovgal <antony@zend.com>                       |
23    |                Andi Gutmans <andi@zend.com>                          |
24    |                Wez Furlong <wez@omniti.com>                          |
25    +----------------------------------------------------------------------+
26 */
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include "php.h"
33 #include "ext/standard/info.h"
34 #include "php_ini.h"
35 
36 #if HAVE_OCI8
37 
38 #include "php_oci8.h"
39 #include "php_oci8_int.h"
40 
41 #if defined(OCI_MAJOR_VERSION) && (OCI_MAJOR_VERSION > 10) && \
42 	(defined(__x86_64__) || defined(__LP64__) || defined(_LP64) || defined(_WIN64))
43 typedef ub8 oci_phpsized_int;
44 #else
45 typedef ub4 oci_phpsized_int;
46 #endif
47 
48 /* {{{ php_oci_statement_create()
49  Create statemend handle and allocate necessary resources */
php_oci_statement_create(php_oci_connection * connection,char * query,int query_len)50 php_oci_statement *php_oci_statement_create(php_oci_connection *connection, char *query, int query_len)
51 {
52 	php_oci_statement *statement;
53 	sword errstatus;
54 
55 	connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
56 
57 	statement = ecalloc(1,sizeof(php_oci_statement));
58 
59 	if (!query_len) {
60 		/* do not allocate stmt handle for refcursors, we'll get it from OCIStmtPrepare2() */
61 		PHP_OCI_CALL(OCIHandleAlloc, (connection->env, (dvoid **)&(statement->stmt), OCI_HTYPE_STMT, 0, NULL));
62 	}
63 
64 	PHP_OCI_CALL(OCIHandleAlloc, (connection->env, (dvoid **)&(statement->err), OCI_HTYPE_ERROR, 0, NULL));
65 
66 	if (query_len > 0) {
67 		PHP_OCI_CALL_RETURN(errstatus, OCIStmtPrepare2,
68 				(
69 				 connection->svc,
70 				 &(statement->stmt),
71 				 connection->err,
72 				 (text *)query,
73 				 query_len,
74 				 NULL,
75 				 0,
76 				 OCI_NTV_SYNTAX,
77 				 OCI_DEFAULT
78 				)
79 		);
80 #ifdef HAVE_OCI8_DTRACE
81 		if (DTRACE_OCI8_SQLTEXT_ENABLED()) {
82 			DTRACE_OCI8_SQLTEXT(connection, connection->client_id, statement, query);
83 		}
84 #endif /* HAVE_OCI8_DTRACE */
85 
86 		if (errstatus != OCI_SUCCESS) {
87 			connection->errcode = php_oci_error(connection->err, errstatus);
88 
89 			PHP_OCI_CALL(OCIStmtRelease, (statement->stmt, statement->err, NULL, 0, OCI_STRLS_CACHE_DELETE));
90 			PHP_OCI_CALL(OCIHandleFree,(statement->err, OCI_HTYPE_ERROR));
91 			PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
92 			efree(statement);
93 			return NULL;
94 		}
95 	}
96 
97 	if (query && query_len) {
98 		statement->last_query = ecalloc(1, query_len + 1);
99 		memcpy(statement->last_query, query, query_len);
100 		statement->last_query_len = query_len;
101 	}
102 	else {
103 		statement->last_query = NULL;
104 		statement->last_query_len = 0;
105 	}
106 
107 	statement->connection = connection;
108 	statement->has_data = 0;
109 	statement->has_descr = 0;
110 	statement->parent_stmtid = 0;
111 	statement->impres_child_stmt = NULL;
112 	statement->impres_count = 0;
113 	statement->impres_flag = PHP_OCI_IMPRES_UNKNOWN;  /* may or may not have Implicit Result Set children */
114 #if PHP_VERSION_ID < 70300
115 	++GC_REFCOUNT(statement->connection->id);
116 #else
117 	GC_ADDREF(statement->connection->id);
118 #endif
119 
120 	if (OCI_G(default_prefetch) >= 0) {
121 		php_oci_statement_set_prefetch(statement, (ub4)OCI_G(default_prefetch));
122 	} else {
123 		php_oci_statement_set_prefetch(statement, (ub4)100); /* semi-arbitrary, "sensible default" */
124 	}
125 
126 	PHP_OCI_REGISTER_RESOURCE(statement, le_statement);
127 
128 	OCI_G(num_statements)++;
129 
130 	return statement;
131 }
132 /* }}} */
133 
134 /* {{{ php_oci_get_implicit_resultset()
135    Fetch implicit result set statement resource */
php_oci_get_implicit_resultset(php_oci_statement * statement)136 php_oci_statement *php_oci_get_implicit_resultset(php_oci_statement *statement)
137 {
138 #if (OCI_MAJOR_VERSION < 12)
139 	php_error_docref(NULL, E_WARNING, "Implicit results are available in Oracle Database 12c onwards");
140 	return NULL;
141 #else
142 	void *result;
143 	ub4   rtype;
144 	php_oci_statement *statement2;  /* implicit result set statement handle */
145 	sword errstatus;
146 
147 	PHP_OCI_CALL_RETURN(errstatus, OCIStmtGetNextResult, (statement->stmt, statement->err, &result, &rtype, OCI_DEFAULT));
148 	if (errstatus == OCI_NO_DATA) {
149 		return NULL;
150 	}
151 
152 	if (rtype != OCI_RESULT_TYPE_SELECT) {
153 		/* Only OCI_RESULT_TYPE_SELECT is supported by Oracle DB 12cR1 */
154 		php_error_docref(NULL, E_WARNING, "Unexpected implicit result type returned from Oracle Database");
155 		return NULL;
156 	} else {
157 		statement2 = ecalloc(1,sizeof(php_oci_statement));
158 
159 		PHP_OCI_CALL(OCIHandleAlloc, (statement->connection->env, (dvoid **)&(statement2->err), OCI_HTYPE_ERROR, 0, NULL));
160 		statement2->stmt = (OCIStmt *)result;
161 		statement2->parent_stmtid = statement->id;
162 		statement2->impres_child_stmt = NULL;
163 		statement2->impres_count = 0;
164 		statement2->impres_flag = PHP_OCI_IMPRES_IS_CHILD;
165 		statement2->connection = statement->connection;
166 		statement2->errcode = 0;
167 		statement2->last_query = NULL;
168 		statement2->last_query_len = 0;
169 		statement2->columns = NULL;
170 		statement2->binds = NULL;
171 		statement2->defines = NULL;
172 		statement2->ncolumns = 0;
173 		statement2->executed = 0;
174 		statement2->has_data = 0;
175 		statement2->has_descr = 0;
176 		statement2->stmttype = 0;
177 
178 #if PHP_VERSION_ID < 70300
179 		GC_REFCOUNT(statement->id)++;
180 		GC_REFCOUNT(statement2->connection->id)++;
181 #else
182 		GC_ADDREF(statement->id);
183 		GC_ADDREF(statement2->connection->id);
184 #endif
185 
186 		php_oci_statement_set_prefetch(statement2, statement->prefetch_count);
187 
188 		PHP_OCI_REGISTER_RESOURCE(statement2, le_statement);
189 
190 		OCI_G(num_statements)++;
191 
192 		return statement2;
193 	}
194 #endif /* OCI_MAJOR_VERSION < 12 */
195 }
196 /* }}} */
197 
198 /* {{{ php_oci_statement_set_prefetch()
199  Set prefetch buffer size for the statement */
php_oci_statement_set_prefetch(php_oci_statement * statement,ub4 prefetch)200 int php_oci_statement_set_prefetch(php_oci_statement *statement, ub4 prefetch )
201 {
202 	sword errstatus;
203 
204 	if (prefetch > 20000) {
205 		prefetch = 20000;		/* keep it somewhat sane */
206 	}
207 
208 	PHP_OCI_CALL_RETURN(errstatus, OCIAttrSet, (statement->stmt, OCI_HTYPE_STMT, &prefetch, 0, OCI_ATTR_PREFETCH_ROWS, statement->err));
209 
210 	if (errstatus != OCI_SUCCESS) {
211 		statement->errcode = php_oci_error(statement->err, errstatus);
212 		PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
213 		statement->prefetch_count = 0;
214 		return 1;
215 	}
216 	statement->prefetch_count = prefetch;
217 	statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
218 	return 0;
219 }
220 /* }}} */
221 
222 /* {{{ php_oci_cleanup_pre_fetch()
223    Helper function to cleanup ref-cursors and descriptors from the previous row */
php_oci_cleanup_pre_fetch(zval * data)224 int php_oci_cleanup_pre_fetch(zval *data)
225 {
226 	php_oci_out_column *outcol = (php_oci_out_column*) Z_PTR_P(data);
227 
228 	if (!outcol->is_descr && !outcol->is_cursor)
229 		return ZEND_HASH_APPLY_KEEP;
230 
231 	switch(outcol->data_type) {
232 		case SQLT_CLOB:
233 		case SQLT_BLOB:
234 		case SQLT_RDD:
235 		case SQLT_BFILE:
236 			if (outcol->descid) {
237 				zend_list_delete(outcol->descid);
238 				outcol->descid = 0;
239 			}
240 			break;
241 		case SQLT_RSET:
242 			if (outcol->stmtid) {
243 				zend_list_delete(outcol->stmtid);
244 				outcol->stmtid = 0;
245 				outcol->nested_statement = NULL;
246 			}
247 			break;
248 		default:
249 			break;
250 	}
251 	return ZEND_HASH_APPLY_KEEP;
252 
253 }
254 /* }}} */
255 
256 /* {{{ php_oci_statement_fetch()
257  Fetch a row from the statement */
php_oci_statement_fetch(php_oci_statement * statement,ub4 nrows)258 int php_oci_statement_fetch(php_oci_statement *statement, ub4 nrows)
259 {
260 	int i;
261 	void *handlepp;
262 	ub4 typep, iterp, idxp;
263 	ub1 in_outp, piecep;
264 	zend_bool piecewisecols = 0;
265 	php_oci_out_column *column;
266 	sword errstatus;
267 
268 	statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
269 
270 	if (statement->has_descr && statement->columns) {
271 		zend_hash_apply(statement->columns, php_oci_cleanup_pre_fetch);
272     }
273 
274 	PHP_OCI_CALL_RETURN(errstatus, OCIStmtFetch, (statement->stmt, statement->err, nrows, OCI_FETCH_NEXT, OCI_DEFAULT));
275 
276 	if (errstatus == OCI_NO_DATA || nrows == 0) {
277 		if (statement->last_query == NULL) {
278 			/* reset define-list for refcursors */
279 			if (statement->columns) {
280 				zend_hash_destroy(statement->columns);
281 				efree(statement->columns);
282 				statement->columns = NULL;
283 				statement->ncolumns = 0;
284 			}
285 			statement->executed = 0;
286 		}
287 
288 		statement->has_data = 0;
289 
290 		if (nrows == 0) {
291 			/* this is exactly what we requested */
292 			return 0;
293 		}
294 		return 1;
295 	}
296 
297 	/* reset length for all piecewise columns */
298 	for (i = 0; i < statement->ncolumns; i++) {
299 		column = php_oci_statement_get_column(statement, i + 1, NULL, 0);
300 		if (column && column->piecewise) {
301 			column->retlen4 = 0;
302 			piecewisecols = 1;
303 		}
304 	}
305 
306 	while (errstatus == OCI_NEED_DATA) {
307 		if (piecewisecols) {
308 			PHP_OCI_CALL_RETURN(errstatus,
309 				OCIStmtGetPieceInfo,
310 				   (
311 					statement->stmt,
312 					statement->err,
313 					&handlepp,
314 					&typep,
315 					&in_outp,
316 					&iterp,
317 					&idxp,
318 					&piecep
319 				   )
320 			);
321 
322 			/* scan through our columns for a piecewise column with a matching handle */
323 			for (i = 0; i < statement->ncolumns; i++) {
324 				column = php_oci_statement_get_column(statement, i + 1, NULL, 0);
325 				if (column && column->piecewise && handlepp == column->oci_define)   {
326 					if (!column->data) {
327 						column->data = (text *) ecalloc(1, PHP_OCI_PIECE_SIZE + 1);
328 					} else {
329 						column->data = erealloc(column->data, column->retlen4 + PHP_OCI_PIECE_SIZE + 1);
330 					}
331 					column->cb_retlen = PHP_OCI_PIECE_SIZE;
332 
333 					/* and instruct fetch to fetch waiting piece into our buffer */
334 					PHP_OCI_CALL(OCIStmtSetPieceInfo,
335 						   (
336 							(void *) column->oci_define,
337 							OCI_HTYPE_DEFINE,
338 							statement->err,
339 							((char*)column->data) + column->retlen4,
340 							&(column->cb_retlen),
341 							piecep,
342 							&column->indicator,
343 							&column->retcode
344 						   )
345 					);
346 				}
347 			}
348 		}
349 
350 		PHP_OCI_CALL_RETURN(errstatus, OCIStmtFetch, (statement->stmt, statement->err, nrows, OCI_FETCH_NEXT, OCI_DEFAULT));
351 
352 		if (piecewisecols) {
353 			for (i = 0; i < statement->ncolumns; i++) {
354 				column = php_oci_statement_get_column(statement, i + 1, NULL, 0);
355 				if (column && column->piecewise && handlepp == column->oci_define)	{
356 					column->retlen4 += column->cb_retlen;
357 				}
358 			}
359 		}
360 	}
361 
362 	if (errstatus == OCI_SUCCESS_WITH_INFO || errstatus == OCI_SUCCESS) {
363 		statement->has_data = 1;
364 
365 		/* do the stuff needed for OCIDefineByName */
366 		for (i = 0; i < statement->ncolumns; i++) {
367 			column = php_oci_statement_get_column(statement, i + 1, NULL, 0);
368 			if (column == NULL) {
369 				continue;
370 			}
371 
372 			if (!column->define) {
373 				continue;
374 			}
375 
376 			ZEND_ASSERT(Z_ISREF(column->define->val));
377 			zval_ptr_dtor(Z_REFVAL(column->define->val));
378 			ZVAL_NULL(Z_REFVAL(column->define->val));
379 			php_oci_column_to_zval(column, Z_REFVAL(column->define->val), 0);
380 		}
381 
382 		return 0;
383 	}
384 
385 	statement->errcode = php_oci_error(statement->err, errstatus);
386 	PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
387 
388 	statement->has_data = 0;
389 
390 	return 1;
391 }
392 /* }}} */
393 
394 /* {{{ php_oci_statement_get_column()
395  Get column from the result set */
php_oci_statement_get_column(php_oci_statement * statement,zend_long column_index,char * column_name,int column_name_len)396 php_oci_out_column *php_oci_statement_get_column(php_oci_statement *statement, zend_long column_index, char *column_name, int column_name_len)
397 {
398 	php_oci_out_column *column = NULL;
399 	int i;
400 
401 	if (statement->columns == NULL) { /* we release the columns at the end of a fetch */
402 		return NULL;
403 	}
404 
405 	if (column_name) {
406 		for (i = 0; i < statement->ncolumns; i++) {
407 			column = php_oci_statement_get_column(statement, i + 1, NULL, 0);
408 			if (column == NULL) {
409 				continue;
410 			} else if (((int) column->name_len == column_name_len) && (!strncmp(column->name, column_name, column_name_len))) {
411 				return column;
412 			}
413 		}
414 	} else if (column_index != -1) {
415 		if ((column = zend_hash_index_find_ptr(statement->columns, column_index)) == NULL) {
416 			return NULL;
417 		}
418 		return column;
419 	}
420 
421 	return NULL;
422 }
423 /* }}} */
424 
425 /* {{{ php_oci_define_callback() */
php_oci_define_callback(dvoid * ctx,OCIDefine * define,ub4 iter,dvoid ** bufpp,ub4 ** alenpp,ub1 * piecep,dvoid ** indpp,ub2 ** rcpp)426 sb4 php_oci_define_callback(dvoid *ctx, OCIDefine *define, ub4 iter, dvoid **bufpp, ub4 **alenpp, ub1 *piecep, dvoid **indpp, ub2 **rcpp)
427 {
428 	php_oci_out_column *outcol = (php_oci_out_column *)ctx;
429 
430 	if (!outcol) {
431 
432 		php_error_docref(NULL, E_WARNING, "Invalid context pointer value");
433 		return OCI_ERROR;
434 	}
435 
436 	switch(outcol->data_type) {
437 		case SQLT_RSET: {
438 				php_oci_statement *nested_stmt;
439 
440 				nested_stmt = php_oci_statement_create(outcol->statement->connection, NULL, 0);
441 				if (!nested_stmt) {
442 					return OCI_ERROR;
443 				}
444 				nested_stmt->parent_stmtid = outcol->statement->id;
445 #if PHP_VERSION_ID < 70300
446 				++GC_REFCOUNT(outcol->statement->id);
447 #else
448 				GC_ADDREF(outcol->statement->id);
449 #endif
450 				outcol->nested_statement = nested_stmt;
451 				outcol->stmtid = nested_stmt->id;
452 
453 				*bufpp = nested_stmt->stmt;
454 				*alenpp = &(outcol->retlen4);
455 				*piecep = OCI_ONE_PIECE;
456 				*indpp = &(outcol->indicator);
457 				*rcpp = &(outcol->retcode);
458 				return OCI_CONTINUE;
459 			}
460 			break;
461 		case SQLT_RDD:
462 		case SQLT_BLOB:
463 		case SQLT_CLOB:
464 		case SQLT_BFILE: {
465 				php_oci_descriptor *descr;
466 				int dtype;
467 
468 				if (outcol->data_type == SQLT_BFILE) {
469 					dtype = OCI_DTYPE_FILE;
470 				} else if (outcol->data_type == SQLT_RDD ) {
471 					dtype = OCI_DTYPE_ROWID;
472 				} else {
473 					dtype = OCI_DTYPE_LOB;
474 				}
475 
476 				descr = php_oci_lob_create(outcol->statement->connection, dtype);
477 				if (!descr) {
478 					return OCI_ERROR;
479 				}
480 				outcol->descid = descr->id;
481 				descr->charset_form = outcol->charset_form;
482 
483 				*bufpp = descr->descriptor;
484 				*alenpp = &(outcol->retlen4);
485 				*piecep = OCI_ONE_PIECE;
486 				*indpp = &(outcol->indicator);
487 				*rcpp = &(outcol->retcode);
488 
489 				return OCI_CONTINUE;
490 			}
491 			break;
492 	}
493 	return OCI_ERROR;
494 }
495 /* }}} */
496 
497 /* {{{ php_oci_statement_execute()
498  Execute statement */
php_oci_statement_execute(php_oci_statement * statement,ub4 mode)499 int php_oci_statement_execute(php_oci_statement *statement, ub4 mode)
500 {
501 	php_oci_out_column *outcol;
502 	OCIParam *param = NULL;
503 	text *colname;
504 	ub4 counter;
505 	ub2 define_type;
506 	ub4 iters;
507 	ub4 colcount;
508 	ub2 dynamic;
509 	dvoid *buf;
510 	sword errstatus;
511 
512 	switch (mode) {
513 		case OCI_COMMIT_ON_SUCCESS:
514 		case OCI_DESCRIBE_ONLY:
515 		case OCI_DEFAULT:
516 			/* only these are allowed */
517 #ifdef HAVE_OCI8_DTRACE
518 			if (DTRACE_OCI8_EXECUTE_MODE_ENABLED()) {
519 				DTRACE_OCI8_EXECUTE_MODE(statement->connection, statement->connection->client_id, statement, mode);
520 			}
521 #endif /* HAVE_OCI8_DTRACE */
522 			break;
523 		default:
524 			php_error_docref(NULL, E_WARNING, "Invalid execute mode given: %d", mode);
525 			return 1;
526 			break;
527 	}
528 
529 	if (!statement->stmttype) {
530 		/* get statement type */
531 		PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, (ub2 *)&statement->stmttype, (ub4 *)0, OCI_ATTR_STMT_TYPE, statement->err));
532 
533 		if (errstatus != OCI_SUCCESS) {
534 			statement->errcode = php_oci_error(statement->err, errstatus);
535 			PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
536 			return 1;
537 		} else {
538 			statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
539 		}
540 	}
541 
542 	if (statement->stmttype == OCI_STMT_SELECT) {
543 		iters = 0;
544 	} else {
545 		iters = 1;
546 	}
547 
548 	if (statement->last_query) { /* Don't execute REFCURSORS or Implicit Result Set handles */
549 
550 		if (statement->binds) {
551 			int result = 0;
552 			zend_hash_apply_with_argument(statement->binds, php_oci_bind_pre_exec, (void *)&result);
553 			if (result) {
554 				return 1;
555 			}
556 		}
557 
558 		/* execute statement */
559 		PHP_OCI_CALL_RETURN(errstatus, OCIStmtExecute, (statement->connection->svc, statement->stmt, statement->err, iters, 0, NULL, NULL, mode));
560 
561 		if (errstatus != OCI_SUCCESS) {
562 			statement->errcode = php_oci_error(statement->err, errstatus);
563 			PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
564 			return 1;
565 		}
566 
567 		if (statement->binds) {
568 			zend_hash_apply(statement->binds, php_oci_bind_post_exec);
569 		}
570 
571 		if (mode & OCI_COMMIT_ON_SUCCESS) {
572 			/* No need to rollback on disconnect */
573 			statement->connection->rb_on_disconnect = 0;
574 		} else if (statement->stmttype != OCI_STMT_SELECT) {
575 			/* Assume some uncommitted DML occurred */
576 			statement->connection->rb_on_disconnect = 1;
577 		}
578 		/* else for SELECT with OCI_NO_AUTO_COMMIT, leave
579 		 * "rb_on_disconnect" at its previous value.  SELECT can't
580 		 * initiate uncommitted DML. (An AUTONOMOUS_TRANSACTION in
581 		 * invoked PL/SQL must explicitly rollback/commit else the
582 		 * SELECT fails).
583 		 */
584 
585 		statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
586 	}
587 
588 	if (statement->stmttype == OCI_STMT_SELECT && statement->executed == 0) {
589 		/* we only need to do the define step is this very statement is executed the first time! */
590 		statement->executed = 1;
591 
592 		ALLOC_HASHTABLE(statement->columns);
593 		zend_hash_init(statement->columns, 13, NULL, php_oci_column_hash_dtor, 0);
594 
595 		counter = 1;
596 
597 		/* get number of columns */
598 		PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, (dvoid *)&colcount, (ub4 *)0, OCI_ATTR_PARAM_COUNT, statement->err));
599 
600 		if (errstatus != OCI_SUCCESS) {
601 			statement->errcode = php_oci_error(statement->err, errstatus);
602 			PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
603 			return 1;
604 		}
605 
606 		statement->ncolumns = colcount;
607 
608 		for (counter = 1; counter <= colcount; counter++) {
609 			outcol = (php_oci_out_column *) ecalloc(1, sizeof(php_oci_out_column));
610 
611 #if PHP_VERSION_ID < 70300
612 			if ((outcol = zend_hash_index_update_ptr(statement->columns, counter, outcol)) == NULL) {
613 				FREE_HASHTABLE(statement->columns);
614 				/* out of memory */
615 				return 1;
616 			}
617 #else
618 			outcol = zend_hash_index_update_ptr(statement->columns, counter, outcol);
619 #endif
620 
621 			/* get column */
622 			PHP_OCI_CALL_RETURN(errstatus, OCIParamGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, statement->err, (dvoid**)&param, counter));
623 
624 			if (errstatus != OCI_SUCCESS) {
625 				statement->errcode = php_oci_error(statement->err, errstatus);
626 				PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
627 				return 1;
628 			}
629 
630 			/* get column datatype */
631 			PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->data_type, (ub4 *)0, OCI_ATTR_DATA_TYPE, statement->err));
632 
633 			if (errstatus != OCI_SUCCESS) {
634 				PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
635 				statement->errcode = php_oci_error(statement->err, errstatus);
636 				PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
637 				return 1;
638 			}
639 
640 			/* get character set form  */
641 			PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->charset_form, (ub4 *)0, OCI_ATTR_CHARSET_FORM, statement->err));
642 
643 			if (errstatus != OCI_SUCCESS) {
644 				PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
645 				statement->errcode = php_oci_error(statement->err, errstatus);
646 				PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
647 				return 1;
648 			}
649 
650 			/* get character set id	 */
651 			PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->charset_id, (ub4 *)0, OCI_ATTR_CHARSET_ID, statement->err));
652 
653 			if (errstatus != OCI_SUCCESS) {
654 				PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
655 				statement->errcode = php_oci_error(statement->err, errstatus);
656 				PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
657 				return 1;
658 			}
659 
660 			/* get size of the column */
661 			PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->data_size, (dvoid *)0, OCI_ATTR_DATA_SIZE, statement->err));
662 
663 			if (errstatus != OCI_SUCCESS) {
664 				PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
665 				statement->errcode = php_oci_error(statement->err, errstatus);
666 				PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
667 				return 1;
668 			}
669 
670 			outcol->storage_size4 = outcol->data_size;
671 			outcol->retlen = outcol->data_size;
672 
673 			/* get scale of the column */
674 			PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->scale, (dvoid *)0, OCI_ATTR_SCALE, statement->err));
675 
676 			if (errstatus != OCI_SUCCESS) {
677 				PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
678 				statement->errcode = php_oci_error(statement->err, errstatus);
679 				PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
680 				return 1;
681 			}
682 
683 			/* get precision of the column */
684 			PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid *)&outcol->precision, (dvoid *)0, OCI_ATTR_PRECISION, statement->err));
685 
686 			if (errstatus != OCI_SUCCESS) {
687 				PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
688 				statement->errcode = php_oci_error(statement->err, errstatus);
689 				PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
690 				return 1;
691 			}
692 
693 			/* get name of the column */
694 			PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)param, OCI_DTYPE_PARAM, (dvoid **)&colname, (ub4 *)&outcol->name_len, (ub4)OCI_ATTR_NAME, statement->err));
695 
696 			if (errstatus != OCI_SUCCESS) {
697 				PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
698 				statement->errcode = php_oci_error(statement->err, errstatus);
699 				PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
700 				return 1;
701 			}
702 			PHP_OCI_CALL(OCIDescriptorFree, (param, OCI_DTYPE_PARAM));
703 
704 			outcol->name = ecalloc(1, outcol->name_len + 1);
705 			memcpy(outcol->name, colname, outcol->name_len);
706 
707 			/* find a user-set define */
708 			if (statement->defines) {
709 				if ((outcol->define = zend_hash_str_find_ptr(statement->defines, outcol->name, outcol->name_len)) != NULL) {
710 					if (outcol->define->type) {
711 						outcol->data_type = outcol->define->type;
712 					}
713 				}
714 			}
715 
716 			buf = 0;
717 			switch (outcol->data_type) {
718 				case SQLT_RSET:
719 					outcol->statement = statement; /* parent handle */
720 
721 					define_type = SQLT_RSET;
722 					outcol->is_cursor = 1;
723 					outcol->statement->has_descr = 1;
724 					outcol->storage_size4 = -1;
725 					outcol->retlen = -1;
726 					dynamic = OCI_DYNAMIC_FETCH;
727 					break;
728 
729 				case SQLT_RDD:	 /* ROWID */
730 				case SQLT_BLOB:	 /* binary LOB */
731 				case SQLT_CLOB:	 /* character LOB */
732 				case SQLT_BFILE: /* binary file LOB */
733 					outcol->statement = statement; /* parent handle */
734 
735 					define_type = outcol->data_type;
736 					outcol->is_descr = 1;
737 					outcol->statement->has_descr = 1;
738 					outcol->storage_size4 = -1;
739 					outcol->chunk_size = 0;
740 					dynamic = OCI_DYNAMIC_FETCH;
741 					break;
742 
743 				case SQLT_LNG:
744 				case SQLT_LBI:
745 					if (outcol->data_type == SQLT_LBI) {
746 						define_type = SQLT_BIN;
747 					} else {
748 						define_type = SQLT_CHR;
749 					}
750 					outcol->storage_size4 = PHP_OCI_MAX_DATA_SIZE;
751 					outcol->piecewise = 1;
752 					dynamic = OCI_DYNAMIC_FETCH;
753 					break;
754 
755 				case SQLT_BIN:
756 				default:
757 					define_type = SQLT_CHR;
758 					if (outcol->data_type == SQLT_BIN) {
759 						define_type = SQLT_BIN;
760 					}
761 					if ((outcol->data_type == SQLT_DAT) || (outcol->data_type == SQLT_NUM)
762 #ifdef SQLT_TIMESTAMP
763 						|| (outcol->data_type == SQLT_TIMESTAMP)
764 #endif
765 #ifdef SQLT_TIMESTAMP_TZ
766 						|| (outcol->data_type == SQLT_TIMESTAMP_TZ)
767 #endif
768 #ifdef SQLT_TIMESTAMP_LTZ
769 						|| (outcol->data_type == SQLT_TIMESTAMP_LTZ)
770 #endif
771 #ifdef SQLT_INTERVAL_YM
772 						|| (outcol->data_type == SQLT_INTERVAL_YM)
773 #endif
774 #ifdef SQLT_INTERVAL_DS
775 						|| (outcol->data_type == SQLT_INTERVAL_DS)
776 #endif
777 						) {
778 						outcol->storage_size4 = 512; /* XXX this should fit "most" NLS date-formats and Numbers */
779 #if defined(SQLT_IBFLOAT) && defined(SQLT_IBDOUBLE)
780 					} else if (outcol->data_type == SQLT_IBFLOAT || outcol->data_type == SQLT_IBDOUBLE) {
781 						outcol->storage_size4 = 1024;
782 #endif
783 					} else {
784 						outcol->storage_size4++; /* add one for string terminator */
785 					}
786 
787 					outcol->storage_size4 *= 3;
788 
789 					dynamic = OCI_DEFAULT;
790 					buf = outcol->data = (text *) safe_emalloc(1, outcol->storage_size4, 0);
791 					memset(buf, 0, outcol->storage_size4);
792 					break;
793 			}
794 
795 			if (dynamic == OCI_DYNAMIC_FETCH) {
796 				PHP_OCI_CALL_RETURN(errstatus,
797 					OCIDefineByPos,
798 					(
799 						statement->stmt,							/* IN/OUT handle to the requested SQL query */
800 						(OCIDefine **)&outcol->oci_define,			/* IN/OUT pointer to a pointer to a define handle */
801 						statement->err,								/* IN/OUT An error handle  */
802 						counter,									/* IN	  position in the select list */
803 						(dvoid *)NULL,								/* IN/OUT pointer to a buffer */
804 						outcol->storage_size4,						/* IN	  The size of each valuep buffer in bytes */
805 						define_type,								/* IN	  The data type */
806 						(dvoid *)&outcol->indicator,				/* IN	  pointer to an indicator variable or arr */
807 						(ub2 *)NULL,								/* IN/OUT Pointer to array of length of data fetched */
808 						(ub2 *)NULL,								/* OUT	  Pointer to array of column-level return codes */
809 						OCI_DYNAMIC_FETCH							/* IN	  mode (OCI_DEFAULT, OCI_DYNAMIC_FETCH) */
810 					)
811 				);
812 
813 			} else {
814 				PHP_OCI_CALL_RETURN(errstatus,
815 					OCIDefineByPos,
816 					(
817 						statement->stmt,							/* IN/OUT handle to the requested SQL query */
818 						(OCIDefine **)&outcol->oci_define,			/* IN/OUT pointer to a pointer to a define handle */
819 						statement->err,								/* IN/OUT An error handle  */
820 						counter,									/* IN	  position in the select list */
821 						(dvoid *)buf,								/* IN/OUT pointer to a buffer */
822 						outcol->storage_size4,						/* IN	  The size of each valuep buffer in bytes */
823 						define_type,								/* IN	  The data type */
824 						(dvoid *)&outcol->indicator,				/* IN	  pointer to an indicator variable or arr */
825 						(ub2 *)&outcol->retlen,						/* IN/OUT Pointer to array of length of data fetched */
826 						(ub2 *)&outcol->retcode,					/* OUT	  Pointer to array of column-level return codes */
827 						OCI_DEFAULT									/* IN	  mode (OCI_DEFAULT, OCI_DYNAMIC_FETCH) */
828 					)
829 				);
830 
831 			}
832 
833 			if (errstatus != OCI_SUCCESS) {
834 				statement->errcode = php_oci_error(statement->err, errstatus);
835 				PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
836 				return 1;
837 			}
838 
839 			/* additional OCIDefineDynamic() call */
840 			switch (outcol->data_type) {
841 				case SQLT_RSET:
842 				case SQLT_RDD:
843 				case SQLT_BLOB:
844 				case SQLT_CLOB:
845 				case SQLT_BFILE:
846 					PHP_OCI_CALL_RETURN(errstatus,
847 						OCIDefineDynamic,
848 						(
849 							outcol->oci_define,
850 							statement->err,
851 							(dvoid *)outcol,
852 							php_oci_define_callback
853 						)
854 					);
855 
856 					if (errstatus != OCI_SUCCESS) {
857 						statement->errcode = php_oci_error(statement->err, errstatus);
858 						PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
859 						return 1;
860 					}
861 					break;
862 			}
863 		}
864 		statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
865 	}
866 
867 	return 0;
868 }
869 /* }}} */
870 
871 /* {{{ php_oci_statement_cancel()
872  Cancel statement */
php_oci_statement_cancel(php_oci_statement * statement)873 int php_oci_statement_cancel(php_oci_statement *statement)
874 {
875 	return php_oci_statement_fetch(statement, 0);
876 }
877 /* }}} */
878 
879 /* {{{ php_oci_statement_free()
880  Destroy statement handle and free associated resources */
php_oci_statement_free(php_oci_statement * statement)881 void php_oci_statement_free(php_oci_statement *statement)
882 {
883 	if (statement->stmt) {
884 		if (statement->last_query_len) { /* FIXME: magical */
885 			PHP_OCI_CALL(OCIStmtRelease, (statement->stmt, statement->err, NULL, 0, statement->errcode ? OCI_STRLS_CACHE_DELETE : OCI_DEFAULT));
886 		} else if (statement->impres_flag != PHP_OCI_IMPRES_IS_CHILD) {  /* Oracle doc says don't free Implicit Result Set handles */
887 			PHP_OCI_CALL(OCIHandleFree, (statement->stmt, OCI_HTYPE_STMT));
888 		}
889 		statement->stmt = NULL;
890 	}
891 
892 	if (statement->err) {
893 		PHP_OCI_CALL(OCIHandleFree, (statement->err, OCI_HTYPE_ERROR));
894 		statement->err = NULL;
895 	}
896 
897 	if (statement->last_query) {
898 		efree(statement->last_query);
899 	}
900 
901 	if (statement->binds) {
902 		zend_hash_destroy(statement->binds);
903 		efree(statement->binds);
904 	}
905 
906 	if (statement->defines) {
907 		zend_hash_destroy(statement->defines);
908 		efree(statement->defines);
909 	}
910 
911 	if (statement->columns) {
912 		zend_hash_destroy(statement->columns);
913 		efree(statement->columns);
914 	}
915 
916 	if (statement->parent_stmtid) {
917 		zend_list_delete(statement->parent_stmtid);
918 	}
919 
920 	zend_list_delete(statement->connection->id);
921 	efree(statement);
922 
923 	OCI_G(num_statements)--;
924 }
925 /* }}} */
926 
927 /* {{{ php_oci_bind_pre_exec()
928  Helper function */
php_oci_bind_pre_exec(zval * data,void * result)929 int php_oci_bind_pre_exec(zval *data, void *result)
930 {
931 	php_oci_bind *bind = (php_oci_bind *) Z_PTR_P(data);
932 	zval *zv = &bind->val;
933 
934 	*(int *)result = 0;
935 
936 	ZVAL_DEREF(zv);
937 	if (Z_TYPE_P(zv) == IS_ARRAY) {
938 		/* These checks are currently valid for oci_bind_by_name, not
939 		 * oci_bind_array_by_name.  Also bind->type and
940 		 * bind->indicator are not used for oci_bind_array_by_name.
941 		 */
942 		return 0;
943 	}
944 	switch (bind->type) {
945 		case SQLT_NTY:
946 		case SQLT_BFILEE:
947 		case SQLT_CFILEE:
948 		case SQLT_CLOB:
949 		case SQLT_BLOB:
950 		case SQLT_RDD:
951 			if (Z_TYPE_P(zv) != IS_OBJECT) {
952 				php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
953 				*(int *)result = 1;
954 			}
955 			break;
956 
957 		case SQLT_CHR:
958 		case SQLT_AFC:
959 		case SQLT_INT:
960 		case SQLT_NUM:
961 #if defined(OCI_MAJOR_VERSION) && OCI_MAJOR_VERSION >= 12
962 		case SQLT_BOL:
963 #endif
964 		case SQLT_LBI:
965 		case SQLT_BIN:
966 		case SQLT_LNG:
967 			if (Z_TYPE_P(zv) == IS_RESOURCE || Z_TYPE_P(zv) == IS_OBJECT) {
968 				php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
969 				*(int *)result = 1;
970 			}
971 			break;
972 
973 		case SQLT_RSET:
974 			if (Z_TYPE_P(zv) != IS_RESOURCE) {
975 				php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
976 				*(int *)result = 1;
977 			}
978 			break;
979 	}
980 
981 	/* reset all bind stuff to a normal state... */
982 	bind->indicator = 0;
983 
984 	return 0;
985 }
986 /* }}} */
987 
988 /* {{{ php_oci_bind_post_exec()
989  Helper function */
php_oci_bind_post_exec(zval * data)990 int php_oci_bind_post_exec(zval *data)
991 {
992 	php_oci_bind *bind = (php_oci_bind *) Z_PTR_P(data);
993 	php_oci_connection *connection = bind->parent_statement->connection;
994 	sword errstatus;
995 	zval *zv = &bind->val;
996 
997 	ZVAL_DEREF(zv);
998 	if (bind->indicator == -1) { /* NULL */
999 		if (Z_TYPE_P(zv) == IS_STRING) {
1000 			*Z_STRVAL_P(zv) = '\0'; /* XXX avoid warning in debug mode */
1001 		}
1002 		zval_ptr_dtor(zv);
1003 		ZVAL_NULL(zv);
1004 	} else if (Z_TYPE_P(zv) == IS_STRING
1005 			   && Z_STRLEN_P(zv) > 0
1006 			   && Z_STRVAL_P(zv)[ Z_STRLEN_P(zv) ] != '\0') {
1007 		/* The post- PHP 5.3 feature for "interned" strings disallows
1008 		 * their reallocation but (i) any IN binds either interned or
1009 		 * not should already be null terminated and (ii) for OUT
1010 		 * binds, php_oci_bind_out_callback() should have allocated a
1011 		 * new string that we can modify here.
1012 		 */
1013 #if PHP_VERSION_ID < 70300
1014 		SEPARATE_STRING(zv);
1015 		Z_STR_P(zv) = zend_string_extend(Z_STR_P(zv), Z_STRLEN_P(zv)+1, 0);
1016 #else
1017 		ZVAL_NEW_STR(zv, zend_string_extend(Z_STR_P(zv), Z_STRLEN_P(zv)+1, 0));
1018 #endif
1019 		Z_STRVAL_P(zv)[ Z_STRLEN_P(zv) ] = '\0';
1020 	} else if (Z_TYPE_P(zv) == IS_ARRAY) {
1021 		int i;
1022 		zval *entry = NULL;
1023 		HashTable *hash;
1024 
1025 		SEPARATE_ARRAY(zv);
1026 		hash = HASH_OF(zv);
1027 		zend_hash_internal_pointer_reset(hash);
1028 
1029 		switch (bind->array.type) {
1030 			case SQLT_NUM:
1031 			case SQLT_INT:
1032 			case SQLT_LNG:
1033 				for (i = 0; i < (int) bind->array.current_length; i++) {
1034 					if ((i < (int) bind->array.old_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
1035 						zval_ptr_dtor(entry);
1036 						ZVAL_LONG(entry, ((oci_phpsized_int *)(bind->array.elements))[i]);
1037 						zend_hash_move_forward(hash);
1038 					} else {
1039 						add_next_index_long(zv, ((oci_phpsized_int *)(bind->array.elements))[i]);
1040 					}
1041 				}
1042 				break;
1043 			case SQLT_FLT:
1044 				for (i = 0; i < (int) bind->array.current_length; i++) {
1045 					if ((i < (int) bind->array.old_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
1046 						zval_ptr_dtor(entry);
1047 						ZVAL_DOUBLE(entry, ((double *)(bind->array.elements))[i]);
1048 						zend_hash_move_forward(hash);
1049 					} else {
1050 						add_next_index_double(zv, ((double *)(bind->array.elements))[i]);
1051 					}
1052 				}
1053 				break;
1054 			case SQLT_ODT:
1055 				for (i = 0; i < (int) bind->array.current_length; i++) {
1056 					oratext buff[1024];
1057 					ub4 buff_len = 1024;
1058 
1059 					memset((void*)buff,0,sizeof(buff));
1060 
1061 					if ((i < (int) bind->array.old_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
1062 						PHP_OCI_CALL_RETURN(errstatus, OCIDateToText, (connection->err, &(((OCIDate *)(bind->array.elements))[i]), 0, 0, 0, 0, &buff_len, buff));
1063 						zval_ptr_dtor(entry);
1064 
1065 						if (errstatus != OCI_SUCCESS) {
1066 							connection->errcode = php_oci_error(connection->err, errstatus);
1067 							PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
1068 							ZVAL_NULL(entry);
1069 						} else {
1070 							connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
1071 							ZVAL_STRINGL(entry, (char *)buff, buff_len);
1072 						}
1073 						zend_hash_move_forward(hash);
1074 					} else {
1075 						PHP_OCI_CALL_RETURN(errstatus, OCIDateToText, (connection->err, &(((OCIDate *)(bind->array.elements))[i]), 0, 0, 0, 0, &buff_len, buff));
1076 						if (errstatus != OCI_SUCCESS) {
1077 							connection->errcode = php_oci_error(connection->err, errstatus);
1078 							PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
1079 							add_next_index_null(zv);
1080 						} else {
1081 							connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
1082 							add_next_index_stringl(zv, (char *)buff, buff_len);
1083 						}
1084 					}
1085 				}
1086 				break;
1087 
1088 			case SQLT_AFC:
1089 			case SQLT_CHR:
1090 			case SQLT_VCS:
1091 			case SQLT_AVC:
1092 			case SQLT_STR:
1093 			case SQLT_LVC:
1094 				for (i = 0; i < (int) bind->array.current_length; i++) {
1095 					/* int curr_element_length = strlen(((text *)bind->array.elements)+i*bind->array.max_length); */
1096 					int curr_element_length = bind->array.element_lengths[i];
1097 					if ((i < (int) bind->array.old_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
1098 						zval_ptr_dtor(entry);
1099 						ZVAL_STRINGL(entry, (char *)(((text *)bind->array.elements)+i*bind->array.max_length), curr_element_length);
1100 						zend_hash_move_forward(hash);
1101 					} else {
1102 						add_next_index_stringl(zv, (char *)(((text *)bind->array.elements)+i*bind->array.max_length), curr_element_length);
1103 					}
1104 				}
1105 				break;
1106 		}
1107 	} else if ((Z_TYPE_P(zv) == IS_TRUE) || (Z_TYPE_P(zv) == IS_FALSE)) {
1108 		/* This convetrsion is done on purpose (ext/oci8 uses LVAL as a temorary value) */
1109 		if (Z_LVAL_P(zv) == 0)
1110 			ZVAL_BOOL(zv, FALSE);
1111 		else if (Z_LVAL_P(zv) == 1)
1112 			ZVAL_BOOL(zv, TRUE);
1113 	}
1114 
1115 	return 0;
1116 }
1117 /* }}} */
1118 
1119 /* {{{ php_oci_bind_by_name()
1120  Bind zval to the given placeholder */
php_oci_bind_by_name(php_oci_statement * statement,char * name,size_t name_len,zval * var,zend_long maxlength,ub2 type)1121 int php_oci_bind_by_name(php_oci_statement *statement, char *name, size_t name_len, zval *var, zend_long maxlength, ub2 type)
1122 {
1123 	php_oci_collection *bind_collection = NULL;
1124 	php_oci_descriptor *bind_descriptor = NULL;
1125 	php_oci_statement  *bind_statement	= NULL;
1126 	dvoid *oci_desc					= NULL;
1127 	/* dvoid *php_oci_collection		   = NULL; */
1128 	OCIStmt *oci_stmt				= NULL;
1129 	dvoid *bind_data				= NULL;
1130 	php_oci_bind *old_bind, *bindp;
1131 	int mode = OCI_DATA_AT_EXEC;
1132 	sb4 value_sz = -1;
1133 	sword errstatus;
1134 	zval *param = NULL;
1135 
1136 	ZEND_ASSERT(Z_ISREF_P(var));
1137 	param = Z_REFVAL_P(var);
1138 
1139 	switch (type) {
1140 		case SQLT_NTY:
1141 		{
1142 			zval *tmp;
1143 
1144 			if (Z_TYPE_P(param) != IS_OBJECT || (tmp = zend_hash_str_find(Z_OBJPROP_P(param), "collection", sizeof("collection")-1)) == NULL) {
1145 				php_error_docref(NULL, E_WARNING, "Unable to find collection property");
1146 				return 1;
1147 			}
1148 
1149 			PHP_OCI_ZVAL_TO_COLLECTION_EX(tmp, bind_collection);
1150 			value_sz = sizeof(void*);
1151 			mode = OCI_DEFAULT;
1152 
1153 			if (!bind_collection->collection) {
1154 				return 1;
1155 			}
1156 		}
1157 			break;
1158 		case SQLT_BFILEE:
1159 		case SQLT_CFILEE:
1160 		case SQLT_CLOB:
1161 		case SQLT_BLOB:
1162 		case SQLT_RDD:
1163 		{
1164 			zval *tmp;
1165 
1166 			if (Z_TYPE_P(param) != IS_OBJECT || (tmp = zend_hash_str_find(Z_OBJPROP_P(param), "descriptor", sizeof("descriptor")-1)) == NULL) {
1167 				php_error_docref(NULL, E_WARNING, "Unable to find descriptor property");
1168 				return 1;
1169 			}
1170 
1171 			PHP_OCI_ZVAL_TO_DESCRIPTOR_EX(tmp, bind_descriptor);
1172 
1173 			value_sz = sizeof(void*);
1174 
1175 			oci_desc = bind_descriptor->descriptor;
1176 
1177 			if (!oci_desc) {
1178 				return 1;
1179 			}
1180 		}
1181 			break;
1182 
1183 		case SQLT_INT:
1184 		case SQLT_NUM:
1185 			if (Z_TYPE_P(param) == IS_RESOURCE || Z_TYPE_P(param) == IS_OBJECT) {
1186 				php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
1187 				return 1;
1188 			}
1189 			convert_to_long(param);
1190 			bind_data = (oci_phpsized_int *)&Z_LVAL_P(param);
1191 			value_sz = sizeof(oci_phpsized_int);
1192 			mode = OCI_DEFAULT;
1193 			break;
1194 
1195 		case SQLT_LBI:
1196 		case SQLT_BIN:
1197 		case SQLT_LNG:
1198 		case SQLT_AFC:
1199 		case SQLT_CHR: /* SQLT_CHR is the default value when type was not specified */
1200 			if (Z_TYPE_P(param) == IS_RESOURCE || Z_TYPE_P(param) == IS_OBJECT) {
1201 				php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
1202 				return 1;
1203 			}
1204 			if (Z_TYPE_P(param) != IS_NULL) {
1205 				convert_to_string(param);
1206 			}
1207 			if ((maxlength == -1) || (maxlength == 0)) {
1208 				if (type == SQLT_LNG) {
1209 					value_sz = SB4MAXVAL;
1210 				} else if (Z_TYPE_P(param) == IS_STRING) {
1211 					value_sz = (sb4) Z_STRLEN_P(param);
1212 				} else {
1213 					/* Bug-72524: revert value_sz from PHP_OCI_PIECE_SIZE to 0. This restores PHP 5.6 behavior  */
1214 					value_sz = 0;
1215 				}
1216 			} else {
1217 				value_sz = (sb4) maxlength;
1218 			}
1219 			break;
1220 
1221 		case SQLT_RSET:
1222 			if (Z_TYPE_P(param) != IS_RESOURCE) {
1223 				php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
1224 				return 1;
1225 			}
1226 			PHP_OCI_ZVAL_TO_STATEMENT_EX(param, bind_statement);
1227 			value_sz = sizeof(void*);
1228 
1229 			oci_stmt = bind_statement->stmt;
1230 
1231 			if (!oci_stmt) {
1232 				return 1;
1233 			}
1234 			break;
1235 
1236 #if defined(OCI_MAJOR_VERSION) && OCI_MAJOR_VERSION >= 12
1237 		case SQLT_BOL:
1238 			if (Z_TYPE_P(param) == IS_RESOURCE || Z_TYPE_P(param) == IS_OBJECT) {
1239 				php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
1240 				return 1;
1241 			}
1242 			convert_to_boolean(param);
1243 			bind_data = (zend_long *)&Z_LVAL_P(param);
1244 			if (Z_TYPE_P(param) == IS_TRUE)
1245 				*(zend_long *)bind_data = 1;
1246 			else if (Z_TYPE_P(param) == IS_FALSE)
1247 				*(zend_long *)bind_data = 0;
1248 			else {
1249 				php_error_docref(NULL, E_WARNING, "Invalid variable used for bind");
1250 				return 1;
1251 			}
1252 
1253 			value_sz = sizeof(zend_long);
1254 
1255 			mode = OCI_DEFAULT;
1256 			break;
1257 #endif
1258 
1259 		default:
1260 			php_error_docref(NULL, E_WARNING, "Unknown or unsupported datatype given: %d", (int)type);
1261 			return 1;
1262 			break;
1263 	}
1264 
1265 	if (!statement->binds) {
1266 		ALLOC_HASHTABLE(statement->binds);
1267 		zend_hash_init(statement->binds, 13, NULL, php_oci_bind_hash_dtor, 0);
1268 	}
1269 
1270 	if ((old_bind = zend_hash_str_find_ptr(statement->binds, name, name_len)) != NULL) {
1271 		bindp = old_bind;
1272 		if (!Z_ISUNDEF(bindp->val)) {
1273 			zval_ptr_dtor(&bindp->val);
1274 			ZVAL_UNDEF(&bindp->val);
1275 		}
1276 	} else {
1277 		zend_string *zvtmp;
1278 		zvtmp = zend_string_init(name, name_len, 0);
1279 		bindp = (php_oci_bind *) ecalloc(1, sizeof(php_oci_bind));
1280 		bindp = zend_hash_update_ptr(statement->binds, zvtmp, bindp);
1281 #if PHP_VERSION_ID < 70300
1282 		zend_string_release(zvtmp);
1283 #else
1284 		zend_string_release_ex(zvtmp, 0);
1285 #endif
1286 	}
1287 
1288 	/* Make sure the minimum of value_sz is 1 to avoid ORA-3149
1289 	 * when both in/out parameters are bound with empty strings
1290 	 */
1291 	if (value_sz == 0)
1292 		value_sz = 1;
1293 
1294 	bindp->descriptor = oci_desc;
1295 	bindp->statement = oci_stmt;
1296 	bindp->parent_statement = statement;
1297 	ZVAL_COPY(&bindp->val, var);
1298 	bindp->type = type;
1299 	/* Storing max length set in OCIBindByName() to check it later in
1300 	 * php_oci_bind_in_callback() function to avoid ORA-1406 error while
1301 	 * executing OCIStmtExecute()
1302      */
1303 	bindp->dummy_len = value_sz;
1304 
1305 	PHP_OCI_CALL_RETURN(errstatus,
1306 		OCIBindByName,
1307 		(
1308 			statement->stmt,				 /* statement handle */
1309 			(OCIBind **)&bindp->bind,		 /* bind hdl (will alloc) */
1310 			statement->err,				  	 /* error handle */
1311 			(text*) name,					 /* placeholder name */
1312 			(sb4) name_len,					 /* placeholder length */
1313 			(dvoid *)bind_data,				 /* in/out data */
1314 			value_sz, /* PHP_OCI_MAX_DATA_SIZE, */ /* max size of input/output data */
1315 			type,							 /* in/out data type */
1316 			(dvoid *)&bindp->indicator,		 /* indicator (ignored) */
1317 			(ub2 *)0,						 /* size array (ignored) */
1318 			(ub2 *)&bindp->retcode,			 /* return code (ignored) */
1319 			(ub4)0,							 /* maxarr_len (PL/SQL only?) */
1320 			(ub4 *)0,						 /* actual array size (PL/SQL only?) */
1321 			mode							 /* mode */
1322 		)
1323 	);
1324 
1325 	if (errstatus != OCI_SUCCESS) {
1326 		statement->errcode = php_oci_error(statement->err, errstatus);
1327 		PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
1328 		return 1;
1329 	}
1330 
1331 	if (mode == OCI_DATA_AT_EXEC) {
1332 		PHP_OCI_CALL_RETURN(errstatus, OCIBindDynamic,
1333 				(
1334 				 bindp->bind,
1335 				 statement->err,
1336 				 (dvoid *)bindp,
1337 				 php_oci_bind_in_callback,
1338 				 (dvoid *)bindp,
1339 				 php_oci_bind_out_callback
1340 				)
1341 		);
1342 
1343 		if (errstatus != OCI_SUCCESS) {
1344 			statement->errcode = php_oci_error(statement->err, errstatus);
1345 			PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
1346 			return 1;
1347 		}
1348 	}
1349 
1350 	if (type == SQLT_NTY) {
1351 		/* Bind object */
1352 		PHP_OCI_CALL_RETURN(errstatus, OCIBindObject,
1353 				(
1354 				 bindp->bind,
1355 				 statement->err,
1356 				 bind_collection->tdo,
1357 				 (dvoid **) &(bind_collection->collection),
1358 				 (ub4 *) 0,
1359 				 (dvoid **) 0,
1360 				 (ub4 *) 0
1361 				)
1362 		);
1363 
1364 		if (errstatus) {
1365 			statement->errcode = php_oci_error(statement->err, errstatus);
1366 			PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
1367 			return 1;
1368 		}
1369 	}
1370 
1371 	statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
1372 	return 0;
1373 }
1374 /* }}} */
1375 
1376 /* {{{ php_oci_bind_in_callback()
1377  Callback used when binding LOBs and VARCHARs */
php_oci_bind_in_callback(dvoid * ictxp,OCIBind * bindp,ub4 iter,ub4 index,dvoid ** bufpp,ub4 * alenp,ub1 * piecep,dvoid ** indpp)1378 sb4 php_oci_bind_in_callback(
1379 					dvoid *ictxp,	  /* context pointer */
1380 					OCIBind *bindp,	  /* bind handle */
1381 					ub4 iter,		  /* 0-based execute iteration value */
1382 					ub4 index,		  /* index of current array for PL/SQL or row index for SQL */
1383 					dvoid **bufpp,	  /* pointer to data */
1384 					ub4 *alenp,		  /* size after value/piece has been read */
1385 					ub1 *piecep,	  /* which piece */
1386 					dvoid **indpp)	  /* indicator value */
1387 {
1388 	php_oci_bind *phpbind;
1389 	zval *val;
1390 
1391 	if (!(phpbind=(php_oci_bind *)ictxp) || Z_ISUNDEF(phpbind->val)) {
1392 		php_error_docref(NULL, E_WARNING, "Invalid phpbind pointer value");
1393 		return OCI_ERROR;
1394 	}
1395 
1396 	val = &phpbind->val;
1397 	ZVAL_DEREF(val);
1398 
1399 	if (Z_ISNULL_P(val)) {
1400 		/* we're going to insert a NULL column */
1401 		phpbind->indicator = -1;
1402 		*bufpp = 0;
1403 		*alenp = -1;
1404 		*indpp = (dvoid *)&phpbind->indicator;
1405 	} else	if ((phpbind->descriptor == 0) && (phpbind->statement == 0)) {
1406 		/* "normal string bind */
1407 		convert_to_string(val);
1408 
1409 		*bufpp = Z_STRVAL_P(val);
1410 		*alenp = (ub4) Z_STRLEN_P(val);
1411 		/*
1412 		 * bind_char_1: If max length set in OCIBindByName is less than the
1413 		 * actual length of input string, then we have to overwrite alenp with
1414 		 * max value set in OCIBindByName (dummy_len). Or else it will cause
1415 		 * ORA-1406 error in OCIStmtExecute
1416 		 */
1417 		if ((phpbind->dummy_len > 0) && (phpbind->dummy_len < *alenp))
1418 			*alenp = phpbind->dummy_len;
1419 		*indpp = (dvoid *)&phpbind->indicator;
1420 	} else if (phpbind->statement != 0) {
1421 		/* RSET */
1422 		*bufpp = phpbind->statement;
1423 		*alenp = -1;		/* seems to be allright */
1424 		*indpp = (dvoid *)&phpbind->indicator;
1425 	} else {
1426 		/* descriptor bind */
1427 		*bufpp = phpbind->descriptor;
1428 		*alenp = -1;		/* seems to be allright */
1429 		*indpp = (dvoid *)&phpbind->indicator;
1430 	}
1431 
1432 	*piecep = OCI_ONE_PIECE; /* pass all data in one go */
1433 
1434 	return OCI_CONTINUE;
1435 }
1436 /* }}} */
1437 
1438 /* {{{ php_oci_bind_out_callback()
1439  Callback used when binding LOBs and VARCHARs */
php_oci_bind_out_callback(dvoid * octxp,OCIBind * bindp,ub4 iter,ub4 index,dvoid ** bufpp,ub4 ** alenpp,ub1 * piecep,dvoid ** indpp,ub2 ** rcodepp)1440 sb4 php_oci_bind_out_callback(
1441 					dvoid *octxp,	   /* context pointer */
1442 					OCIBind *bindp,	   /* bind handle */
1443 					ub4 iter,		   /* 0-based execute iteration value */
1444 					ub4 index,		   /* index of current array for PL/SQL or row index for SQL */
1445 					dvoid **bufpp,	   /* pointer to data */
1446 					ub4 **alenpp,	   /* size after value/piece has been read */
1447 					ub1 *piecep,	   /* which piece */
1448 					dvoid **indpp,	   /* indicator value */
1449 					ub2 **rcodepp)	   /* return code */
1450 {
1451 	php_oci_bind *phpbind;
1452 	zval *val;
1453 	sb4 retval = OCI_ERROR;
1454 
1455 	if (!(phpbind=(php_oci_bind *)octxp) || Z_ISUNDEF(phpbind->val)) {
1456 		php_error_docref(NULL, E_WARNING, "Invalid phpbind pointer value");
1457 		return retval;
1458 	}
1459 
1460 	val = &phpbind->val;
1461 	ZVAL_DEREF(val);
1462 
1463 	if (Z_TYPE_P(val) == IS_RESOURCE) {
1464 		/* Processing for ref-cursor out binds */
1465 		if (phpbind->statement != NULL) {
1466 			*bufpp = phpbind->statement;
1467 			*alenpp = &phpbind->dummy_len;
1468 			*piecep = OCI_ONE_PIECE;
1469 			*rcodepp = &phpbind->retcode;
1470 			*indpp = &phpbind->indicator;
1471 		}
1472 		retval = OCI_CONTINUE;
1473 	} else if (Z_TYPE_P(val) == IS_OBJECT) {
1474 		zval *tmp;
1475 		php_oci_descriptor *desc;
1476 
1477 		if (!phpbind->descriptor) {
1478 			return OCI_ERROR;
1479 		}
1480 
1481 		/* Do not use the cached lob size if the descriptor is an
1482 		 * out-bind as the contents would have been changed for in/out
1483 		 * binds (Bug #46994).
1484 		 */
1485 		if ((tmp = zend_hash_str_find(Z_OBJPROP_P(val), "descriptor", sizeof("descriptor")-1)) == NULL) {
1486 			php_error_docref(NULL, E_WARNING, "Unable to find object outbind descriptor property");
1487 			return OCI_ERROR;
1488 		}
1489 		PHP_OCI_ZVAL_TO_DESCRIPTOR_EX(tmp, desc);
1490 		desc->lob_size = -1;	/* force OCI8 to update cached size */
1491 
1492 		*alenpp = &phpbind->dummy_len;
1493 		*bufpp = phpbind->descriptor;
1494 		*piecep = OCI_ONE_PIECE;
1495 		*rcodepp = &phpbind->retcode;
1496 		*indpp = &phpbind->indicator;
1497 		retval = OCI_CONTINUE;
1498 	} else {
1499 		convert_to_string(val);
1500 		zval_ptr_dtor(val);
1501 
1502 		{
1503 			char *p = ecalloc(1, PHP_OCI_PIECE_SIZE);
1504 			ZVAL_STRINGL(val, p, PHP_OCI_PIECE_SIZE);
1505 			efree(p);
1506 		}
1507 #if 0
1508 		Z_STRLEN_P(val) = PHP_OCI_PIECE_SIZE; /* 64K-1 is max XXX */
1509 		Z_STRVAL_P(val) = ecalloc(1, Z_STRLEN_P(val) + 1);
1510 		/* XXX is this right? */
1511 		ZVAL_STRINGL(val, NULL, Z_STRLEN(val) + 1);
1512 #endif
1513 
1514 		/* XXX we assume that zend-zval len has 4 bytes */
1515 		*alenpp = (ub4*) &Z_STRLEN_P(val);
1516 		*bufpp = Z_STRVAL_P(val);
1517 		*piecep = OCI_ONE_PIECE;
1518 		*rcodepp = &phpbind->retcode;
1519 		*indpp = &phpbind->indicator;
1520 		retval = OCI_CONTINUE;
1521 	}
1522 
1523 	return retval;
1524 }
1525 /* }}} */
1526 
1527 /* {{{ php_oci_statement_get_column_helper()
1528  Helper function to get column by name and index */
php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAMETERS,int need_data)1529 php_oci_out_column *php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAMETERS, int need_data)
1530 {
1531 	zval *z_statement, *column_index;
1532 	php_oci_statement *statement;
1533 	php_oci_out_column *column;
1534 
1535 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "rz", &z_statement, &column_index) == FAILURE) {
1536 		return NULL;
1537 	}
1538 
1539 	statement = (php_oci_statement *) zend_fetch_resource_ex(z_statement, "oci8 statement", le_statement);
1540 
1541 	if (!statement) {
1542 		return NULL;
1543 	}
1544 
1545 	if (need_data && !statement->has_data) {
1546 		return NULL;
1547 	}
1548 
1549 	if (Z_TYPE_P(column_index) == IS_STRING) {
1550 		column = php_oci_statement_get_column(statement, -1, Z_STRVAL_P(column_index), (int) Z_STRLEN_P(column_index));
1551 		if (!column) {
1552 			php_error_docref(NULL, E_WARNING, "Invalid column name \"%s\"", Z_STRVAL_P(column_index));
1553 			return NULL;
1554 		}
1555 	} else {
1556 #if PHP_VERSION_ID < 70300
1557 		zval tmp;
1558 		/* NB: for PHP4 compat only, it should be using 'Z' instead */
1559 		tmp = *column_index;
1560 		zval_copy_ctor(&tmp);
1561 		convert_to_long(&tmp);
1562 		column = php_oci_statement_get_column(statement, Z_LVAL(tmp), NULL, 0);
1563 		if (!column) {
1564 			php_error_docref(NULL, E_WARNING, "Invalid column index \"" ZEND_LONG_FMT "\"", Z_LVAL(tmp));
1565 			zval_ptr_dtor(&tmp);
1566 			return NULL;
1567 		}
1568 		zval_ptr_dtor(&tmp);
1569 #else
1570 		zend_long tmp;
1571 		/* NB: for PHP4 compat only, it should be using 'Z' instead */
1572 
1573 		tmp = zval_get_long(column_index);
1574 		column = php_oci_statement_get_column(statement, tmp, NULL, 0);
1575 		if (!column) {
1576 			php_error_docref(NULL, E_WARNING, "Invalid column index \"" ZEND_LONG_FMT "\"", tmp);
1577 			return NULL;
1578 		}
1579 #endif
1580 	}
1581 	return column;
1582 }
1583 /* }}} */
1584 
1585 /* {{{ php_oci_statement_get_type()
1586  Return type of the statement */
php_oci_statement_get_type(php_oci_statement * statement,ub2 * type)1587 int php_oci_statement_get_type(php_oci_statement *statement, ub2 *type)
1588 {
1589 	ub2 statement_type;
1590 	sword errstatus;
1591 
1592 	*type = 0;
1593 
1594 	PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, (ub2 *)&statement_type, (ub4 *)0, OCI_ATTR_STMT_TYPE, statement->err));
1595 
1596 	if (errstatus != OCI_SUCCESS) {
1597 		statement->errcode = php_oci_error(statement->err, errstatus);
1598 		PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
1599 		return 1;
1600 	}
1601 	statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
1602 	*type = statement_type;
1603 
1604 	return 0;
1605 }
1606 /* }}} */
1607 
1608 /* {{{ php_oci_statement_get_numrows()
1609  Get the number of rows fetched to the clientside (NOT the number of rows in the result set) */
php_oci_statement_get_numrows(php_oci_statement * statement,ub4 * numrows)1610 int php_oci_statement_get_numrows(php_oci_statement *statement, ub4 *numrows)
1611 {
1612 	ub4 statement_numrows;
1613 	sword errstatus;
1614 
1615 	*numrows = 0;
1616 
1617 	PHP_OCI_CALL_RETURN(errstatus, OCIAttrGet, ((dvoid *)statement->stmt, OCI_HTYPE_STMT, (ub4 *)&statement_numrows, (ub4 *)0, OCI_ATTR_ROW_COUNT, statement->err));
1618 
1619 	if (errstatus != OCI_SUCCESS) {
1620 		statement->errcode = php_oci_error(statement->err, errstatus);
1621 		PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
1622 		return 1;
1623 	}
1624 	statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
1625 	*numrows = statement_numrows;
1626 
1627 	return 0;
1628 }
1629 /* }}} */
1630 
1631 /* {{{ php_oci_bind_array_by_name()
1632  Bind arrays to PL/SQL types */
php_oci_bind_array_by_name(php_oci_statement * statement,char * name,size_t name_len,zval * var,zend_long max_table_length,zend_long maxlength,zend_long type)1633 int php_oci_bind_array_by_name(php_oci_statement *statement, char *name, size_t name_len, zval *var, zend_long max_table_length, zend_long maxlength, zend_long type)
1634 {
1635 	php_oci_bind *bind;
1636 	sword errstatus;
1637 	zend_string *zvtmp;
1638 	zval *val;
1639 
1640 	ZEND_ASSERT(Z_ISREF_P(var));
1641 	val = Z_REFVAL_P(var);
1642 #if PHP_VERSION_ID < 70300
1643 	SEPARATE_ZVAL_NOREF(val);
1644 	convert_to_array(val);
1645 #else
1646 	convert_to_array(val);
1647 	SEPARATE_ARRAY(val);
1648 #endif
1649 
1650 	if (maxlength < -1) {
1651 		php_error_docref(NULL, E_WARNING, "Invalid max length value (" ZEND_LONG_FMT ")", maxlength);
1652 		return 1;
1653 	}
1654 
1655 	switch(type) {
1656 		case SQLT_NUM:
1657 		case SQLT_INT:
1658 		case SQLT_LNG:
1659 			bind = php_oci_bind_array_helper_number(val, max_table_length);
1660 			break;
1661 
1662 		case SQLT_FLT:
1663 			bind = php_oci_bind_array_helper_double(val, max_table_length);
1664 			break;
1665 
1666 		case SQLT_AFC:
1667 		case SQLT_CHR:
1668 		case SQLT_VCS:
1669 		case SQLT_AVC:
1670 		case SQLT_STR:
1671 		case SQLT_LVC:
1672 			if (maxlength == -1 && zend_hash_num_elements(Z_ARRVAL_P(val)) == 0) {
1673 				php_error_docref(NULL, E_WARNING, "You must provide max length value for empty arrays");
1674 				return 1;
1675 			}
1676 			bind = php_oci_bind_array_helper_string(val, max_table_length, maxlength);
1677 			break;
1678 		case SQLT_ODT:
1679 			bind = php_oci_bind_array_helper_date(val, max_table_length, statement->connection);
1680 			break;
1681 		default:
1682 			php_error_docref(NULL, E_WARNING, "Unknown or unsupported datatype given: " ZEND_LONG_FMT, type);
1683 			return 1;
1684 			break;
1685 	}
1686 
1687 	if (bind == NULL) {
1688 		/* failed to generate bind struct */
1689 		return 1;
1690 	}
1691 
1692 	bind->descriptor = NULL;
1693 	bind->statement = NULL;
1694 	bind->parent_statement = statement;
1695 	bind->bind = NULL;
1696 	ZVAL_COPY(&bind->val, var);
1697 	bind->array.type = type;
1698 	bind->indicator = 0;  		/* not used for array binds */
1699 	bind->type = 0; 			/* not used for array binds */
1700 
1701 	PHP_OCI_CALL_RETURN(errstatus,
1702 							OCIBindByName,
1703 							(
1704 								statement->stmt,
1705 								(OCIBind **)&bind->bind,
1706 								statement->err,
1707 								(text *)name,
1708 								(sb4) name_len,
1709 								(dvoid *) bind->array.elements,
1710 								(sb4) bind->array.max_length,
1711 								(ub2)type,
1712 								(dvoid *)bind->array.indicators,
1713 								(ub2 *)bind->array.element_lengths,
1714 								(ub2 *)0, /* bindp->array.retcodes, */
1715 								(ub4) max_table_length,
1716 								(ub4 *) &(bind->array.current_length),
1717 								(ub4) OCI_DEFAULT
1718 							)
1719 						);
1720 
1721 
1722 	if (errstatus != OCI_SUCCESS) {
1723 		if (bind->array.elements) {
1724 			efree(bind->array.elements);
1725 		}
1726 
1727 		if (bind->array.element_lengths) {
1728 			efree(bind->array.element_lengths);
1729 		}
1730 
1731 		if (bind->array.indicators) {
1732 			efree(bind->array.indicators);
1733 		}
1734 
1735 		zval_ptr_dtor(&bind->val);
1736 
1737 		efree(bind);
1738 
1739 		statement->errcode = php_oci_error(statement->err, errstatus);
1740 		PHP_OCI_HANDLE_ERROR(statement->connection, statement->errcode);
1741 		return 1;
1742 	}
1743 
1744 	if (!statement->binds) {
1745 		ALLOC_HASHTABLE(statement->binds);
1746 		zend_hash_init(statement->binds, 13, NULL, php_oci_bind_hash_dtor, 0);
1747 	}
1748 
1749 	zvtmp = zend_string_init(name, name_len, 0);
1750 	zend_hash_update_ptr(statement->binds, zvtmp, bind);
1751 #if PHP_VERSION_ID < 70300
1752 	zend_string_release(zvtmp);
1753 #else
1754 	zend_string_release_ex(zvtmp, 0);
1755 #endif
1756 
1757 	statement->errcode = 0; /* retain backwards compat with OCI8 1.4 */
1758 	return 0;
1759 }
1760 /* }}} */
1761 
1762 /* {{{ php_oci_bind_array_helper_string()
1763  Bind arrays to PL/SQL types */
php_oci_bind_array_helper_string(zval * var,zend_long max_table_length,zend_long maxlength)1764 php_oci_bind *php_oci_bind_array_helper_string(zval *var, zend_long max_table_length, zend_long maxlength)
1765 {
1766 	php_oci_bind *bind;
1767 	ub4 i;
1768 	HashTable *hash;
1769 	zval *entry;
1770 
1771 	SEPARATE_ARRAY(var); /* TODO: may be use new HashTable iteration and prevent inplace modification */
1772 	hash = HASH_OF(var);
1773 
1774 	if (maxlength == -1) {
1775 		zend_hash_internal_pointer_reset(hash);
1776 		while ((entry = zend_hash_get_current_data(hash)) != NULL) {
1777 			convert_to_string_ex(entry);
1778 
1779 			if (maxlength == -1 || Z_STRLEN_P(entry) > (size_t) maxlength) {
1780 				maxlength = Z_STRLEN_P(entry) + 1;
1781 			}
1782 
1783 			zend_hash_move_forward(hash);
1784 		}
1785 	}
1786 
1787 	bind = emalloc(sizeof(php_oci_bind));
1788 	ZVAL_UNDEF(&bind->val);
1789 	bind->array.elements		= (text *)safe_emalloc(max_table_length * (maxlength + 1), sizeof(text), 0);
1790 	memset(bind->array.elements, 0, max_table_length * (maxlength + 1) * sizeof(text));
1791 	bind->array.current_length	= zend_hash_num_elements(Z_ARRVAL_P(var));
1792 	bind->array.old_length		= bind->array.current_length;
1793 	bind->array.max_length		= (ub4) maxlength;
1794 	bind->array.element_lengths	= safe_emalloc(max_table_length, sizeof(ub2), 0);
1795 	memset(bind->array.element_lengths, 0, max_table_length*sizeof(ub2));
1796 	bind->array.indicators		= safe_emalloc(max_table_length, sizeof(sb2), 0);
1797 	memset(bind->array.indicators, 0, max_table_length*sizeof(sb2));
1798 
1799 	zend_hash_internal_pointer_reset(hash);
1800 
1801 	for (i = 0; i < bind->array.current_length; i++) {
1802 		if ((entry = zend_hash_get_current_data(hash)) != NULL) {
1803 			convert_to_string_ex(entry);
1804 			bind->array.element_lengths[i] = (ub2) Z_STRLEN_P(entry);
1805 			if (Z_STRLEN_P(entry) == 0) {
1806 				bind->array.indicators[i] = -1;
1807 			}
1808 			zend_hash_move_forward(hash);
1809 		} else {
1810 			break;
1811 		}
1812 	}
1813 
1814 	zend_hash_internal_pointer_reset(hash);
1815 	for (i = 0; i < max_table_length; i++) {
1816 		if ((i < bind->array.current_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
1817 			int element_length;
1818 
1819 			convert_to_string_ex(entry);
1820 			element_length = ((size_t) maxlength > Z_STRLEN_P(entry)) ? (int) Z_STRLEN_P(entry) : (int) maxlength;
1821 
1822 			memcpy((text *)bind->array.elements + i*maxlength, Z_STRVAL_P(entry), element_length);
1823 			((text *)bind->array.elements)[i*maxlength + element_length] = '\0';
1824 
1825 			zend_hash_move_forward(hash);
1826 		} else {
1827 			((text *)bind->array.elements)[i*maxlength] = '\0';
1828 		}
1829 	}
1830 	zend_hash_internal_pointer_reset(hash);
1831 
1832 	return bind;
1833 }
1834 /* }}} */
1835 
1836 /* {{{ php_oci_bind_array_helper_number()
1837  Bind arrays to PL/SQL types */
php_oci_bind_array_helper_number(zval * var,zend_long max_table_length)1838 php_oci_bind *php_oci_bind_array_helper_number(zval *var, zend_long max_table_length)
1839 {
1840 	php_oci_bind *bind;
1841 	ub4 i;
1842 	HashTable *hash;
1843 	zval *entry;
1844 
1845 	SEPARATE_ARRAY(var); /* TODO: may be use new HashTable iteration and prevent inplace modification */
1846 	hash = HASH_OF(var);
1847 
1848 	bind = emalloc(sizeof(php_oci_bind));
1849 	ZVAL_UNDEF(&bind->val);
1850 	bind->array.elements		= (oci_phpsized_int *)safe_emalloc(max_table_length, sizeof(oci_phpsized_int), 0);
1851 	bind->array.current_length	= zend_hash_num_elements(Z_ARRVAL_P(var));
1852 	bind->array.old_length		= bind->array.current_length;
1853 	bind->array.max_length		= sizeof(oci_phpsized_int);
1854 	bind->array.element_lengths	= safe_emalloc(max_table_length, sizeof(ub2), 0);
1855 	memset(bind->array.element_lengths, 0, max_table_length * sizeof(ub2));
1856 	bind->array.indicators		= NULL;
1857 
1858 	zend_hash_internal_pointer_reset(hash);
1859 	for (i = 0; i < max_table_length; i++) {
1860 		if (i < bind->array.current_length) {
1861 			bind->array.element_lengths[i] = sizeof(oci_phpsized_int);
1862 		}
1863 		if ((i < bind->array.current_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
1864 			convert_to_long_ex(entry);
1865 			((oci_phpsized_int *)bind->array.elements)[i] = (oci_phpsized_int) Z_LVAL_P(entry);
1866 			zend_hash_move_forward(hash);
1867 		} else {
1868 			((oci_phpsized_int *)bind->array.elements)[i] = 0;
1869 		}
1870 	}
1871 	zend_hash_internal_pointer_reset(hash);
1872 
1873 	return bind;
1874 }
1875 /* }}} */
1876 
1877 /* {{{ php_oci_bind_array_helper_double()
1878  Bind arrays to PL/SQL types */
php_oci_bind_array_helper_double(zval * var,zend_long max_table_length)1879 php_oci_bind *php_oci_bind_array_helper_double(zval *var, zend_long max_table_length)
1880 {
1881 	php_oci_bind *bind;
1882 	ub4 i;
1883 	HashTable *hash;
1884 	zval *entry;
1885 
1886 	SEPARATE_ARRAY(var); /* TODO: may be use new HashTable iteration and prevent inplace modification */
1887 	hash = HASH_OF(var);
1888 
1889 	bind = emalloc(sizeof(php_oci_bind));
1890 	ZVAL_UNDEF(&bind->val);
1891 	bind->array.elements		= (double *)safe_emalloc(max_table_length, sizeof(double), 0);
1892 	bind->array.current_length	= zend_hash_num_elements(Z_ARRVAL_P(var));
1893 	bind->array.old_length		= bind->array.current_length;
1894 	bind->array.max_length		= sizeof(double);
1895 	bind->array.element_lengths	= safe_emalloc(max_table_length, sizeof(ub2), 0);
1896 	memset(bind->array.element_lengths, 0, max_table_length * sizeof(ub2));
1897 	bind->array.indicators		= NULL;
1898 
1899 	zend_hash_internal_pointer_reset(hash);
1900 	for (i = 0; i < max_table_length; i++) {
1901 		if (i < bind->array.current_length) {
1902 			bind->array.element_lengths[i] = sizeof(double);
1903 		}
1904 		if ((i < bind->array.current_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
1905 			convert_to_double_ex(entry);
1906 			((double *)bind->array.elements)[i] = (double) Z_DVAL_P(entry);
1907 			zend_hash_move_forward(hash);
1908 		} else {
1909 			((double *)bind->array.elements)[i] = 0;
1910 		}
1911 	}
1912 	zend_hash_internal_pointer_reset(hash);
1913 
1914 	return bind;
1915 }
1916 /* }}} */
1917 
1918 /* {{{ php_oci_bind_array_helper_date()
1919  Bind arrays to PL/SQL types */
php_oci_bind_array_helper_date(zval * var,zend_long max_table_length,php_oci_connection * connection)1920 php_oci_bind *php_oci_bind_array_helper_date(zval *var, zend_long max_table_length, php_oci_connection *connection)
1921 {
1922 	php_oci_bind *bind;
1923 	ub4 i;
1924 	HashTable *hash;
1925 	zval *entry;
1926 	sword errstatus;
1927 
1928 	SEPARATE_ARRAY(var); /* TODO: may be use new HashTable iteration and prevent inplace modification */
1929 	hash = HASH_OF(var);
1930 
1931 	bind = emalloc(sizeof(php_oci_bind));
1932 	ZVAL_UNDEF(&bind->val);
1933 	bind->array.elements		= (OCIDate *)safe_emalloc(max_table_length, sizeof(OCIDate), 0);
1934 	bind->array.current_length	= zend_hash_num_elements(Z_ARRVAL_P(var));
1935 	bind->array.old_length		= bind->array.current_length;
1936 	bind->array.max_length		= sizeof(OCIDate);
1937 	bind->array.element_lengths	= safe_emalloc(max_table_length, sizeof(ub2), 0);
1938 	memset(bind->array.element_lengths, 0, max_table_length * sizeof(ub2));
1939 	bind->array.indicators		= NULL;
1940 
1941 	zend_hash_internal_pointer_reset(hash);
1942 	for (i = 0; i < max_table_length; i++) {
1943 		OCIDate oci_date;
1944 		if (i < bind->array.current_length) {
1945 			bind->array.element_lengths[i] = sizeof(OCIDate);
1946 		}
1947 		if ((i < bind->array.current_length) && (entry = zend_hash_get_current_data(hash)) != NULL) {
1948 
1949 			convert_to_string_ex(entry);
1950 			PHP_OCI_CALL_RETURN(errstatus, OCIDateFromText, (connection->err, (CONST text *)Z_STRVAL_P(entry), (ub4) Z_STRLEN_P(entry), NULL, 0, NULL, 0, &oci_date));
1951 
1952 			if (errstatus != OCI_SUCCESS) {
1953 				/* failed to convert string to date */
1954 				efree(bind->array.element_lengths);
1955 				efree(bind->array.elements);
1956 				efree(bind);
1957 				connection->errcode = php_oci_error(connection->err, errstatus);
1958 				PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
1959 				return NULL;
1960 			}
1961 
1962 			((OCIDate *)bind->array.elements)[i] = oci_date;
1963 			zend_hash_move_forward(hash);
1964 		} else {
1965 			PHP_OCI_CALL_RETURN(errstatus, OCIDateFromText, (connection->err, (CONST text *)"01-JAN-00", sizeof("01-JAN-00")-1, NULL, 0, NULL, 0, &oci_date));
1966 
1967 			if (errstatus != OCI_SUCCESS) {
1968 				/* failed to convert string to date */
1969 				efree(bind->array.element_lengths);
1970 				efree(bind->array.elements);
1971 				efree(bind);
1972 				connection->errcode = php_oci_error(connection->err, errstatus);
1973 				PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
1974 				return NULL;
1975 			}
1976 
1977 			((OCIDate *)bind->array.elements)[i] = oci_date;
1978 		}
1979 		connection->errcode = 0; /* retain backwards compat with OCI8 1.4 */
1980 	}
1981 	zend_hash_internal_pointer_reset(hash);
1982 
1983 	return bind;
1984 }
1985 /* }}} */
1986 
1987 #endif /* HAVE_OCI8 */
1988 
1989 /*
1990  * Local variables:
1991  * tab-width: 4
1992  * c-basic-offset: 4
1993  * End:
1994  * vim600: noet sw=4 ts=4 fdm=marker
1995  * vim<600: noet sw=4 ts=4
1996  */
1997