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

D:/Perso/dev/ocilib/ocilib/src/long.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: long.c, v 3.8.1 2010-12-13 00:00 Vincent Rogier $
00033  * --------------------------------------------------------------------------------------------- */
00034 
00035 #include "ocilib_internal.h"
00036 
00037 /* ********************************************************************************************* *
00038  *                             PRIVATE FUNCTIONS
00039  * ********************************************************************************************* */
00040 
00041 /* --------------------------------------------------------------------------------------------- *
00042  * OCI_LongInit
00043  * --------------------------------------------------------------------------------------------- */
00044 
00045 OCI_Long * OCI_LongInit
00046 (
00047     OCI_Statement *stmt,
00048     OCI_Long     **plg,
00049     OCI_Define    *def,
00050     unsigned int   type
00051 )
00052 {
00053     boolean res  = TRUE;
00054     OCI_Long *lg = NULL;
00055 
00056     OCI_CHECK(plg == NULL, NULL);
00057 
00058     if (*plg == NULL)
00059         *plg = (OCI_Long *) OCI_MemAlloc(OCI_IPC_LONG, sizeof(*lg),
00060                                          (size_t) 1, TRUE);
00061 
00062     if (*plg != NULL)
00063     {
00064         lg = *plg;
00065 
00066         lg->size   = 0;
00067         lg->stmt   = stmt;
00068         lg->def    = def;
00069         lg->type   = type;
00070         lg->offset = 0;
00071 
00072         if (def != NULL)
00073             lg->hstate = OCI_OBJECT_FETCHED_CLEAN;
00074         else
00075             lg->hstate = OCI_OBJECT_ALLOCATED;
00076     }
00077     else
00078         res = FALSE;
00079 
00080     OCI_RESULT(res);
00081 
00082     return lg;
00083 }
00084 
00085 /* ********************************************************************************************* *
00086  *                            PUBLIC FUNCTIONS
00087  * ********************************************************************************************* */
00088 
00089 /* --------------------------------------------------------------------------------------------- *
00090  * OCI_LongCreate
00091  * --------------------------------------------------------------------------------------------- */
00092 
00093 OCI_Long * OCI_API OCI_LongCreate
00094 (
00095     OCI_Statement *stmt,
00096     unsigned int   type
00097 )
00098 {
00099     OCI_Long *lg = NULL;
00100 
00101     OCI_CHECK_INITIALIZED(NULL);
00102 
00103     OCI_CHECK_PTR(OCI_IPC_STATEMENT, stmt, NULL);
00104 
00105     lg = OCI_LongInit(stmt, &lg, NULL, type);
00106 
00107     OCI_RESULT(lg != NULL);
00108 
00109     return lg;
00110 }
00111 
00112 /* --------------------------------------------------------------------------------------------- *
00113  * OCI_LongFree
00114  * --------------------------------------------------------------------------------------------- */
00115 
00116 boolean OCI_API OCI_LongFree
00117 (
00118     OCI_Long *lg
00119 )
00120 {
00121     OCI_CHECK_PTR(OCI_IPC_LONG, lg, FALSE);
00122 
00123     OCI_CHECK_OBJECT_FETCHED(lg, FALSE);
00124 
00125     OCI_FREE(lg->buffer);
00126     OCI_FREE(lg);
00127 
00128     OCI_RESULT(TRUE);
00129 
00130     return TRUE;
00131 }
00132 
00133 /* --------------------------------------------------------------------------------------------- *
00134  * OCI_LongGetType
00135  * --------------------------------------------------------------------------------------------- */
00136 
00137 unsigned int OCI_API OCI_LongGetType
00138 (
00139     OCI_Long *lg
00140 )
00141 {
00142     OCI_CHECK_PTR(OCI_IPC_LONG, lg, OCI_UNKNOWN);
00143 
00144     OCI_RESULT(TRUE);
00145 
00146     return lg->type;
00147 }
00148 
00149 /* --------------------------------------------------------------------------------------------- *
00150  * OCI_LongRead
00151  * --------------------------------------------------------------------------------------------- */
00152 
00153 unsigned int OCI_API OCI_LongRead
00154 (
00155     OCI_Long    *lg,
00156     void        *buffer,
00157     unsigned int len
00158 )
00159 {
00160     unsigned int size = len;
00161     unsigned int fact = 1;
00162 
00163     OCI_CHECK_PTR(OCI_IPC_LONG, lg, 0);
00164     OCI_CHECK_PTR(OCI_IPC_VOID, buffer, 0);
00165 
00166     OCI_CHECK_MIN(lg->stmt->con, lg->stmt, size, 1, 0);
00167 
00168     OCI_CHECK(lg->offset >= lg->size, 0);
00169 
00170     /* lg->size and lg offset are still expressed in odtext units even
00171        if the buffer had already been expanded to dtext *
00172     */
00173 
00174     if (lg->type == OCI_CLONG)
00175         len *= (unsigned int) sizeof(odtext);
00176 
00177     /* check buffer size to read */
00178 
00179     if ((size + lg->offset) > lg->size)
00180     {
00181         size = lg->size - lg->offset;
00182     }
00183 
00184     /* copy buffer */
00185 
00186     memcpy(buffer, lg->buffer + (size_t) lg->offset*fact, (size_t) (size*fact));
00187 
00188     lg->offset += size;
00189 
00190     if (lg->type == OCI_CLONG)
00191     {
00192         ((dtext *) buffer)[size] = 0;
00193         size                    /= (unsigned int) sizeof(dtext);
00194     }
00195 
00196     OCI_RESULT(TRUE);
00197 
00198     return size;
00199 }
00200 
00201 /* --------------------------------------------------------------------------------------------- *
00202  * OCI_LongWrite
00203  * --------------------------------------------------------------------------------------------- */
00204 
00205 unsigned int OCI_API OCI_LongWrite
00206 (
00207     OCI_Long    *lg,
00208     void        *buffer,
00209     unsigned int len
00210 )
00211 {
00212     boolean res  = TRUE;
00213     sword code   = OCI_SUCCESS;
00214     void *obuf   = NULL;
00215     void *handle = NULL;
00216     ub1 in_out   = OCI_PARAM_IN;
00217     ub1 piece    = OCI_ONE_PIECE;
00218     ub4 type     = 0;
00219     ub4 iter     = 0;
00220     ub4 dx       = 0;
00221     ub4 count    = 0;
00222 
00223     OCI_CHECK_PTR(OCI_IPC_VOID, buffer, 0);
00224     OCI_CHECK_PTR(OCI_IPC_LONG, lg, 0);
00225 
00226     OCI_CHECK_MIN(lg->stmt->con, lg->stmt, len, 1, 0);
00227 
00228     if (lg->type == OCI_CLONG)
00229         len *= (unsigned int) sizeof(dtext);
00230 
00231     if (lg->type == OCI_CLONG)
00232         obuf = OCI_GetInputDataString(buffer, (int *) &len);
00233     else
00234         obuf = buffer;
00235 
00236     /* get piece info */
00237 
00238     OCI_CALL1
00239     (
00240         res, lg->stmt->con, lg->stmt,
00241 
00242         OCIStmtGetPieceInfo(lg->stmt->stmt, lg->stmt->con->err, &handle,
00243                             &type, &in_out, &iter, &dx, &piece)
00244     )
00245 
00246     /* set up piece type */
00247 
00248     if (len > 0)
00249         piece = (ub1) ((lg->size > 0) ? OCI_NEXT_PIECE : OCI_FIRST_PIECE);
00250     else
00251         piece = (ub1) OCI_LAST_PIECE;
00252 
00253     /* correct size to read for last piece */
00254 
00255     if ((lg->size + len) >= lg->stmt->long_size)
00256     {
00257         piece = OCI_LAST_PIECE;
00258         count = lg->stmt->long_size - lg->size;
00259     }
00260     else
00261     {
00262         count = len;
00263     }
00264 
00265     /* set up info for writing */
00266 
00267     OCI_CALL1
00268     (
00269         res, lg->stmt->con, lg->stmt,
00270 
00271         OCIStmtSetPieceInfo(handle, type, lg->stmt->con->err, (dvoid *) obuf,
00272                             &count,  piece, (dvoid *) NULL, (ub2 *) NULL)
00273     )
00274 
00275     /* perform write call */
00276 
00277     if (res == TRUE)
00278     {
00279         code = OCIStmtExecute(lg->stmt->con->cxt, lg->stmt->stmt,
00280                               lg->stmt->con->err, (ub4) 1, (ub4) 0,
00281                               (OCISnapshot *) NULL, (OCISnapshot *) NULL,
00282                               (ub4) 0);
00283     }
00284 
00285     if ((code != OCI_SUCCESS) && (code != OCI_NEED_DATA))
00286     {
00287         if (code == OCI_SUCCESS_WITH_INFO)
00288         {
00289             OCI_ExceptionOCI(lg->stmt->con->err, lg->stmt->con, lg->stmt, TRUE);
00290         }
00291         else
00292         {
00293             OCI_ExceptionOCI(lg->stmt->con->err, lg->stmt->con, lg->stmt, FALSE);
00294             res = FALSE;
00295         }
00296     }
00297 
00298     if (lg->type == OCI_CLONG)
00299         OCI_ReleaseDataString(obuf);
00300 
00301     /* update size */
00302 
00303     if (res == TRUE)
00304     {
00305         lg->size += count;
00306 
00307         /* at this point, count is expressed in odtext bytes for character LONGs
00308          **/
00309 
00310         if (lg->type == OCI_CLONG)
00311             count /= (unsigned int) sizeof(odtext);
00312 
00313     }
00314 
00315     OCI_RESULT(res);
00316 
00317     return count;
00318 }
00319 
00320 /* --------------------------------------------------------------------------------------------- *
00321  * OCI_LongGetSize
00322  * --------------------------------------------------------------------------------------------- */
00323 
00324 unsigned int OCI_API OCI_LongGetSize
00325 (
00326     OCI_Long *lg
00327 )
00328 {
00329     unsigned int size = 0;
00330 
00331     OCI_CHECK_PTR(OCI_IPC_LONG, lg, 0);
00332 
00333     size = lg->size;
00334 
00335     if (lg->type == OCI_CLONG)
00336         size /= (unsigned int) sizeof(odtext);
00337 
00338     OCI_RESULT(TRUE);
00339 
00340     return size;
00341 }
00342 
00343 /* --------------------------------------------------------------------------------------------- *
00344  * OCI_LongGetBuffer
00345  * --------------------------------------------------------------------------------------------- */
00346 
00347 void * OCI_API OCI_LongGetBuffer
00348 (
00349     OCI_Long *lg
00350 )
00351 {
00352     OCI_CHECK_PTR(OCI_IPC_LONG, lg, NULL);
00353 
00354     OCI_RESULT(TRUE);
00355 
00356     return (void *) lg->buffer;
00357 }

Generated on Mon Dec 13 2010 22:32:01 for OCILIB (C Driver for Oracle) by  doxygen 1.7.2