OCILIB (C and C++ Driver for Oracle)  4.7.5
Open source and cross platform Oracle Driver delivering efficient access to Oracle databases.
Loading...
Searching...
No Matches
Oracle NUMBER manipulation (optional)

Detailed Description

OcilibCApiLongs

OCILIB encapsulates Oracle SQL all Numeric types using C native data types. But it also provides an optional OCI_Number handle for manipulating and accessing Oracle NUMBER type. OCI_Number provides management for some special value that cannot be addressed in C such as positive and negative infinity.

Example
#include "ocilib.h"
/* requires script demo/number.sql */
void err_handler(OCI_Error *err)
{
printf("%s\n", OCI_ErrorGetString(err));
}
int main(void)
{
OCI_Connection *cn = NULL;
OCI_Statement *st = NULL;
OCI_Resultset *rs = NULL;
OCI_TypeInfo *tc = NULL;
OCI_TypeInfo *to = NULL;
OCI_Number *nm = NULL;
OCI_Coll *cl = NULL;
OCI_Elem *el = NULL;
OCI_Object *ob = NULL;
OCI_Number *no = NULL;
OCI_Number *nt = NULL;
OCI_Number **ar = NULL;
unsigned int i, n;
otext buf[128] = "";
if (!OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT))
{
return EXIT_FAILURE;
}
cn = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
tc = OCI_TypeInfoGet(cn, "test_num_coll_t", OCI_TIF_TYPE);
to = OCI_TypeInfoGet(cn, "test_num_t", OCI_TIF_TYPE);
cl = OCI_CollCreate(tc);
ob = OCI_ObjectCreate(cn, to);
el = OCI_ElemCreate(tc);
nm = OCI_NumberCreate(cn);
ar = OCI_NumberArrayCreate(cn, 5);
// Testing fetching numbers (as number and using implicit conversion number to string)
OCI_Prepare(st, "select value from test_number");
rs = OCI_GetResultset(st);
while (OCI_FetchNext(rs))
{
const otext *s = OCI_GetString(rs, 1);
OCI_NumberToText(n, NULL, sizeof(buf), buf);
printf("%s - %s\n", buf, s);
}
// Testing binding external single number
OCI_Prepare(st, "begin :1 := :1 *2 ; end;");
OCI_BindNumber(st, ":1", nm);
OCI_NumberFromText(nm, "1234.4321", NULL);
OCI_NumberToText(nm, NULL, sizeof(buf), buf);
printf("%s\n", buf);
// Testing binding internal single number
OCI_Prepare(st, "begin :1 := :1 *2 ; end;");
OCI_SetBindAllocation(st, OCI_BAM_INTERNAL);
OCI_BindNumber(st, ":1", NULL);
OCI_NumberFromText(nt, "1234.4321", NULL);
OCI_NumberToText(nt, NULL, sizeof(buf), buf);
printf("%s\n", buf);
OCI_SetBindAllocation(st, OCI_BAM_EXTERNAL);
// Testing registering number for a returning into statement
OCI_Prepare(st, "update test_number set value = value *2 returning value into :1");
OCI_RegisterNumber(st, ":1");
rs = OCI_GetResultset(st);
while (OCI_FetchNext(rs))
{
printf("%s\n", OCI_GetString(rs, 1));
}
// testing Collections
OCI_NumberFromText(nm, "1111.2222", NULL);
OCI_CollAppend(cl, el);
OCI_NumberFromText(nm, "3333.4444", NULL);
OCI_CollAppend(cl, el);
printf("%d\n", OCI_CollGetCount(cl));
for (i = 1, n = OCI_CollGetCount(cl); i <= n; i++)
{
OCI_NumberToText(n, NULL, sizeof(buf), buf);
printf("%s\n", buf);
}
// Testing objects
OCI_NumberFromText(nm, "5555.6666", NULL);
OCI_NumberToText(nm, NULL, sizeof(buf), buf);
printf("%s\n", buf);
OCI_ObjectSetNumber(ob, "value", nm);
no = OCI_ObjectGetNumber(ob, "value");
OCI_NumberToText(no, NULL, sizeof(buf), buf);
printf("%s\n", buf);
// Testing fetching infinite values to string
OCI_Prepare(st, "SELECT utl_raw.cast_to_number('FF65'), utl_raw.cast_to_number('00') from dual");
rs = OCI_GetResultset(st);
while (OCI_FetchNext(rs))
{
printf("pos infinite = [%s]\n", OCI_GetString(rs, 1));
printf("neg infinite = [%s]\n", OCI_GetString(rs, 2));
}
// Testing array of numbers
OCI_NumberFromText(ar[0], "1.2", NULL);
OCI_NumberFromText(ar[1], "~", NULL);
OCI_NumberFromText(ar[2], "5.6", NULL);
OCI_NumberFromText(ar[3], "-~", NULL);
OCI_NumberFromText(ar[4], "9.2", NULL);
OCI_NumberToText(ar[0], NULL, sizeof(buf), buf);
printf("%s\n", buf);
OCI_NumberToText(ar[1], NULL, sizeof(buf), buf);
printf("%s\n", buf);
OCI_NumberToText(ar[2], NULL, sizeof(buf), buf);
printf("%s\n", buf);
OCI_NumberToText(ar[3], NULL, sizeof(buf), buf);
printf("%s\n", buf);
OCI_NumberToText(ar[4], NULL, sizeof(buf), buf);
printf("%s\n", buf);
return EXIT_SUCCESS;
}
OCI_SYM_PUBLIC OCI_Bind *OCI_API OCI_GetBind(OCI_Statement *stmt, unsigned int index)
Return the bind handle at the given index in the internal array of bind handle.
OCI_SYM_PUBLIC void *OCI_API OCI_BindGetData(OCI_Bind *bnd)
Return the user defined data associated with a bind handle.
OCI_SYM_PUBLIC boolean OCI_API OCI_BindNumber(OCI_Statement *stmt, const otext *name, OCI_Number *data)
Bind an Number variable.
OCI_SYM_PUBLIC boolean OCI_API OCI_ElemSetNumber(OCI_Elem *elem, OCI_Number *value)
Set a number value to a collection element.
OCI_SYM_PUBLIC boolean OCI_API OCI_CollAppend(OCI_Coll *coll, OCI_Elem *elem)
Append the given element at the end of the collection.
OCI_SYM_PUBLIC boolean OCI_API OCI_ElemFree(OCI_Elem *elem)
Free a local collection element.
OCI_SYM_PUBLIC boolean OCI_API OCI_CollFree(OCI_Coll *coll)
Free a local collection.
OCI_SYM_PUBLIC unsigned int OCI_API OCI_CollGetCount(OCI_Coll *coll)
Returns the current number of elements of the given collection.
OCI_SYM_PUBLIC OCI_Elem *OCI_API OCI_CollGetElem(OCI_Coll *coll, unsigned int index)
Return the element at the given position in the collection.
OCI_SYM_PUBLIC OCI_Elem *OCI_API OCI_ElemCreate(OCI_TypeInfo *typinf)
Create a local collection element instance based on a collection type descriptor.
OCI_SYM_PUBLIC OCI_Number *OCI_API OCI_ElemGetNumber(OCI_Elem *elem)
Return the number value of the given collection element.
OCI_SYM_PUBLIC OCI_Coll *OCI_API OCI_CollCreate(OCI_TypeInfo *typinf)
Create a local collection instance.
OCI_SYM_PUBLIC boolean OCI_API OCI_ConnectionFree(OCI_Connection *con)
Close a physical connection to an Oracle database server.
OCI_SYM_PUBLIC OCI_Connection *OCI_API OCI_ConnectionCreate(const otext *db, const otext *user, const otext *pwd, unsigned int mode)
Create a physical connection to an Oracle database server.
struct OCI_Elem OCI_Elem
Oracle Collection item representation.
Definition: types.h:329
struct OCI_Connection OCI_Connection
Oracle physical connection.
Definition: types.h:124
struct OCI_Object OCI_Object
Oracle Named types representation.
Definition: types.h:309
struct OCI_Statement OCI_Statement
Oracle SQL or PL/SQL statement.
Definition: types.h:136
struct OCI_Coll OCI_Coll
Oracle Collections (VARRAYs and Nested Tables) representation.
Definition: types.h:319
struct OCI_Error OCI_Error
Encapsulates an Oracle or OCILIB exception.
Definition: types.h:390
struct OCI_Number OCI_Number
Oracle NUMBER representation.
Definition: types.h:269
struct OCI_TypeInfo OCI_TypeInfo
Type info metadata handle.
Definition: types.h:366
struct OCI_Resultset OCI_Resultset
Collection of output columns from a select statement.
Definition: types.h:163
OCI_SYM_PUBLIC const otext *OCI_API OCI_ErrorGetString(OCI_Error *err)
Retrieve error message from error handle.
OCI_SYM_PUBLIC boolean OCI_API OCI_RegisterNumber(OCI_Statement *stmt, const otext *name)
Register a register output bind placeholder.
OCI_SYM_PUBLIC OCI_Number *OCI_API OCI_GetNumber(OCI_Resultset *rs, unsigned int index)
Return the current Number value of the column at the given index in the resultset.
OCI_SYM_PUBLIC boolean OCI_API OCI_FetchNext(OCI_Resultset *rs)
Fetch the next row of the resultset.
OCI_SYM_PUBLIC const otext *OCI_API OCI_GetString(OCI_Resultset *rs, unsigned int index)
Return the current string value of the column at the given index in the resultset.
OCI_SYM_PUBLIC OCI_Resultset *OCI_API OCI_GetResultset(OCI_Statement *stmt)
Retrieve the resultset handle from an executed statement.
OCI_SYM_PUBLIC boolean OCI_API OCI_Cleanup(void)
Clean up all resources allocated by the library.
OCI_SYM_PUBLIC boolean OCI_API OCI_Initialize(POCI_ERROR err_handler, const otext *lib_path, unsigned int mode)
Initialize the library.
OCI_SYM_PUBLIC OCI_TypeInfo *OCI_API OCI_TypeInfoGet(OCI_Connection *con, const otext *name, unsigned int type)
Retrieve the available type info information.
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberArrayFree(OCI_Number **numbers)
Free an array of number objects.
OCI_SYM_PUBLIC OCI_Number **OCI_API OCI_NumberArrayCreate(OCI_Connection *con, unsigned int nbelem)
Create an array of number object.
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberFree(OCI_Number *number)
Free a number object.
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberFromText(OCI_Number *number, const otext *str, const otext *fmt)
Convert a string to a number and store it in the given number handle.
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberToText(OCI_Number *number, const otext *fmt, int size, otext *str)
Convert a number value from the given number handle to a string.
OCI_SYM_PUBLIC OCI_Number *OCI_API OCI_NumberCreate(OCI_Connection *con)
Create a local number object.
OCI_SYM_PUBLIC boolean OCI_API OCI_SetBindAllocation(OCI_Statement *stmt, unsigned int mode)
Set the current bind allocation mode that will be used for subsequent binding calls.
OCI_SYM_PUBLIC OCI_Statement *OCI_API OCI_StatementCreate(OCI_Connection *con)
Create a statement object and return its handle.
OCI_SYM_PUBLIC boolean OCI_API OCI_StatementFree(OCI_Statement *stmt)
Free a statement and all resources associated to it (resultsets ...)
OCI_SYM_PUBLIC boolean OCI_API OCI_Prepare(OCI_Statement *stmt, const otext *sql)
Prepare a SQL statement or PL/SQL block.
OCI_SYM_PUBLIC boolean OCI_API OCI_Execute(OCI_Statement *stmt)
Execute a prepared SQL statement or PL/SQL block.
OCI_SYM_PUBLIC boolean OCI_API OCI_ObjectFree(OCI_Object *obj)
Free a local object.
OCI_SYM_PUBLIC boolean OCI_API OCI_ObjectSetNumber(OCI_Object *obj, const otext *attr, OCI_Number *value)
Set an object attribute of type number.
OCI_SYM_PUBLIC OCI_Object *OCI_API OCI_ObjectCreate(OCI_Connection *con, OCI_TypeInfo *typinf)
Create a local object instance.
OCI_SYM_PUBLIC OCI_Number *OCI_API OCI_ObjectGetNumber(OCI_Object *obj, const otext *attr)
Return the number value of the given object attribute.

Functions

OCI_SYM_PUBLIC OCI_Number *OCI_API OCI_NumberCreate (OCI_Connection *con)
 Create a local number object.
 
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberFree (OCI_Number *number)
 Free a number object.
 
OCI_SYM_PUBLIC OCI_Number **OCI_API OCI_NumberArrayCreate (OCI_Connection *con, unsigned int nbelem)
 Create an array of number object.
 
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberArrayFree (OCI_Number **numbers)
 Free an array of number objects.
 
OCI_SYM_PUBLIC int OCI_API OCI_NumberAssign (OCI_Number *number, OCI_Number *number_src)
 Assign the value of a number handle to another one.
 
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberToText (OCI_Number *number, const otext *fmt, int size, otext *str)
 Convert a number value from the given number handle to a string.
 
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberFromText (OCI_Number *number, const otext *str, const otext *fmt)
 Convert a string to a number and store it in the given number handle.
 
OCI_SYM_PUBLIC unsigned char *OCI_API OCI_NumberGetContent (OCI_Number *number)
 Return the number value content.
 
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberSetContent (OCI_Number *number, unsigned char *content)
 Assign the number value content.
 
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberSetValue (OCI_Number *number, unsigned int type, void *value)
 Assign the number value with the value of a native C numeric type.
 
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberGetValue (OCI_Number *number, unsigned int type, void *value)
 Assign the number value to a native C numeric type.
 
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberAdd (OCI_Number *number, unsigned int type, void *value)
 Add the value of a native C numeric type to the given number.
 
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberSub (OCI_Number *number, unsigned int type, void *value)
 Subtract the value of a native C numeric type to the given number.
 
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberMultiply (OCI_Number *number, unsigned int type, void *value)
 Multiply the given number with the value of a native C numeric.
 
OCI_SYM_PUBLIC boolean OCI_API OCI_NumberDivide (OCI_Number *number, unsigned int type, void *value)
 Divide the given number with the value of a native C numeric.
 
OCI_SYM_PUBLIC int OCI_API OCI_NumberCompare (OCI_Number *number1, OCI_Number *number2)
 Compares two number handles.
 

Function Documentation

◆ OCI_NumberCreate()

OCI_SYM_PUBLIC OCI_Number *OCI_API OCI_NumberCreate ( OCI_Connection con)

#include <api.h>

Create a local number object.

Parameters
con- Connection handle
Note
Parameter 'con' can be NULL in order to manipulate numbers independently from database connections
Returns
Return the number handle on success otherwise NULL on failure

◆ OCI_NumberFree()

OCI_SYM_PUBLIC boolean OCI_API OCI_NumberFree ( OCI_Number number)

#include <api.h>

Free a number object.

Parameters
number- Number handle
Warning
Only Numbers created with OCI_NumberCreate() should be freed by OCI_NumberFree()
Returns
TRUE on success otherwise FALSE

◆ OCI_NumberArrayCreate()

OCI_SYM_PUBLIC OCI_Number **OCI_API OCI_NumberArrayCreate ( OCI_Connection con,
unsigned int  nbelem 
)

#include <api.h>

Create an array of number object.

Parameters
con- Connection handle
nbelem- number of elements in the array
Note
see OCI_NumberCreate() for more details
Returns
Return the number handle array on success otherwise NULL on failure

◆ OCI_NumberArrayFree()

OCI_SYM_PUBLIC boolean OCI_API OCI_NumberArrayFree ( OCI_Number **  numbers)

#include <api.h>

Free an array of number objects.

Parameters
numbers- Array of number objects
Warning
Only arrays of numbers created with OCI_NumberArrayCreate() should be freed by OCI_NumberArrayFree()
Returns
TRUE on success otherwise FALSE

◆ OCI_NumberAssign()

OCI_SYM_PUBLIC int OCI_API OCI_NumberAssign ( OCI_Number number,
OCI_Number number_src 
)

#include <api.h>

Assign the value of a number handle to another one.

Parameters
number- Destination number handle
number_src- Source number handle
Returns
TRUE on success otherwise FALSE

Referenced by ocilib::Number::Clone().

◆ OCI_NumberToText()

OCI_SYM_PUBLIC boolean OCI_API OCI_NumberToText ( OCI_Number number,
const otext *  fmt,
int  size,
otext *  str 
)

#include <api.h>

Convert a number value from the given number handle to a string.

Parameters
number- source number handle
fmt- Number format
size- Destination string size in characters
str- Destination date string
Note
Output string can be one the following 'magic strings':
  • '~' for positive infinity
  • '-~' for negative infinity
Returns
TRUE on success otherwise FALSE

Referenced by ocilib::Number::ToString().

◆ OCI_NumberFromText()

OCI_SYM_PUBLIC boolean OCI_API OCI_NumberFromText ( OCI_Number number,
const otext *  str,
const otext *  fmt 
)

#include <api.h>

Convert a string to a number and store it in the given number handle.

Parameters
number- Destination number handle
str- Source number string
fmt- Number format
Note
Input string can be one the following 'magic strings':
  • '~' for positive infinity
  • '-~' for negative infinity
Returns
TRUE on success otherwise FALSE

Referenced by ocilib::Number::FromString().

◆ OCI_NumberGetContent()

OCI_SYM_PUBLIC unsigned char *OCI_API OCI_NumberGetContent ( OCI_Number number)

#include <api.h>

Return the number value content.

Parameters
number- number handle
Note
Returned content is a buffer of 22 bytes corresponding to Oracle C native representation of NUMBER values See oracle Documentation of its layout
Returns
TRUE on success otherwise FALSE

◆ OCI_NumberSetContent()

OCI_SYM_PUBLIC boolean OCI_API OCI_NumberSetContent ( OCI_Number number,
unsigned char *  content 
)

#include <api.h>

Assign the number value content.

Parameters
number- number handle
content- raw number content
Note
See OCI_NumberSetContent() for more information
Returns
TRUE on success otherwise FALSE

◆ OCI_NumberSetValue()

OCI_SYM_PUBLIC boolean OCI_API OCI_NumberSetValue ( OCI_Number number,
unsigned int  type,
void *  value 
)

#include <api.h>

Assign the number value with the value of a native C numeric type.

Parameters
number- number handle
type- native C type to assign
value- pointer to value to set
Note
Argument param type can be :
  • OCI_NUM_SHORT : value is a pointer to a signed short
  • OCI_NUM_USHORT : value is a pointer to an unsigned short
  • OCI_NUM_INT : value is a pointer to a signed int
  • OCI_NUM_UINT : value is a pointer to an unsigned short
  • OCI_NUM_BIGINT : value is a pointer to a signed big_int
  • OCI_NUM_BIGUINT : value is a pointer to an unsigned big_uint
  • OCI_NUM_FLOAT : value is a pointer to an float
  • OCI_NUM_DOUBLE : value is a pointer to a double
  • OCI_NUM_NUMBER : value is a pointer to the return value of OCI_NumberGetContent()
Returns
TRUE on success otherwise FALSE

◆ OCI_NumberGetValue()

OCI_SYM_PUBLIC boolean OCI_API OCI_NumberGetValue ( OCI_Number number,
unsigned int  type,
void *  value 
)

#include <api.h>

Assign the number value to a native C numeric type.

Parameters
number- number handle
type- native C type to assign
value- pointer to a native C variable
Note
See OCI_NumberSetValue() for more information
Returns
TRUE on success otherwise FALSE

◆ OCI_NumberAdd()

OCI_SYM_PUBLIC boolean OCI_API OCI_NumberAdd ( OCI_Number number,
unsigned int  type,
void *  value 
)

#include <api.h>

Add the value of a native C numeric type to the given number.

Parameters
number- number handle
type- native C type of the variable
value- pointer to a native C variable to add
Note
See OCI_NumberSetValue() for more information
Returns
TRUE on success otherwise FALSE

◆ OCI_NumberSub()

OCI_SYM_PUBLIC boolean OCI_API OCI_NumberSub ( OCI_Number number,
unsigned int  type,
void *  value 
)

#include <api.h>

Subtract the value of a native C numeric type to the given number.

Parameters
number- number handle
type- native C type of the variable
value- pointer to a native C variable to subtract
Note
See OCI_NumberSetValue() for more information
Returns
TRUE on success otherwise FALSE

◆ OCI_NumberMultiply()

OCI_SYM_PUBLIC boolean OCI_API OCI_NumberMultiply ( OCI_Number number,
unsigned int  type,
void *  value 
)

#include <api.h>

Multiply the given number with the value of a native C numeric.

Parameters
number- number handle
type- native C type of the variable
value- pointer to a native C variable to multiply by
Note
See OCI_NumberSetValue() for more information
Returns
TRUE on success otherwise FALSE

◆ OCI_NumberDivide()

OCI_SYM_PUBLIC boolean OCI_API OCI_NumberDivide ( OCI_Number number,
unsigned int  type,
void *  value 
)

#include <api.h>

Divide the given number with the value of a native C numeric.

Parameters
number- number handle
type- native C type of the variable
value- pointer to a native C variable to divide by
Note
See OCI_NumberSetValue() for more information
Returns
TRUE on success otherwise FALSE

◆ OCI_NumberCompare()

OCI_SYM_PUBLIC int OCI_API OCI_NumberCompare ( OCI_Number number1,
OCI_Number number2 
)

#include <api.h>

Compares two number handles.

Parameters
number1- number1 handle
number2- number2 handle
Returns
  • -1 if number1 is smaller than number2,
  • 0 if they are equal
  • 1 if number1 is greater than number2.