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

procedure TForm1.Define;

begin

// open or create the tables

Create_OrderList_Table;

Create_OrderItem_Table;

Create_ItemMaster_Table;

Create_CustomerMaster_Table;

end;

procedure TForm1.Create_OrderList_Table;

var

do_create : boolean;

begin

do_create := false;

try

// try to open the table

TabOrderList.Table := 'ORDERLIST';

TabOrderList.Active := true;

except

// table may not exist, try creating it

do_create := true;

end;

if do_create then

begin

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;

except

on E : ECtError do Application.ShowException(E);

end;

end

else

begin

Check_Table_Mode(TabOrderList);

end;

end;

procedure TForm1.Create_OrderItem_Table;

var

do_create : boolean;

begin

do_create := false;

try

// try to open the table

TabOrderItem.Table := 'ORDERITEMS';

TabOrderItem.Active := true;

except

// table may not exist, try creating it

do_create := true;

end;

if do_create then

begin

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;

except

on E : ECtError do Application.ShowException(E);

end;

end

else

begin

Check_Table_Mode(TabOrderItem);

end;

end;

procedure TForm1.Create_ItemMaster_Table;

var

do_create : boolean;

begin

do_create := false;

try

TabItemMast.Table := 'ITEMMAST';

TabItemMast.Active := true;

except

// table may not exist, try creating it

do_create := true;

end;

if do_create then

begin

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;

except

on E : ECtError do Application.ShowException(E);

end;

end

else

begin

Check_Table_Mode(TabItemMast);

end;

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.CreateMode := CTCREATE_TRNLOG;

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(TabOrderItem);

// 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) = 0 then

begin

hTable.UpdateCreateMode(hTable.CreateMode xor CTCREATE_TRNLOG);

end;

except

on E : ECtError do Application.ShowException(E);

end;

end;

TOCIndex