OCILIB (C Driver for Oracle) 3.9.1
Functions
Hash tables

Detailed Description

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"

int main(void)
{
    int i, n;
    OCI_Connection *cn;
    OCI_Statement  *st;
    OCI_Resultset  *rs;
    OCI_HashTable *table;
    OCI_HashEntry *e;
    OCI_HashValue *v;

    if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT))
        return EXIT_FAILURE;

    cn    = OCI_ConnectionCreate("db", "usr", "pwd", OCI_SESSION_DEFAULT);
    st    = OCI_StatementCreate(cn);
    table = 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(table, 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", "Cars", OCI_HashGetInt(table, "Cars"));

    /* browse the hash table */

    n = OCI_HashGetSize(table);

    for (i = 0; i < n; i++)
    {
        e = OCI_HashGetEntry(table, i);

        while (e != NULL)
        {
            printf (">key: '%s'\n", e->key);

            v = e->values;

            while (v != NULL)
            {
                printf ("..... value : '%i'\n", v->value.num);
                v = v->next;
            }

            e = e->next;
        }
    }

    /* destroy table */

    OCI_HashFree(table);
    
    OCI_Cleanup();
 
    return EXIT_SUCCESS;
}

Functions

OCI_EXPORT OCI_HashTable *OCI_API OCI_HashCreate (unsigned int size, unsigned int type)
 Create a hash table.
OCI_EXPORT boolean OCI_API OCI_HashFree (OCI_HashTable *table)
 Destroy a hash table.
OCI_EXPORT unsigned int OCI_API OCI_HashGetSize (OCI_HashTable *table)
 Return the size of the hash table.
OCI_EXPORT unsigned int OCI_API OCI_HashGetType (OCI_HashTable *table)
 Return the type of the hash table.
OCI_EXPORT boolean OCI_API OCI_HashAddString (OCI_HashTable *table, const mtext *key, const mtext *value)
 Add a pair string key / string value to the hash table.
OCI_EXPORT const mtext *OCI_API OCI_HashGetString (OCI_HashTable *table, const mtext *key)
 Return the string value associated to the given key.
OCI_EXPORT boolean OCI_API OCI_HashAddInt (OCI_HashTable *table, const mtext *key, int value)
 Adds a pair string key / integer value to the hash table.
OCI_EXPORT int OCI_API OCI_HashGetInt (OCI_HashTable *table, const mtext *key)
 Return the integer value associated to the given key.
OCI_EXPORT boolean OCI_API OCI_HashAddPointer (OCI_HashTable *table, const mtext *key, void *value)
 Adds a pair string key / pointer value to the hash table.
OCI_EXPORT void *OCI_API OCI_HashGetPointer (OCI_HashTable *table, const mtext *key)
 Return a pointer associated with the given key.
OCI_EXPORT OCI_HashEntry *OCI_API OCI_HashLookup (OCI_HashTable *table, const mtext *key, boolean create)
 Lookup for an entry matching the key in the table.
OCI_EXPORT OCI_HashValue *OCI_API OCI_HashGetValue (OCI_HashTable *table, const mtext *key)
 Return the first hash slot that matches the key.
OCI_EXPORT 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_EXPORT OCI_HashTable* OCI_API OCI_HashCreate ( unsigned int  size,
unsigned int  type 
)

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

Definition at line 77 of file hash.c.

References OCI_HashFree().

Referenced by OCI_ThreadKeyCreate().

OCI_EXPORT boolean OCI_API OCI_HashFree ( OCI_HashTable table)

Destroy a hash table.

Parameters:
table- Table handle
Returns:
TRUE on success otherwise FALSE

Definition at line 122 of file hash.c.

Referenced by OCI_HashCreate().

OCI_EXPORT unsigned int OCI_API OCI_HashGetSize ( OCI_HashTable table)

Return the size of the hash table.

Parameters:
table- Table handle

Definition at line 186 of file hash.c.

OCI_EXPORT unsigned int OCI_API OCI_HashGetType ( OCI_HashTable table)

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:
Hashtable datatype or OCI_UNKNOWN the input handle is NULL

Definition at line 202 of file hash.c.

OCI_EXPORT boolean OCI_API OCI_HashAddString ( OCI_HashTable table,
const mtext *  key,
const mtext *  value 
)

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

Definition at line 409 of file hash.c.

OCI_EXPORT const mtext* OCI_API OCI_HashGetString ( OCI_HashTable table,
const mtext *  key 
)

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

Definition at line 263 of file hash.c.

References OCI_HashGetValue().

OCI_EXPORT boolean OCI_API OCI_HashAddInt ( OCI_HashTable table,
const mtext *  key,
int  value 
)

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

Definition at line 434 of file hash.c.

OCI_EXPORT int OCI_API OCI_HashGetInt ( OCI_HashTable table,
const mtext *  key 
)

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

Definition at line 291 of file hash.c.

References OCI_HashGetValue().

OCI_EXPORT boolean OCI_API OCI_HashAddPointer ( OCI_HashTable table,
const mtext *  key,
void *  value 
)

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

Definition at line 459 of file hash.c.

Referenced by OCI_ThreadKeyCreate().

OCI_EXPORT void* OCI_API OCI_HashGetPointer ( OCI_HashTable table,
const mtext *  key 
)

Return a pointer associated with the given key.

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

Definition at line 319 of file hash.c.

References OCI_HashGetValue().

Referenced by OCI_ThreadKeyGetValue(), and OCI_ThreadKeySetValue().

OCI_EXPORT OCI_HashEntry* OCI_API OCI_HashLookup ( OCI_HashTable table,
const mtext *  key,
boolean  create 
)

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

Definition at line 484 of file hash.c.

Referenced by OCI_HashGetValue().

OCI_EXPORT OCI_HashValue* OCI_API OCI_HashGetValue ( OCI_HashTable table,
const mtext *  key 
)

Return the first hash slot that matches the key.

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

Definition at line 218 of file hash.c.

References OCI_HashLookup().

Referenced by OCI_HashGetInt(), OCI_HashGetPointer(), and OCI_HashGetString().

OCI_EXPORT 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.

Parameters:
table- Table handle
index- index
Returns:
Slot handle otherwise NULL

Definition at line 245 of file hash.c.