Product Documentation

c-treeDB API for C

Previous Topic

Next Topic

Define

Define() establishes specific data definitions. This involves defining columns/fields and creating the tables/files with optional indexes.

Below is the code for Define():

/*

* Define()

*

* Open the table, if they exist. Otherwise create and open the table

*/

#ifdef PROTOTYPE

VOID Define(VOID)

#else

VOID Define()

#endif

{

printf("DEFINE\n");

Create_CustomerMaster_Table();

}

/*

* Create_CustomerMaster_Table()

*

* Open table CustomerMaster, if it exists. Otherwise create it

* along with its indexes and open it

*/

#ifdef PROTOTYPE

VOID Create_CustomerMaster_Table(VOID)

#else

VOID Create_CustomerMaster_Table()

#endif

{

CTHANDLE pField1, pField2, pField3, pField4;

CTHANDLE pField5, pField6, pField7;

CTHANDLE pIndex;

CTHANDLE pIseg;

/* define table CustomerMaster */

printf("\ttable CustomerMaster\n");

/* allocate a table handle */

if ((hTable = ctdbAllocTable(hDatabase)) == NULL)

Handle_Error("Create_CustomerMaster_Table(): ctdbAllocTable()");

/* open table */

if (ctdbOpenTable(hTable, "custmast", CTOPEN_NORMAL))

{

/* define table fields */

pField1 = ctdbAddField(hTable, "cm_custnumb", CT_FSTRING, 4);

pField2 = ctdbAddField(hTable, "cm_custzipc", CT_FSTRING, 9);

pField3 = ctdbAddField(hTable, "cm_custstat", CT_FSTRING, 2);

pField4 = ctdbAddField(hTable, "cm_custratg", CT_FSTRING, 1);

pField5 = ctdbAddField(hTable, "cm_custname", CT_STRING, 47);

pField6 = ctdbAddField(hTable, "cm_custaddr", CT_STRING, 47);

pField7 = ctdbAddField(hTable, "cm_custcity", CT_STRING, 47);

if (!pField1 || !pField2 || !pField3 || !pField4 ||

!pField5 || !pField6|| !pField7)

Handle_Error("Create_CustomerMaster_Table(): ctdbAddField()");

/* define index */

pIndex = ctdbAddIndex(hTable, "cm_custnumb_idx", CTINDEX_FIXED, NO, NO);

pIseg = ctdbAddSegment(pIndex, pField1, CTSEG_SCHSEG);

if (!pIndex || !pIseg)

Handle_Error("Create_CustomerMaster_Table(): ctdbAddIndex()|ctdbAddSegment()");

/* create table */

if (ctdbCreateTable(hTable, "custmast", CTCREATE_NORMAL))

Handle_Error("Create_CustomerMaster_Table(): ctdbCreateTable()");

/* open table */

if (ctdbOpenTable(hTable, "custmast", CTOPEN_NORMAL))

Handle_Error("Create_CustomerMaster_Table(): ctdbOpenTable()");

}

else

{

Check_Table_Mode(hTable);

/* confirm the index exists, if not then add the index

*

* this scenario arises out of the fact that this table was created in tutorial 1

* without indexes. The index is now created by the call to ctdbAlterTable

*/

if (ctdbGetIndexByName(hTable, "cm_custnumb_idx") == NULL)

{

pField1 = ctdbGetFieldByName(hTable, "cm_custnumb");

pIndex = ctdbAddIndex(hTable, "cm_custnumb_idx", CTINDEX_FIXED, NO, NO);

pIseg = ctdbAddSegment(pIndex, pField1, CTSEG_SCHSEG);

if (!pIndex || !pIseg)

Handle_Error("Create_CustomerMaster_Table(): ctdbAddIndex()|ctdbAddSegment()");

if (ctdbAlterTable(hTable, CTDB_ALTER_NORMAL) != CTDBRET_OK)

Handle_Error("Create_CustomerMaster_Table(): ctdbAlterTable()");

}

}

}

/*

* Check_Table_Mode()

*

* Check if existing table has transaction processing flag enabled.

* If a table is under transaction processing control, modify the

* table mode to disable transaction processing

*/

#ifdef PROTOTYPE

VOID Check_Table_Mode(CTHANDLE hTable)

#else

VOID Check_Table_Mode(hTable)

CTHANDLE hTable;

#endif

{

CTCREATE_MODE mode;

/* get table create mode */

mode = ctdbGetTableCreateMode(hTable);

/* check if table is under transaction processing control */

if ((mode & CTCREATE_TRNLOG))

{

/* change file mode to disable transaction processing */

mode ^= CTCREATE_TRNLOG;

if (ctdbUpdateCreateMode(hTable, mode) != CTDBRET_OK)

Handle_Error("Check_Table_Mode(); ctdbUpdateCreateMode");

}

}

TOCIndex