C:/Users/vincent/Data/Perso/dev/ocilib/ocilib/src/iterator.c

00001 /*
00002    +----------------------------------------------------------------------+   
00003    |                                                                      |
00004    |                     OCILIB - C Driver for Oracle                     |
00005    |                                                                      |
00006    |                      (C Wrapper for Oracle OCI)                      |
00007    |                                                                      |
00008    +----------------------------------------------------------------------+
00009    |                      Website : http://www.ocilib.net                 |
00010    +----------------------------------------------------------------------+
00011    |               Copyright (c) 2007-2009 Vincent ROGIER                 |
00012    +----------------------------------------------------------------------+
00013    | This library is free software; you can redistribute it and/or        |
00014    | modify it under the terms of the GNU Lesser General Public           |
00015    | License as published by the Free Software Foundation; either         |
00016    | version 2 of the License, or (at your option) any later version.     |
00017    |                                                                      |
00018    | This library is distributed in the hope that it will be useful,      |
00019    | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
00020    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
00021    | Lesser General Public License for more details.                      |
00022    |                                                                      |
00023    | You should have received a copy of the GNU Lesser General Public     |
00024    | License along with this library; if not, write to the Free           |
00025    | Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   |
00026    +----------------------------------------------------------------------+
00027    |          Author: Vincent ROGIER <vince.rogier@gmail.com>             |
00028    +----------------------------------------------------------------------+ 
00029 */
00030 
00031 /* ------------------------------------------------------------------------ *
00032  * $Id: iterator.c, v 3.4.0 2009-07-30 17:40 Vince $
00033  * ------------------------------------------------------------------------ */
00034 
00035 #include "ocilib_internal.h"
00036 
00037 /* ************************************************************************ *
00038  *                            PUBLIC FUNCTIONS
00039  * ************************************************************************ */
00040 
00041 /* ------------------------------------------------------------------------ *
00042  * OCI_IterCreate
00043  * ------------------------------------------------------------------------ */
00044 
00045 OCI_Iter * OCI_API OCI_IterCreate(OCI_Coll *coll)
00046 {
00047     boolean res    = TRUE;
00048     OCI_Iter *iter = NULL;
00049 
00050     OCI_CHECK_INITIALIZED(NULL);
00051 
00052     OCI_CHECK_PTR(OCI_IPC_COLLECTION, coll, NULL);
00053 
00054     /* allocate iterator structure */
00055 
00056     iter = (OCI_Iter *) OCI_MemAlloc(OCI_IPC_ITERATOR, sizeof(*iter), 1, TRUE);
00057 
00058     if (iter != NULL)
00059     {
00060         iter->coll      = coll;
00061         iter->eoc       = FALSE;
00062         iter->boc       = TRUE;
00063 
00064         /* create iterator */
00065 
00066         OCI_CALL2
00067         (
00068             res, iter->coll->con,  
00069             
00070             OCIIterCreate(OCILib.env, iter->coll->con->err, coll->handle, 
00071                           &iter->handle)
00072         )
00073 
00074         /* create data element accessor */
00075 
00076        if (res == TRUE)
00077            iter->elem = OCI_ElemInit(coll->con, &iter->elem, NULL, 
00078                                      (OCIInd *) NULL, coll->typinf);
00079     
00080        if (res == TRUE)
00081            res = (iter->elem != NULL);
00082     
00083     }
00084     else
00085         res = FALSE;
00086 
00087     /* check for success */
00088 
00089     if (res == FALSE)
00090     {
00091         OCI_IterFree(iter);
00092         iter = NULL;
00093     }
00094 
00095     OCI_RESULT(res);
00096 
00097     return iter;
00098 }
00099 
00100 /* ------------------------------------------------------------------------ *
00101  * OCI_IterFree
00102  * ------------------------------------------------------------------------ */
00103 
00104 boolean OCI_API OCI_IterFree(OCI_Iter *iter)
00105 {
00106     boolean res = TRUE;
00107 
00108     OCI_CHECK_PTR(OCI_IPC_ITERATOR, iter, FALSE);
00109     
00110     /* close iterator handle */
00111 
00112     if (iter->handle != NULL)
00113     {
00114         OCI_CALL2
00115         (
00116             res, iter->coll->con,  
00117 
00118             OCIIterDelete(OCILib.env, iter->coll->con->err, &iter->handle)
00119         )
00120     }
00121 
00122     /* free data element accessor */
00123     
00124     if (iter->elem != NULL)
00125     {
00126         res = OCI_ElemFree(iter->elem);
00127         iter->elem = NULL;
00128     }
00129    
00130     /* free iterator structure */
00131 
00132     OCI_FREE(iter);
00133 
00134     OCI_RESULT(res);
00135 
00136     return res;
00137 }
00138 
00139 /* ------------------------------------------------------------------------ *
00140  * OCI_IterGetNext
00141  * ------------------------------------------------------------------------ */
00142 
00143 OCI_Elem * OCI_API OCI_IterGetNext(OCI_Iter *iter)
00144 {
00145     boolean res    = TRUE;
00146     OCI_Elem *elem = NULL;
00147 
00148     OCI_CHECK_PTR(OCI_IPC_ITERATOR, iter, FALSE);
00149     
00150     OCI_CHECK(iter->eoc == TRUE, FALSE);
00151 
00152     iter->elem->init = FALSE;
00153 
00154     OCI_CALL2
00155     (
00156         res, iter->coll->con,  
00157 
00158         OCIIterNext(OCILib.env, iter->coll->con->err, iter->handle,
00159                     &iter->elem->handle, (dvoid **) &iter->elem->pind,
00160                     &iter->eoc)
00161     )
00162 
00163     if ((res == TRUE) && (iter->eoc == FALSE))
00164     {
00165         elem = iter->elem;
00166         elem->ind = *elem->pind;
00167     }
00168        
00169     OCI_RESULT(elem != NULL);
00170 
00171     return elem;
00172 }
00173 
00174 /* ------------------------------------------------------------------------ *
00175  * OCI_IterGetPrev
00176  * ------------------------------------------------------------------------ */
00177 
00178 OCI_Elem * OCI_API OCI_IterGetPrev(OCI_Iter *iter)
00179 {
00180     boolean res    = TRUE;
00181     OCI_Elem *elem = NULL;
00182 
00183     OCI_CHECK_PTR(OCI_IPC_ITERATOR, iter, FALSE);
00184     
00185     OCI_CHECK(iter->boc == TRUE, FALSE);
00186 
00187     iter->elem->init = FALSE;
00188 
00189     OCI_CALL2
00190     (
00191         res, iter->coll->con,  
00192 
00193         OCIIterPrev(OCILib.env, iter->coll->con->err, iter->handle, 
00194                     &iter->elem->handle, (dvoid **) &iter->elem->pind, 
00195                     &iter->boc)
00196     )
00197 
00198     if ((res == TRUE) && (iter->boc == FALSE))
00199     {
00200         elem = iter->elem;
00201         elem->ind = *elem->pind;
00202     }
00203 
00204     OCI_RESULT(elem != NULL);
00205 
00206     return elem;
00207 
00208 }

Generated on Thu Jul 30 17:41:52 2009 for OCILIB (C Driver for Oracle) by  doxygen 1.5.4