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

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