• Main Page
  • Modules
  • Data Structures
  • Files
  • File List

D:/Perso/dev/ocilib/ocilib/src/collection.c

00001 /*
00002     +-----------------------------------------------------------------------------------------+
00003     |                                                                                         |
00004     |                               OCILIB - C Driver for Oracle                              |
00005     |                                                                                         |
00006     |                                (C Wrapper for Oracle OCI)                               |
00007     |                                                                                         |
00008     |                              Website : http://www.ocilib.net                            |
00009     |                                                                                         |
00010     |             Copyright (c) 2007-2010 Vincent ROGIER <vince.rogier@ocilib.net>            |
00011     |                                                                                         |
00012     +-----------------------------------------------------------------------------------------+
00013     |                                                                                         |
00014     |             This library is free software; you can redistribute it and/or               |
00015     |             modify it under the terms of the GNU Lesser General Public                  |
00016     |             License as published by the Free Software Foundation; either                |
00017     |             version 2 of the License, or (at your option) any later version.            |
00018     |                                                                                         |
00019     |             This library is distributed in the hope that it will be useful,             |
00020     |             but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00021     |             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU           |
00022     |             Lesser General Public License for more details.                             |
00023     |                                                                                         |
00024     |             You should have received a copy of the GNU Lesser General Public            |
00025     |             License along with this library; if not, write to the Free                  |
00026     |             Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.          |
00027     |                                                                                         |
00028     +-----------------------------------------------------------------------------------------+
00029 */
00030 
00031 /* --------------------------------------------------------------------------------------------- *
00032  * $Id: collection.c, v 3.8.0 2010-10-24 21:53 Vincent Rogier $
00033  * --------------------------------------------------------------------------------------------- */
00034 
00035 #include "ocilib_internal.h"
00036 
00037 /* ********************************************************************************************* *
00038  *                             PRIVATE FUNCTIONS
00039  * ********************************************************************************************* */
00040 
00041 /* --------------------------------------------------------------------------------------------- *
00042  * OCI_CollInit
00043  * --------------------------------------------------------------------------------------------- */
00044 
00045 OCI_Coll * OCI_CollInit
00046 (
00047     OCI_Connection *con,
00048     OCI_Coll      **pcoll,
00049     void           *handle,
00050     OCI_TypeInfo   *typinf
00051 )
00052 {
00053     OCI_Coll *coll = NULL;
00054     boolean res    = TRUE;
00055 
00056     OCI_CHECK(pcoll == NULL, NULL);
00057 
00058     if (*pcoll == NULL)
00059         *pcoll = (OCI_Coll *) OCI_MemAlloc(OCI_IPC_COLLECTION, sizeof(*coll),
00060                                            (size_t) 1, TRUE);
00061 
00062     if (*pcoll != NULL)
00063     {
00064         coll = *pcoll;
00065 
00066         coll->con    = con;
00067         coll->handle = handle;
00068         coll->typinf = typinf;
00069 
00070         if ((coll->handle == NULL) || (coll->hstate == OCI_OBJECT_ALLOCATED_ARRAY))
00071         {
00072             /* allocates handle for non fetched collection */
00073 
00074             if (coll->hstate != OCI_OBJECT_ALLOCATED_ARRAY)
00075             {
00076                 coll->hstate = OCI_OBJECT_ALLOCATED;
00077             }
00078 
00079             OCI_CALL2
00080             (
00081                 res, con,
00082 
00083                 OCI_ObjectNew(OCILib.env, con->err, con->cxt, typinf->ccode,
00084                               typinf->tdo, (void *) NULL, OCI_DURATION_SESSION,
00085                               TRUE, (dvoid **) &coll->handle)
00086             )
00087         }
00088         else
00089             coll->hstate = OCI_OBJECT_FETCHED_CLEAN;
00090     }
00091     else
00092         res = FALSE;
00093 
00094     /* check for failure */
00095 
00096     if (res == FALSE)
00097     {
00098         OCI_CollFree(coll);
00099         coll = NULL;
00100     }
00101 
00102     return coll;
00103 }
00104 
00105 /* ********************************************************************************************* *
00106  *                             PUBLIC FUNCTIONS
00107  * ********************************************************************************************* */
00108 
00109 /* --------------------------------------------------------------------------------------------- *
00110  * OCI_CollCreate
00111  * --------------------------------------------------------------------------------------------- */
00112 
00113 OCI_Coll * OCI_API OCI_CollCreate
00114 (
00115     OCI_TypeInfo *typinf
00116 )
00117 {
00118     OCI_Coll *coll = NULL;
00119 
00120     OCI_CHECK_INITIALIZED(NULL);
00121 
00122     OCI_CHECK_PTR(OCI_IPC_TYPE_INFO, typinf, NULL);
00123     OCI_CHECK(typinf->ccode == OCI_UNKNOWN, NULL)
00124 
00125     coll = OCI_CollInit(typinf->con, &coll, (OCIColl *) NULL, typinf);
00126 
00127     OCI_RESULT(coll != NULL);
00128 
00129     return coll;
00130 }
00131 
00132 /* --------------------------------------------------------------------------------------------- *
00133  * OCI_CollFree
00134  * --------------------------------------------------------------------------------------------- */
00135 
00136 boolean OCI_API OCI_CollFree
00137 (
00138     OCI_Coll *coll
00139 )
00140 {
00141     OCI_CHECK_PTR(OCI_IPC_COLLECTION, coll, FALSE);
00142     OCI_CHECK_OBJECT_FETCHED(coll, FALSE);
00143 
00144     /* free data element accessor */
00145 
00146     if (coll->elem != NULL)
00147     {
00148         coll->elem->hstate = OCI_OBJECT_FETCHED_DIRTY;
00149         OCI_ElemFree(coll->elem);
00150         coll->elem = NULL;
00151     }
00152 
00153     /* free collection for local object */
00154 
00155     if ((coll->hstate == OCI_OBJECT_ALLOCATED      ) ||
00156         (coll->hstate == OCI_OBJECT_ALLOCATED_ARRAY))
00157     {
00158         OCI_OCIObjectFree(OCILib.env, coll->typinf->con->err,
00159                           coll->handle, OCI_OBJECTFREE_NONULL);
00160     }
00161 
00162     if (coll->hstate != OCI_OBJECT_ALLOCATED_ARRAY)
00163     {
00164         OCI_FREE(coll);
00165     }
00166 
00167     OCI_RESULT(TRUE);
00168 
00169     return TRUE;
00170 }
00171 
00172 /* --------------------------------------------------------------------------------------------- *
00173  * OCI_CollArrayCreate
00174  * --------------------------------------------------------------------------------------------- */
00175 
00176 OCI_Coll ** OCI_API OCI_CollArrayCreate
00177 (
00178     OCI_Connection *con,
00179     OCI_TypeInfo   *typinf,
00180     unsigned int    nbelem
00181 )
00182 {
00183     OCI_Array *arr   = NULL;
00184     OCI_Coll **colls = NULL;
00185 
00186     arr = OCI_ArrayCreate(con, nbelem, OCI_CDT_COLLECTION, 0,
00187                           sizeof(OCIColl *), sizeof(OCI_Coll),
00188                           0, typinf);
00189 
00190     if (arr != NULL)
00191     {
00192         colls = (OCI_Coll **) arr->tab_obj;
00193     }
00194 
00195     return colls;
00196 }
00197 
00198 /* --------------------------------------------------------------------------------------------- *
00199  * OCI_CollArrayFree
00200  * --------------------------------------------------------------------------------------------- */
00201 
00202 boolean OCI_API OCI_CollArrayFree
00203 (
00204     OCI_Coll **colls
00205 )
00206 {
00207     return OCI_ArrayFreeFromHandles((void **) colls);
00208 }
00209 
00210 /* --------------------------------------------------------------------------------------------- *
00211  * OCI_CollAssign
00212  * --------------------------------------------------------------------------------------------- */
00213 
00214 boolean OCI_API OCI_CollAssign
00215 (
00216     OCI_Coll *coll,
00217     OCI_Coll *coll_src
00218 )
00219 {
00220     boolean res = TRUE;
00221 
00222     OCI_CHECK_PTR(OCI_IPC_COLLECTION, coll,     FALSE);
00223     OCI_CHECK_PTR(OCI_IPC_COLLECTION, coll_src, FALSE);
00224 
00225     OCI_CHECK_COMPAT(coll->con,
00226                      coll->typinf->cols[0].icode == coll_src->typinf->cols[0].icode,
00227                      FALSE);
00228 
00229     OCI_CALL2
00230     (
00231         res, coll->con,
00232 
00233         OCICollAssign(OCILib.env, coll->con->err, coll_src->handle, coll->handle)
00234     )
00235 
00236     OCI_RESULT(res);
00237 
00238     return res;
00239 }
00240 
00241 /* --------------------------------------------------------------------------------------------- *
00242  * OCI_CollGetType
00243  * --------------------------------------------------------------------------------------------- */
00244 
00245 unsigned int OCI_API OCI_CollGetType
00246 (
00247     OCI_Coll *coll
00248 )
00249 {
00250     unsigned int type = OCI_UNKNOWN;
00251 
00252     OCI_CHECK_PTR(OCI_IPC_COLLECTION, coll, OCI_UNKNOWN);
00253 
00254     if (coll->typinf->ccode == OCI_TYPECODE_TABLE)
00255         type = OCI_COLL_NESTED_TABLE;
00256     else if(coll->typinf->ccode == OCI_TYPECODE_VARRAY)
00257         type = OCI_COLL_VARRAY;
00258 
00259     OCI_RESULT(TRUE);
00260 
00261     return type;
00262 }
00263 
00264 /* --------------------------------------------------------------------------------------------- *
00265  * OCI_CollGetMax
00266  * --------------------------------------------------------------------------------------------- */
00267 
00268 unsigned int OCI_API OCI_CollGetMax
00269 (
00270     OCI_Coll *coll
00271 )
00272 {
00273     int max = 0;
00274 
00275     OCI_CHECK_PTR(OCI_IPC_COLLECTION, coll, 0);
00276 
00277     max = OCICollMax(OCILib.env, coll->handle);
00278 
00279     OCI_RESULT(TRUE);
00280 
00281     return (unsigned int) max;
00282 }
00283 
00284 /* --------------------------------------------------------------------------------------------- *
00285  * OCI_CollGetSize
00286  * --------------------------------------------------------------------------------------------- */
00287 
00288 unsigned int OCI_API OCI_CollGetSize
00289 (
00290     OCI_Coll *coll
00291 )
00292 {
00293     boolean res = TRUE;
00294     sb4 size    = 0;
00295 
00296     OCI_CHECK_PTR(OCI_IPC_COLLECTION, coll, 0);
00297 
00298     OCI_CALL2
00299     (
00300         res, coll->con,
00301 
00302         OCICollSize(OCILib.env, coll->con->err, coll->handle, &size)
00303     )
00304 
00305     OCI_RESULT(res);
00306 
00307     return (unsigned int) size;
00308 }
00309 
00310 /* --------------------------------------------------------------------------------------------- *
00311  * OCI_CollTrim
00312  * --------------------------------------------------------------------------------------------- */
00313 
00314 boolean OCI_API OCI_CollTrim
00315 (
00316     OCI_Coll    *coll,
00317     unsigned int nb_elem
00318 )
00319 {
00320     boolean res       = TRUE;
00321     unsigned int size = 0;
00322 
00323     OCI_CHECK_PTR(OCI_IPC_COLLECTION, coll, FALSE);
00324 
00325     size = OCI_CollGetSize(coll);
00326 
00327     OCI_CHECK_BOUND(coll->con, (sb4) nb_elem, (sb4) 0, (sb4) size, FALSE);
00328 
00329     OCI_CALL2
00330     (
00331         res, coll->con,
00332 
00333         OCICollTrim(OCILib.env, coll->con->err, (sb4) nb_elem, coll->handle)
00334     )
00335 
00336     OCI_RESULT(res);
00337 
00338     return res;
00339 }
00340 
00341 /* --------------------------------------------------------------------------------------------- *
00342  * OCI_CollGetAt
00343  * --------------------------------------------------------------------------------------------- */
00344 
00345 OCI_Elem * OCI_API OCI_CollGetAt
00346 (
00347     OCI_Coll    *coll,
00348     unsigned int index
00349 )
00350 {
00351     boolean res    = TRUE;
00352     boolean exists = FALSE;
00353     void *data     = NULL;
00354     OCIInd *p_ind  = NULL;
00355     OCI_Elem *elem = NULL;
00356 
00357     OCI_CHECK_PTR(OCI_IPC_COLLECTION, coll, NULL);
00358 
00359     OCI_CALL2
00360     (
00361         res, coll->con,
00362 
00363         OCICollGetElem(OCILib.env, coll->con->err, coll->handle, (sb4) index-1,
00364                        &exists, &data, (dvoid **) (dvoid *) &p_ind)
00365     )
00366 
00367     if (res == TRUE && exists == TRUE && data != NULL)
00368     {
00369         elem = coll->elem = OCI_ElemInit(coll->con, &coll->elem,
00370                                          data, p_ind, coll->typinf);
00371     }
00372 
00373     OCI_RESULT(res);
00374 
00375     return elem;
00376 }
00377 
00378 /* --------------------------------------------------------------------------------------------- *
00379  * OCI_CollGetAt2
00380  * --------------------------------------------------------------------------------------------- */
00381 
00382 boolean OCI_API OCI_CollGetAt2
00383 (
00384     OCI_Coll    *coll,
00385     unsigned int index,
00386     OCI_Elem    *elem
00387 )
00388 {
00389     boolean res    = TRUE;
00390     boolean exists = FALSE;
00391     void *data     = NULL;
00392     OCIInd *p_ind  = NULL;
00393 
00394     OCI_CHECK_PTR(OCI_IPC_COLLECTION, coll, FALSE);
00395     OCI_CHECK_PTR(OCI_IPC_ELEMENT, elem, FALSE);
00396 
00397     OCI_CHECK_COMPAT(coll->con,
00398                      elem->typinf->cols[0].type == coll->typinf->cols[0].type,
00399                      FALSE);
00400 
00401     OCI_CALL2
00402     (
00403         res, coll->con,
00404 
00405         OCICollGetElem(OCILib.env, coll->con->err, coll->handle, (sb4) index-1,
00406                        &exists, &data, (dvoid **) (dvoid *) &p_ind)
00407     )
00408 
00409     if (res == TRUE && exists == TRUE && data != NULL)
00410     {
00411         res = (OCI_ElemInit(coll->con, &elem, data, p_ind, coll->typinf) != NULL);
00412     }
00413     else
00414     {
00415         OCI_ElemSetNullIndicator(elem, OCI_IND_NULL);
00416     }
00417 
00418     OCI_RESULT(res);
00419 
00420     return res;
00421 }
00422 
00423 /* --------------------------------------------------------------------------------------------- *
00424  * OCI_CollSetAt
00425  * --------------------------------------------------------------------------------------------- */
00426 
00427 boolean OCI_API OCI_CollSetAt
00428 (
00429     OCI_Coll    *coll,
00430     unsigned int index,
00431     OCI_Elem    *elem
00432 )
00433 {
00434     boolean res = TRUE;
00435 
00436     OCI_CHECK_PTR(OCI_IPC_COLLECTION, coll, FALSE);
00437     OCI_CHECK_PTR(OCI_IPC_ELEMENT, elem, FALSE);
00438 
00439     OCI_CHECK_COMPAT(coll->con,
00440                      elem->typinf->cols[0].type == coll->typinf->cols[0].type,
00441                      FALSE);
00442 
00443     OCI_CALL2
00444     (
00445         res, coll->con,
00446 
00447         OCICollAssignElem(OCILib.env, coll->con->err, (sb4) index-1, elem->handle,
00448                           elem->pind,coll->handle)
00449     )
00450 
00451     OCI_RESULT(res);
00452 
00453     return res;
00454 }
00455 
00456 /* --------------------------------------------------------------------------------------------- *
00457  * OCI_CollAppend
00458  * --------------------------------------------------------------------------------------------- */
00459 
00460 boolean OCI_API OCI_CollAppend
00461 (
00462     OCI_Coll *coll,
00463     OCI_Elem *elem
00464 )
00465 {
00466     boolean res = TRUE;
00467 
00468     OCI_CHECK_PTR(OCI_IPC_COLLECTION, coll, FALSE);
00469     OCI_CHECK_PTR(OCI_IPC_ELEMENT, elem, FALSE);
00470 
00471     OCI_CHECK_COMPAT(coll->con,
00472                      elem->typinf->cols[0].type == coll->typinf->cols[0].type,
00473                      FALSE);
00474 
00475     OCI_CALL2
00476     (
00477         res, coll->con,
00478 
00479         OCICollAppend(OCILib.env, coll->con->err, elem->handle, elem->pind,
00480                       coll->handle)
00481     )
00482 
00483     OCI_RESULT(res);
00484 
00485     return res;
00486 }
00487 
00488 /* --------------------------------------------------------------------------------------------- *
00489  * OCI_CollGetTypeInfo
00490  * --------------------------------------------------------------------------------------------- */
00491 
00492 OCI_TypeInfo * OCI_API OCI_CollGetTypeInfo
00493 (
00494     OCI_Coll *coll
00495 )
00496 {
00497     OCI_CHECK_PTR(OCI_IPC_COLLECTION, coll, NULL);
00498 
00499     OCI_RESULT(TRUE);
00500 
00501     return coll->typinf;
00502 }
00503 
00504 /* --------------------------------------------------------------------------------------------- *
00505  * OCI_CollClear
00506  * --------------------------------------------------------------------------------------------- */
00507 
00508 boolean OCI_API OCI_CollClear
00509 (
00510     OCI_Coll *coll
00511 )
00512 {
00513     boolean res = TRUE;
00514 
00515     unsigned int size = OCI_CollGetSize(coll);
00516 
00517     if (size > 0)
00518     {
00519         res = OCI_CollTrim(coll, size);
00520     }
00521 
00522     OCI_RESULT(res);
00523 
00524     return res;
00525 }
00526 

Generated on Sun Oct 24 2010 22:02:54 for OCILIB (C Driver for Oracle) by  doxygen 1.7.1