xref: /PHP-5.3/ext/oci8/oci8_interface.c (revision a2045ff3)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2013 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 /* $Id$ */
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #include "php.h"
35 #include "ext/standard/info.h"
36 #include "php_ini.h"
37 
38 #if HAVE_OCI8
39 
40 #include "php_oci8.h"
41 #include "php_oci8_int.h"
42 
43 #ifndef OCI_STMT_CALL
44 #define OCI_STMT_CALL 10
45 #endif
46 
47 /* {{{ proto bool oci_define_by_name(resource stmt, string name, mixed &var [, int type])
48    Define a PHP variable to an Oracle column by name */
49 /* if you want to define a LOB/CLOB etc make sure you allocate it via OCINewDescriptor BEFORE defining!!! */
PHP_FUNCTION(oci_define_by_name)50 PHP_FUNCTION(oci_define_by_name)
51 {
52 	zval *stmt, *var;
53 	char *name;
54 	int name_len;
55 	long type = 0;
56 	php_oci_statement *statement;
57 	php_oci_define *define, *tmp_define;
58 
59 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsz/|l", &stmt, &name, &name_len, &var, &type) == FAILURE) {
60 		return;
61 	}
62 
63 	if (!name_len) {
64 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Column name cannot be empty");
65 		RETURN_FALSE;
66 	}
67 
68 	PHP_OCI_ZVAL_TO_STATEMENT(stmt, statement);
69 
70 	if (statement->defines == NULL) {
71 		ALLOC_HASHTABLE(statement->defines);
72 		zend_hash_init(statement->defines, 13, NULL, php_oci_define_hash_dtor, 0);
73 	}
74 
75 	define = ecalloc(1,sizeof(php_oci_define));
76 
77 	if (zend_hash_add(statement->defines, name, name_len, define, sizeof(php_oci_define), (void **)&tmp_define) == SUCCESS) {
78 		efree(define);
79 		define = tmp_define;
80 	} else {
81 		efree(define);
82 		RETURN_FALSE;
83 	}
84 
85 	define->name = (text*) estrndup(name, name_len);
86 	define->name_len = name_len;
87 	define->type = type;
88 	define->zval = var;
89 	zval_add_ref(&var);
90 
91 	RETURN_TRUE;
92 }
93 /* }}} */
94 
95 /* {{{ proto bool oci_bind_by_name(resource stmt, string name, mixed &var, [, int maxlength [, int type]])
96    Bind a PHP variable to an Oracle placeholder by name */
97 /* if you want to bind a LOB/CLOB etc make sure you allocate it via OCINewDescriptor BEFORE binding!!! */
PHP_FUNCTION(oci_bind_by_name)98 PHP_FUNCTION(oci_bind_by_name)
99 {
100 	ub2	bind_type = SQLT_CHR; /* unterminated string */
101 	int name_len;
102 	long maxlen = -1, type = 0;
103 	char *name;
104 	zval *z_statement;
105 	zval *bind_var = NULL;
106 	php_oci_statement *statement;
107 
108 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsz/|ll", &z_statement, &name, &name_len, &bind_var, &maxlen, &type) == FAILURE) {
109 		return;
110 	}
111 
112 	if (type) {
113 		bind_type = (ub2) type;
114 	}
115 
116 	PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
117 
118 	if (php_oci_bind_by_name(statement, name, name_len, bind_var, maxlen, bind_type TSRMLS_CC)) {
119 		RETURN_FALSE;
120 	}
121 	RETURN_TRUE;
122 }
123 /* }}} */
124 
125 /* {{{ proto bool oci_bind_array_by_name(resource stmt, string name, array &var, int max_table_length [, int max_item_length [, int type ]])
126    Bind a PHP array to an Oracle PL/SQL type by name */
PHP_FUNCTION(oci_bind_array_by_name)127 PHP_FUNCTION(oci_bind_array_by_name)
128 {
129 	int name_len;
130 	long max_item_len = -1;
131 	long max_array_len = 0;
132 	long type = SQLT_AFC;
133 	char *name;
134 	zval *z_statement;
135 	zval *bind_var = NULL;
136 	php_oci_statement *statement;
137 
138 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rsz/l|ll", &z_statement, &name, &name_len, &bind_var, &max_array_len, &max_item_len, &type) == FAILURE) {
139 		return;
140 	}
141 
142 	PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
143 
144 	if (ZEND_NUM_ARGS() == 5 && max_item_len <= 0) {
145 		max_item_len = -1;
146 	}
147 
148 	if (max_array_len <= 0) {
149 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Maximum array length must be greater than zero");
150 		RETURN_FALSE;
151 	}
152 
153 	if (php_oci_bind_array_by_name(statement, name, name_len, bind_var, max_array_len, max_item_len, type TSRMLS_CC)) {
154 		RETURN_FALSE;
155 	}
156 	RETURN_TRUE;
157 }
158 /* }}} */
159 
160 /* {{{ proto bool oci_free_descriptor()
161    Deletes large object description */
PHP_FUNCTION(oci_free_descriptor)162 PHP_FUNCTION(oci_free_descriptor)
163 {
164 	zval **tmp, *z_descriptor = getThis();
165 	php_oci_descriptor *descriptor;
166 
167 	if (!getThis()) {
168 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &z_descriptor, oci_lob_class_entry_ptr) == FAILURE) {
169 			return;
170 		}
171 	}
172 
173 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
174 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
175 		RETURN_FALSE;
176 	}
177 
178 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
179 
180 	zend_list_delete(descriptor->id);
181 	RETURN_TRUE;
182 }
183 /* }}} */
184 
185 /* {{{ proto bool oci_lob_save( string data [, int offset ])
186    Saves a large object */
PHP_FUNCTION(oci_lob_save)187 PHP_FUNCTION(oci_lob_save)
188 {
189 	zval **tmp, *z_descriptor = getThis();
190 	php_oci_descriptor *descriptor;
191 	char *data;
192 	int data_len;
193 	long offset = 0;
194 	ub4 bytes_written;
195 
196 	if (getThis()) {
197 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &offset) == FAILURE) {
198 			return;
199 		}
200 	}
201 	else {
202 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os|l", &z_descriptor, oci_lob_class_entry_ptr, &data, &data_len, &offset) == FAILURE) {
203 			return;
204 		}
205 	}
206 
207 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
208 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
209 		RETURN_FALSE;
210 	}
211 
212 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
213 
214 	if (offset < 0) {
215 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset parameter must be greater than or equal to 0");
216 		RETURN_FALSE;
217 	}
218 
219 	if (php_oci_lob_write(descriptor, offset, data, data_len, &bytes_written TSRMLS_CC)) {
220 		RETURN_FALSE;
221 	}
222 	RETURN_TRUE;
223 }
224 /* }}} */
225 
226 /* {{{ proto bool oci_lob_import( string filename )
227    Loads file into a LOB */
PHP_FUNCTION(oci_lob_import)228 PHP_FUNCTION(oci_lob_import)
229 {
230 	zval **tmp, *z_descriptor = getThis();
231 	php_oci_descriptor *descriptor;
232 	char *filename;
233 	int filename_len;
234 
235 	if (getThis()) {
236 #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 3) || (PHP_MAJOR_VERSION > 5)
237 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p", &filename, &filename_len) == FAILURE) {
238 #else
239 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
240 #endif
241 			return;
242 		}
243 	}
244 	else {
245 #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 3) || (PHP_MAJOR_VERSION > 5)
246 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Op", &z_descriptor, oci_lob_class_entry_ptr, &filename, &filename_len) == FAILURE) {
247 #else
248 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os", &z_descriptor, oci_lob_class_entry_ptr, &filename, &filename_len) == FAILURE) {
249 #endif
250 			return;
251 		}
252 	}
253 
254 #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || (PHP_MAJOR_VERSION < 5)
255 	/* The "p" parsing parameter handles this case in PHP 5.4+ */
256 	if (strlen(filename) != filename_len) {
257 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Filename cannot contain null bytes");
258 		RETURN_FALSE;
259 	}
260 #endif
261 
262 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
263 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
264 		RETURN_FALSE;
265 	}
266 
267 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
268 
269 	if (php_oci_lob_import(descriptor, filename TSRMLS_CC)) {
270 		RETURN_FALSE;
271 	}
272 	RETURN_TRUE;
273 }
274 /* }}} */
275 
276 /* {{{ proto string oci_lob_load()
277    Loads a large object */
278 PHP_FUNCTION(oci_lob_load)
279 {
280 	zval **tmp, *z_descriptor = getThis();
281 	php_oci_descriptor *descriptor;
282 	char *buffer = NULL;
283 	ub4 buffer_len;
284 
285 	if (!getThis()) {
286 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &z_descriptor, oci_lob_class_entry_ptr) == FAILURE) {
287 			return;
288 		}
289 	}
290 
291 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
292 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
293 		RETURN_FALSE;
294 	}
295 
296 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
297 
298 	if (php_oci_lob_read(descriptor, -1, 0, &buffer, &buffer_len TSRMLS_CC)) {
299 		RETURN_FALSE;
300 	}
301 	if (buffer_len > 0) {
302 		RETURN_STRINGL(buffer, buffer_len, 0);
303 	}
304 	else {
305 		RETURN_EMPTY_STRING();
306 	}
307 }
308 /* }}} */
309 
310 /* {{{ proto string oci_lob_read( int length )
311    Reads particular part of a large object */
312 PHP_FUNCTION(oci_lob_read)
313 {
314 	zval **tmp, *z_descriptor = getThis();
315 	php_oci_descriptor *descriptor;
316 	long length;
317 	char *buffer;
318 	ub4 buffer_len;
319 
320 	if (getThis()) {
321 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &length) == FAILURE) {
322 			return;
323 		}
324 	}
325 	else {
326 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ol", &z_descriptor, oci_lob_class_entry_ptr, &length) == FAILURE) {
327 			return;
328 		}
329 	}
330 
331 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
332 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
333 		RETURN_FALSE;
334 	}
335 
336 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
337 
338 	if (length <= 0) {
339 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter must be greater than 0");
340 		RETURN_FALSE;
341 	}
342 
343 	if (php_oci_lob_read(descriptor, length, descriptor->lob_current_position, &buffer, &buffer_len TSRMLS_CC)) {
344 		RETURN_FALSE;
345 	}
346 	if (buffer_len > 0) {
347 		RETURN_STRINGL(buffer, buffer_len, 0);
348 	}
349 	else {
350 		RETURN_EMPTY_STRING();
351 	}
352 }
353 /* }}} */
354 
355 /* {{{ proto bool oci_lob_eof()
356    Checks if EOF is reached */
357 PHP_FUNCTION(oci_lob_eof)
358 {
359 	zval **tmp, *z_descriptor = getThis();
360 	php_oci_descriptor *descriptor;
361 	ub4 lob_length;
362 
363 	if (!getThis()) {
364 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &z_descriptor, oci_lob_class_entry_ptr) == FAILURE) {
365 			return;
366 		}
367 	}
368 
369 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
370 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
371 		RETURN_FALSE;
372 	}
373 
374 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
375 
376 	if (!php_oci_lob_get_length(descriptor, &lob_length TSRMLS_CC) && lob_length >= 0) {
377 		if (lob_length == descriptor->lob_current_position) {
378 			RETURN_TRUE;
379 		}
380 	}
381 	RETURN_FALSE;
382 }
383 /* }}} */
384 
385 /* {{{ proto int oci_lob_tell()
386    Tells LOB pointer position */
387 PHP_FUNCTION(oci_lob_tell)
388 {
389 	zval **tmp, *z_descriptor = getThis();
390 	php_oci_descriptor *descriptor;
391 
392 	if (!getThis()) {
393 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &z_descriptor, oci_lob_class_entry_ptr) == FAILURE) {
394 			return;
395 		}
396 	}
397 
398 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
399 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
400 		RETURN_FALSE;
401 	}
402 
403 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
404 
405 	RETURN_LONG(descriptor->lob_current_position);
406 }
407 /* }}} */
408 
409 /* {{{ proto bool oci_lob_rewind()
410    Rewind pointer of a LOB */
411 PHP_FUNCTION(oci_lob_rewind)
412 {
413 	zval **tmp, *z_descriptor = getThis();
414 	php_oci_descriptor *descriptor;
415 
416 	if (!getThis()) {
417 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &z_descriptor, oci_lob_class_entry_ptr) == FAILURE) {
418 			return;
419 		}
420 	}
421 
422 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
423 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
424 		RETURN_FALSE;
425 	}
426 
427 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
428 
429 	descriptor->lob_current_position = 0;
430 
431 	RETURN_TRUE;
432 }
433 /* }}} */
434 
435 /* {{{ proto bool oci_lob_seek( int offset [, int whence ])
436    Moves the pointer of a LOB */
437 PHP_FUNCTION(oci_lob_seek)
438 {
439 	zval **tmp, *z_descriptor = getThis();
440 	php_oci_descriptor *descriptor;
441 	long offset, whence = PHP_OCI_SEEK_SET;
442 	ub4 lob_length;
443 
444 	if (getThis()) {
445 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &offset, &whence) == FAILURE) {
446 			return;
447 		}
448 	}
449 	else {
450 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ol|l", &z_descriptor, oci_lob_class_entry_ptr, &offset, &whence) == FAILURE) {
451 			return;
452 		}
453 	}
454 
455 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
456 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
457 		RETURN_FALSE;
458 	}
459 
460 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
461 
462 	if (php_oci_lob_get_length(descriptor, &lob_length TSRMLS_CC)) {
463 		RETURN_FALSE;
464 	}
465 
466 	switch(whence) {
467 		case PHP_OCI_SEEK_CUR:
468 			descriptor->lob_current_position += offset;
469 			break;
470 		case PHP_OCI_SEEK_END:
471 			if ((descriptor->lob_size + offset) >= 0) {
472 				descriptor->lob_current_position = descriptor->lob_size + offset;
473 			}
474 			else {
475 				descriptor->lob_current_position = 0;
476 			}
477 			break;
478 		case PHP_OCI_SEEK_SET:
479 		default:
480 				descriptor->lob_current_position = (offset > 0) ? offset : 0;
481 			break;
482 	}
483 	RETURN_TRUE;
484 }
485 /* }}} */
486 
487 /* {{{ proto int oci_lob_size()
488    Returns size of a large object */
489 PHP_FUNCTION(oci_lob_size)
490 {
491 	zval **tmp, *z_descriptor = getThis();
492 	php_oci_descriptor *descriptor;
493 	ub4 lob_length;
494 
495 	if (!getThis()) {
496 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &z_descriptor, oci_lob_class_entry_ptr) == FAILURE) {
497 			return;
498 		}
499 	}
500 
501 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
502 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
503 		RETURN_FALSE;
504 	}
505 
506 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
507 
508 	if (php_oci_lob_get_length(descriptor, &lob_length TSRMLS_CC)) {
509 		RETURN_FALSE;
510 	}
511 	RETURN_LONG(lob_length);
512 }
513 /* }}} */
514 
515 /* {{{ proto int oci_lob_write( string string [, int length ])
516    Writes data to current position of a LOB */
517 PHP_FUNCTION(oci_lob_write)
518 {
519 	zval **tmp, *z_descriptor = getThis();
520 	php_oci_descriptor *descriptor;
521 	int data_len;
522 	long write_len = 0;
523 	ub4 bytes_written;
524 	char *data;
525 
526 	if (getThis()) {
527 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &write_len) == FAILURE) {
528 			return;
529 		}
530 
531 		if (ZEND_NUM_ARGS() == 2) {
532 			data_len = MIN(data_len, write_len);
533 		}
534 	}
535 	else {
536 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os|l", &z_descriptor, oci_lob_class_entry_ptr, &data, &data_len, &write_len) == FAILURE) {
537 			return;
538 		}
539 
540 		if (ZEND_NUM_ARGS() == 3) {
541 			data_len = MIN(data_len, write_len);
542 		}
543 	}
544 
545 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
546 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
547 		RETURN_FALSE;
548 	}
549 
550 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
551 
552 	if (data_len <= 0) {
553 		RETURN_LONG(0);
554 	}
555 
556 	if (php_oci_lob_write(descriptor, descriptor->lob_current_position, data, data_len, &bytes_written TSRMLS_CC)) {
557 		RETURN_FALSE;
558 	}
559 	RETURN_LONG(bytes_written);
560 }
561 /* }}} */
562 
563 /* {{{ proto bool oci_lob_append( object lob )
564    Appends data from a LOB to another LOB */
565 PHP_FUNCTION(oci_lob_append)
566 {
567 	zval **tmp_dest, **tmp_from, *z_descriptor_dest = getThis(), *z_descriptor_from;
568 	php_oci_descriptor *descriptor_dest, *descriptor_from;
569 
570 	if (getThis()) {
571 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &z_descriptor_from, oci_lob_class_entry_ptr) == FAILURE) {
572 			return;
573 		}
574 	}
575 	else {
576 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OO", &z_descriptor_dest, oci_lob_class_entry_ptr, &z_descriptor_from, oci_lob_class_entry_ptr) == FAILURE) {
577 			return;
578 		}
579 	}
580 
581 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor_dest), "descriptor", sizeof("descriptor"), (void **)&tmp_dest) == FAILURE) {
582 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property. The first argument should be valid descriptor object");
583 		RETURN_FALSE;
584 	}
585 
586 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor_from), "descriptor", sizeof("descriptor"), (void **)&tmp_from) == FAILURE) {
587 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property. The second argument should be valid descriptor object");
588 		RETURN_FALSE;
589 	}
590 
591 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp_dest, descriptor_dest);
592 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp_from, descriptor_from);
593 
594 	if (php_oci_lob_append(descriptor_dest, descriptor_from TSRMLS_CC)) {
595 		RETURN_FALSE;
596 	}
597 	/* XXX should we increase lob_size here ? */
598 	RETURN_TRUE;
599 }
600 /* }}} */
601 
602 /* {{{ proto bool oci_lob_truncate( [ int length ])
603    Truncates a LOB */
604 PHP_FUNCTION(oci_lob_truncate)
605 {
606 	zval **tmp, *z_descriptor = getThis();
607 	php_oci_descriptor *descriptor;
608 	long trim_length = 0;
609 	ub4 ub_trim_length;
610 
611 	if (getThis()) {
612 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &trim_length) == FAILURE) {
613 			return;
614 		}
615 	}
616 	else {
617 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|l", &z_descriptor, oci_lob_class_entry_ptr, &trim_length) == FAILURE) {
618 			return;
619 		}
620 	}
621 
622 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
623 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
624 		RETURN_FALSE;
625 	}
626 
627 	if (trim_length < 0) {
628 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length must be greater than or equal to zero");
629 		RETURN_FALSE;
630 	}
631 
632 	ub_trim_length = (ub4) trim_length;
633 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
634 
635 	if (php_oci_lob_truncate(descriptor, ub_trim_length TSRMLS_CC)) {
636 		RETURN_FALSE;
637 	}
638 	RETURN_TRUE;
639 }
640 /* }}} */
641 
642 /* {{{ proto int oci_lob_erase( [ int offset [, int length ] ] )
643    Erases a specified portion of the internal LOB, starting at a specified offset */
644 PHP_FUNCTION(oci_lob_erase)
645 {
646 	zval **tmp, *z_descriptor = getThis();
647 	php_oci_descriptor *descriptor;
648 	ub4 bytes_erased;
649 	long offset = -1, length = -1;
650 
651 	if (getThis()) {
652 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ll", &offset, &length) == FAILURE) {
653 			return;
654 		}
655 
656 		if (ZEND_NUM_ARGS() > 0 && offset < 0) {
657 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset must be greater than or equal to 0");
658 			RETURN_FALSE;
659 		}
660 
661 		if (ZEND_NUM_ARGS() > 1 && length < 0) {
662 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length must be greater than or equal to 0");
663 			RETURN_FALSE;
664 		}
665 	}
666 	else {
667 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|ll", &z_descriptor, oci_lob_class_entry_ptr, &offset, &length) == FAILURE) {
668 			return;
669 		}
670 
671 		if (ZEND_NUM_ARGS() > 1 && offset < 0) {
672 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset must be greater than or equal to 0");
673 			RETURN_FALSE;
674 		}
675 
676 		if (ZEND_NUM_ARGS() > 2 && length < 0) {
677 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length must be greater than or equal to 0");
678 			RETURN_FALSE;
679 		}
680 	}
681 
682 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
683 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
684 		RETURN_FALSE;
685 	}
686 
687 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
688 
689 	if (php_oci_lob_erase(descriptor, offset, length, &bytes_erased TSRMLS_CC)) {
690 		RETURN_FALSE;
691 	}
692 	RETURN_LONG(bytes_erased);
693 }
694 /* }}} */
695 
696 /* {{{ proto bool oci_lob_flush( [ int flag ] )
697    Flushes the LOB buffer */
698 PHP_FUNCTION(oci_lob_flush)
699 {
700 	zval **tmp, *z_descriptor = getThis();
701 	php_oci_descriptor *descriptor;
702 	long flush_flag = 0;
703 
704 	if (getThis()) {
705 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flush_flag) == FAILURE) {
706 			return;
707 		}
708 	}
709 	else {
710 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|l", &z_descriptor, oci_lob_class_entry_ptr, &flush_flag) == FAILURE) {
711 			return;
712 		}
713 	}
714 
715 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
716 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
717 		RETURN_FALSE;
718 	}
719 
720 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
721 
722 	if (descriptor->buffering == PHP_OCI_LOB_BUFFER_DISABLED) {
723 		/* buffering wasn't enabled, there is nothing to flush */
724 		RETURN_FALSE;
725 	}
726 
727 	if (php_oci_lob_flush(descriptor, flush_flag TSRMLS_CC)) {
728 		RETURN_FALSE;
729 	}
730 	RETURN_TRUE;
731 }
732 /* }}} */
733 
734 /* {{{ proto bool ocisetbufferinglob( boolean flag )
735    Enables/disables buffering for a LOB */
736 PHP_FUNCTION(ocisetbufferinglob)
737 {
738 	zval **tmp, *z_descriptor = getThis();
739 	php_oci_descriptor *descriptor;
740 	zend_bool flag;
741 
742 	if (getThis()) {
743 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &flag) == FAILURE) {
744 			return;
745 		}
746 	}
747 	else {
748 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ob", &z_descriptor, oci_lob_class_entry_ptr, &flag) == FAILURE) {
749 			return;
750 		}
751 	}
752 
753 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
754 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
755 		RETURN_FALSE;
756 	}
757 
758 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
759 
760 	if (php_oci_lob_set_buffering(descriptor, flag TSRMLS_CC)) {
761 		RETURN_FALSE;
762 	}
763 	RETURN_TRUE;
764 }
765 /* }}} */
766 
767 /* {{{ proto bool ocigetbufferinglob()
768    Returns current state of buffering for a LOB */
769 PHP_FUNCTION(ocigetbufferinglob)
770 {
771 	zval **tmp, *z_descriptor = getThis();
772 	php_oci_descriptor *descriptor;
773 
774 	if (!getThis()) {
775 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &z_descriptor, oci_lob_class_entry_ptr) == FAILURE) {
776 			return;
777 		}
778 	}
779 
780 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
781 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
782 		RETURN_FALSE;
783 	}
784 
785 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
786 
787 	if (descriptor->buffering != PHP_OCI_LOB_BUFFER_DISABLED) {
788 		RETURN_TRUE;
789 	}
790 	RETURN_FALSE;
791 }
792 /* }}} */
793 
794 /* {{{ proto bool oci_lob_copy( object lob_to, object lob_from [, int length ] )
795    Copies data from a LOB to another LOB */
796 PHP_FUNCTION(oci_lob_copy)
797 {
798 	zval **tmp_dest, **tmp_from, *z_descriptor_dest, *z_descriptor_from;
799 	php_oci_descriptor *descriptor_dest, *descriptor_from;
800 	long length = 0;
801 
802 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OO|l", &z_descriptor_dest, oci_lob_class_entry_ptr, &z_descriptor_from, oci_lob_class_entry_ptr, &length) == FAILURE) {
803 		return;
804 	}
805 
806 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor_dest), "descriptor", sizeof("descriptor"), (void **)&tmp_dest) == FAILURE) {
807 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property. The first argument should be valid descriptor object");
808 		RETURN_FALSE;
809 	}
810 
811 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor_from), "descriptor", sizeof("descriptor"), (void **)&tmp_from) == FAILURE) {
812 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property. The second argument should be valid descriptor object");
813 		RETURN_FALSE;
814 	}
815 
816 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp_dest, descriptor_dest);
817 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp_from, descriptor_from);
818 
819 	if (ZEND_NUM_ARGS() == 3 && length < 0) {
820 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter must be greater than 0");
821 		RETURN_FALSE;
822 	}
823 
824 	if (ZEND_NUM_ARGS() == 2) {
825 		/* indicate that we want to copy from the current position to the end of the LOB */
826 		length = -1;
827 	}
828 
829 	if (php_oci_lob_copy(descriptor_dest, descriptor_from, length TSRMLS_CC)) {
830 		RETURN_FALSE;
831 	}
832 	RETURN_TRUE;
833 }
834 /* }}} */
835 
836 /* {{{ proto bool oci_lob_is_equal( object lob1, object lob2 )
837    Tests to see if two LOB/FILE locators are equal */
838 PHP_FUNCTION(oci_lob_is_equal)
839 {
840 	zval **tmp_first, **tmp_second, *z_descriptor_first, *z_descriptor_second;
841 	php_oci_descriptor *descriptor_first, *descriptor_second;
842 	boolean is_equal;
843 
844 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OO", &z_descriptor_first, oci_lob_class_entry_ptr, &z_descriptor_second, oci_lob_class_entry_ptr) == FAILURE) {
845 		return;
846 	}
847 
848 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor_first), "descriptor", sizeof("descriptor"), (void **)&tmp_first) == FAILURE) {
849 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property. The first argument should be valid descriptor object");
850 		RETURN_FALSE;
851 	}
852 
853 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor_second), "descriptor", sizeof("descriptor"), (void **)&tmp_second) == FAILURE) {
854 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property. The second argument should be valid descriptor object");
855 		RETURN_FALSE;
856 	}
857 
858 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp_first, descriptor_first);
859 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp_second, descriptor_second);
860 
861 	if (php_oci_lob_is_equal(descriptor_first, descriptor_second, &is_equal TSRMLS_CC)) {
862 		RETURN_FALSE;
863 	}
864 
865 	if (is_equal == TRUE) {
866 		RETURN_TRUE;
867 	}
868 	RETURN_FALSE;
869 }
870 /* }}} */
871 
872 /* {{{ proto bool oci_lob_export([string filename [, int start [, int length]]])
873    Writes a large object into a file */
874 PHP_FUNCTION(oci_lob_export)
875 {
876 	zval **tmp, *z_descriptor = getThis();
877 	php_oci_descriptor *descriptor;
878 	char *filename;
879 	char *buffer;
880 	int filename_len;
881 	long start = -1, length = -1, block_length;
882 	php_stream *stream;
883 	ub4 lob_length;
884 
885 	if (getThis()) {
886 #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 3) || (PHP_MAJOR_VERSION > 5)
887 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "p|ll", &filename, &filename_len, &start, &length) == FAILURE) {
888 #else
889 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &filename, &filename_len, &start, &length) == FAILURE) {
890 #endif
891 			return;
892 		}
893 
894 		if (ZEND_NUM_ARGS() > 1 && start < 0) {
895 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Start parameter must be greater than or equal to 0");
896 			RETURN_FALSE;
897 		}
898 		if (ZEND_NUM_ARGS() > 2 && length < 0) {
899 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter must be greater than or equal to 0");
900 			RETURN_FALSE;
901 		}
902 	}
903 	else {
904 #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 3) || (PHP_MAJOR_VERSION > 5)
905 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Op|ll", &z_descriptor, oci_lob_class_entry_ptr, &filename, &filename_len, &start, &length) == FAILURE) {
906 #else
907 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os|ll", &z_descriptor, oci_lob_class_entry_ptr, &filename, &filename_len, &start, &length) == FAILURE) {
908 #endif
909 			return;
910 		}
911 
912 		if (ZEND_NUM_ARGS() > 2 && start < 0) {
913 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Start parameter must be greater than or equal to 0");
914 			RETURN_FALSE;
915 		}
916 		if (ZEND_NUM_ARGS() > 3 && length < 0) {
917 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "Length parameter must be greater than or equal to 0");
918 			RETURN_FALSE;
919 		}
920 	}
921 
922 #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || (PHP_MAJOR_VERSION < 5)
923 	/* The "p" parsing parameter handles this case in PHP 5.4+ */
924 	if (strlen(filename) != filename_len) {
925 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Filename cannot contain null bytes");
926 		RETURN_FALSE;
927 	}
928 #endif
929 
930 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
931 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
932 		RETURN_FALSE;
933 	}
934 
935 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
936 
937 	if (php_oci_lob_get_length(descriptor, &lob_length TSRMLS_CC)) {
938 		RETURN_FALSE;
939 	}
940 
941 	if (start == -1) {
942 		start = 0;
943 	}
944 
945 	if (length == -1) {
946 		length = lob_length - descriptor->lob_current_position;
947 	}
948 
949 	if (length == 0) {
950 		/* nothing to write, fail silently */
951 		RETURN_FALSE;
952 	}
953 
954 #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || (PHP_MAJOR_VERSION < 5)
955 	/* Safe mode has been removed in PHP 5.4 */
956 	if (PG(safe_mode) && (!php_checkuid(filename, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
957 		RETURN_FALSE;
958 	}
959 #endif
960 
961 	if (php_check_open_basedir(filename TSRMLS_CC)) {
962 		RETURN_FALSE;
963 	}
964 
965 #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 3) || (PHP_MAJOR_VERSION > 5)
966 	stream = php_stream_open_wrapper_ex(filename, "w", REPORT_ERRORS, NULL, NULL);
967 #else
968 	stream = php_stream_open_wrapper_ex(filename, "w", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL, NULL);
969 #endif
970 
971 	block_length = PHP_OCI_LOB_BUFFER_SIZE;
972 	if (block_length > length) {
973 		block_length = length;
974 	}
975 
976 	while(length > 0) {
977 		ub4 tmp_bytes_read = 0;
978 		if (php_oci_lob_read(descriptor, block_length, start, &buffer, &tmp_bytes_read TSRMLS_CC)) {
979 			php_stream_close(stream);
980 			RETURN_FALSE;
981 		}
982 		if (tmp_bytes_read && !php_stream_write(stream, buffer, tmp_bytes_read)) {
983 			php_stream_close(stream);
984 			efree(buffer);
985 			RETURN_FALSE;
986 		}
987 		if (buffer) {
988 			efree(buffer);
989 		}
990 
991 		length -= tmp_bytes_read;
992 		descriptor->lob_current_position += tmp_bytes_read;
993 		start += tmp_bytes_read;
994 
995 		if (block_length > length) {
996 			block_length = length;
997 		}
998 	}
999 
1000 	php_stream_close(stream);
1001 	RETURN_TRUE;
1002 }
1003 /* }}} */
1004 
1005 /* {{{ proto bool oci_lob_write_temporary(string var [, int lob_type])
1006    Writes temporary blob */
1007 PHP_FUNCTION(oci_lob_write_temporary)
1008 {
1009 	zval **tmp, *z_descriptor = getThis();
1010 	php_oci_descriptor *descriptor;
1011 	char *data;
1012 	int data_len;
1013 	long type = OCI_TEMP_CLOB;
1014 
1015 	if (getThis()) {
1016 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &data_len, &type) == FAILURE) {
1017 			return;
1018 		}
1019 	}
1020 	else {
1021 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os|l", &z_descriptor, oci_lob_class_entry_ptr, &data, &data_len, &type) == FAILURE) {
1022 			return;
1023 		}
1024 	}
1025 
1026 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
1027 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
1028 		RETURN_FALSE;
1029 	}
1030 
1031 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
1032 
1033 	if (php_oci_lob_write_tmp(descriptor, type, data, data_len TSRMLS_CC)) {
1034 		RETURN_FALSE;
1035 	}
1036 	RETURN_TRUE;
1037 }
1038 /* }}} */
1039 
1040 /* {{{ proto bool oci_lob_close()
1041    Closes lob descriptor */
1042 PHP_FUNCTION(oci_lob_close)
1043 {
1044 	zval **tmp, *z_descriptor = getThis();
1045 	php_oci_descriptor *descriptor;
1046 
1047 	if (!getThis()) {
1048 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &z_descriptor, oci_lob_class_entry_ptr) == FAILURE) {
1049 			return;
1050 		}
1051 	}
1052 
1053 	if (zend_hash_find(Z_OBJPROP_P(z_descriptor), "descriptor", sizeof("descriptor"), (void **)&tmp) == FAILURE) {
1054 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find descriptor property");
1055 		RETURN_FALSE;
1056 	}
1057 
1058 	PHP_OCI_ZVAL_TO_DESCRIPTOR(*tmp, descriptor);
1059 
1060 	if (php_oci_lob_close(descriptor TSRMLS_CC)) {
1061 		RETURN_FALSE;
1062 	}
1063 	RETURN_TRUE;
1064 }
1065 /* }}} */
1066 
1067 /* {{{ proto object oci_new_descriptor(resource connection [, int type])
1068    Initialize a new empty descriptor LOB/FILE (LOB is default) */
1069 PHP_FUNCTION(oci_new_descriptor)
1070 {
1071 	zval *z_connection;
1072 	php_oci_connection *connection;
1073 	php_oci_descriptor *descriptor;
1074 	long type = OCI_DTYPE_LOB;
1075 
1076 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &z_connection, &type) == FAILURE) {
1077 		return;
1078 	}
1079 
1080 	PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
1081 
1082 	/* php_oci_lob_create() checks type */
1083 	descriptor = php_oci_lob_create(connection, type TSRMLS_CC);
1084 
1085 	if (!descriptor) {
1086 		RETURN_NULL();
1087 	}
1088 
1089 	object_init_ex(return_value, oci_lob_class_entry_ptr);
1090 	add_property_resource(return_value, "descriptor", descriptor->id);
1091 }
1092 /* }}} */
1093 
1094 /* {{{ proto bool oci_rollback(resource connection)
1095    Rollback the current context */
1096 PHP_FUNCTION(oci_rollback)
1097 {
1098 	zval *z_connection;
1099 	php_oci_connection *connection;
1100 
1101 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_connection) == FAILURE) {
1102 		return;
1103 	}
1104 
1105 	PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
1106 
1107 	if (connection->descriptors) {
1108 		php_oci_connection_descriptors_free(connection TSRMLS_CC);
1109 	}
1110 
1111 	if (php_oci_connection_rollback(connection TSRMLS_CC)) {
1112 		RETURN_FALSE;
1113 	}
1114 	RETURN_TRUE;
1115 }
1116 /* }}} */
1117 
1118 /* {{{ proto bool oci_commit(resource connection)
1119    Commit the current context */
1120 PHP_FUNCTION(oci_commit)
1121 {
1122 	zval *z_connection;
1123 	php_oci_connection *connection;
1124 
1125 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_connection) == FAILURE) {
1126 		return;
1127 	}
1128 
1129 	PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
1130 
1131 	if (connection->descriptors) {
1132 		php_oci_connection_descriptors_free(connection TSRMLS_CC);
1133 	}
1134 
1135 	if (php_oci_connection_commit(connection TSRMLS_CC)) {
1136 		RETURN_FALSE;
1137 	}
1138 	RETURN_TRUE;
1139 }
1140 /* }}} */
1141 
1142 /* {{{ proto string oci_field_name(resource stmt, int col)
1143    Tell the name of a column */
1144 PHP_FUNCTION(oci_field_name)
1145 {
1146 	php_oci_out_column *column;
1147 
1148 	if ( ( column = php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) ) ) {
1149 		RETURN_STRINGL(column->name, column->name_len, 1);
1150 	}
1151 	RETURN_FALSE;
1152 }
1153 /* }}} */
1154 
1155 /* {{{ proto int oci_field_size(resource stmt, int col)
1156    Tell the maximum data size of a column */
1157 PHP_FUNCTION(oci_field_size)
1158 {
1159 	php_oci_out_column *column;
1160 
1161 	if ( ( column = php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) ) ) {
1162 		/* Handle data type of LONG */
1163 		if (column->data_type == SQLT_LNG){
1164 			RETURN_LONG(column->storage_size4);
1165 		}
1166 		RETURN_LONG(column->data_size);
1167 	}
1168 	RETURN_FALSE;
1169 }
1170 /* }}} */
1171 
1172 /* {{{ proto int oci_field_scale(resource stmt, int col)
1173    Tell the scale of a column */
1174 PHP_FUNCTION(oci_field_scale)
1175 {
1176 	php_oci_out_column *column;
1177 
1178 	if ( ( column = php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) ) ) {
1179 		RETURN_LONG(column->scale);
1180 	}
1181 	RETURN_FALSE;
1182 }
1183 /* }}} */
1184 
1185 /* {{{ proto int oci_field_precision(resource stmt, int col)
1186    Tell the precision of a column */
1187 PHP_FUNCTION(oci_field_precision)
1188 {
1189 	php_oci_out_column *column;
1190 
1191 	if ( ( column = php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) ) ) {
1192 		RETURN_LONG(column->precision);
1193 	}
1194 	RETURN_FALSE;
1195 }
1196 /* }}} */
1197 
1198 /* {{{ proto mixed oci_field_type(resource stmt, int col)
1199    Tell the data type of a column */
1200 PHP_FUNCTION(oci_field_type)
1201 {
1202 	php_oci_out_column *column;
1203 
1204 	column = php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1205 
1206 	if (!column) {
1207 		RETURN_FALSE;
1208 	}
1209 
1210 	switch (column->data_type) {
1211 #ifdef SQLT_TIMESTAMP
1212 		case SQLT_TIMESTAMP:
1213 			RETVAL_STRING("TIMESTAMP",1);
1214 			break;
1215 #endif
1216 #ifdef SQLT_TIMESTAMP_TZ
1217 		case SQLT_TIMESTAMP_TZ:
1218 			RETVAL_STRING("TIMESTAMP WITH TIMEZONE",1);
1219 			break;
1220 #endif
1221 #ifdef SQLT_TIMESTAMP_LTZ
1222 		case SQLT_TIMESTAMP_LTZ:
1223 			RETVAL_STRING("TIMESTAMP WITH LOCAL TIMEZONE",1);
1224 			break;
1225 #endif
1226 #ifdef SQLT_INTERVAL_YM
1227 		case SQLT_INTERVAL_YM:
1228 			RETVAL_STRING("INTERVAL YEAR TO MONTH",1);
1229 			break;
1230 #endif
1231 #ifdef SQLT_INTERVAL_DS
1232 		case SQLT_INTERVAL_DS:
1233 			RETVAL_STRING("INTERVAL DAY TO SECOND",1);
1234 			break;
1235 #endif
1236 		case SQLT_DAT:
1237 			RETVAL_STRING("DATE",1);
1238 			break;
1239 		case SQLT_NUM:
1240 			RETVAL_STRING("NUMBER",1);
1241 			break;
1242 		case SQLT_LNG:
1243 			RETVAL_STRING("LONG",1);
1244 			break;
1245 		case SQLT_BIN:
1246 			RETVAL_STRING("RAW",1);
1247 			break;
1248 		case SQLT_LBI:
1249 			RETVAL_STRING("LONG RAW",1);
1250 			break;
1251 		case SQLT_CHR:
1252 			RETVAL_STRING("VARCHAR2",1);
1253 			break;
1254 		case SQLT_RSET:
1255 			RETVAL_STRING("REFCURSOR",1);
1256 			break;
1257 		case SQLT_AFC:
1258 			RETVAL_STRING("CHAR",1);
1259 			break;
1260 		case SQLT_BLOB:
1261 			RETVAL_STRING("BLOB",1);
1262 			break;
1263 		case SQLT_CLOB:
1264 			RETVAL_STRING("CLOB",1);
1265 			break;
1266 		case SQLT_BFILE:
1267 			RETVAL_STRING("BFILE",1);
1268 			break;
1269 		case SQLT_RDD:
1270 			RETVAL_STRING("ROWID",1);
1271 			break;
1272 		default:
1273 			RETVAL_LONG(column->data_type);
1274 	}
1275 }
1276 /* }}} */
1277 
1278 /* {{{ proto int oci_field_type_raw(resource stmt, int col)
1279    Tell the raw oracle data type of a column */
1280 PHP_FUNCTION(oci_field_type_raw)
1281 {
1282 	php_oci_out_column *column;
1283 
1284 	column = php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1285 	if (column) {
1286 		RETURN_LONG(column->data_type);
1287 	}
1288 	RETURN_FALSE;
1289 }
1290 /* }}} */
1291 
1292 /* {{{ proto bool oci_field_is_null(resource stmt, int col)
1293    Tell whether a column is NULL */
1294 PHP_FUNCTION(oci_field_is_null)
1295 {
1296 	php_oci_out_column *column;
1297 
1298 	if ( ( column = php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0) ) ) {
1299 		if (column->indicator == -1) {
1300 			RETURN_TRUE;
1301 		}
1302 	}
1303 	RETURN_FALSE;
1304 }
1305 /* }}} */
1306 
1307 /* {{{ proto void oci_internal_debug(int onoff)
1308    Toggle internal debugging output for the OCI extension */
1309 PHP_FUNCTION(oci_internal_debug)
1310 {
1311 	zend_bool on_off;
1312 
1313 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &on_off) == FAILURE) {
1314 		return;
1315 	}
1316 	OCI_G(debug_mode) = on_off;
1317 }
1318 /* }}} */
1319 
1320 /* {{{ proto bool oci_execute(resource stmt [, int mode])
1321    Execute a parsed statement */
1322 PHP_FUNCTION(oci_execute)
1323 {
1324 	zval *z_statement;
1325 	php_oci_statement *statement;
1326 	long mode = OCI_COMMIT_ON_SUCCESS;
1327 
1328 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &z_statement, &mode) == FAILURE) {
1329 		return;
1330 	}
1331 
1332 	PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
1333 
1334 	if (php_oci_statement_execute(statement, mode TSRMLS_CC)) {
1335 		RETURN_FALSE;
1336 	}
1337 	RETURN_TRUE;
1338 }
1339 /* }}} */
1340 
1341 /* {{{ proto bool oci_cancel(resource stmt)
1342    Cancel reading from a cursor */
1343 PHP_FUNCTION(oci_cancel)
1344 {
1345 	zval *z_statement;
1346 	php_oci_statement *statement;
1347 
1348 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_statement) == FAILURE) {
1349 		return;
1350 	}
1351 
1352 	PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
1353 
1354 	if (php_oci_statement_cancel(statement TSRMLS_CC)) {
1355 		RETURN_FALSE;
1356 	}
1357 	RETURN_TRUE;
1358 }
1359 /* }}} */
1360 
1361 /* {{{ proto bool oci_fetch(resource stmt)
1362    Prepare a new row of data for reading */
1363 PHP_FUNCTION(oci_fetch)
1364 {
1365 	zval *z_statement;
1366 	php_oci_statement *statement;
1367 	ub4 nrows = 1; /* only one row at a time is supported for now */
1368 
1369 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_statement) == FAILURE) {
1370 		return;
1371 	}
1372 
1373 	PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
1374 
1375 	if (php_oci_statement_fetch(statement, nrows TSRMLS_CC)) {
1376 		RETURN_FALSE;
1377 	}
1378 	RETURN_TRUE;
1379 }
1380 /* }}} */
1381 
1382 /* {{{ proto int ocifetchinto(resource stmt, array &output [, int mode])
1383    Fetch a row of result data into an array */
1384 PHP_FUNCTION(ocifetchinto)
1385 {
1386 	php_oci_fetch_row(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_OCI_NUM, 3);
1387 }
1388 /* }}} */
1389 
1390 /* {{{ proto int oci_fetch_all(resource stmt, array &output[, int skip[, int maxrows[, int flags]]])
1391    Fetch all rows of result data into an array */
1392 PHP_FUNCTION(oci_fetch_all)
1393 {
1394 	zval *z_statement, *array, *element, *tmp;
1395 	php_oci_statement *statement;
1396 	php_oci_out_column **columns;
1397 	zval ***outarrs;
1398 	ub4 nrows = 1;
1399 	int i;
1400 	long rows = 0, flags = 0, skip = 0, maxrows = -1;
1401 
1402 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rz/|lll", &z_statement, &array, &skip, &maxrows, &flags) == FAILURE) {
1403 		return;
1404 	}
1405 
1406 	PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
1407 
1408 	zval_dtor(array);
1409 	array_init(array);
1410 
1411 	while (skip--) {
1412 		if (php_oci_statement_fetch(statement, nrows TSRMLS_CC)) {
1413 			RETURN_LONG(0);
1414 		}
1415 	}
1416 
1417 	if (flags & PHP_OCI_FETCHSTATEMENT_BY_ROW) {
1418 		columns = safe_emalloc(statement->ncolumns, sizeof(php_oci_out_column *), 0);
1419 
1420 		for (i = 0; i < statement->ncolumns; i++) {
1421 			columns[ i ] = php_oci_statement_get_column(statement, i + 1, NULL, 0 TSRMLS_CC);
1422 		}
1423 
1424 		while (!php_oci_statement_fetch(statement, nrows TSRMLS_CC)) {
1425 			zval *row;
1426 
1427 			MAKE_STD_ZVAL(row);
1428 			array_init(row);
1429 
1430 			for (i = 0; i < statement->ncolumns; i++) {
1431 				MAKE_STD_ZVAL(element);
1432 				php_oci_column_to_zval(columns[ i ], element, PHP_OCI_RETURN_LOBS TSRMLS_CC);
1433 
1434 				if (flags & PHP_OCI_NUM) {
1435 					zend_hash_next_index_insert(Z_ARRVAL_P(row), &element, sizeof(zval*), NULL);
1436 				} else { /* default to ASSOC */
1437 #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 1) || (PHP_MAJOR_VERSION > 5)
1438 					/* zend_symtable_update is only available in 5.2+ */
1439 					zend_symtable_update(Z_ARRVAL_P(row), columns[ i ]->name, columns[ i ]->name_len+1, &element, sizeof(zval*), NULL);
1440 #else
1441 					/* This code path means Bug #45458 will remain broken when OCI8 is built with PHP 4 */
1442 					zend_hash_update(Z_ARRVAL_P(row), columns[ i ]->name, columns[ i ]->name_len+1, &element, sizeof(zval*), NULL);
1443 #endif
1444 				}
1445 			}
1446 
1447 			zend_hash_next_index_insert(Z_ARRVAL_P(array), &row, sizeof(zval*), NULL);
1448 			rows++;
1449 
1450 			if (maxrows != -1 && rows == maxrows) {
1451 				php_oci_statement_cancel(statement TSRMLS_CC);
1452 				break;
1453 			}
1454 		}
1455 		efree(columns);
1456 
1457 	} else { /* default to BY_COLUMN */
1458 		columns = safe_emalloc(statement->ncolumns, sizeof(php_oci_out_column *), 0);
1459 		outarrs = safe_emalloc(statement->ncolumns, sizeof(zval*), 0);
1460 
1461 		if (flags & PHP_OCI_NUM) {
1462 			for (i = 0; i < statement->ncolumns; i++) {
1463 				columns[ i ] = php_oci_statement_get_column(statement, i + 1, NULL, 0 TSRMLS_CC);
1464 
1465 				MAKE_STD_ZVAL(tmp);
1466 				array_init(tmp);
1467 				zend_hash_next_index_insert(Z_ARRVAL_P(array), &tmp, sizeof(zval*), (void **) &(outarrs[ i ]));
1468 			}
1469 		} else { /* default to ASSOC */
1470 			for (i = 0; i < statement->ncolumns; i++) {
1471 				columns[ i ] = php_oci_statement_get_column(statement, i + 1, NULL, 0 TSRMLS_CC);
1472 
1473 				MAKE_STD_ZVAL(tmp);
1474 				array_init(tmp);
1475 #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 1) || (PHP_MAJOR_VERSION > 5)
1476 				/* zend_symtable_update is only available in 5.2+ */
1477 				zend_symtable_update(Z_ARRVAL_P(array), columns[ i ]->name, columns[ i ]->name_len+1, (void *) &tmp, sizeof(zval*), (void **) &(outarrs[ i ]));
1478 #else
1479 				/* This code path means Bug #45458 will remain broken when OCI8 is built with PHP 4 */
1480 				zend_hash_update(Z_ARRVAL_P(array), columns[ i ]->name, columns[ i ]->name_len+1, (void *) &tmp, sizeof(zval*), (void **) &(outarrs[ i ]));
1481 #endif
1482 			}
1483 		}
1484 
1485 		while (!php_oci_statement_fetch(statement, nrows TSRMLS_CC)) {
1486 			for (i = 0; i < statement->ncolumns; i++) {
1487 				MAKE_STD_ZVAL(element);
1488 				php_oci_column_to_zval(columns[ i ], element, PHP_OCI_RETURN_LOBS TSRMLS_CC);
1489 				zend_hash_index_update((*(outarrs[ i ]))->value.ht, rows, (void *)&element, sizeof(zval*), NULL);
1490 			}
1491 
1492 			rows++;
1493 
1494 			if (maxrows != -1 && rows == maxrows) {
1495 				php_oci_statement_cancel(statement TSRMLS_CC);
1496 				break;
1497 			}
1498 		}
1499 
1500 		efree(columns);
1501 		efree(outarrs);
1502 	}
1503 
1504 	RETURN_LONG(rows);
1505 }
1506 /* }}} */
1507 
1508 /* {{{ proto object oci_fetch_object( resource stmt )
1509    Fetch a result row as an object */
1510 PHP_FUNCTION(oci_fetch_object)
1511 {
1512 	php_oci_fetch_row(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_OCI_ASSOC | PHP_OCI_RETURN_NULLS, 2);
1513 
1514 	if (Z_TYPE_P(return_value) == IS_ARRAY) {
1515 		object_and_properties_init(return_value, ZEND_STANDARD_CLASS_DEF_PTR, Z_ARRVAL_P(return_value));
1516 	}
1517 }
1518 /* }}} */
1519 
1520 /* {{{ proto array oci_fetch_row( resource stmt )
1521    Fetch a result row as an enumerated array */
1522 PHP_FUNCTION(oci_fetch_row)
1523 {
1524 	php_oci_fetch_row(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_OCI_NUM | PHP_OCI_RETURN_NULLS, 1);
1525 }
1526 /* }}} */
1527 
1528 /* {{{ proto array oci_fetch_assoc( resource stmt )
1529    Fetch a result row as an associative array */
1530 PHP_FUNCTION(oci_fetch_assoc)
1531 {
1532 	php_oci_fetch_row(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_OCI_ASSOC | PHP_OCI_RETURN_NULLS, 1);
1533 }
1534 /* }}} */
1535 
1536 /* {{{ proto array oci_fetch_array( resource stmt [, int mode ])
1537    Fetch a result row as an array */
1538 PHP_FUNCTION(oci_fetch_array)
1539 {
1540 	php_oci_fetch_row(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_OCI_BOTH | PHP_OCI_RETURN_NULLS, 2);
1541 }
1542 /* }}} */
1543 
1544 /* {{{ proto bool oci_free_statement(resource stmt)
1545    Free all resources associated with a statement */
1546 PHP_FUNCTION(oci_free_statement)
1547 {
1548 	zval *z_statement;
1549 	php_oci_statement *statement;
1550 
1551 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_statement) == FAILURE) {
1552 		return;
1553 	}
1554 
1555 	PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
1556 
1557 	zend_list_delete(statement->id);
1558 	RETURN_TRUE;
1559 }
1560 /* }}} */
1561 
1562 /* {{{ proto bool oci_close(resource connection)
1563    Disconnect from database */
1564 PHP_FUNCTION(oci_close)
1565 {
1566 	/* oci_close for pconnect (if old_oci_close_semantics not set) would
1567 	 * release the connection back to the client-side session pool (and to the
1568 	 * server-side pool if Database Resident Connection Pool is being used).
1569 	 * Subsequent pconnects in the same script are not guaranteed to get the
1570 	 * same database session.
1571 	 */
1572 
1573 	zval *z_connection;
1574 	php_oci_connection *connection;
1575 
1576 	if (OCI_G(old_oci_close_semantics)) {
1577 		/* do nothing to keep BC */
1578 		return;
1579 	}
1580 
1581 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_connection) == FAILURE) {
1582 		return;
1583 	}
1584 
1585 	PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
1586 	zend_list_delete(connection->rsrc_id);
1587 
1588 	ZVAL_NULL(z_connection);
1589 
1590 	RETURN_TRUE;
1591 }
1592 /* }}} */
1593 
1594 /* {{{ proto resource oci_new_connect(string user, string pass [, string db])
1595    Connect to an Oracle database and log on. Returns a new session. */
1596 PHP_FUNCTION(oci_new_connect)
1597 {
1598 	php_oci_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
1599 }
1600 /* }}} */
1601 
1602 /* {{{ proto resource oci_connect(string user, string pass [, string db [, string charset [, int session_mode ]])
1603    Connect to an Oracle database and log on. Returns a new session. */
1604 PHP_FUNCTION(oci_connect)
1605 {
1606 	php_oci_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
1607 }
1608 /* }}} */
1609 
1610 /* {{{ proto resource oci_pconnect(string user, string pass [, string db [, string charset ]])
1611    Connect to an Oracle database using a persistent connection and log on. Returns a new session. */
1612 PHP_FUNCTION(oci_pconnect)
1613 {
1614 	php_oci_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
1615 }
1616 /* }}} */
1617 
1618 /* {{{ proto array oci_error([resource stmt|connection|global])
1619    Return the last error of stmt|connection|global. If no error happened returns false. */
1620 PHP_FUNCTION(oci_error)
1621 {
1622 	zval *arg = NULL;
1623 	php_oci_statement *statement;
1624 	php_oci_connection *connection;
1625 	text *errbuf;
1626 	sb4 errcode = 0;
1627 	sword error = OCI_SUCCESS;
1628 	dvoid *errh = NULL;
1629 	ub2 error_offset = 0;
1630 	text *sqltext = NULL;
1631 
1632 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &arg) == FAILURE) {
1633 		return;
1634 	}
1635 
1636 	if (ZEND_NUM_ARGS() > 0) {
1637 		statement = (php_oci_statement *) zend_fetch_resource(&arg TSRMLS_CC, -1, NULL, NULL, 1, le_statement);
1638 
1639 		if (statement) {
1640 			errh = statement->err;
1641 			error = statement->errcode;
1642 
1643 			if (php_oci_fetch_sqltext_offset(statement, &sqltext, &error_offset TSRMLS_CC)) {
1644 				RETURN_FALSE;
1645 			}
1646 			goto go_out;
1647 		}
1648 
1649 		connection = (php_oci_connection *) zend_fetch_resource(&arg TSRMLS_CC, -1, NULL, NULL, 1, le_connection);
1650 		if (connection) {
1651 			errh = connection->err;
1652 			error = connection->errcode;
1653 			goto go_out;
1654 		}
1655 
1656 		connection = (php_oci_connection *) zend_fetch_resource(&arg TSRMLS_CC, -1, NULL, NULL, 1, le_pconnection);
1657 		if (connection) {
1658 			errh = connection->err;
1659 			error = connection->errcode;
1660 			goto go_out;
1661 		}
1662 	} else {
1663 		errh = OCI_G(err);
1664 		error = OCI_G(errcode);
1665 	}
1666 
1667 go_out:
1668 	if (error == OCI_SUCCESS) { /* no error set in the handle */
1669 		RETURN_FALSE;
1670 	}
1671 
1672 	if (!errh) {
1673 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Oci_error: unable to find error handle");
1674 		RETURN_FALSE;
1675 	}
1676 
1677 	errcode = php_oci_fetch_errmsg(errh, &errbuf TSRMLS_CC);
1678 
1679 	if (errcode) {
1680 		array_init(return_value);
1681 		add_assoc_long(return_value, "code", errcode);
1682 		add_assoc_string(return_value, "message", (char*) errbuf, 0);
1683 		add_assoc_long(return_value, "offset", error_offset);
1684 		add_assoc_string(return_value, "sqltext", sqltext ? (char *) sqltext : "", 1);
1685 	} else {
1686 		RETURN_FALSE;
1687 	}
1688 }
1689 /* }}} */
1690 
1691 /* {{{ proto int oci_num_fields(resource stmt)
1692    Return the number of result columns in a statement */
1693 PHP_FUNCTION(oci_num_fields)
1694 {
1695 	zval *z_statement;
1696 	php_oci_statement *statement;
1697 
1698 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_statement) == FAILURE) {
1699 		return;
1700 	}
1701 
1702 	PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
1703 
1704 	RETURN_LONG(statement->ncolumns);
1705 }
1706 /* }}} */
1707 
1708 /* {{{ proto resource oci_parse(resource connection, string statement)
1709    Parse a SQL or PL/SQL statement and return a statement resource */
1710 PHP_FUNCTION(oci_parse)
1711 {
1712 	zval *z_connection;
1713 	php_oci_connection *connection;
1714 	php_oci_statement *statement;
1715 	char *query;
1716 	int query_len;
1717 
1718 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_connection, &query, &query_len) == FAILURE) {
1719 		return;
1720 	}
1721 
1722 	PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
1723 
1724 	statement = php_oci_statement_create(connection, query, query_len TSRMLS_CC);
1725 
1726 	if (statement) {
1727 		RETURN_RESOURCE(statement->id);
1728 	}
1729 	RETURN_FALSE;
1730 }
1731 /* }}} */
1732 
1733 /* {{{ proto bool oci_set_prefetch(resource stmt, int prefetch_rows)
1734   Sets the number of rows to be prefetched on execute to prefetch_rows for stmt */
1735 PHP_FUNCTION(oci_set_prefetch)
1736 {
1737 	zval *z_statement;
1738 	php_oci_statement *statement;
1739 	long size;
1740 
1741 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &z_statement, &size) == FAILURE) {
1742 		return;
1743 	}
1744 
1745 	PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
1746 
1747 	if (php_oci_statement_set_prefetch(statement, size TSRMLS_CC)) {
1748 		RETURN_FALSE;
1749 	}
1750 	RETURN_TRUE;
1751 }
1752 /* }}} */
1753 
1754 /* {{{ proto bool oci_set_client_identifier(resource connection, string value)
1755   Sets the client identifier attribute on the connection */
1756 PHP_FUNCTION(oci_set_client_identifier)
1757 {
1758 	zval *z_connection;
1759 	php_oci_connection *connection;
1760 	char *client_id;
1761 	int client_id_len;
1762 
1763 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_connection, &client_id, &client_id_len) == FAILURE) {
1764 		return;
1765 	}
1766 
1767 	PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
1768 
1769 	PHP_OCI_CALL_RETURN(OCI_G(errcode), OCIAttrSet, ((dvoid *) connection->session, (ub4) OCI_HTYPE_SESSION, (dvoid *) client_id, (ub4) client_id_len, (ub4) OCI_ATTR_CLIENT_IDENTIFIER, OCI_G(err)));
1770 
1771 	if (OCI_G(errcode) != OCI_SUCCESS) {
1772 		php_oci_error(OCI_G(err), OCI_G(errcode) TSRMLS_CC);
1773 		RETURN_FALSE;
1774 	}
1775 
1776 	RETURN_TRUE;
1777 }
1778 /* }}} */
1779 
1780 /* {{{ proto bool oci_set_edition(string value)
1781   Sets the edition attribute for all subsequent connections created */
1782 PHP_FUNCTION(oci_set_edition)
1783 {
1784 #if ((OCI_MAJOR_VERSION > 11) || ((OCI_MAJOR_VERSION == 11) && (OCI_MINOR_VERSION >= 2)))
1785 	char *edition;
1786 	int edition_len;
1787 
1788 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &edition, &edition_len) == FAILURE) {
1789 		return;
1790 	}
1791 
1792 	if (OCI_G(edition)) {
1793 		efree(OCI_G(edition));
1794 		OCI_G(edition) = NULL;
1795 	}
1796 
1797 	if (edition) {
1798 		OCI_G(edition) = (char *)safe_emalloc(edition_len+1, sizeof(text), 0);
1799 		memcpy(OCI_G(edition), edition, edition_len);
1800 		OCI_G(edition)[edition_len] = '\0';
1801 	}
1802 
1803 	RETURN_TRUE;
1804 #else
1805 	php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unsupported attribute type");
1806 	RETURN_FALSE;
1807 #endif
1808 }
1809 /* }}} */
1810 
1811 /* {{{ proto bool oci_set_module_name(resource connection, string value)
1812   Sets the module attribute on the connection */
1813 PHP_FUNCTION(oci_set_module_name)
1814 {
1815 #if (OCI_MAJOR_VERSION >= 10)
1816 	zval *z_connection;
1817 	php_oci_connection *connection;
1818 	char *module;
1819 	int module_len;
1820 
1821 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_connection, &module, &module_len) == FAILURE) {
1822 		return;
1823 	}
1824 
1825 	PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
1826 
1827 	PHP_OCI_CALL_RETURN(OCI_G(errcode), OCIAttrSet, ((dvoid *) connection->session, (ub4) OCI_HTYPE_SESSION, (dvoid *) module, (ub4) module_len, (ub4) OCI_ATTR_MODULE, OCI_G(err)));
1828 
1829 	if (OCI_G(errcode) != OCI_SUCCESS) {
1830 		php_oci_error(OCI_G(err), OCI_G(errcode) TSRMLS_CC);
1831 		RETURN_FALSE;
1832 	}
1833 
1834 	RETURN_TRUE;
1835 #else
1836 	php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unsupported attribute type");
1837 	RETURN_FALSE;
1838 #endif
1839 }
1840 /* }}} */
1841 
1842 /* {{{ proto bool oci_set_action(resource connection, string value)
1843   Sets the action attribute on the connection */
1844 PHP_FUNCTION(oci_set_action)
1845 {
1846 #if (OCI_MAJOR_VERSION >= 10)
1847 	zval *z_connection;
1848 	php_oci_connection *connection;
1849 	char *action;
1850 	int action_len;
1851 
1852 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_connection, &action, &action_len) == FAILURE) {
1853 		return;
1854 	}
1855 
1856 	PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
1857 
1858 	PHP_OCI_CALL_RETURN(OCI_G(errcode), OCIAttrSet, ((dvoid *) connection->session, (ub4) OCI_HTYPE_SESSION, (dvoid *) action, (ub4) action_len, (ub4) OCI_ATTR_ACTION, OCI_G(err)));
1859 
1860 	if (OCI_G(errcode) != OCI_SUCCESS) {
1861 		php_oci_error(OCI_G(err), OCI_G(errcode) TSRMLS_CC);
1862 		RETURN_FALSE;
1863 	}
1864 
1865 	RETURN_TRUE;
1866 #else
1867 	php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unsupported attribute type");
1868 	RETURN_FALSE;
1869 #endif
1870 }
1871 /* }}} */
1872 
1873 /* {{{ proto bool oci_set_client_info(resource connection, string value)
1874   Sets the client info attribute on the connection */
1875 PHP_FUNCTION(oci_set_client_info)
1876 {
1877 #if (OCI_MAJOR_VERSION >= 10)
1878 	zval *z_connection;
1879 	php_oci_connection *connection;
1880 	char *client_info;
1881 	int client_info_len;
1882 
1883 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &z_connection, &client_info, &client_info_len) == FAILURE) {
1884 		return;
1885 	}
1886 
1887 	PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
1888 
1889 	PHP_OCI_CALL_RETURN(OCI_G(errcode), OCIAttrSet, ((dvoid *) connection->session, (ub4) OCI_HTYPE_SESSION, (dvoid *) client_info, (ub4) client_info_len, (ub4) OCI_ATTR_CLIENT_INFO, OCI_G(err)));
1890 
1891 	if (OCI_G(errcode) != OCI_SUCCESS) {
1892 		php_oci_error(OCI_G(err), OCI_G(errcode) TSRMLS_CC);
1893 		RETURN_FALSE;
1894 	}
1895 
1896 	RETURN_TRUE;
1897 #else
1898 	php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unsupported attribute type");
1899 	RETURN_FALSE;
1900 #endif
1901 }
1902 /* }}} */
1903 
1904 /* {{{ proto bool oci_password_change(resource connection, string username, string old_password, string new_password)
1905   Changes the password of an account */
1906 PHP_FUNCTION(oci_password_change)
1907 {
1908 	zval *z_connection;
1909 	char *user, *pass_old, *pass_new, *dbname;
1910 	int user_len, pass_old_len, pass_new_len, dbname_len;
1911 	php_oci_connection *connection;
1912 
1913 #if (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 4) || (PHP_MAJOR_VERSION < 5)
1914 	/* Safe mode has been removed in PHP 5.4 */
1915 	if (PG(safe_mode)) {
1916 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "is disabled in Safe Mode");
1917 		RETURN_FALSE;
1918 	}
1919 #endif
1920 
1921 	if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "rsss", &z_connection, &user, &user_len, &pass_old, &pass_old_len, &pass_new, &pass_new_len) == SUCCESS) {
1922 		PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
1923 
1924 		if (!user_len) {
1925 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "username cannot be empty");
1926 			RETURN_FALSE;
1927 		}
1928 		if (!pass_old_len) {
1929 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "old password cannot be empty");
1930 			RETURN_FALSE;
1931 		}
1932 		if (!pass_new_len) {
1933 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "new password cannot be empty");
1934 			RETURN_FALSE;
1935 		}
1936 
1937 		if (php_oci_password_change(connection, user, user_len, pass_old, pass_old_len, pass_new, pass_new_len TSRMLS_CC)) {
1938 			RETURN_FALSE;
1939 		}
1940 		RETURN_TRUE;
1941 	} else if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS() TSRMLS_CC, "ssss", &dbname, &dbname_len, &user, &user_len, &pass_old, &pass_old_len, &pass_new, &pass_new_len) == SUCCESS) {
1942 
1943 		if (!user_len) {
1944 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "username cannot be empty");
1945 			RETURN_FALSE;
1946 		}
1947 		if (!pass_old_len) {
1948 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "old password cannot be empty");
1949 			RETURN_FALSE;
1950 		}
1951 		if (!pass_new_len) {
1952 			php_error_docref(NULL TSRMLS_CC, E_WARNING, "new password cannot be empty");
1953 			RETURN_FALSE;
1954 		}
1955 
1956 		connection = php_oci_do_connect_ex(user, user_len, pass_old, pass_old_len, pass_new, pass_new_len, dbname, dbname_len, NULL, OCI_DEFAULT, 0, 0 TSRMLS_CC);
1957 		if (!connection) {
1958 			RETURN_FALSE;
1959 		}
1960 		RETURN_RESOURCE(connection->rsrc_id);
1961 	}
1962 	WRONG_PARAM_COUNT;
1963 }
1964 /* }}} */
1965 
1966 /* {{{ proto resource oci_new_cursor(resource connection)
1967    Return a new cursor (Statement-Handle) - use this to bind ref-cursors! */
1968 PHP_FUNCTION(oci_new_cursor)
1969 {
1970 	zval *z_connection;
1971 	php_oci_connection *connection;
1972 	php_oci_statement *statement;
1973 
1974 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_connection) == FAILURE) {
1975 		return;
1976 	}
1977 
1978 	PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
1979 
1980 	statement = php_oci_statement_create(connection, NULL, 0 TSRMLS_CC);
1981 
1982 	if (statement) {
1983 		RETURN_RESOURCE(statement->id);
1984 	}
1985 	RETURN_FALSE;
1986 }
1987 /* }}} */
1988 
1989 /* {{{ proto string oci_result(resource stmt, mixed column)
1990    Return a single column of result data */
1991 PHP_FUNCTION(oci_result)
1992 {
1993 	php_oci_out_column *column;
1994 
1995 	column = php_oci_statement_get_column_helper(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1996 	if(column) {
1997 		php_oci_column_to_zval(column, return_value, 0 TSRMLS_CC);
1998 	}
1999 	else {
2000 		RETURN_FALSE;
2001 	}
2002 }
2003 /* }}} */
2004 
2005 /* {{{ proto string oci_client_version()
2006    Return a string containing runtime client library version information */
2007 PHP_FUNCTION(oci_client_version)
2008 {
2009 	char *version = NULL;
2010 
2011 	php_oci_client_get_version(&version TSRMLS_CC);
2012 	RETURN_STRING(version, 0);
2013 }
2014 /* }}} */
2015 
2016 /* {{{ proto string oci_server_version(resource connection)
2017    Return a string containing server version information */
2018 PHP_FUNCTION(oci_server_version)
2019 {
2020 	zval *z_connection;
2021 	php_oci_connection *connection;
2022 	char *version = NULL;
2023 
2024 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_connection) == FAILURE) {
2025 		return;
2026 	}
2027 
2028 	PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
2029 
2030 	if (php_oci_server_get_version(connection, &version TSRMLS_CC)) {
2031 		RETURN_FALSE;
2032 	}
2033 
2034 	RETURN_STRING(version, 0);
2035 }
2036 /* }}} */
2037 
2038 /* {{{ proto string oci_statement_type(resource stmt)
2039    Return the query type of an OCI statement */
2040 PHP_FUNCTION(oci_statement_type)
2041 {
2042 	zval *z_statement;
2043 	php_oci_statement *statement;
2044 	ub2 type;
2045 
2046 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_statement) == FAILURE) {
2047 		return;
2048 	}
2049 
2050 	PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
2051 
2052 	if (php_oci_statement_get_type(statement, &type TSRMLS_CC)) {
2053 		RETURN_FALSE;
2054 	}
2055 
2056 	switch (type) {
2057 		case OCI_STMT_SELECT:
2058 			RETVAL_STRING("SELECT",1);
2059 			break;
2060 		case OCI_STMT_UPDATE:
2061 			RETVAL_STRING("UPDATE",1);
2062 			break;
2063 		case OCI_STMT_DELETE:
2064 			RETVAL_STRING("DELETE",1);
2065 			break;
2066 		case OCI_STMT_INSERT:
2067 			RETVAL_STRING("INSERT",1);
2068 			break;
2069 		case OCI_STMT_CREATE:
2070 			RETVAL_STRING("CREATE",1);
2071 			break;
2072 		case OCI_STMT_DROP:
2073 			RETVAL_STRING("DROP",1);
2074 			break;
2075 		case OCI_STMT_ALTER:
2076 			RETVAL_STRING("ALTER",1);
2077 			break;
2078 		case OCI_STMT_BEGIN:
2079 			RETVAL_STRING("BEGIN",1);
2080 			break;
2081 		case OCI_STMT_DECLARE:
2082 			RETVAL_STRING("DECLARE",1);
2083 			break;
2084 		case OCI_STMT_CALL:
2085 			RETVAL_STRING("CALL",1);
2086 			break;
2087 		default:
2088 			RETVAL_STRING("UNKNOWN",1);
2089 	}
2090 }
2091 /* }}} */
2092 
2093 /* {{{ proto int oci_num_rows(resource stmt)
2094    Return the row count of an OCI statement */
2095 PHP_FUNCTION(oci_num_rows)
2096 {
2097 	zval *z_statement;
2098 	php_oci_statement *statement;
2099 	ub4 rowcount;
2100 
2101 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &z_statement) == FAILURE) {
2102 		return;
2103 	}
2104 
2105 	PHP_OCI_ZVAL_TO_STATEMENT(z_statement, statement);
2106 
2107 	if (php_oci_statement_get_numrows(statement, &rowcount TSRMLS_CC)) {
2108 		RETURN_FALSE;
2109 	}
2110 	RETURN_LONG(rowcount);
2111 }
2112 /* }}} */
2113 
2114 /* {{{ proto bool oci_free_collection()
2115    Deletes collection object*/
2116 PHP_FUNCTION(oci_free_collection)
2117 {
2118 	zval **tmp, *z_collection = getThis();
2119 	php_oci_collection *collection;
2120 
2121 	if (!getThis()) {
2122 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &z_collection, oci_coll_class_entry_ptr) == FAILURE) {
2123 			return;
2124 		}
2125 	}
2126 
2127 	if (zend_hash_find(Z_OBJPROP_P(z_collection), "collection", sizeof("collection"), (void **)&tmp) == FAILURE) {
2128 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find collection property");
2129 		RETURN_FALSE;
2130 	}
2131 
2132 	PHP_OCI_ZVAL_TO_COLLECTION(*tmp, collection);
2133 
2134 	zend_list_delete(collection->id);
2135 	RETURN_TRUE;
2136 }
2137 /* }}} */
2138 
2139 /* {{{ proto bool oci_collection_append(string value)
2140    Append an object to the collection */
2141 PHP_FUNCTION(oci_collection_append)
2142 {
2143 	zval **tmp, *z_collection = getThis();
2144 	php_oci_collection *collection;
2145 	char *value;
2146 	int value_len;
2147 
2148 	if (getThis()) {
2149 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &value, &value_len) == FAILURE) {
2150 			return;
2151 		}
2152 	}
2153 	else {
2154 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os", &z_collection, oci_coll_class_entry_ptr, &value, &value_len) == FAILURE) {
2155 			return;
2156 		}
2157 	}
2158 
2159 	if (zend_hash_find(Z_OBJPROP_P(z_collection), "collection", sizeof("collection"), (void **)&tmp) == FAILURE) {
2160 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find collection property");
2161 		RETURN_FALSE;
2162 	}
2163 
2164 	PHP_OCI_ZVAL_TO_COLLECTION(*tmp, collection);
2165 
2166 	if (php_oci_collection_append(collection, value, value_len TSRMLS_CC)) {
2167 		RETURN_FALSE;
2168 	}
2169 	RETURN_TRUE;
2170 }
2171 /* }}} */
2172 
2173 /* {{{ proto string oci_collection_element_get(int ndx)
2174    Retrieve the value at collection index ndx */
2175 PHP_FUNCTION(oci_collection_element_get)
2176 {
2177 	zval **tmp, *z_collection = getThis();
2178 	php_oci_collection *collection;
2179 	long element_index;
2180 	zval *value;
2181 
2182 	if (getThis()) {
2183 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &element_index) == FAILURE) {
2184 			return;
2185 		}
2186 	}
2187 	else {
2188 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ol", &z_collection, oci_coll_class_entry_ptr, &element_index) == FAILURE) {
2189 			return;
2190 		}
2191 	}
2192 
2193 	if (zend_hash_find(Z_OBJPROP_P(z_collection), "collection", sizeof("collection"), (void **)&tmp) == FAILURE) {
2194 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find collection property");
2195 		RETURN_FALSE;
2196 	}
2197 
2198 	PHP_OCI_ZVAL_TO_COLLECTION(*tmp, collection);
2199 
2200 	if (php_oci_collection_element_get(collection, element_index, &value TSRMLS_CC)) {
2201 		RETURN_FALSE;
2202 	}
2203 
2204 	*return_value = *value;
2205 	zval_copy_ctor(return_value);
2206 	zval_ptr_dtor(&value);
2207 }
2208 /* }}} */
2209 
2210 /* {{{ proto bool oci_collection_assign(object from)
2211    Assign a collection from another existing collection */
2212 PHP_FUNCTION(oci_collection_assign)
2213 {
2214 	zval **tmp_dest, **tmp_from, *z_collection_dest = getThis(), *z_collection_from;
2215 	php_oci_collection *collection_dest, *collection_from;
2216 
2217 	if (getThis()) {
2218 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &z_collection_from, oci_coll_class_entry_ptr) == FAILURE) {
2219 			return;
2220 		}
2221 	}
2222 	else {
2223 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OO", &z_collection_dest, oci_coll_class_entry_ptr, &z_collection_from, oci_coll_class_entry_ptr) == FAILURE) {
2224 			return;
2225 		}
2226 	}
2227 
2228 	if (zend_hash_find(Z_OBJPROP_P(z_collection_dest), "collection", sizeof("collection"), (void **)&tmp_dest) == FAILURE) {
2229 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find collection property. The first argument should be valid collection object");
2230 		RETURN_FALSE;
2231 	}
2232 
2233 	if (zend_hash_find(Z_OBJPROP_P(z_collection_from), "collection", sizeof("collection"), (void **)&tmp_from) == FAILURE) {
2234 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find collection property. The second argument should be valid collection object");
2235 		RETURN_FALSE;
2236 	}
2237 
2238 	PHP_OCI_ZVAL_TO_COLLECTION(*tmp_dest, collection_dest);
2239 	PHP_OCI_ZVAL_TO_COLLECTION(*tmp_from, collection_from);
2240 
2241 	if (php_oci_collection_assign(collection_dest, collection_from TSRMLS_CC)) {
2242 		RETURN_FALSE;
2243 	}
2244 	RETURN_TRUE;
2245 }
2246 /* }}} */
2247 
2248 /* {{{ proto bool oci_collection_element_assign(int index, string val)
2249    Assign element val to collection at index ndx */
2250 PHP_FUNCTION(oci_collection_element_assign)
2251 {
2252 	zval **tmp, *z_collection = getThis();
2253 	php_oci_collection *collection;
2254 	int value_len;
2255 	long element_index;
2256 	char *value;
2257 
2258 	if (getThis()) {
2259 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &element_index, &value, &value_len) == FAILURE) {
2260 			return;
2261 		}
2262 	}
2263 	else {
2264 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ols", &z_collection, oci_coll_class_entry_ptr, &element_index, &value, &value_len) == FAILURE) {
2265 			return;
2266 		}
2267 	}
2268 
2269 	if (zend_hash_find(Z_OBJPROP_P(z_collection), "collection", sizeof("collection"), (void **)&tmp) == FAILURE) {
2270 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find collection property");
2271 		RETURN_FALSE;
2272 	}
2273 
2274 	PHP_OCI_ZVAL_TO_COLLECTION(*tmp, collection);
2275 
2276 	if (php_oci_collection_element_set(collection, element_index, value, value_len TSRMLS_CC)) {
2277 		RETURN_FALSE;
2278 	}
2279 	RETURN_TRUE;
2280 }
2281 /* }}} */
2282 
2283 /* {{{ proto int oci_collection_size()
2284    Return the size of a collection */
2285 PHP_FUNCTION(oci_collection_size)
2286 {
2287 	zval **tmp, *z_collection = getThis();
2288 	php_oci_collection *collection;
2289 	sb4 size = 0;
2290 
2291 	if (!getThis()) {
2292 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &z_collection, oci_coll_class_entry_ptr) == FAILURE) {
2293 			return;
2294 		}
2295 	}
2296 
2297 	if (zend_hash_find(Z_OBJPROP_P(z_collection), "collection", sizeof("collection"), (void **)&tmp) == FAILURE) {
2298 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find collection property");
2299 		RETURN_FALSE;
2300 	}
2301 
2302 	PHP_OCI_ZVAL_TO_COLLECTION(*tmp, collection);
2303 
2304 	if (php_oci_collection_size(collection, &size TSRMLS_CC)) {
2305 		RETURN_FALSE;
2306 	}
2307 	RETURN_LONG(size);
2308 }
2309 /* }}} */
2310 
2311 /* {{{ proto int oci_collection_max()
2312    Return the max value of a collection. For a varray this is the maximum length of the array */
2313 PHP_FUNCTION(oci_collection_max)
2314 {
2315 	zval **tmp, *z_collection = getThis();
2316 	php_oci_collection *collection;
2317 	long max;
2318 
2319 	if (!getThis()) {
2320 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &z_collection, oci_coll_class_entry_ptr) == FAILURE) {
2321 			return;
2322 		}
2323 	}
2324 
2325 	if (zend_hash_find(Z_OBJPROP_P(z_collection), "collection", sizeof("collection"), (void **)&tmp) == FAILURE) {
2326 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find collection property");
2327 		RETURN_FALSE;
2328 	}
2329 
2330 	PHP_OCI_ZVAL_TO_COLLECTION(*tmp, collection);
2331 
2332 	if (php_oci_collection_max(collection, &max TSRMLS_CC)) {
2333 		RETURN_FALSE;
2334 	}
2335 	RETURN_LONG(max);
2336 }
2337 /* }}} */
2338 
2339 /* {{{ proto bool oci_collection_trim(int num)
2340    Trim num elements from the end of a collection */
2341 PHP_FUNCTION(oci_collection_trim)
2342 {
2343 	zval **tmp, *z_collection = getThis();
2344 	php_oci_collection *collection;
2345 	long trim_size;
2346 
2347 	if (getThis()) {
2348 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &trim_size) == FAILURE) {
2349 			return;
2350 		}
2351 	}
2352 	else {
2353 		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Ol", &z_collection, oci_coll_class_entry_ptr, &trim_size) == FAILURE) {
2354 			return;
2355 		}
2356 	}
2357 
2358 	if (zend_hash_find(Z_OBJPROP_P(z_collection), "collection", sizeof("collection"), (void **)&tmp) == FAILURE) {
2359 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to find collection property");
2360 		RETURN_FALSE;
2361 	}
2362 
2363 	PHP_OCI_ZVAL_TO_COLLECTION(*tmp, collection);
2364 
2365 	if (php_oci_collection_trim(collection, trim_size TSRMLS_CC)) {
2366 		RETURN_FALSE;
2367 	}
2368 	RETURN_TRUE;
2369 }
2370 /* }}} */
2371 
2372 /* {{{ proto object oci_new_collection(resource connection, string tdo [, string schema])
2373    Initialize a new collection */
2374 PHP_FUNCTION(oci_new_collection)
2375 {
2376 	zval *z_connection;
2377 	php_oci_connection *connection;
2378 	php_oci_collection *collection;
2379 	char *tdo, *schema = NULL;
2380 	int tdo_len, schema_len = 0;
2381 
2382 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs|s", &z_connection, &tdo, &tdo_len, &schema, &schema_len) == FAILURE) {
2383 		return;
2384 	}
2385 
2386 	PHP_OCI_ZVAL_TO_CONNECTION(z_connection, connection);
2387 
2388 	if ( (collection = php_oci_collection_create(connection, tdo, tdo_len, schema, schema_len TSRMLS_CC)) ) {
2389 		object_init_ex(return_value, oci_coll_class_entry_ptr);
2390 		add_property_resource(return_value, "collection", collection->id);
2391 	}
2392 	else {
2393 		RETURN_FALSE;
2394 	}
2395 }
2396 /* }}} */
2397 
2398 #endif /* HAVE_OCI8 */
2399 
2400 /*
2401  * Local variables:
2402  * tab-width: 4
2403  * c-basic-offset: 4
2404  * End:
2405  * vim600: noet sw=4 ts=4 fdm=marker
2406  * vim<600: noet sw=4 ts=4
2407  */
2408