Back to index

php5  5.3.10
oci8_collection.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Stig Sæther Bakken <ssb@php.net>                            |
00016    |          Thies C. Arntzen <thies@thieso.net>                         |
00017    |                                                                      |
00018    | Collection support by Andy Sautins <asautins@veripost.net>           |
00019    | Temporary LOB support by David Benson <dbenson@mancala.com>          |
00020    | ZTS per process OCIPLogon by Harald Radi <harald.radi@nme.at>        |
00021    |                                                                      |
00022    | Redesigned by: Antony Dovgal <antony@zend.com>                       |
00023    |                Andi Gutmans <andi@zend.com>                          |
00024    |                Wez Furlong <wez@omniti.com>                          |
00025    +----------------------------------------------------------------------+
00026 */
00027 
00028 /* $Id: oci8_collection.c 321634 2012-01-01 13:15:04Z felipe $ */
00029 
00030 
00031 
00032 #ifdef HAVE_CONFIG_H
00033 #include "config.h"
00034 #endif
00035 
00036 #include "php.h"
00037 #include "ext/standard/info.h"
00038 #include "php_ini.h"
00039 
00040 #if HAVE_OCI8
00041 
00042 #include "php_oci8.h"
00043 #include "php_oci8_int.h"
00044 
00045 /* {{{ php_oci_collection_create()
00046  Create and return connection handle */
00047 php_oci_collection * php_oci_collection_create(php_oci_connection *connection, char *tdo, int tdo_len, char *schema, int schema_len TSRMLS_DC)
00048 {      
00049        dvoid *dschp1 = NULL;
00050        dvoid *parmp1;
00051        dvoid *parmp2;
00052        php_oci_collection *collection;
00053        
00054        collection = emalloc(sizeof(php_oci_collection));
00055 
00056        collection->connection = connection;
00057        collection->collection = NULL;
00058        zend_list_addref(collection->connection->rsrc_id);
00059 
00060        /* get type handle by name */
00061        PHP_OCI_CALL_RETURN(connection->errcode, OCITypeByName,
00062                      (
00063                       connection->env,
00064                       connection->err,
00065                       connection->svc,
00066                       (text *) schema,
00067                       (ub4) schema_len,
00068                       (text *) tdo,
00069                       (ub4) tdo_len,
00070                       (CONST text *) 0,
00071                       (ub4) 0,
00072                       OCI_DURATION_SESSION,
00073                       OCI_TYPEGET_ALL,
00074                       &(collection->tdo)
00075                      )
00076        );
00077 
00078        if (connection->errcode != OCI_SUCCESS) {
00079               goto CLEANUP;
00080        }
00081 
00082        /* allocate describe handle */
00083        PHP_OCI_CALL_RETURN(connection->errcode, OCIHandleAlloc, (connection->env, (dvoid **) &dschp1, (ub4) OCI_HTYPE_DESCRIBE, (size_t) 0, (dvoid **) 0));
00084 
00085        if (connection->errcode != OCI_SUCCESS) {
00086               goto CLEANUP;
00087        }
00088 
00089        /* describe TDO */
00090        PHP_OCI_CALL_RETURN(connection->errcode, OCIDescribeAny,
00091                      (
00092                       connection->svc,
00093                       connection->err,
00094                       (dvoid *) collection->tdo,
00095                       (ub4) 0,
00096                       OCI_OTYPE_PTR,
00097                       (ub1) OCI_DEFAULT,
00098                       (ub1) OCI_PTYPE_TYPE,
00099                       dschp1
00100                      )
00101        );
00102 
00103        if (connection->errcode != OCI_SUCCESS) {
00104               goto CLEANUP;
00105        }
00106 
00107        /* get first parameter handle */
00108        PHP_OCI_CALL_RETURN(connection->errcode, OCIAttrGet, ((dvoid *) dschp1, (ub4) OCI_HTYPE_DESCRIBE, (dvoid *)&parmp1, (ub4 *)0, (ub4)OCI_ATTR_PARAM, connection->err));
00109 
00110        if (connection->errcode != OCI_SUCCESS) {
00111               goto CLEANUP;
00112        }
00113 
00114        /* get the collection type code of the attribute */
00115        PHP_OCI_CALL_RETURN(connection->errcode, OCIAttrGet,
00116                      (
00117                       (dvoid*) parmp1,
00118                       (ub4) OCI_DTYPE_PARAM,
00119                       (dvoid*) &(collection->coll_typecode),
00120                       (ub4 *) 0,
00121                       (ub4) OCI_ATTR_COLLECTION_TYPECODE,
00122                       connection->err
00123                      )
00124        );
00125 
00126        if (connection->errcode != OCI_SUCCESS) {
00127               goto CLEANUP;
00128        }
00129 
00130        switch(collection->coll_typecode) {
00131               case OCI_TYPECODE_TABLE:
00132               case OCI_TYPECODE_VARRAY:
00133                      /* get collection element handle */
00134                      PHP_OCI_CALL_RETURN(connection->errcode, OCIAttrGet,
00135                                    (
00136                                     (dvoid*) parmp1,
00137                                     (ub4) OCI_DTYPE_PARAM,
00138                                     (dvoid*) &parmp2,
00139                                     (ub4 *) 0,
00140                                     (ub4) OCI_ATTR_COLLECTION_ELEMENT,
00141                                     connection->err
00142                                    )
00143                      );
00144 
00145                      if (connection->errcode != OCI_SUCCESS) {
00146                             goto CLEANUP;
00147                      }
00148 
00149                      /* get REF of the TDO for the type */
00150                      PHP_OCI_CALL_RETURN(connection->errcode, OCIAttrGet,
00151                                    (
00152                                     (dvoid*) parmp2,
00153                                     (ub4) OCI_DTYPE_PARAM,
00154                                     (dvoid*) &(collection->elem_ref),
00155                                     (ub4 *) 0,
00156                                     (ub4) OCI_ATTR_REF_TDO,
00157                                     connection->err
00158                                    )
00159                      );
00160 
00161                      if (connection->errcode != OCI_SUCCESS) {
00162                             goto CLEANUP;
00163                      }
00164 
00165                      /* get the TDO (only header) */
00166                      PHP_OCI_CALL_RETURN(connection->errcode, OCITypeByRef,
00167                                    (
00168                                     connection->env,
00169                                     connection->err,
00170                                     collection->elem_ref,
00171                                     OCI_DURATION_SESSION,
00172                                     OCI_TYPEGET_HEADER,
00173                                     &(collection->element_type)
00174                                    )
00175                      );
00176 
00177                      if (connection->errcode != OCI_SUCCESS) {
00178                             goto CLEANUP;
00179                      }
00180 
00181                      /* get typecode */
00182                      PHP_OCI_CALL_RETURN(connection->errcode, OCIAttrGet,
00183                                    (
00184                                     (dvoid*) parmp2,
00185                                     (ub4) OCI_DTYPE_PARAM,
00186                                     (dvoid*) &(collection->element_typecode),
00187                                     (ub4 *) 0,
00188                                     (ub4) OCI_ATTR_TYPECODE,
00189                                     connection->err
00190                                    )
00191                      );
00192 
00193                      if (connection->errcode != OCI_SUCCESS) {
00194                             goto CLEANUP;
00195                      }
00196                      break;
00197                      /* we only support VARRAYs and TABLEs */
00198               default:
00199                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "unknown collection type %d", collection->coll_typecode);
00200                      break;
00201        }      
00202 
00203        /* Create object to hold return table */
00204        PHP_OCI_CALL_RETURN(connection->errcode, OCIObjectNew,
00205               (
00206                      connection->env,
00207                      connection->err,
00208                      connection->svc,
00209                      OCI_TYPECODE_TABLE,
00210                      collection->tdo,
00211                      (dvoid *)0,
00212                      OCI_DURATION_DEFAULT,
00213                      TRUE,
00214                      (dvoid **) &(collection->collection)
00215               )
00216        );
00217 
00218        if (connection->errcode != OCI_SUCCESS) {
00219               goto CLEANUP;
00220        }
00221 
00222        /* free the describe handle (Bug #44113) */
00223        PHP_OCI_CALL(OCIHandleFree, ((dvoid *) dschp1, OCI_HTYPE_DESCRIBE));
00224        PHP_OCI_REGISTER_RESOURCE(collection, le_collection);
00225        return collection;
00226        
00227 CLEANUP:
00228 
00229        if (dschp1) {
00230               /* free the describe handle (Bug #44113) */
00231               PHP_OCI_CALL(OCIHandleFree, ((dvoid *) dschp1, OCI_HTYPE_DESCRIBE));
00232        }
00233        connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00234        PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00235        php_oci_collection_close(collection TSRMLS_CC);  
00236        return NULL;
00237 } /* }}} */
00238 
00239 /* {{{ php_oci_collection_size()
00240  Return size of the collection */
00241 int php_oci_collection_size(php_oci_collection *collection, sb4 *size TSRMLS_DC)
00242 {
00243        php_oci_connection *connection = collection->connection;
00244        
00245        PHP_OCI_CALL_RETURN(connection->errcode, OCICollSize, (connection->env, connection->err, collection->collection, (sb4 *)size));
00246 
00247        if (connection->errcode != OCI_SUCCESS) {
00248               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00249               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00250               return 1;
00251        }
00252        return 0;
00253 } /* }}} */
00254 
00255 /* {{{ php_oci_collection_max()
00256  Return max number of elements in the collection */
00257 int php_oci_collection_max(php_oci_collection *collection, long *max TSRMLS_DC)
00258 {
00259        php_oci_connection *connection = collection->connection;
00260        
00261        PHP_OCI_CALL_RETURN(*max, OCICollMax, (connection->env, collection->collection));
00262 
00263        /* error handling is not necessary here? */
00264        return 0;
00265 } /* }}} */
00266 
00267 /* {{{ php_oci_collection_trim()
00268  Trim collection to the given number of elements */
00269 int php_oci_collection_trim(php_oci_collection *collection, long trim_size TSRMLS_DC)
00270 {
00271        php_oci_connection *connection = collection->connection;
00272        
00273        PHP_OCI_CALL_RETURN(connection->errcode, OCICollTrim, (connection->env, connection->err, trim_size, collection->collection));
00274 
00275        if (connection->errcode != OCI_SUCCESS) {
00276               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00277               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00278               return 1;
00279        }
00280        return 0;
00281 } /* }}} */
00282 
00283 /* {{{ php_oci_collection_append_null()
00284  Append NULL element to the end of the collection */
00285 int php_oci_collection_append_null(php_oci_collection *collection TSRMLS_DC)
00286 {
00287        OCIInd null_index = OCI_IND_NULL;
00288        php_oci_connection *connection = collection->connection;
00289 
00290        /* append NULL element */
00291        PHP_OCI_CALL_RETURN(connection->errcode, OCICollAppend, (connection->env, connection->err, (dvoid *)0, &null_index, collection->collection));
00292        
00293        if (connection->errcode != OCI_SUCCESS) {
00294               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00295               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00296               return 1;
00297        }
00298        return 0;
00299 } /* }}} */
00300 
00301 /* {{{ php_oci_collection_append_date()
00302  Append DATE element to the end of the collection (use "DD-MON-YY" format) */
00303 int php_oci_collection_append_date(php_oci_collection *collection, char *date, int date_len TSRMLS_DC)
00304 {
00305        OCIInd new_index = OCI_IND_NOTNULL;
00306        OCIDate oci_date;
00307        php_oci_connection *connection = collection->connection;
00308 
00309        /* format and language are NULLs, so format is "DD-MON-YY" and language is the default language of the session */
00310        PHP_OCI_CALL_RETURN(connection->errcode, OCIDateFromText, (connection->err, (CONST text *)date, date_len, NULL, 0, NULL, 0, &oci_date));
00311 
00312        if (connection->errcode != OCI_SUCCESS) {
00313               /* failed to convert string to date */
00314               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00315               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00316               return 1;
00317        }
00318 
00319        PHP_OCI_CALL_RETURN(connection->errcode, OCICollAppend,
00320                      (
00321                       connection->env,
00322                       connection->err,
00323                       (dvoid *) &oci_date,
00324                       (dvoid *) &new_index,
00325                       (OCIColl *) collection->collection
00326                      )
00327        );
00328 
00329        if (connection->errcode != OCI_SUCCESS) {
00330               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00331               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00332               return 1;
00333        }
00334                      
00335        return 0;
00336 } /* }}} */
00337 
00338 /* {{{ php_oci_collection_append_number()
00339  Append NUMBER to the end of the collection */
00340 int php_oci_collection_append_number(php_oci_collection *collection, char *number, int number_len TSRMLS_DC)
00341 {
00342        OCIInd new_index = OCI_IND_NOTNULL;
00343        double element_double;
00344        OCINumber oci_number;
00345        php_oci_connection *connection = collection->connection;
00346 
00347 #if (PHP_MAJOR_VERSION == 4 && PHP_MINOR_VERSION == 3 && PHP_RELEASE_VERSION < 10)
00348        /* minimum PHP version ext/oci8/config.m4 accepts is 4.3.9 */
00349        element_double = strtod(number, NULL);
00350 #else
00351        /* zend_strtod was introduced in PHP 4.3.10 */
00352        element_double = zend_strtod(number, NULL);
00353 #endif
00354                      
00355        PHP_OCI_CALL_RETURN(connection->errcode, OCINumberFromReal, (connection->err, &element_double, sizeof(double), &oci_number));
00356 
00357        if (connection->errcode != OCI_SUCCESS) {
00358               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00359               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00360               return 1;
00361        }
00362 
00363        PHP_OCI_CALL_RETURN(connection->errcode, OCICollAppend,
00364                      (
00365                       connection->env,
00366                       connection->err,
00367                       (dvoid *) &oci_number,
00368                       (dvoid *) &new_index,
00369                       (OCIColl *) collection->collection
00370                      )
00371        );
00372 
00373        if (connection->errcode != OCI_SUCCESS) {
00374               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00375               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00376               return 1;
00377        }
00378 
00379        return 0;
00380 } /* }}} */
00381 
00382 /* {{{ php_oci_collection_append_string()
00383  Append STRING to the end of the collection */
00384 int php_oci_collection_append_string(php_oci_collection *collection, char *element, int element_len TSRMLS_DC)
00385 {
00386        OCIInd new_index = OCI_IND_NOTNULL;
00387        OCIString *ocistr = (OCIString *)0;
00388        php_oci_connection *connection = collection->connection;
00389                      
00390        PHP_OCI_CALL_RETURN(connection->errcode, OCIStringAssignText, (connection->env, connection->err, (CONST oratext *)element, element_len, &ocistr));
00391 
00392        if (connection->errcode != OCI_SUCCESS) {
00393               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00394               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00395               return 1;
00396        }
00397 
00398        PHP_OCI_CALL_RETURN(connection->errcode, OCICollAppend,
00399                      (
00400                       connection->env,
00401                       connection->err,
00402                       (dvoid *) ocistr,
00403                       (dvoid *) &new_index,
00404                       (OCIColl *) collection->collection
00405                      )
00406        );
00407 
00408        if (connection->errcode != OCI_SUCCESS) {
00409               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00410               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00411               return 1;
00412        }
00413 
00414        return 0;
00415 } /* }}} */
00416 
00417 /* {{{ php_oci_collection_append()
00418  Append wrapper. Appends any supported element to the end of the collection */
00419 int php_oci_collection_append(php_oci_collection *collection, char *element, int element_len TSRMLS_DC)
00420 {
00421        if (element_len == 0) {
00422               return php_oci_collection_append_null(collection TSRMLS_CC);
00423        }
00424        
00425        switch(collection->element_typecode) {
00426               case OCI_TYPECODE_DATE:
00427                      return php_oci_collection_append_date(collection, element, element_len TSRMLS_CC);
00428                      break;
00429                      
00430               case OCI_TYPECODE_VARCHAR2 :
00431                      return php_oci_collection_append_string(collection, element, element_len TSRMLS_CC);
00432                      break;
00433 
00434               case OCI_TYPECODE_UNSIGNED16 :                                         /* UNSIGNED SHORT   */
00435               case OCI_TYPECODE_UNSIGNED32 :                                          /* UNSIGNED LONG   */
00436               case OCI_TYPECODE_REAL :                                                             /* REAL      */
00437               case OCI_TYPECODE_DOUBLE :                                                           /* DOUBLE    */
00438               case OCI_TYPECODE_INTEGER :                                                                /* INT */
00439               case OCI_TYPECODE_SIGNED16 :                                                          /* SHORT    */
00440               case OCI_TYPECODE_SIGNED32 :                                                           /* LONG    */
00441               case OCI_TYPECODE_DECIMAL :                                                         /* DECIMAL    */
00442               case OCI_TYPECODE_FLOAT :                                                           /* FLOAT      */
00443               case OCI_TYPECODE_NUMBER :                                                          /* NUMBER     */
00444               case OCI_TYPECODE_SMALLINT :                                                        /* SMALLINT */
00445                      return php_oci_collection_append_number(collection, element, element_len TSRMLS_CC);
00446                      break;
00447 
00448               default:
00449                      php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unknown or unsupported type of element: %d", collection->element_typecode);
00450                      return 1;
00451                      break;
00452        }
00453        /* never reached */
00454        return 1;
00455 } /* }}} */
00456 
00457 /* {{{ php_oci_collection_element_get()
00458  Get the element with the given index */
00459 int php_oci_collection_element_get(php_oci_collection *collection, long index, zval **result_element TSRMLS_DC)
00460 {
00461        php_oci_connection *connection = collection->connection;
00462        dvoid *element;
00463        OCIInd *element_index;
00464        boolean exists;
00465        oratext buff[1024];
00466        ub4 buff_len = 1024;
00467        
00468        MAKE_STD_ZVAL(*result_element);
00469        ZVAL_NULL(*result_element);
00470 
00471        PHP_OCI_CALL_RETURN(connection->errcode, OCICollGetElem,
00472                      (
00473                       connection->env,
00474                       connection->err,
00475                       collection->collection,
00476                       (ub4)index,
00477                       &exists,
00478                       &element,
00479                       (dvoid **)&element_index
00480                      )
00481        );
00482 
00483        if (connection->errcode != OCI_SUCCESS) {
00484               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00485               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00486               FREE_ZVAL(*result_element);
00487               return 1;
00488        }
00489        
00490        if (exists == 0) {
00491               /* element doesn't exist */
00492               FREE_ZVAL(*result_element);
00493               return 1;
00494        }
00495 
00496        if (*element_index == OCI_IND_NULL) {
00497               /* this is not an error, we're returning NULL here */
00498               return 0;
00499        }
00500 
00501        switch (collection->element_typecode) {
00502               case OCI_TYPECODE_DATE:
00503                      PHP_OCI_CALL_RETURN(connection->errcode, OCIDateToText, (connection->err, element, 0, 0, 0, 0, &buff_len, buff));
00504        
00505                      if (connection->errcode != OCI_SUCCESS) {
00506                             connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00507                             PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00508                             FREE_ZVAL(*result_element);
00509                             return 1;
00510                      }
00511 
00512                      ZVAL_STRINGL(*result_element, (char *)buff, buff_len, 1);
00513                      Z_STRVAL_P(*result_element)[buff_len] = '\0';
00514                      
00515                      return 0;
00516                      break;
00517 
00518               case OCI_TYPECODE_VARCHAR2:
00519               {
00520                      OCIString *oci_string = *(OCIString **)element;
00521                      text *str;
00522                      
00523                      PHP_OCI_CALL_RETURN(str, OCIStringPtr, (connection->env, oci_string));
00524                      
00525                      if (str) {
00526                             ZVAL_STRING(*result_element, (char *)str, 1);
00527                      }
00528                      return 0;
00529               }
00530                      break;
00531 
00532               case OCI_TYPECODE_UNSIGNED16:                                         /* UNSIGNED SHORT  */
00533               case OCI_TYPECODE_UNSIGNED32:                                         /* UNSIGNED LONG  */
00534               case OCI_TYPECODE_REAL:                                                      /* REAL          */
00535               case OCI_TYPECODE_DOUBLE:                                             /* DOUBLE  */
00536               case OCI_TYPECODE_INTEGER:                                            /* INT */
00537               case OCI_TYPECODE_SIGNED16:                                           /* SHORT  */
00538               case OCI_TYPECODE_SIGNED32:                                           /* LONG        */
00539               case OCI_TYPECODE_DECIMAL:                                            /* DECIMAL    */
00540               case OCI_TYPECODE_FLOAT:                                              /* FLOAT      */
00541               case OCI_TYPECODE_NUMBER:                                             /* NUMBER     */
00542               case OCI_TYPECODE_SMALLINT:                                           /* SMALLINT */
00543               {
00544                      double double_number;
00545                      
00546                      PHP_OCI_CALL_RETURN(connection->errcode, OCINumberToReal, (connection->err, (CONST OCINumber *) element, (uword) sizeof(double), (dvoid *) &double_number));
00547 
00548                      if (connection->errcode != OCI_SUCCESS) {
00549                             connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00550                             PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00551                             FREE_ZVAL(*result_element);
00552                             return 1;
00553                      }
00554                      
00555                      ZVAL_DOUBLE(*result_element, double_number);
00556 
00557                      return 0;
00558               }
00559                      break;
00560               default:
00561                      php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unknown or unsupported type of element: %d", collection->element_typecode);
00562                      FREE_ZVAL(*result_element);
00563                      return 1;
00564                      break;
00565        }
00566        /* never reached */
00567        return 1;
00568 } /* }}} */
00569 
00570 /* {{{ php_oci_collection_element_set_null()
00571  Set the element with the given index to NULL */
00572 int php_oci_collection_element_set_null(php_oci_collection *collection, long index TSRMLS_DC)
00573 {
00574        OCIInd null_index = OCI_IND_NULL;
00575        php_oci_connection *connection = collection->connection;
00576 
00577        /* set NULL element */
00578        PHP_OCI_CALL_RETURN(connection->errcode, OCICollAssignElem, (connection->env, connection->err, (ub4) index, (dvoid *)"", &null_index, collection->collection));
00579        
00580        if (connection->errcode != OCI_SUCCESS) {
00581               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00582               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00583               return 1;
00584        }
00585        return 0;
00586 } /* }}} */
00587 
00588 /* {{{ php_oci_collection_element_set_date()
00589  Change element's value to the given DATE */
00590 int php_oci_collection_element_set_date(php_oci_collection *collection, long index, char *date, int date_len TSRMLS_DC)
00591 {
00592        OCIInd new_index = OCI_IND_NOTNULL;
00593        OCIDate oci_date;
00594        php_oci_connection *connection = collection->connection;
00595 
00596        /* format and language are NULLs, so format is "DD-MON-YY" and language is the default language of the session */
00597        PHP_OCI_CALL_RETURN(connection->errcode, OCIDateFromText, (connection->err, (CONST text *)date, date_len, NULL, 0, NULL, 0, &oci_date));
00598 
00599        if (connection->errcode != OCI_SUCCESS) {
00600               /* failed to convert string to date */
00601               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00602               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00603               return 1;
00604        }
00605 
00606        PHP_OCI_CALL_RETURN(connection->errcode, OCICollAssignElem,
00607                      (
00608                       connection->env,
00609                       connection->err,
00610                       (ub4)index,
00611                       (dvoid *) &oci_date,
00612                       (dvoid *) &new_index,
00613                       (OCIColl *) collection->collection
00614                       )
00615        );
00616 
00617        if (connection->errcode != OCI_SUCCESS) {
00618               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00619               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00620               return 1;
00621        }
00622                      
00623        return 0;
00624 } /* }}} */
00625 
00626 /* {{{ php_oci_collection_element_set_number()
00627  Change element's value to the given NUMBER */
00628 int php_oci_collection_element_set_number(php_oci_collection *collection, long index, char *number, int number_len TSRMLS_DC)
00629 {
00630        OCIInd new_index = OCI_IND_NOTNULL;
00631        double element_double;
00632        OCINumber oci_number;
00633        php_oci_connection *connection = collection->connection;
00634 
00635 #if (PHP_MAJOR_VERSION == 4 && PHP_MINOR_VERSION == 3 && PHP_RELEASE_VERSION < 10)
00636        /* minimum PHP version ext/oci8/config.m4 accepts is 4.3.9 */
00637        element_double = strtod(number, NULL);
00638 #else
00639        /* zend_strtod was introduced in PHP 4.3.10 */
00640        element_double = zend_strtod(number, NULL);
00641 #endif
00642                      
00643        PHP_OCI_CALL_RETURN(connection->errcode, OCINumberFromReal, (connection->err, &element_double, sizeof(double), &oci_number));
00644 
00645        if (connection->errcode != OCI_SUCCESS) {
00646               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00647               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00648               return 1;
00649        }
00650 
00651        PHP_OCI_CALL_RETURN(connection->errcode, OCICollAssignElem,
00652                      (
00653                       connection->env,
00654                       connection->err,
00655                       (ub4) index,
00656                       (dvoid *) &oci_number,
00657                       (dvoid *) &new_index,
00658                       (OCIColl *) collection->collection
00659                       )
00660        );
00661 
00662        if (connection->errcode != OCI_SUCCESS) {
00663               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00664               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00665               return 1;
00666        }
00667 
00668        return 0;
00669 } /* }}} */
00670 
00671 /* {{{ php_oci_collection_element_set_string()
00672  Change element's value to the given string */
00673 int php_oci_collection_element_set_string(php_oci_collection *collection, long index, char *element, int element_len TSRMLS_DC)
00674 {
00675        OCIInd new_index = OCI_IND_NOTNULL;
00676        OCIString *ocistr = (OCIString *)0;
00677        php_oci_connection *connection = collection->connection;
00678                      
00679        PHP_OCI_CALL_RETURN(connection->errcode, OCIStringAssignText, (connection->env, connection->err, (CONST oratext *)element, element_len, &ocistr));
00680 
00681        if (connection->errcode != OCI_SUCCESS) {
00682               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00683               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00684               return 1;
00685        }
00686 
00687        PHP_OCI_CALL_RETURN(connection->errcode, OCICollAssignElem,
00688                      (
00689                       connection->env,
00690                       connection->err,
00691                       (ub4)index,
00692                       (dvoid *) ocistr,
00693                       (dvoid *) &new_index,
00694                       (OCIColl *) collection->collection
00695                       )
00696        );
00697 
00698        if (connection->errcode != OCI_SUCCESS) {
00699               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00700               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00701               return 1;
00702        }
00703 
00704        return 0;
00705 } /* }}} */
00706 
00707 /* {{{ php_oci_collection_element_set()
00708  Collection element setter */
00709 int php_oci_collection_element_set(php_oci_collection *collection, long index, char *value, int value_len TSRMLS_DC)
00710 {
00711        if (value_len == 0) {
00712               return php_oci_collection_element_set_null(collection, index TSRMLS_CC);
00713        }
00714        
00715        switch(collection->element_typecode) {
00716               case OCI_TYPECODE_DATE:
00717                      return php_oci_collection_element_set_date(collection, index, value, value_len TSRMLS_CC);
00718                      break;
00719                      
00720               case OCI_TYPECODE_VARCHAR2 :
00721                      return php_oci_collection_element_set_string(collection, index, value, value_len TSRMLS_CC);
00722                      break;
00723 
00724               case OCI_TYPECODE_UNSIGNED16 :                                         /* UNSIGNED SHORT   */
00725               case OCI_TYPECODE_UNSIGNED32 :                                          /* UNSIGNED LONG   */
00726               case OCI_TYPECODE_REAL :                                                             /* REAL      */
00727               case OCI_TYPECODE_DOUBLE :                                                           /* DOUBLE    */
00728               case OCI_TYPECODE_INTEGER :                                                                /* INT */
00729               case OCI_TYPECODE_SIGNED16 :                                                          /* SHORT    */
00730               case OCI_TYPECODE_SIGNED32 :                                                           /* LONG    */
00731               case OCI_TYPECODE_DECIMAL :                                                         /* DECIMAL    */
00732               case OCI_TYPECODE_FLOAT :                                                           /* FLOAT      */
00733               case OCI_TYPECODE_NUMBER :                                                          /* NUMBER     */
00734               case OCI_TYPECODE_SMALLINT :                                                        /* SMALLINT */
00735                      return php_oci_collection_element_set_number(collection, index, value, value_len TSRMLS_CC);
00736                      break;
00737 
00738               default:
00739                      php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Unknown or unsupported type of element: %d", collection->element_typecode);
00740                      return 1;
00741                      break;
00742        }
00743        /* never reached */
00744        return 1;
00745 } /* }}} */
00746 
00747 /* {{{ php_oci_collection_assign()
00748  Assigns a value to the collection from another collection */
00749 int php_oci_collection_assign(php_oci_collection *collection_dest, php_oci_collection *collection_from TSRMLS_DC)
00750 {
00751        php_oci_connection *connection = collection_dest->connection;
00752        
00753        PHP_OCI_CALL_RETURN(connection->errcode, OCICollAssign, (connection->env, connection->err, collection_from->collection, collection_dest->collection));
00754 
00755        if (connection->errcode != OCI_SUCCESS) {
00756               connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00757               PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00758               return 1;
00759        }
00760        return 0;
00761 } /* }}} */
00762 
00763 /* {{{ php_oci_collection_close()
00764  Destroy collection and all associated resources */
00765 void php_oci_collection_close(php_oci_collection *collection TSRMLS_DC)
00766 {
00767        php_oci_connection *connection = collection->connection;
00768 
00769        if (collection->collection) {
00770               PHP_OCI_CALL_RETURN(connection->errcode, OCIObjectFree, (connection->env, connection->err, (dvoid *)collection->collection, (ub2)OCI_OBJECTFREE_FORCE));
00771 
00772               if (connection->errcode != OCI_SUCCESS) {
00773                      connection->errcode = php_oci_error(connection->err, connection->errcode TSRMLS_CC);
00774                      PHP_OCI_HANDLE_ERROR(connection, connection->errcode);
00775               }
00776        }
00777        
00778        zend_list_delete(collection->connection->rsrc_id);
00779        
00780        efree(collection);
00781        return;
00782 } /* }}} */
00783 
00784 #endif /* HAVE_OCI8 */
00785 
00786 /*
00787  * Local variables:
00788  * tab-width: 4
00789  * c-basic-offset: 4
00790  * End:
00791  * vim600: noet sw=4 ts=4 fdm=marker
00792  * vim<600: noet sw=4 ts=4
00793  */