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
'
Sub Define()
Console.WriteLine("DEFINE")
Create_CustomerMaster_Table()
Create_CustomerOrders_Table()
Create_OrderItems_Table()
Create_ItemMaster_Table()
End Sub
'
' Create_CustomerMaster_Table()
'
' Open table CustomerMaster, if it exists. Otherwise create it
' along with its indexes and open it
'
Sub Create_CustomerMaster_Table()
Dim do_create As Boolean = False
Dim field1 As CTField
Dim index1 As CTIndex
' define table CustomerMaster
Console.WriteLine(ControlChars.Tab + "table CustomerMaster")
Try
tableCustMast.Open("custmast", OPEN_MODE.NORMAL_OPEN)
Catch
' table does not exist
do_create = True
End Try
If (do_create) Then
' define table fields
Console.WriteLine(ControlChars.Tab + "Add fields...")
Try
field1 = tableCustMast.AddField("cm_custnumb", FIELD_TYPE.FSTRING, 4)
tableCustMast.AddField("cm_custzipc", FIELD_TYPE.FSTRING, 9)
tableCustMast.AddField("cm_custstat", FIELD_TYPE.FSTRING, 2)
tableCustMast.AddField("cm_custratg", FIELD_TYPE.FSTRING, 1)
tableCustMast.AddField("cm_custname", FIELD_TYPE.VSTRING, 47)
tableCustMast.AddField("cm_custaddr", FIELD_TYPE.VSTRING, 47)
tableCustMast.AddField("cm_custcity", FIELD_TYPE.VSTRING, 47)
' define index
index1 = tableCustMast.AddIndex("cm_custnumb_idx", KEY_TYPE.FIXED_INDEX, False, False)
index1.AddSegment(field1, SEG_MODE.SCHSEG_SEG)
' create table
Console.WriteLine(ControlChars.Tab + "Create table...")
tableCustMast.Create("custmast", CREATE_MODE.TRNLOG_CREATE)
' open table
Console.WriteLine(ControlChars.Tab + "Open table...")
tableCustMast.Open("custmast", OPEN_MODE.NORMAL_OPEN)
Catch E As CTException
Handle_Exception(E)
End Try
Else
Check_Table_Mode(tableCustMast)
' confirm the index exists, if not then add the index
'
' Me scenario arises out of the fact that Me table was created in tutorial 1
' without indexes. The index is now created by the call to ctdbAlterTable
do_create = False
Try
tableCustMast.GetIndex("cm_custnumb_idx")
Catch
do_create = True
End Try
If (do_create) Then
Try
field1 = tableCustMast.GetField("cm_custnumb")
index1 = tableCustMast.AddIndex("cm_custnumb_idx", KEY_TYPE.FIXED_INDEX, False, False)
index1.AddSegment(field1, SEG_MODE.SCHSEG_SEG)
tableCustMast.Alter(ALTER_TABLE.NORMAL)
Catch E As CTException
Handle_Exception(E)
End Try
End If
End If
End Sub
'
' Create_CustomerOrders_Table()
'
' Open table CustomerOrders, if it exists. Otherwise create it
' along with its indexes and open it
'
Sub Create_CustomerOrders_Table()
Dim do_create As Boolean = False
Dim field1, field2 As CTField
Dim index1, index2 As CTIndex
' define table CustomerOrders
Console.WriteLine(ControlChars.Tab + "table CustomerOrders")
Try
tableCustOrdr.Open("custordr", OPEN_MODE.NORMAL_OPEN)
Catch
' table does not exist
do_create = True
End Try
If (do_create) Then
Try
' define table fields
tableCustOrdr.AddField("co_ordrdate", FIELD_TYPE.DATE, 4)
tableCustOrdr.AddField("co_promdate", FIELD_TYPE.DATE, 4)
field1 = tableCustOrdr.AddField("co_ordrnumb", FIELD_TYPE.FSTRING, 6)
field2 = tableCustOrdr.AddField("co_custnumb", FIELD_TYPE.FSTRING, 4)
' define indexes
index1 = tableCustOrdr.AddIndex("co_ordrnumb_idx", KEY_TYPE.LEADING_INDEX, False, False)
index1.AddSegment(field1, SEG_MODE.SCHSEG_SEG)
index2 = tableCustOrdr.AddIndex("co_custnumb_idx", KEY_TYPE.LEADING_INDEX, True, False)
index2.AddSegment(field2, SEG_MODE.SCHSEG_SEG)
' create table
Console.WriteLine(ControlChars.Tab + "Create table...")
tableCustOrdr.Create("custordr", CREATE_MODE.TRNLOG_CREATE)
' open table
Console.WriteLine(ControlChars.Tab + "Open table...")
tableCustOrdr.Open("custordr", OPEN_MODE.NORMAL_OPEN)
Catch E As CTException
Handle_Exception(E)
End Try
Else
Check_Table_Mode(tableCustOrdr)
End If
End Sub
'
' Create_OrderItems_Table()
'
' Open table OrderItems, if it exists. Otherwise create it
' along with its indexes and open it
'
Sub Create_OrderItems_Table()
Dim do_create As Boolean = False
Dim field1, field2, field3 As CTField
Dim index1, index2 As CTIndex
' define table OrderItems
Console.WriteLine(ControlChars.Tab + "table OrderItems")
Try
tableOrdrItem.Open("ordritem", OPEN_MODE.NORMAL_OPEN)
Catch
' table does not exist
do_create = True
End Try
If (do_create) Then
Try
' define table fields
field1 = tableOrdrItem.AddField("oi_sequnumb", FIELD_TYPE.INT2, 2)
tableOrdrItem.AddField("oi_quantity", FIELD_TYPE.INT2, 2)
field2 = tableOrdrItem.AddField("oi_ordrnumb", FIELD_TYPE.FSTRING, 6)
field3 = tableOrdrItem.AddField("oi_itemnumb", FIELD_TYPE.FSTRING, 5)
' define indexes
index1 = tableOrdrItem.AddIndex("oi_ordrnumb_idx", KEY_TYPE.LEADING_INDEX, False, False)
index1.AddSegment(field2, SEG_MODE.SCHSEG_SEG)
index1.AddSegment(field1, SEG_MODE.SCHSEG_SEG)
index2 = tableOrdrItem.AddIndex("oi_itemnumb_idx", KEY_TYPE.LEADING_INDEX, True, False)
index2.AddSegment(field3, SEG_MODE.SCHSEG_SEG)
' create table
Console.WriteLine(ControlChars.Tab + "Create table...")
tableOrdrItem.Create("ordritem", CREATE_MODE.TRNLOG_CREATE)
' open table
Console.WriteLine(ControlChars.Tab + "Open table...")
tableOrdrItem.Open("ordritem", OPEN_MODE.NORMAL_OPEN)
Catch E As CTException
Handle_Exception(E)
End Try
Else
Check_Table_Mode(tableCustOrdr)
End If
End Sub
'
' Create_ItemMaster_Table()
'
' Open table ItemMaster, if it exists. Otherwise create it
' along with its indexes and open it
'
Sub Create_ItemMaster_Table()
Dim do_create As Boolean = False
Dim field1 As CTField
Dim index1 As CTIndex
' define table ItemMaster
Console.WriteLine(ControlChars.Tab + "table ItemMaster")
Try
tableItemMast.Open("itemmast", OPEN_MODE.NORMAL_OPEN)
Catch
' table does not exist
do_create = True
End Try
If (do_create) Then
Try
' define table fields
tableItemMast.AddField("im_itemwght", FIELD_TYPE.INT4, 4)
tableItemMast.AddField("im_itempric", FIELD_TYPE.MONEY, 4)
field1 = tableItemMast.AddField("im_itemnumb", FIELD_TYPE.FSTRING, 5)
tableItemMast.AddField("im_itemdesc", FIELD_TYPE.VSTRING, 47)
' define indexes
index1 = tableItemMast.AddIndex("im_itemnumb_idx", KEY_TYPE.LEADING_INDEX, False, False)
index1.AddSegment(field1, SEG_MODE.SCHSEG_SEG)
' create table
Console.WriteLine(ControlChars.Tab + "Create table...")
tableItemMast.Create("itemmast", CREATE_MODE.TRNLOG_CREATE)
' open table
Console.WriteLine(ControlChars.Tab + "Open table...")
tableItemMast.Open("itemmast", OPEN_MODE.NORMAL_OPEN)
Catch E As CTException
Handle_Exception(E)
End Try
Else
Check_Table_Mode(tableCustOrdr)
End If
End Sub
'
' Check_Table_Mode()
'
' Check if existing table has transaction processing flag enabled.
' If a table is under transaction processing control, modify the
' table mode to disable transaction processing
'
Sub Check_Table_Mode(ByVal table As CTTable)
Try
' get table create mode
Dim mode As CREATE_MODE = table.GetCreateMode()
' check if table is not under transaction processing control
If ((mode And CREATE_MODE.TRNLOG_CREATE) = 0) Then
' change file mode to enable transaction processing
mode = mode Or CREATE_MODE.TRNLOG_CREATE
table.UpdateCreateMode(mode)
End If
Catch E As CTException
Handle_Exception(E)
End Try
End Sub