Define() establishes specific data definitions. This involves defining columns/fields and creating the tables/files with optional indexes.
Below is the code for Define():
procedure TForm1.Define;
begin
// open or create the tables
Create_CustomerMaster_Table;
end;
procedure TForm1.Create_CustomerMaster_Table;
var
do_create : boolean;
begin
do_create := false;
try
TabCustMast.Table := 'CUSTMAST';
TabCustMast.Active := true;
except
// table may not exist, try creating it
do_create := true;
end;
if do_create then
begin
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;
except
on E : ECtError do Application.ShowException(E);
end;
end
else
begin
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
begin
TabCustMast.GetIndex('custnum');
end;
except
begin
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;
except
// failed to add index
on E : ECtError do Application.ShowException(E);
end;
end;
end;
end;
end;
procedure TForm1.Check_Table_Mode(hTable : TCtTable);
begin
try
// And off the Transaction bit, IF it is set
if (hTable.CreateMode and CTCREATE_TRNLOG) = CTCREATE_TRNLOG then
begin
hTable.UpdateCreateMode(hTable.CreateMode xor CTCREATE_TRNLOG);
end;
except
on E : ECtError do Application.ShowException(E);
end;
end;