1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | 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: oci8_collection.c 272374 2008-12-31 11:17:49Z sebastian $ */
29 :
30 :
31 :
32 : #ifdef HAVE_CONFIG_H
33 : #include "config.h"
34 : #endif
35 :
36 : #include "php.h"
37 : #include "ext/standard/info.h"
38 : #include "php_ini.h"
39 :
40 : #if HAVE_OCI8 && PHP_OCI8_HAVE_COLLECTIONS
41 :
42 : #include "php_oci8.h"
43 : #include "php_oci8_int.h"
44 :
45 : /* {{{ php_oci_collection_create()
46 : Create and return connection handle */
47 : php_oci_collection * php_oci_collection_create(php_oci_connection *connection, char *tdo, int tdo_len, char *schema, int schema_len TSRMLS_DC)
48 70053 : {
49 70053 : dvoid *dschp1 = NULL;
50 : dvoid *parmp1;
51 : dvoid *parmp2;
52 : php_oci_collection *collection;
53 :
54 70053 : collection = emalloc(sizeof(php_oci_collection));
55 :
56 70053 : collection->connection = connection;
57 70053 : collection->collection = NULL;
58 70053 : zend_list_addref(collection->connection->rsrc_id);
59 :
60 : /* get type handle by name */
61 70053 : PHP_OCI_CALL_RETURN(connection->errcode, OCITypeByName,
62 : (
63 : connection->env,
64 : connection->err,
65 : connection->svc,
66 : (text *) schema,
67 : (ub4) schema_len,
68 : (text *) tdo,
69 : (ub4) tdo_len,
70 : (CONST text *) 0,
71 : (ub4) 0,
72 : OCI_DURATION_SESSION,
73 : OCI_TYPEGET_ALL,
74 : &(collection->tdo)
75 : )
76 : );
77 :
78 70053 : if (connection->errcode != OCI_SUCCESS) {
79 6 : goto CLEANUP;
80 : }
81 :
82 : /* allocate describe handle */
83 70047 : PHP_OCI_CALL_RETURN(connection->errcode, OCIHandleAlloc, (connection->env, (dvoid **) &dschp1, (ub4) OCI_HTYPE_DESCRIBE, (size_t) 0, (dvoid **) 0));
84 :
85 70047 : if (connection->errcode != OCI_SUCCESS) {
86 0 : goto CLEANUP;
87 : }
88 :
89 : /* describe TDO */
90 70047 : PHP_OCI_CALL_RETURN(connection->errcode, OCIDescribeAny,
91 : (
92 : connection->svc,
93 : connection->err,
94 : (dvoid *) collection->tdo,
95 : (ub4) 0,
96 : OCI_OTYPE_PTR,
97 : (ub1) OCI_DEFAULT,
98 : (ub1) OCI_PTYPE_TYPE,
99 : dschp1
100 : )
101 : );
102 :
103 70047 : if (connection->errcode != OCI_SUCCESS) {
104 0 : goto CLEANUP;
105 : }
106 :
107 : /* get first parameter handle */
108 70047 : PHP_OCI_CALL_RETURN(connection->errcode, OCIAttrGet, ((dvoid *) dschp1, (ub4) OCI_HTYPE_DESCRIBE, (dvoid *)&parmp1, (ub4 *)0, (ub4)OCI_ATTR_PARAM, connection->err));
109 :
110 70047 : if (connection->errcode != OCI_SUCCESS) {
111 0 : goto CLEANUP;
112 : }
113 :
114 : /* get the collection type code of the attribute */
115 70047 : PHP_OCI_CALL_RETURN(connection->errcode, OCIAttrGet,
116 : (
117 : (dvoid*) parmp1,
118 : (ub4) OCI_DTYPE_PARAM,
119 : (dvoid*) &(collection->coll_typecode),
120 : (ub4 *) 0,
121 : (ub4) OCI_ATTR_COLLECTION_TYPECODE,
122 : connection->err
123 : )
124 : );
125 :
126 70047 : if (connection->errcode != OCI_SUCCESS) {
127 0 : goto CLEANUP;
128 : }
129 :
130 70047 : switch(collection->coll_typecode) {
131 : case OCI_TYPECODE_TABLE:
132 : case OCI_TYPECODE_VARRAY:
133 : /* get collection element handle */
134 70047 : PHP_OCI_CALL_RETURN(connection->errcode, OCIAttrGet,
135 : (
136 : (dvoid*) parmp1,
137 : (ub4) OCI_DTYPE_PARAM,
138 : (dvoid*) &parmp2,
139 : (ub4 *) 0,
140 : (ub4) OCI_ATTR_COLLECTION_ELEMENT,
141 : connection->err
142 : )
143 : );
144 :
145 70047 : if (connection->errcode != OCI_SUCCESS) {
146 0 : goto CLEANUP;
147 : }
148 :
149 : /* get REF of the TDO for the type */
150 70047 : PHP_OCI_CALL_RETURN(connection->errcode, OCIAttrGet,
151 : (
152 : (dvoid*) parmp2,
153 : (ub4) OCI_DTYPE_PARAM,
154 : (dvoid*) &(collection->elem_ref),
155 : (ub4 *) 0,
156 : (ub4) OCI_ATTR_REF_TDO,
157 : connection->err
158 : )
159 : );
160 :
161 70047 : if (connection->errcode != OCI_SUCCESS) {
162 0 : goto CLEANUP;
163 : }
164 :
165 : /* get the TDO (only header) */
166 70047 : PHP_OCI_CALL_RETURN(connection->errcode, OCITypeByRef,
167 : (
168 : connection->env,
169 : connection->err,
170 : collection->elem_ref,
171 : OCI_DURATION_SESSION,
172 : OCI_TYPEGET_HEADER,
173 : &(collection->element_type)
174 : )
175 : );
176 :
177 70047 : if (connection->errcode != OCI_SUCCESS) {
178 0 : goto CLEANUP;
179 : }
180 :
181 : /* get typecode */
182 70047 : PHP_OCI_CALL_RETURN(connection->errcode, OCIAttrGet,
183 : (
184 : (dvoid*) parmp2,
185 : (ub4) OCI_DTYPE_PARAM,
186 : (dvoid*) &(collection->element_typecode),
187 : (ub4 *) 0,
188 : (ub4) OCI_ATTR_TYPECODE,
189 : connection->err
190 : )
191 : );
192 :
193 70047 : if (connection->errcode != OCI_SUCCESS) {
194 0 : goto CLEANUP;
195 : }
196 70047 : break;
197 : /* we only support VARRAYs and TABLEs */
198 : default:
199 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "unknown collection type %d", collection->coll_typecode);
200 : break;
201 : }
202 :
203 : /* Create object to hold return table */
204 70047 : PHP_OCI_CALL_RETURN(connection->errcode, OCIObjectNew,
205 : (
206 : connection->env,
207 : connection->err,
208 : connection->svc,
209 : OCI_TYPECODE_TABLE,
210 : collection->tdo,
211 : (dvoid *)0,
212 : OCI_DURATION_DEFAULT,
213 : TRUE,
214 : (dvoid **) &(collection->collection)
215 : )
216 : );
217 :
218 70047 : if (connection->errcode != OCI_SUCCESS) {
219 0 : goto CLEANUP;
220 : }
221 :
222 : /* free the describe handle (Bug #44113) */
223 70047 : PHP_OCI_CALL(OCIHandleFree, ((dvoid *) dschp1, OCI_HTYPE_DESCRIBE));
224 70047 : PHP_OCI_REGISTER_RESOURCE(collection, le_collection);
225 70047 : return collection;
226 :
227 6 : CLEANUP:
228 :
229 6 : if (dschp1) {
230 : /* free the describe handle (Bug #44113) */
231 0 : PHP_OCI_CALL(OCIHandleFree, ((dvoid *) dschp1, OCI_HTYPE_DESCRIBE));
232 : }
233 6 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
234 6 : php_oci_collection_close(collection TSRMLS_CC);
235 6 : return NULL;
236 : } /* }}} */
237 :
238 : /* {{{ php_oci_collection_size()
239 : Return size of the collection */
240 : int php_oci_collection_size(php_oci_collection *collection, sb4 *size TSRMLS_DC)
241 4 : {
242 4 : php_oci_connection *connection = collection->connection;
243 :
244 4 : PHP_OCI_CALL_RETURN(connection->errcode, OCICollSize, (connection->env, connection->err, collection->collection, (sb4 *)size));
245 :
246 4 : if (connection->errcode != OCI_SUCCESS) {
247 0 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
248 0 : return 1;
249 : }
250 4 : return 0;
251 : } /* }}} */
252 :
253 : /* {{{ php_oci_collection_max()
254 : Return max number of elements in the collection */
255 : int php_oci_collection_max(php_oci_collection *collection, long *max TSRMLS_DC)
256 3 : {
257 3 : php_oci_connection *connection = collection->connection;
258 :
259 3 : PHP_OCI_CALL_RETURN(*max, OCICollMax, (connection->env, collection->collection));
260 :
261 : /* error handling is not necessary here? */
262 3 : return 0;
263 : } /* }}} */
264 :
265 : /* {{{ php_oci_collection_trim()
266 : Trim collection to the given number of elements */
267 : int php_oci_collection_trim(php_oci_collection *collection, long trim_size TSRMLS_DC)
268 8 : {
269 8 : php_oci_connection *connection = collection->connection;
270 :
271 8 : PHP_OCI_CALL_RETURN(connection->errcode, OCICollTrim, (connection->env, connection->err, trim_size, collection->collection));
272 :
273 8 : if (connection->errcode != OCI_SUCCESS) {
274 3 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
275 3 : return 1;
276 : }
277 5 : return 0;
278 : } /* }}} */
279 :
280 : /* {{{ php_oci_collection_append_null()
281 : Append NULL element to the end of the collection */
282 : int php_oci_collection_append_null(php_oci_collection *collection TSRMLS_DC)
283 2 : {
284 2 : OCIInd null_index = OCI_IND_NULL;
285 2 : php_oci_connection *connection = collection->connection;
286 :
287 : /* append NULL element */
288 2 : PHP_OCI_CALL_RETURN(connection->errcode, OCICollAppend, (connection->env, connection->err, (dvoid *)0, &null_index, collection->collection));
289 :
290 2 : if (connection->errcode != OCI_SUCCESS) {
291 0 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
292 0 : return 1;
293 : }
294 2 : return 0;
295 : } /* }}} */
296 :
297 : /* {{{ php_oci_collection_append_date()
298 : Append DATE element to the end of the collection (use "DD-MON-YY" format) */
299 : int php_oci_collection_append_date(php_oci_collection *collection, char *date, int date_len TSRMLS_DC)
300 7 : {
301 7 : OCIInd new_index = OCI_IND_NOTNULL;
302 : OCIDate oci_date;
303 7 : php_oci_connection *connection = collection->connection;
304 :
305 : /* format and language are NULLs, so format is "DD-MON-YY" and language is the default language of the session */
306 7 : PHP_OCI_CALL_RETURN(connection->errcode, OCIDateFromText, (connection->err, (CONST text *)date, date_len, NULL, 0, NULL, 0, &oci_date));
307 :
308 7 : if (connection->errcode != OCI_SUCCESS) {
309 : /* failed to convert string to date */
310 3 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
311 3 : return 1;
312 : }
313 :
314 4 : PHP_OCI_CALL_RETURN(connection->errcode, OCICollAppend,
315 : (
316 : connection->env,
317 : connection->err,
318 : (dvoid *) &oci_date,
319 : (dvoid *) &new_index,
320 : (OCIColl *) collection->collection
321 : )
322 : );
323 :
324 4 : if (connection->errcode != OCI_SUCCESS) {
325 0 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
326 0 : return 1;
327 : }
328 :
329 4 : return 0;
330 : } /* }}} */
331 :
332 : /* {{{ php_oci_collection_append_number()
333 : Append NUMBER to the end of the collection */
334 : int php_oci_collection_append_number(php_oci_collection *collection, char *number, int number_len TSRMLS_DC)
335 14 : {
336 14 : OCIInd new_index = OCI_IND_NOTNULL;
337 : double element_double;
338 : OCINumber oci_number;
339 14 : php_oci_connection *connection = collection->connection;
340 :
341 14 : element_double = zend_strtod(number, NULL);
342 :
343 14 : PHP_OCI_CALL_RETURN(connection->errcode, OCINumberFromReal, (connection->err, &element_double, sizeof(double), &oci_number));
344 :
345 14 : if (connection->errcode != OCI_SUCCESS) {
346 0 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
347 0 : return 1;
348 : }
349 :
350 14 : PHP_OCI_CALL_RETURN(connection->errcode, OCICollAppend,
351 : (
352 : connection->env,
353 : connection->err,
354 : (dvoid *) &oci_number,
355 : (dvoid *) &new_index,
356 : (OCIColl *) collection->collection
357 : )
358 : );
359 :
360 14 : if (connection->errcode != OCI_SUCCESS) {
361 0 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
362 0 : return 1;
363 : }
364 :
365 14 : return 0;
366 : } /* }}} */
367 :
368 : /* {{{ php_oci_collection_append_string()
369 : Append STRING to the end of the collection */
370 : int php_oci_collection_append_string(php_oci_collection *collection, char *element, int element_len TSRMLS_DC)
371 8 : {
372 8 : OCIInd new_index = OCI_IND_NOTNULL;
373 8 : OCIString *ocistr = (OCIString *)0;
374 8 : php_oci_connection *connection = collection->connection;
375 :
376 8 : PHP_OCI_CALL_RETURN(connection->errcode, OCIStringAssignText, (connection->env, connection->err, (CONST oratext *)element, element_len, &ocistr));
377 :
378 8 : if (connection->errcode != OCI_SUCCESS) {
379 0 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
380 0 : return 1;
381 : }
382 :
383 8 : PHP_OCI_CALL_RETURN(connection->errcode, OCICollAppend,
384 : (
385 : connection->env,
386 : connection->err,
387 : (dvoid *) ocistr,
388 : (dvoid *) &new_index,
389 : (OCIColl *) collection->collection
390 : )
391 : );
392 :
393 8 : if (connection->errcode != OCI_SUCCESS) {
394 0 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
395 0 : return 1;
396 : }
397 :
398 8 : return 0;
399 : } /* }}} */
400 :
401 : /* {{{ php_oci_collection_append()
402 : Append wrapper. Appends any supported element to the end of the collection */
403 : int php_oci_collection_append(php_oci_collection *collection, char *element, int element_len TSRMLS_DC)
404 32 : {
405 32 : if (element_len == 0) {
406 2 : return php_oci_collection_append_null(collection TSRMLS_CC);
407 : }
408 :
409 30 : switch(collection->element_typecode) {
410 : case OCI_TYPECODE_DATE:
411 7 : return php_oci_collection_append_date(collection, element, element_len TSRMLS_CC);
412 : break;
413 :
414 : case OCI_TYPECODE_VARCHAR2 :
415 8 : return php_oci_collection_append_string(collection, element, element_len TSRMLS_CC);
416 : break;
417 :
418 : case OCI_TYPECODE_UNSIGNED16 : /* UNSIGNED SHORT */
419 : case OCI_TYPECODE_UNSIGNED32 : /* UNSIGNED LONG */
420 : case OCI_TYPECODE_REAL : /* REAL */
421 : case OCI_TYPECODE_DOUBLE : /* DOUBLE */
422 : case OCI_TYPECODE_INTEGER : /* INT */
423 : case OCI_TYPECODE_SIGNED16 : /* SHORT */
424 : case OCI_TYPECODE_SIGNED32 : /* LONG */
425 : case OCI_TYPECODE_DECIMAL : /* DECIMAL */
426 : case OCI_TYPECODE_FLOAT : /* FLOAT */
427 : case OCI_TYPECODE_NUMBER : /* NUMBER */
428 : case OCI_TYPECODE_SMALLINT : /* SMALLINT */
429 14 : return php_oci_collection_append_number(collection, element, element_len TSRMLS_CC);
430 : break;
431 :
432 : default:
433 1 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unknown or unsupported type of element: %d", collection->element_typecode);
434 1 : return 1;
435 : break;
436 : }
437 : /* never reached */
438 : return 1;
439 : } /* }}} */
440 :
441 : /* {{{ php_oci_collection_element_get()
442 : Get the element with the given index */
443 : int php_oci_collection_element_get(php_oci_collection *collection, long index, zval **result_element TSRMLS_DC)
444 47 : {
445 47 : php_oci_connection *connection = collection->connection;
446 : dvoid *element;
447 : OCIInd *element_index;
448 : boolean exists;
449 : oratext buff[1024];
450 47 : ub4 buff_len = 1024;
451 :
452 47 : MAKE_STD_ZVAL(*result_element);
453 47 : ZVAL_NULL(*result_element);
454 :
455 47 : PHP_OCI_CALL_RETURN(connection->errcode, OCICollGetElem,
456 : (
457 : connection->env,
458 : connection->err,
459 : collection->collection,
460 : (ub4)index,
461 : &exists,
462 : &element,
463 : (dvoid **)&element_index
464 : )
465 : );
466 :
467 47 : if (connection->errcode != OCI_SUCCESS) {
468 0 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
469 0 : FREE_ZVAL(*result_element);
470 0 : return 1;
471 : }
472 :
473 47 : if (exists == 0) {
474 : /* element doesn't exist */
475 18 : FREE_ZVAL(*result_element);
476 18 : return 1;
477 : }
478 :
479 29 : if (*element_index == OCI_IND_NULL) {
480 : /* this is not an error, we're returning NULL here */
481 4 : return 0;
482 : }
483 :
484 25 : switch (collection->element_typecode) {
485 : case OCI_TYPECODE_DATE:
486 4 : PHP_OCI_CALL_RETURN(connection->errcode, OCIDateToText, (connection->err, element, 0, 0, 0, 0, &buff_len, buff));
487 :
488 4 : if (connection->errcode != OCI_SUCCESS) {
489 0 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
490 0 : FREE_ZVAL(*result_element);
491 0 : return 1;
492 : }
493 :
494 4 : ZVAL_STRINGL(*result_element, (char *)buff, buff_len, 1);
495 4 : Z_STRVAL_P(*result_element)[buff_len] = '\0';
496 :
497 4 : return 0;
498 : break;
499 :
500 : case OCI_TYPECODE_VARCHAR2:
501 : {
502 4 : OCIString *oci_string = *(OCIString **)element;
503 : text *str;
504 :
505 4 : PHP_OCI_CALL_RETURN(str, OCIStringPtr, (connection->env, oci_string));
506 :
507 4 : if (str) {
508 4 : ZVAL_STRING(*result_element, (char *)str, 1);
509 : }
510 4 : return 0;
511 : }
512 : break;
513 :
514 : case OCI_TYPECODE_UNSIGNED16: /* UNSIGNED SHORT */
515 : case OCI_TYPECODE_UNSIGNED32: /* UNSIGNED LONG */
516 : case OCI_TYPECODE_REAL: /* REAL */
517 : case OCI_TYPECODE_DOUBLE: /* DOUBLE */
518 : case OCI_TYPECODE_INTEGER: /* INT */
519 : case OCI_TYPECODE_SIGNED16: /* SHORT */
520 : case OCI_TYPECODE_SIGNED32: /* LONG */
521 : case OCI_TYPECODE_DECIMAL: /* DECIMAL */
522 : case OCI_TYPECODE_FLOAT: /* FLOAT */
523 : case OCI_TYPECODE_NUMBER: /* NUMBER */
524 : case OCI_TYPECODE_SMALLINT: /* SMALLINT */
525 : {
526 : double double_number;
527 :
528 17 : PHP_OCI_CALL_RETURN(connection->errcode, OCINumberToReal, (connection->err, (CONST OCINumber *) element, (uword) sizeof(double), (dvoid *) &double_number));
529 :
530 17 : if (connection->errcode != OCI_SUCCESS) {
531 0 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
532 0 : FREE_ZVAL(*result_element);
533 0 : return 1;
534 : }
535 :
536 17 : ZVAL_DOUBLE(*result_element, double_number);
537 :
538 17 : return 0;
539 : }
540 : break;
541 : default:
542 0 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unknown or unsupported type of element: %d", collection->element_typecode);
543 0 : FREE_ZVAL(*result_element);
544 0 : return 1;
545 : break;
546 : }
547 : /* never reached */
548 : return 1;
549 : } /* }}} */
550 :
551 : /* {{{ php_oci_collection_element_set_null()
552 : Set the element with the given index to NULL */
553 : int php_oci_collection_element_set_null(php_oci_collection *collection, long index TSRMLS_DC)
554 3 : {
555 3 : OCIInd null_index = OCI_IND_NULL;
556 3 : php_oci_connection *connection = collection->connection;
557 :
558 : /* set NULL element */
559 3 : PHP_OCI_CALL_RETURN(connection->errcode, OCICollAssignElem, (connection->env, connection->err, (ub4) index, (dvoid *)"", &null_index, collection->collection));
560 :
561 3 : if (connection->errcode != OCI_SUCCESS) {
562 1 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
563 1 : return 1;
564 : }
565 2 : return 0;
566 : } /* }}} */
567 :
568 : /* {{{ php_oci_collection_element_set_date()
569 : Change element's value to the given DATE */
570 : int php_oci_collection_element_set_date(php_oci_collection *collection, long index, char *date, int date_len TSRMLS_DC)
571 3 : {
572 3 : OCIInd new_index = OCI_IND_NOTNULL;
573 : OCIDate oci_date;
574 3 : php_oci_connection *connection = collection->connection;
575 :
576 : /* format and language are NULLs, so format is "DD-MON-YY" and language is the default language of the session */
577 3 : PHP_OCI_CALL_RETURN(connection->errcode, OCIDateFromText, (connection->err, (CONST text *)date, date_len, NULL, 0, NULL, 0, &oci_date));
578 :
579 3 : if (connection->errcode != OCI_SUCCESS) {
580 : /* failed to convert string to date */
581 0 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
582 0 : return 1;
583 : }
584 :
585 3 : PHP_OCI_CALL_RETURN(connection->errcode, OCICollAssignElem,
586 : (
587 : connection->env,
588 : connection->err,
589 : (ub4)index,
590 : (dvoid *) &oci_date,
591 : (dvoid *) &new_index,
592 : (OCIColl *) collection->collection
593 : )
594 : );
595 :
596 3 : if (connection->errcode != OCI_SUCCESS) {
597 1 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
598 1 : return 1;
599 : }
600 :
601 2 : return 0;
602 : } /* }}} */
603 :
604 : /* {{{ php_oci_collection_element_set_number()
605 : Change element's value to the given NUMBER */
606 : int php_oci_collection_element_set_number(php_oci_collection *collection, long index, char *number, int number_len TSRMLS_DC)
607 10 : {
608 10 : OCIInd new_index = OCI_IND_NOTNULL;
609 : double element_double;
610 : OCINumber oci_number;
611 10 : php_oci_connection *connection = collection->connection;
612 :
613 10 : element_double = zend_strtod(number, NULL);
614 :
615 10 : PHP_OCI_CALL_RETURN(connection->errcode, OCINumberFromReal, (connection->err, &element_double, sizeof(double), &oci_number));
616 :
617 10 : if (connection->errcode != OCI_SUCCESS) {
618 0 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
619 0 : return 1;
620 : }
621 :
622 10 : PHP_OCI_CALL_RETURN(connection->errcode, OCICollAssignElem,
623 : (
624 : connection->env,
625 : connection->err,
626 : (ub4) index,
627 : (dvoid *) &oci_number,
628 : (dvoid *) &new_index,
629 : (OCIColl *) collection->collection
630 : )
631 : );
632 :
633 10 : if (connection->errcode != OCI_SUCCESS) {
634 5 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
635 5 : return 1;
636 : }
637 :
638 5 : return 0;
639 : } /* }}} */
640 :
641 : /* {{{ php_oci_collection_element_set_string()
642 : Change element's value to the given string */
643 : int php_oci_collection_element_set_string(php_oci_collection *collection, long index, char *element, int element_len TSRMLS_DC)
644 3 : {
645 3 : OCIInd new_index = OCI_IND_NOTNULL;
646 3 : OCIString *ocistr = (OCIString *)0;
647 3 : php_oci_connection *connection = collection->connection;
648 :
649 3 : PHP_OCI_CALL_RETURN(connection->errcode, OCIStringAssignText, (connection->env, connection->err, (CONST oratext *)element, element_len, &ocistr));
650 :
651 3 : if (connection->errcode != OCI_SUCCESS) {
652 0 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
653 0 : return 1;
654 : }
655 :
656 3 : PHP_OCI_CALL_RETURN(connection->errcode, OCICollAssignElem,
657 : (
658 : connection->env,
659 : connection->err,
660 : (ub4)index,
661 : (dvoid *) ocistr,
662 : (dvoid *) &new_index,
663 : (OCIColl *) collection->collection
664 : )
665 : );
666 :
667 3 : if (connection->errcode != OCI_SUCCESS) {
668 1 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
669 1 : return 1;
670 : }
671 :
672 2 : return 0;
673 : } /* }}} */
674 :
675 : /* {{{ php_oci_collection_element_set()
676 : Collection element setter */
677 : int php_oci_collection_element_set(php_oci_collection *collection, long index, char *value, int value_len TSRMLS_DC)
678 20 : {
679 20 : if (value_len == 0) {
680 3 : return php_oci_collection_element_set_null(collection, index TSRMLS_CC);
681 : }
682 :
683 17 : switch(collection->element_typecode) {
684 : case OCI_TYPECODE_DATE:
685 3 : return php_oci_collection_element_set_date(collection, index, value, value_len TSRMLS_CC);
686 : break;
687 :
688 : case OCI_TYPECODE_VARCHAR2 :
689 3 : return php_oci_collection_element_set_string(collection, index, value, value_len TSRMLS_CC);
690 : break;
691 :
692 : case OCI_TYPECODE_UNSIGNED16 : /* UNSIGNED SHORT */
693 : case OCI_TYPECODE_UNSIGNED32 : /* UNSIGNED LONG */
694 : case OCI_TYPECODE_REAL : /* REAL */
695 : case OCI_TYPECODE_DOUBLE : /* DOUBLE */
696 : case OCI_TYPECODE_INTEGER : /* INT */
697 : case OCI_TYPECODE_SIGNED16 : /* SHORT */
698 : case OCI_TYPECODE_SIGNED32 : /* LONG */
699 : case OCI_TYPECODE_DECIMAL : /* DECIMAL */
700 : case OCI_TYPECODE_FLOAT : /* FLOAT */
701 : case OCI_TYPECODE_NUMBER : /* NUMBER */
702 : case OCI_TYPECODE_SMALLINT : /* SMALLINT */
703 10 : return php_oci_collection_element_set_number(collection, index, value, value_len TSRMLS_CC);
704 : break;
705 :
706 : default:
707 1 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unknown or unsupported type of element: %d", collection->element_typecode);
708 1 : return 1;
709 : break;
710 : }
711 : /* never reached */
712 : return 1;
713 : } /* }}} */
714 :
715 : /* {{{ php_oci_collection_assign()
716 : Assigns a value to the collection from another collection */
717 : int php_oci_collection_assign(php_oci_collection *collection_dest, php_oci_collection *collection_from TSRMLS_DC)
718 11 : {
719 11 : php_oci_connection *connection = collection_dest->connection;
720 :
721 11 : PHP_OCI_CALL_RETURN(connection->errcode, OCICollAssign, (connection->env, connection->err, collection_from->collection, collection_dest->collection));
722 :
723 11 : if (connection->errcode != OCI_SUCCESS) {
724 0 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
725 0 : return 1;
726 : }
727 11 : return 0;
728 : } /* }}} */
729 :
730 : /* {{{ php_oci_collection_close()
731 : Destroy collection and all associated resources */
732 : void php_oci_collection_close(php_oci_collection *collection TSRMLS_DC)
733 70053 : {
734 70053 : php_oci_connection *connection = collection->connection;
735 :
736 70053 : if (collection->collection) {
737 70047 : PHP_OCI_CALL_RETURN(connection->errcode, OCIObjectFree, (connection->env, connection->err, (dvoid *)collection->collection, (ub2)OCI_OBJECTFREE_FORCE));
738 :
739 70047 : if (connection->errcode != OCI_SUCCESS) {
740 0 : php_oci_error(connection->err, connection->errcode TSRMLS_CC);
741 : }
742 : }
743 :
744 70053 : zend_list_delete(collection->connection->rsrc_id);
745 :
746 70053 : efree(collection);
747 : return;
748 : } /* }}} */
749 :
750 : #endif /* HAVE_OCI8 */
751 :
752 : /*
753 : * Local variables:
754 : * tab-width: 4
755 : * c-basic-offset: 4
756 : * End:
757 : * vim600: noet sw=4 ts=4 fdm=marker
758 : * vim<600: noet sw=4 ts=4
759 : */
|