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 it exists. Otherwise create and open the table

//

VOID Define(VOID)

{

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

//

VOID Create_CustomerMaster_Table(VOID)

{

CTBOOL do_create = NO;

// define table CustomerMaster

printf("\ttable CustomerMaster\n");

try

{

tableCustMast->Open("custmast", CTOPEN_NORMAL);

}

catch (CTException E)

{

// table does not exist

do_create = YES;

}

if (do_create)

{

try

{

// define table fields

CTField field1 = tableCustMast->AddField("cm_custnumb", CT_FSTRING, 4);

tableCustMast->AddField("cm_custzipc", CT_FSTRING, 9);

tableCustMast->AddField("cm_custstat", CT_FSTRING, 2);

tableCustMast->AddField("cm_custratg", CT_FSTRING, 1);

tableCustMast->AddField("cm_custname", CT_STRING, 47);

tableCustMast->AddField("cm_custaddr", CT_STRING, 47);

tableCustMast->AddField("cm_custcity", CT_STRING, 47);

// define index

CTIndex index1 = tableCustMast->AddIndex("cm_custnumb_idx", CTINDEX_FIXED, NO, NO);

index1.AddSegment(field1, CTSEG_SCHSEG);

// create table

printf("\tCreate table...\n");

tableCustMast->Create("custmast", CTCREATE_TRNLOG);

// open table

printf("\tOpen table...\n");

tableCustMast->Open("custmast", CTOPEN_NORMAL);

}

catch (CTException E)

{

Handle_Exception(E);

}

}

else

{

Check_Table_Mode(tableCustMast);

// 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

do_create = NO;

try

{

tableCustMast->GetIndex("cm_custnumb_idx");

}

catch (CTException E)

{

do_create = YES;

}

if (do_create)

{

try

{

CTField field1 = tableCustMast->GetField("cm_custnumb");

CTIndex index1 = tableCustMast->AddIndex("cm_custnumb_idx", CTINDEX_FIXED, NO, NO);

index1.AddSegment(field1, CTSEG_SCHSEG);

tableCustMast->Alter(CTDB_ALTER_NORMAL);

}

catch (CTException E)

{

Handle_Exception(E);

}

}

}

}

//

// Create_CustomerOrders_Table()

//

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

// along with its indexes and open it

//

VOID Create_CustomerOrders_Table(VOID)

{

CTBOOL do_create = NO;

// define table CustomerOrders

printf("\ttable CustomerOrders\n");

try

{

tableCustOrdr->Open("custordr", CTOPEN_NORMAL);

}

catch (CTException E)

{

// table does not exist

do_create = YES;

}

if (do_create)

{

try

{

// define table fields

tableCustOrdr->AddField("co_ordrdate", CT_DATE, 4);

tableCustOrdr->AddField("co_promdate", CT_DATE, 4);

CTField field1 = tableCustOrdr->AddField("co_ordrnumb", CT_FSTRING, 6);

CTField field2 = tableCustOrdr->AddField("co_custnumb", CT_FSTRING, 4);

// define indexes

CTIndex index1 = tableCustOrdr->AddIndex("co_ordrnumb_idx", CTINDEX_LEADING, NO, NO);

index1.AddSegment(field1, CTSEG_SCHSEG);

CTIndex index2 = tableCustOrdr->AddIndex("co_custnumb_idx", CTINDEX_LEADING, YES, NO);

index2.AddSegment(field2, CTSEG_SCHSEG);

// create table

printf("\tCreate table...\n");

tableCustOrdr->Create("custordr", CTCREATE_TRNLOG);

// open table

printf("\tOpen table...\n");

tableCustOrdr->Open("custordr", CTOPEN_NORMAL);

}

catch (CTException E)

{

Handle_Exception(E);

}

}

else

Check_Table_Mode(tableCustOrdr);

}

//

// Create_OrderItems_Table()

//

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

// along with its indexes and open it

//

VOID Create_OrderItems_Table(VOID)

{

CTBOOL do_create = NO;

// define table OrderItems

printf("\ttable OrderItems\n");

try

{

tableOrdrItem->Open("ordritem", CTOPEN_NORMAL);

}

catch (CTException E)

{

// table does not exist

do_create = YES;

}

if (do_create)

{

try

{

// define table fields

CTField field1 = tableOrdrItem->AddField("oi_sequnumb", CT_INT2, 2);

tableOrdrItem->AddField("oi_quantity", CT_INT2, 2);

CTField field2 = tableOrdrItem->AddField("oi_ordrnumb", CT_FSTRING, 6);

CTField field3 = tableOrdrItem->AddField("oi_itemnumb", CT_FSTRING, 5);

// define indexes

CTIndex index1 = tableOrdrItem->AddIndex("oi_ordrnumb_idx", CTINDEX_LEADING, NO, NO);

index1.AddSegment(field2, CTSEG_SCHSEG);

index1.AddSegment(field1, CTSEG_SCHSEG);

CTIndex index2 = tableOrdrItem->AddIndex("oi_itemnumb_idx", CTINDEX_LEADING, YES, NO);

index2.AddSegment(field3, CTSEG_SCHSEG);

// create table

printf("\tCreate table...\n");

tableOrdrItem->Create("ordritem", CTCREATE_TRNLOG);

// open table

printf("\tOpen table...\n");

tableOrdrItem->Open("ordritem", CTOPEN_NORMAL);

}

catch (CTException E)

{

Handle_Exception(E);

}

}

else

Check_Table_Mode(tableOrdrItem);

}

//

// Create_ItemMaster_Table()

//

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

// along with its indexes and open it

//

VOID Create_ItemMaster_Table(VOID)

{

CTBOOL do_create = NO;

// define table ItemMaster

printf("\ttable ItemMaster\n");

try

{

tableItemMast->Open("itemmast", CTOPEN_NORMAL);

}

catch (CTException E)

{

// table does not exist

do_create = YES;

}

if (do_create)

{

try

{

// define table fields

tableItemMast->AddField("im_itemwght", CT_INT4, 4);

tableItemMast->AddField("im_itempric", CT_MONEY, 4);

CTField field1 = tableItemMast->AddField("im_itemnumb", CT_FSTRING, 5);

tableItemMast->AddField("im_itemdesc", CT_STRING, 47);

// define indexes

CTIndex index1 = tableItemMast->AddIndex("im_itemnumb_idx", CTINDEX_FIXED, NO, NO);

index1.AddSegment(field1, CTSEG_SCHSEG);

// create table

printf("\tCreate table...\n");

tableItemMast->Create("itemmast", CTCREATE_TRNLOG);

// open table

printf("\tOpen table...\n");

tableItemMast->Open("itemmast", CTOPEN_NORMAL);

}

catch (CTException E)

{

Handle_Exception(E);

}

}

else

Check_Table_Mode(tableItemMast);

}

//

// 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

//

VOID Check_Table_Mode(CTTable* table)

{

try

{

// get table create mode

CTCREATE_MODE mode = table->GetCreateMode();

// check if table is not under transaction processing control

if (!(mode & CTCREATE_TRNLOG))

{

// change file mode to enable transaction processing

mode |= CTCREATE_TRNLOG;

table->UpdateCreateMode(mode);

}

}

catch (CTException E)

{

Handle_Exception(E);

}

}

TOCIndex