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()
 *
 * Create the tables
 */
void Define(void)
{
   os_printf(DH_STRING_LITERAL("DEFINE\n"));

   /* delete tables ... */
   Delete_Tables();
   /* ...and re-create them with constraints */
   Create_CustomerMaster_Table();
   Create_ItemMaster_Table();
   Create_CustomerOrders_Table();
   Create_OrderItems_Table();

   EXEC SQL COMMIT WORK ;
}


/*
 * Create_CustomerMaster_Table()
 *
 * Create the CustomerMaster
 */
void Create_CustomerMaster_Table(void)
{
EXEC SQL BEGIN DECLARE SECTION ;
   char  sCommand[512];
EXEC SQL END DECLARE SECTION ;

   /* define table CustomerMaster */
   os_printf(DH_STRING_LITERAL("\ttable CustomerMaster\n"));

   os_sprintf (sCommand, DH_STRING_LITERAL("CREATE TABLE custmast ( \
         cm_custnumb CHAR(4) PRIMARY KEY, \
         cm_custzipc CHAR(9), \
         cm_custstat CHAR(2), \
         cm_custrtng CHAR(1), \
         cm_custname VARCHAR(47), \
         cm_custaddr VARCHAR(47), \
         cm_custcity VARCHAR(47) \
      )"));
   EXEC SQL EXECUTE IMMEDIATE :sCommand ;

   if (sqlca.sqlcode)
      Handle_Error(sqlca.sqlcode, sqlca.sqlerrm);
}


/*
 * Create_CustomerOrders_Table()
 *
 * Create the CustomerOrders
 */
void Create_CustomerOrders_Table(void)
{
EXEC SQL BEGIN DECLARE SECTION ;
   char  sCommand[512];
EXEC SQL END DECLARE SECTION ;

   /* define table CustomerOrders */
   os_printf(DH_STRING_LITERAL("\ttable CustomerOrders\n"));

   os_sprintf (sCommand, DH_STRING_LITERAL("CREATE TABLE custordr ( \
         co_ordrdate DATE, \
         co_promdate DATE, \
         co_ordrnumb CHAR(6) PRIMARY KEY, \
         co_custnumb CHAR(4), \
         FOREIGN KEY (co_custnumb) REFERENCES custmast \
      )"));
   EXEC SQL EXECUTE IMMEDIATE :sCommand ;

   if (sqlca.sqlcode)
      Handle_Error(sqlca.sqlcode, sqlca.sqlerrm);
}


/*
 * Create_OrderItems_Table()
 *
 * Create the OrderItems
 */
void Create_OrderItems_Table(void)
{
EXEC SQL BEGIN DECLARE SECTION ;
   char  sCommand[512];
EXEC SQL END DECLARE SECTION ;

   /* define table OrderItems */
   os_printf(DH_STRING_LITERAL("\ttable OrderItems\n"));

   os_sprintf (sCommand, DH_STRING_LITERAL("CREATE TABLE ordritem ( \
         oi_sequnumb SMALLINT, \
         oi_quantity SMALLINT, \
         oi_ordrnumb CHAR(6), \
         oi_itemnumb CHAR(5), \
         FOREIGN KEY (oi_itemnumb) REFERENCES itemmast, \
         FOREIGN KEY (oi_ordrnumb) REFERENCES custordr \
      )"));
   EXEC SQL EXECUTE IMMEDIATE :sCommand ;

   if (sqlca.sqlcode)
      Handle_Error(sqlca.sqlcode, sqlca.sqlerrm);
}


/*
 * Create_ItemMaster_Table()
 *
 * Create the ItemMaster
 */
void Create_ItemMaster_Table(void)
{
EXEC SQL BEGIN DECLARE SECTION ;
   char  sCommand[512];
EXEC SQL END DECLARE SECTION ;

   /* define table ItemMaster */
   os_printf(DH_STRING_LITERAL("\ttable ItemMaster\n"));

   os_sprintf (sCommand, DH_STRING_LITERAL("CREATE TABLE itemmast ( \
         im_itemwght INTEGER, \
         im_itempric MONEY, \
         im_itemnumb CHAR(5) PRIMARY KEY, \
         im_itemdesc VARCHAR(47) \
      )"));
   EXEC SQL EXECUTE IMMEDIATE :sCommand ;

   if (sqlca.sqlcode)
      Handle_Error(sqlca.sqlcode, sqlca.sqlerrm);
}