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
Hash tables

Detailed Description

OcilibCApiFormatting

OCILIB uses hash tables internally for index/name columns mapping.

OCILIB makes public its hash table’s implementation public for general purpose uses.

OCI_HashTable objects manage string keys / values that can be :

This hash table implementation :

Internal conception
Note
  • The internal hash function computes the index in the array where the entry has to be inserted/looked up.
Collisions are handled by chaining method.
#include "ocilib.h"
/* requires script demo/products.sql */
void err_handler(OCI_Error *err)
{
printf("%s\n", OCI_ErrorGetString(err));
}
int main(void)
{
int i, n;
if (!OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT))
{
return EXIT_FAILURE;
}
cn = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
ht = OCI_HashCreate(256, OCI_HASH_INTEGER);
/* fill the hash table with data from DB */
OCI_ExecuteStmt(st, "select code, name from products");
rs = OCI_GetResultset(st);
while (OCI_FetchNext(rs))
{
OCI_HashAddInt(ht, OCI_GetString2(rs, "name"), OCI_GetInt2(rs, "code"));
}
printf("%d row(s) fetched\n", OCI_GetRowCount(rs));
/* lookup an entry */
printf("code for '%s' is : %d\n", "product 1", OCI_HashGetInt(ht, "product 1"));
/* browse the hash table */
n = OCI_HashGetSize(ht);
for (i = 0; i < n; i++)
{
he = OCI_HashGetEntry(ht, i);
while (he)
{
printf(">key: '%s'\n", he->key);
hv = he->values;
while (hv)
{
printf("..... value : '%i'\n", hv->value.num);
hv = hv->next;
}
he = he->next;
}
}
/* destroy table */
return EXIT_SUCCESS;
}
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_HashTable OCI_HashTable
OCILIB implementation of hash tables.
Definition: types.h:376
struct OCI_Connection OCI_Connection
Oracle physical connection.
Definition: types.h:124
struct OCI_Statement OCI_Statement
Oracle SQL or PL/SQL statement.
Definition: types.h:136
struct OCI_HashEntry OCI_HashEntry
Hash table entry.
struct OCI_Error OCI_Error
Encapsulates an Oracle or OCILIB exception.
Definition: types.h:390
struct OCI_HashValue OCI_HashValue
Hash table entry value.
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 const otext *OCI_API OCI_GetString2(OCI_Resultset *rs, const otext *name)
Return the current string value of the column from its name in the resultset.
OCI_SYM_PUBLIC boolean OCI_API OCI_FetchNext(OCI_Resultset *rs)
Fetch the next row of the resultset.
OCI_SYM_PUBLIC unsigned int OCI_API OCI_GetRowCount(OCI_Resultset *rs)
Retrieve the number of rows fetched so far.
OCI_SYM_PUBLIC OCI_Resultset *OCI_API OCI_GetResultset(OCI_Statement *stmt)
Retrieve the resultset handle from an executed statement.
OCI_SYM_PUBLIC int OCI_API OCI_GetInt2(OCI_Resultset *rs, const otext *name)
Return the current integer value of the column from its name in the resultset.
OCI_SYM_PUBLIC OCI_HashEntry *OCI_API OCI_HashGetEntry(OCI_HashTable *table, unsigned int index)
Return the entry slot of the hash table internal list at the given position.
OCI_SYM_PUBLIC unsigned int OCI_API OCI_HashGetSize(OCI_HashTable *table)
Return the size of the hash table.
OCI_SYM_PUBLIC int OCI_API OCI_HashGetInt(OCI_HashTable *table, const otext *key)
Return the integer value associated to the given key.
OCI_SYM_PUBLIC OCI_HashTable *OCI_API OCI_HashCreate(unsigned int size, unsigned int type)
Create a hash table.
OCI_SYM_PUBLIC boolean OCI_API OCI_HashFree(OCI_HashTable *table)
Destroy a hash table.
OCI_SYM_PUBLIC boolean OCI_API OCI_HashAddInt(OCI_HashTable *table, const otext *key, int value)
Adds a pair string key / integer value to the hash table.
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_Statement *OCI_API OCI_StatementCreate(OCI_Connection *con)
Create a statement object and return its handle.
OCI_SYM_PUBLIC boolean OCI_API OCI_ExecuteStmt(OCI_Statement *stmt, const otext *sql)
Prepare and Execute a SQL statement or PL/SQL block.
OCI_SYM_PUBLIC boolean OCI_API OCI_StatementFree(OCI_Statement *stmt)
Free a statement and all resources associated to it (resultsets ...)

Functions

OCI_SYM_PUBLIC OCI_HashTable *OCI_API OCI_HashCreate (unsigned int size, unsigned int type)
 Create a hash table.
 
OCI_SYM_PUBLIC boolean OCI_API OCI_HashFree (OCI_HashTable *table)
 Destroy a hash table.
 
OCI_SYM_PUBLIC unsigned int OCI_API OCI_HashGetSize (OCI_HashTable *table)
 Return the size of the hash table.
 
OCI_SYM_PUBLIC unsigned int OCI_API OCI_HashGetType (OCI_HashTable *table)
 Return the type of the hash table.
 
OCI_SYM_PUBLIC boolean OCI_API OCI_HashAddString (OCI_HashTable *table, const otext *key, const otext *value)
 Add a pair string key / string value to the hash table.
 
OCI_SYM_PUBLIC const otext *OCI_API OCI_HashGetString (OCI_HashTable *table, const otext *key)
 Return the string value associated to the given key.
 
OCI_SYM_PUBLIC boolean OCI_API OCI_HashAddInt (OCI_HashTable *table, const otext *key, int value)
 Adds a pair string key / integer value to the hash table.
 
OCI_SYM_PUBLIC int OCI_API OCI_HashGetInt (OCI_HashTable *table, const otext *key)
 Return the integer value associated to the given key.
 
OCI_SYM_PUBLIC boolean OCI_API OCI_HashAddPointer (OCI_HashTable *table, const otext *key, void *value)
 Adds a pair string key / pointer value to the hash table.
 
OCI_SYM_PUBLIC void *OCI_API OCI_HashGetPointer (OCI_HashTable *table, const otext *key)
 Return a pointer associated with the given key.
 
OCI_SYM_PUBLIC OCI_HashEntry *OCI_API OCI_HashLookup (OCI_HashTable *table, const otext *key, boolean create)
 Lookup for an entry matching the key in the table.
 
OCI_SYM_PUBLIC OCI_HashValue *OCI_API OCI_HashGetValue (OCI_HashTable *table, const otext *key)
 Return the first hash slot that matches the key.
 
OCI_SYM_PUBLIC OCI_HashEntry *OCI_API OCI_HashGetEntry (OCI_HashTable *table, unsigned int index)
 Return the entry slot of the hash table internal list at the given position.
 

Function Documentation

◆ OCI_HashCreate()

OCI_SYM_PUBLIC OCI_HashTable *OCI_API OCI_HashCreate ( unsigned int  size,
unsigned int  type 
)

#include <api.h>

Create a hash table.

Parameters
size- size of the hash table
type- type of the hash table
Note
Parameter can be one of the following values :
  • OCI_HASH_STRING : string values
  • OCI_HASH_INTEGER : integer values
  • OCI_HASH_POINTER : pointer values
Returns
Hash handle on success or NULL on failure

◆ OCI_HashFree()

OCI_SYM_PUBLIC boolean OCI_API OCI_HashFree ( OCI_HashTable table)

#include <api.h>

Destroy a hash table.

Parameters
table- Table handle
Returns
TRUE on success otherwise FALSE

◆ OCI_HashGetSize()

OCI_SYM_PUBLIC unsigned int OCI_API OCI_HashGetSize ( OCI_HashTable table)

#include <api.h>

Return the size of the hash table.

Parameters
table- Table handle

◆ OCI_HashGetType()

OCI_SYM_PUBLIC unsigned int OCI_API OCI_HashGetType ( OCI_HashTable table)

#include <api.h>

Return the type of the hash table.

Parameters
table- Table handle
Note
the return value can be one of the following values :
  • OCI_HASH_STRING : string values
  • OCI_HASH_INTEGER : integer values
  • OCI_HASH_POINTER : pointer values
Returns
Hash table data type or OCI_UNKNOWN the input handle is NULL

◆ OCI_HashAddString()

OCI_SYM_PUBLIC boolean OCI_API OCI_HashAddString ( OCI_HashTable table,
const otext *  key,
const otext *  value 
)

#include <api.h>

Add a pair string key / string value to the hash table.

Parameters
table- Table handle
key- String key
value- string value
Returns
TRUE on success otherwise FALSE

◆ OCI_HashGetString()

OCI_SYM_PUBLIC const otext *OCI_API OCI_HashGetString ( OCI_HashTable table,
const otext *  key 
)

#include <api.h>

Return the string value associated to the given key.

Parameters
table- Table handle
key- String key
Returns
Stored string associated with the key otherwise NULL

◆ OCI_HashAddInt()

OCI_SYM_PUBLIC boolean OCI_API OCI_HashAddInt ( OCI_HashTable table,
const otext *  key,
int  value 
)

#include <api.h>

Adds a pair string key / integer value to the hash table.

Parameters
table- Table handle
key- String key
value- Integer value
Returns
TRUE on success otherwise FALSE

◆ OCI_HashGetInt()

OCI_SYM_PUBLIC int OCI_API OCI_HashGetInt ( OCI_HashTable table,
const otext *  key 
)

#include <api.h>

Return the integer value associated to the given key.

Parameters
table- Table handle
key- String key
Returns
Stored integer associated with the key otherwise 0

◆ OCI_HashAddPointer()

OCI_SYM_PUBLIC boolean OCI_API OCI_HashAddPointer ( OCI_HashTable table,
const otext *  key,
void *  value 
)

#include <api.h>

Adds a pair string key / pointer value to the hash table.

Parameters
table- Table handle
key- String key
value- Pointer value
Returns
TRUE on success otherwise FALSE

◆ OCI_HashGetPointer()

OCI_SYM_PUBLIC void *OCI_API OCI_HashGetPointer ( OCI_HashTable table,
const otext *  key 
)

#include <api.h>

Return a pointer associated with the given key.

Parameters
table- Table handle
key- String key
Returns
Stored pointer associated with the key otherwise NULL

◆ OCI_HashLookup()

OCI_SYM_PUBLIC OCI_HashEntry *OCI_API OCI_HashLookup ( OCI_HashTable table,
const otext *  key,
boolean  create 
)

#include <api.h>

Lookup for an entry matching the key in the table.

Parameters
table- Table handle
key- String key
create- Do create the entry if not exists
Returns
Entry handle if key found/added otherwise NULL

◆ OCI_HashGetValue()

OCI_SYM_PUBLIC OCI_HashValue *OCI_API OCI_HashGetValue ( OCI_HashTable table,
const otext *  key 
)

#include <api.h>

Return the first hash slot that matches the key.

Parameters
table- Table handle
key- String key
Returns
Slot handle if key found otherwise NULL

◆ OCI_HashGetEntry()

OCI_SYM_PUBLIC OCI_HashEntry *OCI_API OCI_HashGetEntry ( OCI_HashTable table,
unsigned int  index 
)

#include <api.h>

Return the entry slot of the hash table internal list at the given position.

Parameters
table- Table handle
index- index
Warning
Index start at at
Returns
Slot handle otherwise NULL