OCILIB (C Driver for Oracle) 3.12.1
|
OCILIB supports all Oracle collections:
PL/SQL tables are implemented by binding regular C arrays with the array interface (using OCI_BindArrayOfXXX() calls)
Varrays and Nested tables are implemented in OCILIB with the type OCI_Coll. It's possible to bind and fetch Varrays and Nested tables using OCI_Coll handle.
It's also possible to declare local collections based on some database type without using queries
OCI (and thus OCILIB) offers the possibility to access collection elements :
Collection Items are implemented through the type OCI_Elem and use the series of calls OCI_ElemGetXXX() and OCI_ElemSetXXX() to manipulate elements content values
#include "ocilib.h" int main(void) { OCI_Connection *cn; OCI_Statement *st; OCI_Resultset *rs; OCI_Coll *coll; OCI_Iter *iter; OCI_Elem *elem; OCI_TypeInfo *type; OCI_Object *obj; int i, n; if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT)) return EXIT_FAILURE; cn = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT); /* Varray binding -------------------------------------------------------- */ st = OCI_StatementCreate(cn); /* create the collection */ type = OCI_TypeInfoGet(cn, "Varray_type", OCI_TIF_TYPE); coll = OCI_CollCreate(type); /* bind the local collection to a PL/SQL procedure */ OCI_Prepare(st, "begin load_array(:array); end;"); OCI_BindColl(st, ":array", coll); OCI_Execute(st); /* the procedure has filled the collection and we can iterate it using an iterator */ iter = OCI_IterCreate(coll); elem = OCI_IterGetNext(iter); while (elem != NULL) { printf("value %s\n", OCI_ElemGetString(elem)); elem = OCI_IterGetNext(iter); } OCI_IterFree(iter); OCI_CollFree(coll); /* Varray SQL fetch ------------------------------------------------------- */ /* query on a table with varray column */ OCI_ExecuteStmt(st, "SELECT * from table_article"); rs = OCI_GetResultset(st); while (OCI_FetchNext(rs)) { /* iterate the collection using an iterator */ coll = OCI_GetColl(rs, 2); iter = OCI_IterCreate(coll); elem = OCI_IterGetNext(iter); printf("article #%d\n", OCI_GetInt(rs, 1)); while (elem != NULL) { obj = OCI_ElemGetObject(elem); printf(".... code %d, name%s \n", OCI_ObjectGetInt(obj, "ID"), OCI_ObjectGetString(obj, "NAME")); elem = OCI_IterGetNext(iter); } OCI_IterFree(iter); } /* Nested table fetch ------------------------------------------------------- */ /* query on a table with nested table column */ OCI_ExecuteStmt(st, "SELECT * from table_sales"); rs = OCI_GetResultset(st); while (OCI_FetchNext(rs)) { coll = OCI_GetColl(rs, 2); printf("Sale #%d\n", OCI_GetInt(rs, 1)); /* iterate the collection by accessing element by index */ n = OCI_CollGetSize(coll); for(i = 1; i <= n; i++) { elem = OCI_CollGetAt(coll, i); obj = OCI_ElemGetObject(elem); printf(".... employee %s, amount %s \n", OCI_ObjectGetString(obj, "EMP"), OCI_ObjectGetString(obj, "AMOUNT")); } } OCI_Cleanup(); return EXIT_SUCCESS; }
Functions | |
OCI_EXPORT OCI_Coll *OCI_API | OCI_CollCreate (OCI_TypeInfo *typinf) |
Create a local collection instance. | |
OCI_EXPORT boolean OCI_API | OCI_CollFree (OCI_Coll *coll) |
Free a local collection. | |
OCI_EXPORT OCI_Coll **OCI_API | OCI_CollArrayCreate (OCI_Connection *con, OCI_TypeInfo *typinf, unsigned int nbelem) |
Create an array of Collection object. | |
OCI_EXPORT boolean OCI_API | OCI_CollArrayFree (OCI_Coll **colls) |
Free an arrray of Collection objects. | |
OCI_EXPORT boolean OCI_API | OCI_CollAssign (OCI_Coll *coll, OCI_Coll *coll_src) |
Assign a collection to another one. | |
OCI_EXPORT OCI_TypeInfo *OCI_API | OCI_CollGetTypeInfo (OCI_Coll *coll) |
Return the type info object associated to the collection. | |
OCI_EXPORT unsigned int OCI_API | OCI_CollGetType (OCI_Coll *coll) |
Return the collection type. | |
OCI_EXPORT unsigned int OCI_API | OCI_CollGetMax (OCI_Coll *coll) |
Returns the maximum number of elements of the given collection. | |
OCI_EXPORT unsigned int OCI_API | OCI_CollGetSize (OCI_Coll *coll) |
Returns the current number of elements of the given collection. | |
OCI_EXPORT boolean OCI_API | OCI_CollTrim (OCI_Coll *coll, unsigned int nb_elem) |
Trims the given number of elements from the end of the collection. | |
OCI_EXPORT boolean OCI_API | OCI_CollClear (OCI_Coll *coll) |
clear all items of the given collection | |
OCI_EXPORT OCI_Elem *OCI_API | OCI_CollGetAt (OCI_Coll *coll, unsigned int index) |
Return the element at the given position in the collection. | |
OCI_EXPORT boolean OCI_API | OCI_CollGetAt2 (OCI_Coll *coll, unsigned int index, OCI_Elem *elem) |
Return the element at the given position in the collection. | |
OCI_EXPORT boolean OCI_API | OCI_CollSetAt (OCI_Coll *coll, unsigned int index, OCI_Elem *elem) |
Assign the given element value to the element at the given position in the collection. | |
OCI_EXPORT boolean OCI_API | OCI_CollAppend (OCI_Coll *coll, OCI_Elem *elem) |
Append the given element at the end of the collection. | |
OCI_EXPORT OCI_Iter *OCI_API | OCI_IterCreate (OCI_Coll *coll) |
Create an iterator handle to iterate through a collection. | |
OCI_EXPORT boolean OCI_API | OCI_IterFree (OCI_Iter *iter) |
Free an iterator handle. | |
OCI_EXPORT OCI_Elem *OCI_API | OCI_IterGetNext (OCI_Iter *iter) |
Get the next element in the collection. | |
OCI_EXPORT OCI_Elem *OCI_API | OCI_IterGetPrev (OCI_Iter *iter) |
Get the previous element in the collection. | |
OCI_EXPORT OCI_Elem *OCI_API | OCI_ElemCreate (OCI_TypeInfo *typinf) |
Create a local collection element instance based on a collection type descriptor. | |
OCI_EXPORT boolean OCI_API | OCI_ElemFree (OCI_Elem *elem) |
Free a local collection element. | |
OCI_EXPORT short OCI_API | OCI_ElemGetShort (OCI_Elem *elem) |
Return the short value of the given collection element. | |
OCI_EXPORT unsigned short OCI_API | OCI_ElemGetUnsignedShort (OCI_Elem *elem) |
Return the unsigned short value of the given collection element. | |
OCI_EXPORT int OCI_API | OCI_ElemGetInt (OCI_Elem *elem) |
Return the int value of the given collection element. | |
OCI_EXPORT unsigned int OCI_API | OCI_ElemGetUnsignedInt (OCI_Elem *elem) |
Return the unsigned int value of the given collection element. | |
OCI_EXPORT big_int OCI_API | OCI_ElemGetBigInt (OCI_Elem *elem) |
Return the big int value of the given collection element. | |
OCI_EXPORT big_uint OCI_API | OCI_ElemGetUnsignedBigInt (OCI_Elem *elem) |
Return the unsigned big int value of the given collection element. | |
OCI_EXPORT double OCI_API | OCI_ElemGetDouble (OCI_Elem *elem) |
Return the Double value of the given collection element. | |
OCI_EXPORT float OCI_API | OCI_ElemGetFloat (OCI_Elem *elem) |
Return the float value of the given collection element. | |
OCI_EXPORT const dtext *OCI_API | OCI_ElemGetString (OCI_Elem *elem) |
Return the String value of the given collection element. | |
OCI_EXPORT unsigned int OCI_API | OCI_ElemGetRaw (OCI_Elem *elem, void *value, unsigned int len) |
Read the RAW value of the collection element into the given buffer. | |
OCI_EXPORT OCI_Date *OCI_API | OCI_ElemGetDate (OCI_Elem *elem) |
Return the Date value of the given collection element. | |
OCI_EXPORT OCI_Timestamp *OCI_API | OCI_ElemGetTimestamp (OCI_Elem *elem) |
Return the Timestamp value of the given collection element. | |
OCI_EXPORT OCI_Interval *OCI_API | OCI_ElemGetInterval (OCI_Elem *elem) |
Return the Interval value of the given collection element. | |
OCI_EXPORT OCI_Lob *OCI_API | OCI_ElemGetLob (OCI_Elem *elem) |
Return the Lob value of the given collection element. | |
OCI_EXPORT OCI_File *OCI_API | OCI_ElemGetFile (OCI_Elem *elem) |
Return the File value of the given collection element. | |
OCI_EXPORT OCI_Object *OCI_API | OCI_ElemGetObject (OCI_Elem *elem) |
Return the object value of the given collection element. | |
OCI_EXPORT OCI_Coll *OCI_API | OCI_ElemGetColl (OCI_Elem *elem) |
Return the collection value of the given collection element. | |
OCI_EXPORT OCI_Ref *OCI_API | OCI_ElemGetRef (OCI_Elem *elem) |
Return the Ref value of the given collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetShort (OCI_Elem *elem, short value) |
Set a short value to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetUnsignedShort (OCI_Elem *elem, unsigned short value) |
Set a unsigned short value to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetInt (OCI_Elem *elem, int value) |
Set a int value to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetUnsignedInt (OCI_Elem *elem, unsigned int value) |
Set a unsigned int value to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetBigInt (OCI_Elem *elem, big_int value) |
Set a big int value to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetUnsignedBigInt (OCI_Elem *elem, big_uint value) |
Set a unsigned big_int value to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetDouble (OCI_Elem *elem, double value) |
Set a double value to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetFloat (OCI_Elem *elem, float value) |
Set a float value to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetString (OCI_Elem *elem, const dtext *value) |
Set a string value to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetRaw (OCI_Elem *elem, void *value, unsigned int len) |
Set a RAW value to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetDate (OCI_Elem *elem, OCI_Date *value) |
Assign a Date handle to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetTimestamp (OCI_Elem *elem, OCI_Timestamp *value) |
Assign a Timestamp handle to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetInterval (OCI_Elem *elem, OCI_Interval *value) |
Assign an Interval handle to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetColl (OCI_Elem *elem, OCI_Coll *value) |
Assign a Collection handle to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetObject (OCI_Elem *elem, OCI_Object *value) |
Assign an Object handle to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetLob (OCI_Elem *elem, OCI_Lob *value) |
Assign a Lob handle to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetFile (OCI_Elem *elem, OCI_File *value) |
Assign a File handle to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetRef (OCI_Elem *elem, OCI_Ref *value) |
Assign a Ref handle to a collection element. | |
OCI_EXPORT boolean OCI_API | OCI_ElemIsNull (OCI_Elem *elem) |
Check if the collection element value is null. | |
OCI_EXPORT boolean OCI_API | OCI_ElemSetNull (OCI_Elem *elem) |
Set a collection element value to null. |
OCI_EXPORT OCI_Coll* OCI_API OCI_CollCreate | ( | OCI_TypeInfo * | typinf | ) |
Create a local collection instance.
typinf | - Type info handle of the collection type descriptor |
Definition at line 119 of file collection.c.
OCI_EXPORT boolean OCI_API OCI_CollFree | ( | OCI_Coll * | coll | ) |
Free a local collection.
coll | - Collection handle |
Definition at line 142 of file collection.c.
References OCI_ElemFree().
Referenced by OCI_ElemFree().
OCI_EXPORT OCI_Coll** OCI_API OCI_CollArrayCreate | ( | OCI_Connection * | con, |
OCI_TypeInfo * | typinf, | ||
unsigned int | nbelem | ||
) |
Create an array of Collection object.
con | - Connection handle |
typinf | - Object type (type info handle) |
nbelem | - number of elements in the array |
Definition at line 181 of file collection.c.
OCI_EXPORT boolean OCI_API OCI_CollArrayFree | ( | OCI_Coll ** | colls | ) |
Free an arrray of Collection objects.
colls | - Array of Collection objects |
Definition at line 206 of file collection.c.
Assign a collection to another one.
coll | - Destination Collection handle |
coll_src | - Source Collection handle |
Definition at line 218 of file collection.c.
Referenced by OCI_ElemSetColl().
OCI_EXPORT OCI_TypeInfo* OCI_API OCI_CollGetTypeInfo | ( | OCI_Coll * | coll | ) |
Return the type info object associated to the collection.
coll | - Collection handle |
Definition at line 490 of file collection.c.
OCI_EXPORT unsigned int OCI_API OCI_CollGetType | ( | OCI_Coll * | coll | ) |
Return the collection type.
coll | - Collection handle |
Definition at line 247 of file collection.c.
OCI_EXPORT unsigned int OCI_API OCI_CollGetMax | ( | OCI_Coll * | coll | ) |
Returns the maximum number of elements of the given collection.
coll | - Collection handle |
Definition at line 274 of file collection.c.
OCI_EXPORT unsigned int OCI_API OCI_CollGetSize | ( | OCI_Coll * | coll | ) |
Returns the current number of elements of the given collection.
coll | - Collection handle |
Definition at line 294 of file collection.c.
Referenced by OCI_CollClear(), and OCI_CollTrim().
OCI_EXPORT boolean OCI_API OCI_CollTrim | ( | OCI_Coll * | coll, |
unsigned int | nb_elem | ||
) |
Trims the given number of elements from the end of the collection.
coll | - Collection handle |
nb_elem | - Number of elements to trim |
Definition at line 320 of file collection.c.
References OCI_CollGetSize().
Referenced by OCI_CollClear().
OCI_EXPORT boolean OCI_API OCI_CollClear | ( | OCI_Coll * | coll | ) |
clear all items of the given collection
coll | - Collection handle |
Definition at line 506 of file collection.c.
References OCI_CollGetSize(), and OCI_CollTrim().
Return the element at the given position in the collection.
coll | - Collection handle |
index | - Index of the destination element |
Definition at line 351 of file collection.c.
Return the element at the given position in the collection.
coll | - Collection handle |
index | - Index of the destination element |
elem | - Element handle to hold the collection item data |
Definition at line 387 of file collection.c.
Assign the given element value to the element at the given position in the collection.
coll | - Collection handle |
index | - Index of the destination element |
elem | - Source element handle to assign |
Definition at line 430 of file collection.c.
Append the given element at the end of the collection.
coll | - Collection handle |
elem | - Element handle to add |
Definition at line 461 of file collection.c.
Create an iterator handle to iterate through a collection.
coll | - Collection handle |
Definition at line 46 of file iterator.c.
References OCI_IterFree().
OCI_EXPORT boolean OCI_API OCI_IterFree | ( | OCI_Iter * | iter | ) |
Free an iterator handle.
iter | - Iterator handle |
Definition at line 108 of file iterator.c.
References OCI_ElemFree().
Referenced by OCI_IterCreate().
Get the next element in the collection.
iter | - Iterator handle |
Definition at line 151 of file iterator.c.
Get the previous element in the collection.
iter | - Iterator handle |
Definition at line 187 of file iterator.c.
OCI_EXPORT OCI_Elem* OCI_API OCI_ElemCreate | ( | OCI_TypeInfo * | typinf | ) |
OCI_EXPORT boolean OCI_API OCI_ElemFree | ( | OCI_Elem * | elem | ) |
Free a local collection element.
elem | - Element handle |
Definition at line 261 of file element.c.
References OCI_CollFree(), OCI_DateFree(), OCI_FileFree(), OCI_IntervalFree(), OCI_LobFree(), OCI_ObjectFree(), OCI_RefFree(), and OCI_TimestampFree().
Referenced by OCI_CollFree(), and OCI_IterFree().
OCI_EXPORT short OCI_API OCI_ElemGetShort | ( | OCI_Elem * | elem | ) |
OCI_EXPORT unsigned short OCI_API OCI_ElemGetUnsignedShort | ( | OCI_Elem * | elem | ) |
OCI_EXPORT int OCI_API OCI_ElemGetInt | ( | OCI_Elem * | elem | ) |
OCI_EXPORT unsigned int OCI_API OCI_ElemGetUnsignedInt | ( | OCI_Elem * | elem | ) |
OCI_EXPORT big_uint OCI_API OCI_ElemGetUnsignedBigInt | ( | OCI_Elem * | elem | ) |
OCI_EXPORT double OCI_API OCI_ElemGetDouble | ( | OCI_Elem * | elem | ) |
OCI_EXPORT float OCI_API OCI_ElemGetFloat | ( | OCI_Elem * | elem | ) |
OCI_EXPORT const dtext* OCI_API OCI_ElemGetString | ( | OCI_Elem * | elem | ) |
OCI_EXPORT unsigned int OCI_API OCI_ElemGetRaw | ( | OCI_Elem * | elem, |
void * | value, | ||
unsigned int | len | ||
) |
OCI_EXPORT OCI_Timestamp* OCI_API OCI_ElemGetTimestamp | ( | OCI_Elem * | elem | ) |
OCI_EXPORT OCI_Interval* OCI_API OCI_ElemGetInterval | ( | OCI_Elem * | elem | ) |
OCI_EXPORT OCI_Object* OCI_API OCI_ElemGetObject | ( | OCI_Elem * | elem | ) |
OCI_EXPORT boolean OCI_API OCI_ElemSetShort | ( | OCI_Elem * | elem, |
short | value | ||
) |
OCI_EXPORT boolean OCI_API OCI_ElemSetUnsignedShort | ( | OCI_Elem * | elem, |
unsigned short | value | ||
) |
OCI_EXPORT boolean OCI_API OCI_ElemSetInt | ( | OCI_Elem * | elem, |
int | value | ||
) |
OCI_EXPORT boolean OCI_API OCI_ElemSetUnsignedInt | ( | OCI_Elem * | elem, |
unsigned int | value | ||
) |
OCI_EXPORT boolean OCI_API OCI_ElemSetUnsignedBigInt | ( | OCI_Elem * | elem, |
big_uint | value | ||
) |
OCI_EXPORT boolean OCI_API OCI_ElemSetDouble | ( | OCI_Elem * | elem, |
double | value | ||
) |
OCI_EXPORT boolean OCI_API OCI_ElemSetFloat | ( | OCI_Elem * | elem, |
float | value | ||
) |
OCI_EXPORT boolean OCI_API OCI_ElemSetString | ( | OCI_Elem * | elem, |
const dtext * | value | ||
) |
Set a string value to a collection element.
elem | - Element handle |
value | - String value |
Definition at line 942 of file element.c.
References OCI_ElemSetNull().
OCI_EXPORT boolean OCI_API OCI_ElemSetRaw | ( | OCI_Elem * | elem, |
void * | value, | ||
unsigned int | len | ||
) |
Set a RAW value to a collection element.
elem | - Element handle |
value | - Raw value |
len | - Size of the raw value |
Definition at line 976 of file element.c.
References OCI_ElemSetNull().
Assign a Date handle to a collection element.
elem | - Element handle |
value | - Date Handle |
Definition at line 1016 of file element.c.
References OCI_DateAssign(), and OCI_ElemSetNull().
OCI_EXPORT boolean OCI_API OCI_ElemSetTimestamp | ( | OCI_Elem * | elem, |
OCI_Timestamp * | value | ||
) |
Assign a Timestamp handle to a collection element.
elem | - Element handle |
value | - Timestamp Handle |
Definition at line 1060 of file element.c.
References OCI_ElemSetNull(), and OCI_TimestampAssign().
OCI_EXPORT boolean OCI_API OCI_ElemSetInterval | ( | OCI_Elem * | elem, |
OCI_Interval * | value | ||
) |
Assign an Interval handle to a collection element.
elem | - Element handle |
value | - Interval Handle |
Definition at line 1105 of file element.c.
References OCI_ElemSetNull(), and OCI_IntervalAssign().
Assign a Collection handle to a collection element.
elem | - Element handle |
value | - Collection Handle |
Definition at line 1150 of file element.c.
References OCI_CollAssign(), and OCI_ElemSetNull().
OCI_EXPORT boolean OCI_API OCI_ElemSetObject | ( | OCI_Elem * | elem, |
OCI_Object * | value | ||
) |
Assign an Object handle to a collection element.
elem | - Element handle |
value | - Object Handle |
Definition at line 1195 of file element.c.
References OCI_ElemSetNull(), and OCI_ObjectAssign().
Assign a Lob handle to a collection element.
elem | - Element handle |
value | - Lob Handle |
Definition at line 1241 of file element.c.
References OCI_ElemSetNull(), and OCI_LobAssign().
Assign a File handle to a collection element.
elem | - Element handle |
value | - File Handle |
Definition at line 1287 of file element.c.
References OCI_ElemSetNull(), and OCI_FileAssign().
Assign a Ref handle to a collection element.
elem | - Element handle |
value | - Ref Handle |
Definition at line 1333 of file element.c.
References OCI_ElemSetNull(), and OCI_RefAssign().
OCI_EXPORT boolean OCI_API OCI_ElemIsNull | ( | OCI_Elem * | elem | ) |
OCI_EXPORT boolean OCI_API OCI_ElemSetNull | ( | OCI_Elem * | elem | ) |
Set a collection element value to null.
elem | - Element handle |
Definition at line 1402 of file element.c.
Referenced by OCI_ElemSetColl(), OCI_ElemSetDate(), OCI_ElemSetFile(), OCI_ElemSetInterval(), OCI_ElemSetLob(), OCI_ElemSetObject(), OCI_ElemSetRaw(), OCI_ElemSetRef(), OCI_ElemSetString(), and OCI_ElemSetTimestamp().