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

D:/Perso/dev/ocilib/ocilib/src/bind.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: bind.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_BindFree
00043  * --------------------------------------------------------------------------------------------- */
00044 
00045 boolean OCI_BindFree
00046 (
00047     OCI_Bind *bnd
00048 )
00049 {
00050     if (bnd->alloc == TRUE)
00051         OCI_FREE(bnd->buf.data);
00052 
00053     if (bnd->stmt->bind_alloc_mode == OCI_BAM_INTERNAL)
00054     {
00055         if (bnd->is_array)
00056         {
00057             OCI_ArrayFreeFromHandles(bnd->input);
00058         }
00059         else
00060         {
00061             switch (bnd->type)
00062             {
00063             case OCI_CDT_DATETIME:
00064 
00065                 OCI_DateFree((OCI_Date *) bnd->input);
00066                 break;
00067 
00068             case OCI_CDT_LOB:
00069 
00070                 OCI_LobFree((OCI_Lob *) bnd->input);
00071                 break;
00072 
00073             case OCI_CDT_FILE:
00074 
00075                 OCI_FileFree((OCI_File *) bnd->input);
00076                 break;
00077 
00078             case OCI_CDT_OBJECT:
00079 
00080                 OCI_ObjectFree((OCI_Object *) bnd->input);
00081                 break;
00082 
00083             case OCI_CDT_COLLECTION:
00084 
00085                 OCI_CollFree((OCI_Coll *) bnd->input);;
00086                 break;
00087 
00088             case OCI_CDT_TIMESTAMP:
00089 
00090                 OCI_TimestampFree((OCI_Timestamp *) bnd->input);
00091                 break;
00092 
00093             case OCI_CDT_INTERVAL:
00094 
00095                 OCI_IntervalFree((OCI_Interval *) bnd->input);
00096                 break;
00097             case OCI_CDT_REF:
00098 
00099                 OCI_RefFree((OCI_Ref *) bnd->input);
00100                 break;
00101 
00102             default:
00103 
00104                 OCI_MemFree(bnd->input);
00105             }
00106         }
00107     }
00108 
00109     OCI_FREE(bnd->buf.inds);
00110     OCI_FREE(bnd->buf.obj_inds);
00111     OCI_FREE(bnd->buf.lens);
00112     OCI_FREE(bnd->buf.tmpbuf);
00113 
00114     OCI_FREE(bnd->plrcds);
00115 
00116     OCI_FREE(bnd->name);
00117     OCI_FREE(bnd);
00118 
00119     return TRUE;
00120 }
00121 
00122 /* --------------------------------------------------------------------------------------------- *
00123  * OCI_BindAllocData
00124  * --------------------------------------------------------------------------------------------- */
00125 
00126 boolean OCI_BindAllocData
00127 (
00128     OCI_Bind *bnd
00129 )
00130 {
00131     boolean res = FALSE;
00132 
00133     if (bnd->is_array)
00134     {
00135         unsigned int struct_size = 0;
00136         unsigned int elem_size   = 0;
00137         unsigned int handle_type = 0;
00138 
00139         OCI_Array *arr = NULL;
00140 
00141         switch (bnd->type)
00142         {
00143         case OCI_CDT_NUMERIC:
00144             if (bnd->code == SQLT_VNU)
00145             {
00146                 elem_size   = sizeof(big_int);
00147                 struct_size = sizeof(OCINumber);
00148             }
00149             else
00150             {
00151                 struct_size = bnd->size;
00152             }
00153             break;
00154         case OCI_CDT_DATETIME:
00155             struct_size = sizeof(OCI_Date);
00156             elem_size   = sizeof(OCIDate);
00157             break;
00158         case OCI_CDT_TEXT:
00159             struct_size = bnd->size;
00160             break;
00161         case OCI_CDT_LOB:
00162             struct_size = sizeof(OCI_Lob);
00163             elem_size   = sizeof(OCILobLocator *);
00164             break;
00165         case OCI_CDT_FILE:
00166             struct_size = sizeof(OCI_File);
00167             elem_size   = sizeof(OCILobLocator *);
00168             break;
00169         case OCI_CDT_TIMESTAMP:
00170             struct_size = sizeof(OCI_Timestamp);
00171             elem_size   = sizeof(OCIDateTime *);
00172             break;
00173         case OCI_CDT_INTERVAL:
00174             struct_size = sizeof(OCI_Interval);
00175             elem_size   = sizeof(OCIInterval *);
00176             break;
00177         case OCI_CDT_RAW:
00178             struct_size = bnd->size;
00179             break;
00180         case OCI_CDT_OBJECT:
00181             struct_size = sizeof(OCI_Object);
00182             elem_size   = sizeof(void *);
00183             break;
00184         case OCI_CDT_COLLECTION:
00185             struct_size = sizeof(OCI_Coll);
00186             elem_size   = sizeof(OCIColl *);
00187             break;
00188         case OCI_CDT_REF:
00189             struct_size = sizeof(OCI_Ref);
00190             elem_size   = sizeof(OCIRef *);
00191             break;
00192         }
00193 
00194         arr = OCI_ArrayCreate(bnd->stmt->con,
00195                               bnd->buf.count,
00196                               bnd->type,
00197                               bnd->subtype,
00198                               elem_size,
00199                               struct_size,
00200                               handle_type,
00201                               bnd->typinf);
00202 
00203         if (arr != NULL)
00204         {
00205             if (elem_size > 0)
00206             {
00207                 bnd->buf.data = arr->mem_handle;
00208                 bnd->input    = arr->tab_obj;
00209             }
00210             else
00211             {
00212                 bnd->buf.data = arr->mem_struct;
00213                 bnd->input    = bnd->buf.data;
00214             }
00215         }
00216     }
00217     else
00218     {
00219         switch (bnd->type)
00220         {
00221         case OCI_CDT_NUMERIC:
00222         {
00223             bnd->input = (void **) OCI_MemAlloc(OCI_IPC_VOID,
00224                                                 bnd->size,
00225                                                 1, TRUE);
00226             bnd->buf.data = (void **) bnd->input;
00227             break;
00228         }
00229         case OCI_CDT_DATETIME:
00230         {
00231             OCI_Date *date = OCI_DateCreate(bnd->stmt->con);
00232 
00233             if (date != NULL)
00234             {
00235                 bnd->input    = (void **) date;
00236                 bnd->buf.data = (void **) date->handle;
00237             }
00238 
00239             break;
00240         }
00241         case OCI_CDT_TEXT:
00242         {
00243             bnd->input = (void **) OCI_MemAlloc(OCI_IPC_STRING,
00244                                                 bnd->size,
00245                                                 1, TRUE);
00246             bnd->buf.data = (void **) bnd->input;
00247             break;
00248         }
00249         case OCI_CDT_LOB:
00250         {
00251             OCI_Lob *lob = OCI_LobCreate(bnd->stmt->con, bnd->subtype);
00252 
00253             if (lob != NULL)
00254             {
00255                 bnd->input    = (void **) lob;
00256                 bnd->buf.data = (void **) lob->handle;
00257             }
00258 
00259             break;
00260         }
00261         case OCI_CDT_FILE:
00262         {
00263             OCI_File *file = OCI_FileCreate(bnd->stmt->con,
00264                                             bnd->subtype);
00265 
00266             if (file != NULL)
00267             {
00268                 bnd->input    = (void **) file;
00269                 bnd->buf.data = (void **) file->handle;
00270             }
00271 
00272             break;
00273         }
00274         case OCI_CDT_TIMESTAMP:
00275         {
00276             OCI_Timestamp *tmsp = OCI_TimestampCreate(bnd->stmt->con,
00277                                                       bnd->subtype);
00278 
00279             if (tmsp != NULL)
00280             {
00281                 bnd->input    = (void **) tmsp;
00282                 bnd->buf.data = (void **) tmsp->handle;
00283             }
00284 
00285             break;
00286         }
00287         case OCI_CDT_INTERVAL:
00288         {
00289             OCI_Interval *itv = OCI_IntervalCreate(bnd->stmt->con,
00290                                                    bnd->subtype);
00291 
00292             if (itv != NULL)
00293             {
00294                 bnd->input    = (void **) itv;
00295                 bnd->buf.data = (void **) itv->handle;
00296             }
00297 
00298             break;
00299         }
00300         case OCI_CDT_RAW:
00301         {
00302             bnd->input = (void **) OCI_MemAlloc(OCI_IPC_VOID,
00303                                                 bnd->size,
00304                                                 1, TRUE);
00305             bnd->buf.data = (void **) bnd->input;
00306             break;
00307         }
00308         case OCI_CDT_OBJECT:
00309         {
00310             OCI_Object *obj = OCI_ObjectCreate(bnd->stmt->con,
00311                                                bnd->typinf);
00312 
00313             if (obj != NULL)
00314             {
00315                 bnd->input    = (void **) obj;
00316                 bnd->buf.data = (void **) obj->handle;
00317             }
00318 
00319             break;
00320         }
00321         case OCI_CDT_COLLECTION:
00322         {
00323             OCI_Coll *coll = OCI_CollCreate(bnd->typinf);
00324 
00325             if (coll != NULL)
00326             {
00327                 bnd->input    = (void **) coll;
00328                 bnd->buf.data = (void **) coll->handle;
00329             }
00330 
00331             break;
00332         }
00333         case OCI_CDT_REF:
00334         {
00335             OCI_Ref *ref =  OCI_RefCreate(bnd->stmt->con,
00336                                           bnd->typinf);
00337 
00338             if (ref != NULL)
00339             {
00340                 bnd->input    = (void **) ref;
00341                 bnd->buf.data = (void **) ref->handle;
00342             }
00343 
00344             break;
00345         }
00346         }
00347     }
00348 
00349     res = (bnd->input != NULL);
00350 
00351     return res;
00352 }
00353 
00354 /* ********************************************************************************************* *
00355  *                            PUBLIC FUNCTIONS
00356  * ********************************************************************************************* */
00357 
00358 /* --------------------------------------------------------------------------------------------- *
00359  * OCI_BindGetName
00360  * --------------------------------------------------------------------------------------------- */
00361 
00362 const mtext * OCI_API OCI_BindGetName
00363 (
00364     OCI_Bind *bnd
00365 )
00366 {
00367     OCI_CHECK_PTR(OCI_IPC_BIND, bnd, NULL);
00368 
00369     OCI_RESULT(TRUE);
00370 
00371     return (const mtext *) bnd->name;
00372 }
00373 
00374 /* --------------------------------------------------------------------------------------------- *
00375  * OCI_BindGetType
00376  * --------------------------------------------------------------------------------------------- */
00377 
00378 unsigned int OCI_API OCI_BindGetType
00379 (
00380     OCI_Bind *bnd
00381 )
00382 {
00383     OCI_CHECK_PTR(OCI_IPC_BIND, bnd, OCI_UNKNOWN);
00384 
00385     OCI_RESULT(TRUE);
00386 
00387     return (unsigned int) bnd->type;
00388 }
00389 
00390 /* --------------------------------------------------------------------------------------------- *
00391  * OCI_BindGetSubtype
00392  * --------------------------------------------------------------------------------------------- */
00393 
00394 unsigned int OCI_API OCI_BindGetSubtype
00395 (
00396     OCI_Bind *bnd
00397 )
00398 {
00399     unsigned int type = OCI_UNKNOWN;
00400 
00401     OCI_CHECK_PTR(OCI_IPC_BIND, bnd, OCI_UNKNOWN);
00402 
00403     OCI_RESULT(TRUE);
00404 
00405     if (bnd->type == OCI_CDT_LONG      ||
00406         bnd->type == OCI_CDT_LOB       ||
00407         bnd->type == OCI_CDT_FILE      ||
00408         bnd->type == OCI_CDT_TIMESTAMP ||
00409         bnd->type == OCI_CDT_INTERVAL)
00410     {
00411         type = bnd->subtype;
00412     }
00413 
00414     return type;
00415 }
00416 
00417 /* --------------------------------------------------------------------------------------------- *
00418  * OCI_BindGetDataCount
00419  * --------------------------------------------------------------------------------------------- */
00420 
00421 unsigned int OCI_API OCI_BindGetDataCount
00422 (
00423     OCI_Bind *bnd
00424 )
00425 {
00426     OCI_CHECK_PTR(OCI_IPC_BIND, bnd, 0);
00427 
00428     OCI_RESULT(TRUE);
00429 
00430     return (unsigned int) bnd->buf.count;
00431 }
00432 
00433 /* --------------------------------------------------------------------------------------------- *
00434  * OCI_BindGetData
00435  * --------------------------------------------------------------------------------------------- */
00436 
00437 void * OCI_API OCI_BindGetData
00438 (
00439     OCI_Bind *bnd
00440 )
00441 {
00442     OCI_CHECK_PTR(OCI_IPC_BIND, bnd, NULL);
00443 
00444     OCI_RESULT(TRUE);
00445 
00446     return (void *) bnd->input;
00447 }
00448 
00449 /* --------------------------------------------------------------------------------------------- *
00450  * OCI_BindGetStatement
00451  * --------------------------------------------------------------------------------------------- */
00452 
00453 OCI_EXPORT OCI_Statement * OCI_API OCI_BindGetStatement
00454 (
00455     OCI_Bind *bnd
00456 )
00457 {
00458     OCI_CHECK_PTR(OCI_IPC_BIND, bnd, NULL);
00459 
00460     OCI_RESULT(TRUE);
00461 
00462     return bnd->stmt;
00463 }
00464 
00465 /* --------------------------------------------------------------------------------------------- *
00466  * OCI_BindSetDataSize
00467  * --------------------------------------------------------------------------------------------- */
00468 
00469 boolean OCI_API OCI_BindSetDataSize
00470 (
00471     OCI_Bind    *bnd,
00472     unsigned int size
00473 )
00474 {
00475     return OCI_BindSetDataSizeAtPos(bnd, 1, size);
00476 }
00477 
00478 /* --------------------------------------------------------------------------------------------- *
00479  * OCI_BindSetDataSizeAtPos
00480  * --------------------------------------------------------------------------------------------- */
00481 
00482 boolean OCI_API OCI_BindSetDataSizeAtPos
00483 (
00484     OCI_Bind    *bnd,
00485     unsigned int position,
00486     unsigned int size
00487 )
00488 {
00489     boolean res = FALSE;
00490 
00491     OCI_CHECK_PTR(OCI_IPC_BIND, bnd, FALSE);
00492     OCI_CHECK_BOUND(bnd->stmt->con, position, 1, bnd->buf.count, FALSE);
00493     OCI_CHECK_MIN(bnd->stmt->con, bnd->stmt, size, 1, FALSE);
00494 
00495     if (bnd->buf.lens != NULL)
00496     {
00497 
00498         if (bnd->type == OCI_CDT_TEXT)
00499         {
00500             if (bnd->size == (sb4) size)
00501                 size += (unsigned int) (size_t) sizeof(odtext);
00502 
00503             size *= (unsigned int) sizeof(odtext);
00504         }
00505 
00506         ((ub2 *) bnd->buf.lens)[position-1] = (ub2) size;
00507 
00508         res = TRUE;
00509     }
00510 
00511     OCI_RESULT(TRUE);
00512 
00513     return res;
00514 }
00515 
00516 /* --------------------------------------------------------------------------------------------- *
00517  * OCI_BindGetDataSize
00518  * --------------------------------------------------------------------------------------------- */
00519 
00520 unsigned int OCI_API OCI_BindGetDataSize
00521 (
00522     OCI_Bind *bnd
00523 )
00524 {
00525     return OCI_BindGetDataSizeAtPos(bnd, 1);
00526 }
00527 
00528 /* --------------------------------------------------------------------------------------------- *
00529  * OCI_BindGetDataSizeAtPos
00530  * --------------------------------------------------------------------------------------------- */
00531 
00532 unsigned int OCI_API OCI_BindGetDataSizeAtPos
00533 (
00534     OCI_Bind    *bnd,
00535     unsigned int position
00536 )
00537 {
00538     unsigned int size = 0;
00539 
00540     OCI_CHECK_PTR(OCI_IPC_BIND, bnd, 0);
00541     OCI_CHECK_BOUND(bnd->stmt->con, position, 1, bnd->buf.count, 0);
00542 
00543     if (bnd->buf.lens != NULL)
00544     {
00545         size = (unsigned int) ((ub2 *) bnd->buf.lens)[position-1];
00546 
00547         if (bnd->type == OCI_CDT_TEXT)
00548         {
00549             if (bnd->size == (sb4) size)
00550                 size -= (unsigned int) sizeof(odtext);
00551 
00552             size /= (unsigned int) sizeof(odtext);
00553         }
00554     }
00555 
00556     OCI_RESULT(TRUE);
00557 
00558     return size;
00559 }
00560 
00561 /* --------------------------------------------------------------------------------------------- *
00562  * OCI_BindSetNullAtPos
00563  * --------------------------------------------------------------------------------------------- */
00564 
00565 boolean OCI_API OCI_BindSetNullAtPos
00566 (
00567     OCI_Bind    *bnd,
00568     unsigned int position
00569 )
00570 {
00571     OCI_CHECK_PTR(OCI_IPC_BIND, bnd, FALSE);
00572     OCI_CHECK_BOUND(bnd->stmt->con, position, 1, bnd->buf.count, FALSE);
00573 
00574     if (bnd->buf.inds != NULL)
00575         ((sb2*) bnd->buf.inds)[position-1] = -1;
00576 
00577     OCI_RESULT(TRUE);
00578 
00579     return TRUE;
00580 }
00581 
00582 /* --------------------------------------------------------------------------------------------- *
00583  * OCI_BindSetNull
00584  * --------------------------------------------------------------------------------------------- */
00585 
00586 boolean OCI_API OCI_BindSetNull
00587 (
00588     OCI_Bind *bnd
00589 )
00590 {
00591     return OCI_BindSetNullAtPos(bnd, 1);
00592 }
00593 
00594 /* --------------------------------------------------------------------------------------------- *
00595  * OCI_BindIsNullAtPos
00596  * --------------------------------------------------------------------------------------------- */
00597 
00598 boolean OCI_API OCI_BindIsNullAtPos
00599 (
00600     OCI_Bind    *bnd,
00601     unsigned int position
00602 )
00603 {
00604     boolean ret = TRUE;
00605 
00606     OCI_CHECK_PTR(OCI_IPC_BIND, bnd, FALSE);
00607     OCI_CHECK_BOUND(bnd->stmt->con, position, 1, bnd->buf.count, FALSE);
00608 
00609     if (bnd->buf.inds != NULL)
00610         ret = (((sb2*) bnd->buf.inds)[position-1] == -1);
00611 
00612     OCI_RESULT(TRUE);
00613 
00614     return ret;
00615 }
00616 
00617 /* --------------------------------------------------------------------------------------------- *
00618  * OCI_BindIsNull
00619  * --------------------------------------------------------------------------------------------- */
00620 
00621 boolean OCI_API OCI_BindIsNull
00622 (
00623     OCI_Bind *bnd
00624 )
00625 {
00626     return OCI_BindIsNullAtPos(bnd, 1);
00627 }
00628 
00629 /* --------------------------------------------------------------------------------------------- *
00630  * OCI_BindSetCharsetForm
00631  * --------------------------------------------------------------------------------------------- */
00632 
00633 boolean OCI_API OCI_BindSetCharsetForm
00634 (
00635     OCI_Bind    *bnd,
00636     unsigned int csfrm
00637 )
00638 {
00639     boolean res = TRUE;
00640 
00641     OCI_CHECK_PTR(OCI_IPC_BIND, bnd, FALSE);
00642 
00643     if ((bnd->type == OCI_CDT_TEXT) || (bnd->type == OCI_CDT_LONG))
00644     {
00645         if (csfrm == OCI_CSF_NATIONAL)
00646             bnd->csfrm = SQLCS_NCHAR;
00647         else if (csfrm == OCI_CSF_DEFAULT)
00648             bnd->csfrm = SQLCS_IMPLICIT;
00649 
00650         OCI_CALL1
00651         (
00652             res, bnd->stmt->con, bnd->stmt,
00653 
00654             OCIAttrSet((dvoid *) bnd->buf.handle,
00655                        (ub4    ) OCI_HTYPE_BIND,
00656                        (dvoid *) &bnd->csfrm,
00657                        (ub4    ) sizeof(bnd->csfrm),
00658                        (ub4    ) OCI_ATTR_CHARSET_FORM,
00659                        bnd->stmt->con->err)
00660         )
00661     }
00662 
00663     OCI_RESULT(res);
00664 
00665     return res;
00666 }

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