Define
The Define() step is where specific data definitions are established by your application and/or process. This involves defining columns/fields and creating the tables/files with optional indices. Below is the code for Define(): // // Define() // // Open the table, if it exists. Otherwise create and open the table // VOID Define(VOID) {
printf("DEFINE\n");
Create_CustomerMaster_Table(); } // // Create_CustomerMaster_Table() // // Open table CustomerMaster, if it exists. Otherwise create it // along with its indices and open it // VOID Create_CustomerMaster_Table(VOID) {
CTBOOL do_create = NO; // define table CustomerMaster printf("\ttable CustomerMaster\n");
try {
MyTable.Open("custmast", CTOPEN_NORMAL);
} catch (...) {
// table does not exist do_create = YES; } if (do_create) {
try {
// define table fields CTField field1 = MyTable.AddField("cm_custnumb", CT_FSTRING, 4);
MyTable.AddField("cm_custzipc", CT_FSTRING, 9);
MyTable.AddField("cm_custstat", CT_FSTRING, 2);
MyTable.AddField("cm_custratg", CT_FSTRING, 1);
MyTable.AddField("cm_custname", CT_STRING, 47);
MyTable.AddField("cm_custaddr", CT_STRING, 47);
MyTable.AddField("cm_custcity", CT_STRING, 47);
// define index CTIndex index1 = MyTable.AddIndex("cm_custnumb_idx", CTINDEX_FIXED, NO, NO);
index1.AddSegment(field1, CTSEG_SCHSEG); // create table printf("\tCreate table...\n");
MyTable.Create("custmast", CTCREATE_NORMAL);
// open table printf("\tOpen table...\n");
MyTable.Open("custmast", CTOPEN_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 = NO; try {
MyTable.GetIndex("cm_custnumb_idx");
} catch (CTException E) {
do_create = YES; } if (do_create) {
try {
CTField field1 = MyTable.GetField("cm_custnumb");
CTIndex index1 = MyTable.AddIndex("cm_custnumb_idx", CTINDEX_FIXED, NO, NO);
index1.AddSegment(field1, CTSEG_SCHSEG); MyTable.Alter(CTDB_ALTER_NORMAL); } catch (CTException E) {
Handle_Exception(E); } } } } // // 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 // VOID Check_Table_Mode(CTTable& table) {
try {
// get table create mode CTCREATE_MODE mode = table.GetCreateMode(); // check if table is under transaction processing control if ((mode & CTCREATE_TRNLOG)) {
// change file mode to disable transaction processing mode ^= CTCREATE_TRNLOG; table.UpdateCreateMode(mode); } } catch (CTException E) {
Handle_Exception(E); } } |
|||