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
//
static void Define() throws IOException {
System.out.println("DEFINE");
Create_CustomerMaster_Table();
}
//
// Create_CustomerMaster_Table()
//
// Open table CustomerMaster, if it exists. Otherwise create it
// along with its indices and open it
//
static void Create_CustomerMaster_Table() throws IOException {
boolean do_create = false;
// define table CustomerMaster
System.out.println("\ttable CustomerMaster");
try {
MyTable.Open("custmast", OPEN_MODE.NORMAL);
} catch (CTException E) {
// table does not exist
do_create = true;
}
if (do_create) {
try {
// define table fields
CTField field1 = MyTable.AddField("cm_custnumb", FIELD_TYPE.CHARS, 4);
MyTable.AddField("cm_custzipc", FIELD_TYPE.CHARS, 9);
MyTable.AddField("cm_custstat", FIELD_TYPE.CHARS, 2);
MyTable.AddField("cm_custratg", FIELD_TYPE.CHARS, 1);
MyTable.AddField("cm_custname", FIELD_TYPE.VARCHAR, 47);
MyTable.AddField("cm_custaddr", FIELD_TYPE.VARCHAR, 47);
MyTable.AddField("cm_custcity", FIELD_TYPE.VARCHAR, 47);
// define index
CTIndex index1 = MyTable.AddIndex("cm_custnumb_idx", KEY_TYPE.FIXED_INDEX, false, false);
index1.AddSegment(field1, SEG_MODE.SCHSEG);
// create table
System.out.println("\tCreate table...");
MyTable.Create("custmast", CREATE_MODE.NORMAL);
// open table
System.out.println("\tOpen table...");
MyTable.Open("custmast", OPEN_MODE.NORMAL);
} catch (CTException E) {
Handle_Exception(E);
}
} else {
Check_Table_Mode(MyTable);
// 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. The index is now created by the call to ctdbAlterTable
do_create = false;
try {
MyTable.GetIndex("cm_custnumb_idx");
} catch (CTException E) {
do_create = true;
}
if (do_create) {
try {
CTField field1 = MyTable.GetField("cm_custnumb");
CTIndex index1 = MyTable.AddIndex("cm_custnumb_idx", KEY_TYPE.FIXED_INDEX, false, false);
index1.AddSegment(field1, SEG_MODE.SCHSEG);
MyTable.Alter(ALTER_TABLE.NORMAL);
} catch (CTException E) {
Handle_Exception(E);
}
}
}
}