Product Documentation

c-treeDB API for C#

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

//

// Define()

//

// Open the table, if it exists. Otherwise create and open the table

//

static void Define()

{

Console.WriteLine("DEFINE");

Create_CustomerMaster_Table();

Create_CustomerOrders_Table();

Create_OrderItems_Table();

Create_ItemMaster_Table();

}

//

// Create_CustomerMaster_Table()

//

// Open table CustomerMaster, if it exists. Otherwise create it

// along with its indexes and open it

//

static void Create_CustomerMaster_Table()

{

bool do_create = false;

// define table CustomerMaster

Console.WriteLine("\ttable CustomerMaster");

try

{

tableCustMast.Open("custmast", OPEN_MODE.NORMAL_OPEN);

}

catch (CTException)

{

// table does not exist

do_create = true;

}

if (do_create)

{

try

{

// define table fields

CTField 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

CTIndex index1 = tableCustMast.AddIndex("cm_custnumb_idx", KEY_TYPE.FIXED_INDEX, false, false);

index1.AddSegment(field1, SEG_MODE.SCHSEG_SEG);

// create table

Console.WriteLine("\tCreate table...");

tableCustMast.Create("custmast", CREATE_MODE.TRNLOG_CREATE);

// open table

Console.WriteLine("\tOpen table...");

tableCustMast.Open("custmast", OPEN_MODE.NORMAL_OPEN);

}

catch (CTException E)

{

Handle_Exception(E);

}

}

else

{

Check_Table_Mode(tableCustMast);

// 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

{

tableCustMast.GetIndex("cm_custnumb_idx");

}

catch (CTException)

{

do_create = true;

}

if (do_create)

{

try

{

CTField field1 = tableCustMast.GetField("cm_custnumb");

CTIndex 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 (CTException E)

{

Handle_Exception(E);

}

}

}

}

//

// Create_CustomerOrders_Table()

//

// Open table CustomerOrders, if it exists. Otherwise create it

// along with its indexes and open it

//

static void Create_CustomerOrders_Table()

{

bool do_create = false;

// define table CustomerOrders

Console.WriteLine("\ttable CustomerOrders");

try

{

tableCustOrdr.Open("custordr", OPEN_MODE.NORMAL_OPEN);

}

catch (CTException)

{

// table does not exist

do_create = true;

}

if (do_create)

{

try

{

// define table fields

tableCustOrdr.AddField("co_ordrdate", FIELD_TYPE.DATE, 4);

tableCustOrdr.AddField("co_promdate", FIELD_TYPE.DATE, 4);

CTField field1 = tableCustOrdr.AddField("co_ordrnumb", FIELD_TYPE.FSTRING, 6);

CTField field2 = tableCustOrdr.AddField("co_custnumb", FIELD_TYPE.FSTRING, 4);

// define indexes

CTIndex index1 = tableCustOrdr.AddIndex("co_ordrnumb_idx", KEY_TYPE.LEADING_INDEX, false, false);

index1.AddSegment(field1, SEG_MODE.SCHSEG_SEG);

CTIndex index2 = tableCustOrdr.AddIndex("co_custnumb_idx", KEY_TYPE.LEADING_INDEX, true, false);

index2.AddSegment(field2, SEG_MODE.SCHSEG_SEG);

// create table

Console.WriteLine("\tCreate table...");

tableCustOrdr.Create("custordr", CREATE_MODE.TRNLOG_CREATE);

// open table

Console.WriteLine("\tOpen table...");

tableCustOrdr.Open("custordr", OPEN_MODE.NORMAL_OPEN);

}

catch (CTException E)

{

Handle_Exception(E);

}

}

else

Check_Table_Mode(tableCustOrdr);

}

//

// Create_OrderItems_Table()

//

// Open table OrderItems, if it exists. Otherwise create it

// along with its indexes and open it

//

static void Create_OrderItems_Table()

{

bool do_create = false;

// define table OrderItems

Console.WriteLine("\ttable OrderItems");

try

{

tableOrdrItem.Open("ordritem", OPEN_MODE.NORMAL_OPEN);

}

catch (CTException)

{

// table does not exist

do_create = true;

}

if (do_create)

{

try

{

// define table fields

CTField field1 = tableOrdrItem.AddField("oi_sequnumb", FIELD_TYPE.INT2, 2);

tableOrdrItem.AddField("oi_quantity", FIELD_TYPE.INT2, 2);

CTField field2 = tableOrdrItem.AddField("oi_ordrnumb", FIELD_TYPE.FSTRING, 6);

CTField field3 = tableOrdrItem.AddField("oi_itemnumb", FIELD_TYPE.FSTRING, 5);

// define indexes

CTIndex 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);

CTIndex index2 = tableOrdrItem.AddIndex("oi_itemnumb_idx", KEY_TYPE.LEADING_INDEX, true, false);

index2.AddSegment(field3, SEG_MODE.SCHSEG_SEG);

// create table

Console.WriteLine("\tCreate table...");

tableOrdrItem.Create("ordritem", CREATE_MODE.TRNLOG_CREATE);

// open table

Console.WriteLine("\tOpen table...");

tableOrdrItem.Open("ordritem", OPEN_MODE.NORMAL_OPEN);

}

catch (CTException E)

{

Handle_Exception(E);

}

}

else

Check_Table_Mode(tableOrdrItem);

}

//

// Create_ItemMaster_Table()

//

// Open table ItemMaster, if it exists. Otherwise create it

// along with its indexes and open it

//

static void Create_ItemMaster_Table()

{

bool do_create = false;

// define table ItemMaster

Console.WriteLine("\ttable ItemMaster");

try

{

tableItemMast.Open("itemmast", OPEN_MODE.NORMAL_OPEN);

}

catch (CTException)

{

// table does not exist

do_create = true;

}

if (do_create)

{

try

{

// define table fields

tableItemMast.AddField("im_itemwght", FIELD_TYPE.INT4, 4);

tableItemMast.AddField("im_itempric", FIELD_TYPE.MONEY, 4);

CTField field1 = tableItemMast.AddField("im_itemnumb", FIELD_TYPE.FSTRING, 5);

tableItemMast.AddField("im_itemdesc", FIELD_TYPE.VSTRING, 47);

// define indexes

CTIndex index1 = tableItemMast.AddIndex("im_itemnumb_idx", KEY_TYPE.LEADING_INDEX, false, false);

index1.AddSegment(field1, SEG_MODE.SCHSEG_SEG);

// create table

Console.WriteLine("\tCreate table...");

tableItemMast.Create("itemmast", CREATE_MODE.TRNLOG_CREATE);

// open table

Console.WriteLine("\tOpen table...");

tableItemMast.Open("itemmast", OPEN_MODE.NORMAL_OPEN);

}

catch (CTException E)

{

Handle_Exception(E);

}

}

else

Check_Table_Mode(tableItemMast);

}

//

// 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

//

static void Check_Table_Mode(CTTable table)

{

try

{

// get table create mode

CREATE_MODE mode = table.GetCreateMode();

// check if table is not under transaction processing control

if ((mode & CREATE_MODE.TRNLOG_CREATE) == 0)

{

// change file mode to enable transaction processing

mode |= CREATE_MODE.TRNLOG_CREATE;

table.UpdateCreateMode(mode);

}

}

catch (CTException E)

{

Handle_Exception(E);

}

}

TOCIndex