Product Documentation

VCL/CLX Developers Guide

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():

//---------------------------------------------------------------------------

void TForm1::Define()

{

// open or create the tables

Create_OrderList_Table();

Create_OrderItem_Table();

Create_ItemMaster_Table();

Create_CustomerMaster_Table();

}

//---------------------------------------------------------------------------

void TForm1::Create_OrderList_Table()

{

boolean do_create = false;

try

{

// try to open the table

TabOrderList->Table = "ORDERLIST";

TabOrderList->Active = true;

}

catch (...)

{

// table may not exist, try creating it

do_create = true;

}

if (do_create)

{

try

{

// add fields to table

TabOrderList->AddField("ol_orderdate", CT_DATE, 4);

TabOrderList->AddField("ol_promdate", CT_DATE, 4);

TabOrderList->AddField("ol_ordernum", CT_STRING, 7);

TabOrderList->AddField("ol_custnum", CT_STRING, 4);

// add index to table

TabOrderList->AddIndex("custorder", CTINDEX_FIXED, false, false);

TabOrderList->AddSegment("custorder", "ol_ordernum", CTSEG_SCHSEG);

TabOrderList->AddSegment("custorder", "ol_custnum", CTSEG_SCHSEG);

// create table

TabOrderList->CreateMode = CTCREATE_TRNLOG;

TabOrderList->CreateTable();

// reopen the table

TabOrderList->Table = "ORDERLIST";

TabOrderList->Active = true;

}

catch (ECtError& E)

{

Application->ShowException(&E);

}

}

else

Check_Table_Mode(TabOrderList);

}

//---------------------------------------------------------------------------

void TForm1::Create_OrderItem_Table()

{

boolean do_create = false;

try

{

// try to open the table

TabOrderItem->Table = "ORDERITEMS";

TabOrderItem->Active = true;

}

catch (...)

{

// table may not exist, try creating it

do_create = true;

}

if (do_create)

{

try

{

// add fields to table

TabOrderItem->AddField("oi_ordernum", CT_STRING, 7);

TabOrderItem->AddField("oi_seqnumber", CT_INT2, 2);

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

TabOrderItem->AddField("oi_itemnum", CT_STRING, 6);

// add index to table

TabOrderItem->AddIndex("orderitem", CTINDEX_FIXED, false, false);

TabOrderItem->AddSegment("orderitem", "oi_ordernum", CTSEG_SCHSEG);

TabOrderItem->AddSegment("orderitem", "oi_seqnumber", CTSEG_SCHSEG);

// create table

TabOrderItem->CreateMode = CTCREATE_TRNLOG;

TabOrderItem->CreateTable();

// reopen the table

TabOrderItem->Table = "ORDERITEMS";

TabOrderItem->Active = true;

}

catch (ECtError& E)

{

Application->ShowException(&E);

}

}

else

Check_Table_Mode(TabOrderItem);

}

//---------------------------------------------------------------------------

void TForm1::Create_ItemMaster_Table()

{

boolean do_create = false;

try

{

TabItemMast->Table = "ITEMMAST";

TabItemMast->Active = true;

}

catch (...)

{

// table may not exist, try creating it

do_create = true;

}

if (do_create)

{

try

{

// add fields to table

TabItemMast->AddField("im_weight", CT_INT4, 4);

TabItemMast->AddField("im_price", CT_MONEY, 4);

TabItemMast->AddField("im_itemnum", CT_STRING, 6);

TabItemMast->AddField("im_desc", CT_STRING, 48);

// add index to table

TabItemMast->AddIndex("itemnum", CTINDEX_FIXED, false, false);

TabItemMast->AddSegment("itemnum", "im_itemnum", CTSEG_SCHSEG);

// create the table

TabItemMast->CreateMode = CTCREATE_TRNLOG;

TabItemMast->CreateTable();

// reopen the table

TabItemMast->Table = "ITEMMAST";

TabItemMast->Active = true;

}

catch (ECtError& E)

{

Application->ShowException(&E);

}

}

else

Check_Table_Mode(TabItemMast);

}

//---------------------------------------------------------------------------

void TForm1::Create_CustomerMaster_Table()

{

boolean do_create = false;

try

{

TabCustMast->Table = "CUSTMAST";

TabCustMast->Active = true;

}

catch (...)

{

// table may not exist, try creating it

do_create = true;

}

if (do_create)

{

try

{

// add fields to table

TabCustMast->AddField("cm_custnum", CT_STRING, 5);

TabCustMast->AddField("cm_zip", CT_STRING, 10);

TabCustMast->AddField("cm_state", CT_STRING, 3);

TabCustMast->AddField("cm_rating", CT_STRING, 2);

TabCustMast->AddField("cm_name", CT_STRING, 48);

TabCustMast->AddField("cm_address", CT_STRING, 48);

TabCustMast->AddField("cm_city", CT_STRING, 48);

// add index to table

TabCustMast->AddIndex("custnum", CTINDEX_FIXED, false, false);

TabCustMast->AddSegment("custnum", "cm_custnum", CTSEG_SCHSEG);

// create the table

TabCustMast->CreateMode = CTCREATE_TRNLOG;

TabCustMast->CreateTable();

// reopen the table

TabCustMast->Table = "CUSTMAST";

TabCustMast->Active = true;

}

catch (ECtError& E)

{

Application->ShowException(&E);

}

}

else

{

// FIRST set the TRN mode

Check_Table_Mode(TabCustMast);

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

// Therefore the index & segment is created now and added to the table by the call to ctdbAlterTable

try {

TabCustMast->GetIndex("custnum");

}

catch (...){

try {

TabCustMast->AddIndex("custnum",CTINDEX_FIXED,false, false); // DUPLICATES & NULLS

TabCustMast->AddSegment("custnum", "cm_custnum",CTSEG_SCHSEG);

TabCustMast->AlterTable();

// reopen the table

TabCustMast->Active = false;

TabCustMast->Active = true;

}

catch (ECtError& E) {

// failed to add index

Application->ShowException(&E);

}

}

}

}

//---------------------------------------------------------------------------

void TForm1::Check_Table_Mode(TCtTable* hTable)

{

try

{

// Or in the Transaction bit, IF it is not set

if (!(hTable->CreateMode & CTCREATE_TRNLOG))

{

hTable->UpdateCreateMode(hTable->CreateMode | CTCREATE_TRNLOG);

}

}

catch (ECtError &E)

{

Application->ShowException(&E);

}

}

TOCIndex