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_CustomerMaster_Table();
}
//---------------------------------------------------------------------------
void TForm1::Create_CustomerMaster_Table()
{
bool do_create = false;
try
{
TabCustMast->Table = "CUSTMAST";
TabCustMast->Active = true;
}
catch (ECtError &E)
{
// 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->CreateTable();
// reopen the table
TabCustMast->Table = "CUSTMAST";
TabCustMast->Active = true;
}
catch (ECtError &E)
{
Application->ShowException(&E);
}
}
else
{
// FIRST set the table mode
Check_Table_Mode(TabCustMast);
// 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
{
// And off the Transaction bit, IF it is set
if (hTable->CreateMode & CTCREATE_TRNLOG)
{
hTable->UpdateCreateMode(hTable->CreateMode & ~CTCREATE_TRNLOG);
}
}
catch (ECtError &E)
{
Application->ShowException(&E);
}
}