Previous Topic

Next Topic

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

#ifdef PROTOTYPE
VOID Define(VOID)
#else
VOID Define()
#endif
{
   CTHANDLE hField1, hField2, hField3, hField4;
   CTHANDLE hField5, hField6, hField7;

   printf("DEFINE\n");

   /* allocate a table handle */
   if ((hTable = ctdbAllocTable(hDatabase)) == NULL)
      Handle_Error("Define(); ctdbAllocTable()");

   /* open table */
   printf("\tOpen table...\n");
   if (ctdbOpenTable(hTable, "custmast", CTOPEN_NORMAL))
   {
      /* define table fields */
      printf("\tAdd fields...\n");
      hField1 = ctdbAddField(hTable, "cm_custnumb", CT_FSTRING, 4);
      hField2 = ctdbAddField(hTable, "cm_custzipc", CT_FSTRING, 9);
      hField3 = ctdbAddField(hTable, "cm_custstat", CT_FSTRING, 2);
      hField4 = ctdbAddField(hTable, "cm_custratg", CT_FSTRING, 1);
      hField5 = ctdbAddField(hTable, "cm_custname", CT_STRING, 47);
      hField6 = ctdbAddField(hTable, "cm_custaddr", CT_STRING, 47);
      hField7 = ctdbAddField(hTable, "cm_custcity", CT_STRING, 47);

      if (!hField1 || !hField2 || !hField3 || !hField4 ||
          !hField5 || !hField6|| !hField7)
         Handle_Error("Define(); ctdbAddField()");

      /* create table */
      printf("\tCreate table...\n");
      if (ctdbCreateTable(hTable, "custmast", CTCREATE_NORMAL))
         Handle_Error("Define(); ctdbCreateTable()");

      if (ctdbOpenTable(hTable, "custmast", CTOPEN_NORMAL))
         Handle_Error("Define(); ctdbOpenTable()");
   }
   else
      Check_Table_Mode(hTable);
}


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

#ifdef PROTOTYPE
VOID Check_Table_Mode(CTHANDLE hTable)
#else
VOID Check_Table_Mode(hTable)
CTHANDLE hTable;
#endif
{
   CTCREATE_MODE mode;

   /* get table create mode */
   mode = ctdbGetTableCreateMode(hTable);

   /* check if table is under transaction processing control */
   if ((mode & CTCREATE_TRNLOG))
   {
      /* change file mode to disable transaction processing */
      mode ^= CTCREATE_TRNLOG;
      if (ctdbUpdateCreateMode(hTable, mode) != CTDBRET_OK)
         Handle_Error("Check_Table_Mode(); ctdbUpdateCreateMode");
   }
}