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 tables, if they exist. Otherwise create and open the tables

*/

#ifdef PROTOTYPE

VOID Define(VOID)

#else

VOID Define()

#endif

{

printf("DEFINE\n");

Create_CustomerMaster_Table();

Create_CustomerOrders_Table();

Create_OrderItems_Table();

Create_ItemMaster_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 ((hTableCustMast = ctdbAllocTable(hDatabase)) == NULL)

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

/* open table */

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

{

/* define table fields */

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

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

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

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

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

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

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

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

!pField5 || !pField6|| !pField7)

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

/* define index */

pIndex = ctdbAddIndex(hTableCustMast, "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(hTableCustMast, "custmast", CTCREATE_TRNLOG))

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

/* open table */

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

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

}

else

{

Check_Table_Mode(hTableCustMast);

/* 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(hTableCustMast, "cm_custnumb_idx") == NULL)

{

pField1 = ctdbGetFieldByName(hTableCustMast, "cm_custnumb");

pIndex = ctdbAddIndex(hTableCustMast, "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(hTableCustMast, CTDB_ALTER_NORMAL) != CTDBRET_OK)

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

}

}

}

/*

* Create_CustomerOrders_Table()

*

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

* along with its indexes and open it

*/

#ifdef PROTOTYPE

VOID Create_CustomerOrders_Table(VOID)

#else

VOID Create_CustomerOrders_Table()

#endif

{

CTHANDLE pField1, pField2, pField3, pField4;

CTHANDLE pIndex1, pIndex2;

CTHANDLE pIseg1, pIseg2;

/* define table CustomerOrders */

printf("\ttable CustomerOrders\n");

/* allocate a table handle */

if ((hTableCustOrdr = ctdbAllocTable(hDatabase)) == NULL)

Handle_Error("Create_CustomerOrders_Table(): ctdbAllocTable()");

/* open table */

if (ctdbOpenTable(hTableCustOrdr, "custordr", CTOPEN_NORMAL))

{

/* define table fields */

pField1 = ctdbAddField(hTableCustOrdr, "co_ordrdate", CT_DATE, 4);

pField2 = ctdbAddField(hTableCustOrdr, "co_promdate", CT_DATE, 4);

pField3 = ctdbAddField(hTableCustOrdr, "co_ordrnumb", CT_FSTRING, 6);

pField4 = ctdbAddField(hTableCustOrdr, "co_custnumb", CT_FSTRING, 4);

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

Handle_Error("Define(): ctdbAddField()");

/* define indexes */

pIndex1 = ctdbAddIndex(hTableCustOrdr, "co_ordrnumb_idx", CTINDEX_LEADING, NO, NO);

pIseg1 = ctdbAddSegment(pIndex1, pField3, CTSEG_SCHSEG);

pIndex2 = ctdbAddIndex(hTableCustOrdr, "co_custnumb_idx", CTINDEX_LEADING, YES, NO);

pIseg2 = ctdbAddSegment(pIndex2, pField4, CTSEG_SCHSEG);

if (!pIndex1 || !pIseg1 || !pIndex2 || !pIseg2)

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

/* create table */

if (ctdbCreateTable(hTableCustOrdr, "custordr", CTCREATE_TRNLOG))

Handle_Error("Create_CustomerOrders_Table(): ctdbCreateTable()");

/* open table */

if (ctdbOpenTable(hTableCustOrdr, "custordr", CTOPEN_NORMAL))

Handle_Error("Create_CustomerOrders_Table(): ctdbOpenTable()");

}

else

Check_Table_Mode(hTableCustOrdr);

}

/*

* Create_OrderItems_Table()

*

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

* along with its indexes and open it

*/

#ifdef PROTOTYPE

VOID Create_OrderItems_Table(VOID)

#else

VOID Create_OrderItems_Table()

#endif

{

CTHANDLE pField1, pField2, pField3, pField4;

CTHANDLE pIndex1, pIndex2;

CTHANDLE pIseg1, pIseg2, pIseg3;

/* define table OrderItems */

printf("\ttable OrderItems\n");

/* allocate a table handle */

if ((hTableOrdrItem = ctdbAllocTable(hDatabase)) == NULL)

Handle_Error("Create_OrderItems_Table(): ctdbAllocTable()");

if (ctdbOpenTable(hTableOrdrItem, "ordritem", CTOPEN_NORMAL))

{

/* define table fields */

pField1 = ctdbAddField(hTableOrdrItem,"oi_sequnumb", CT_INT2, 2);

pField2 = ctdbAddField(hTableOrdrItem,"oi_quantity", CT_INT2, 2);

pField3 = ctdbAddField(hTableOrdrItem,"oi_ordrnumb", CT_FSTRING, 6);

pField4 = ctdbAddField(hTableOrdrItem,"oi_itemnumb", CT_FSTRING, 5);

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

Handle_Error("Create_OrderItems_Table(): ctdbAddField()");

/* define indexes */

pIndex1 = ctdbAddIndex(hTableOrdrItem, "oi_ordrnumb_idx" ,CTINDEX_LEADING, NO, NO);

pIseg1 = ctdbAddSegment(pIndex1, pField3, CTSEG_SCHSEG);

pIseg2 = ctdbAddSegment(pIndex1, pField1, CTSEG_SCHSEG);

pIndex2 = ctdbAddIndex(hTableOrdrItem, "oi_itemnumb_idx" ,CTINDEX_LEADING, YES, NO);

pIseg3 = ctdbAddSegment(pIndex2, pField4, CTSEG_SCHSEG);

if (!pIndex1 || !pIseg1 || !pIseg2 || !pIndex2 || !pIseg3)

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

/* create table */

if (ctdbCreateTable(hTableOrdrItem, "ordritem", CTCREATE_TRNLOG))

Handle_Error("Create_OrderItems_Table(): ctdbCreateTable()");

/* open table */

if (ctdbOpenTable(hTableOrdrItem, "ordritem", CTOPEN_NORMAL))

Handle_Error("Create_OrderItems_Table(): ctdbOpenTable()");

}

else

Check_Table_Mode(hTableOrdrItem);

}

/*

* Create_ItemMaster_Table()

*

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

* along with its indexes and open it

*/

#ifdef PROTOTYPE

VOID Create_ItemMaster_Table(VOID)

#else

VOID Create_ItemMaster_Table()

#endif

{

CTHANDLE pField1, pField2, pField3, pField4;

CTHANDLE pIndex;

CTHANDLE pIseg;

/* define table ItemMaster */

printf("\ttable ItemMaster\n");

/* allocate a table handle */

if ((hTableItemMast = ctdbAllocTable(hDatabase)) == NULL)

Handle_Error("Create_ItemMaster_Table(): ctdbAllocTable()");

/* open table */

if (ctdbOpenTable(hTableItemMast, "itemmast", CTOPEN_NORMAL))

{

/* define table fields */

pField1 = ctdbAddField(hTableItemMast, "im_itemwght", CT_INT4, 4);

pField2 = ctdbAddField(hTableItemMast, "im_itempric", CT_MONEY, 4);

pField3 = ctdbAddField(hTableItemMast, "im_itemnumb", CT_FSTRING, 5);

pField4 = ctdbAddField(hTableItemMast, "im_itemdesc", CT_STRING, 47);

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

Handle_Error("Create_ItemMaster_Table(): ctdbAddField()");

/* define index */

pIndex = ctdbAddIndex(hTableItemMast, "im_itemnumb_idx", CTINDEX_FIXED, NO, NO);

pIseg = ctdbAddSegment(pIndex, pField3, CTSEG_SCHSEG);

if (!pIndex || !pIseg)

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

/* create table */

if (ctdbCreateTable(hTableItemMast, "itemmast", CTCREATE_TRNLOG))

Handle_Error("Create_ItemMaster_Table(); ctdbCreateTable()");

/* open table */

if (ctdbOpenTable(hTableItemMast, "itemmast", CTOPEN_NORMAL))

Handle_Error("Create_ItemMaster_Table(): ctdbOpenTable()");

}

else

Check_Table_Mode(hTableItemMast);

}

/*

* Check_Table_Mode()

*

* Check if existing table has transaction processing flag enabled.

* If a table is not ready for transaction, modify the table mode to

* enable 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 not under transaction processing control */

if (!(mode & CTCREATE_TRNLOG))

{

/* change file mode to enable transaction processing */

mode |= CTCREATE_TRNLOG;

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

Handle_Error("Check_Table_Mode(); ctdbUpdateCreateMode");

}

}

TOCIndex