with Kit.Db.Marlowe_Keys;
with Kit.Db.Kit_Field;
with Kit.Db.Kit_Key;
with Kit.Db.Kit_Key_Field;
with Kit.Db.Kit_Record;
with Kit.Db.Kit_Record_Base;
with Kit.Db.Kit_Reference;
with Kit.Db.Kit_Type;
with Kit.Db.Database.Db_Kit_Root_Record;

package body Kit.Db.Database.Db_Kit_Key is

   ------------
   -- Create --
   ------------

   procedure Create is
   begin
      Marlowe_Keys.Handle.Add_Table ("kit_key", 89);
      Marlowe_Keys.T17_Top_Record_Ref := Marlowe_Keys.Handle.Add_Key
         ("kit_key_top_record", Kit_Key_Table_Index, 12);
      Marlowe_Keys.T17_T1_Idx_Ref := Marlowe_Keys.Handle.Add_Key
         ("kit_key_t1_idx", Kit_Key_Table_Index, 16);
      Marlowe_Keys.T17_Kit_Record_Ref := Marlowe_Keys.Handle.Add_Key
         ("kit_key_kit_record", Kit_Key_Table_Index, 16);
      Marlowe_Keys.T17_Record_Key_Ref := Marlowe_Keys.Handle.Add_Key
         ("kit_key_record_key", Kit_Key_Table_Index, 80);
   end Create;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize is
   begin
      Kit_Key_Ref := Kit_Record.Create ("kit_key", 17, 89);
      Kit_Reference.Create
        (8,
         "kit_key",
         Kit_Key_Ref);
      declare
         Type_Ref : constant Kit_Type_Reference := Kit_Type.Get_By_Name
            ("kit_root_record");
      begin
         Kit_Field.Create
           ("kit_root_record",
            Kit_Key_Ref,
            Type_Ref,
            4,
            8,
            False,
            False,
            False,
            False,
            True);
      end;
      declare
         Type_Ref : constant Kit_Type_Reference := Kit_Type.Get_By_Name
            ("bounded_string_64");
      begin
         Kit_Field.Create
           ("name",
            Kit_Key_Ref,
            Type_Ref,
            12,
            64,
            True,
            True,
            True,
            False,
            False);
      end;
      declare
         Type_Ref : constant Kit_Type_Reference := Kit_Type.Get_By_Name
            ("kit_record");
      begin
         Kit_Field.Create
           ("kit_record",
            Kit_Key_Ref,
            Type_Ref,
            76,
            8,
            True,
            True,
            True,
            False,
            False);
      end;
      declare
         Type_Ref : constant Kit_Type_Reference := Kit_Type.Get_By_Name
            ("boolean");
      begin
         Kit_Field.Create
           ("is_unique",
            Kit_Key_Ref,
            Type_Ref,
            84,
            1,
            True,
            True,
            True,
            False,
            False);
      end;
      declare
         Type_Ref : constant Kit_Type_Reference := Kit_Type.Get_By_Name
            ("natural");
      begin
         Kit_Field.Create
           ("length",
            Kit_Key_Ref,
            Type_Ref,
            85,
            4,
            True,
            True,
            True,
            False,
            False);
      end;
      declare
         Ref : constant Kit_Key_Reference := Kit_Key.Create ("t1_idx",
            Kit_Key_Ref, True, 16);
      begin
         declare
            Field_Ref : constant Kit_Field_Reference :=
               Kit_Field.Get_By_Record_Field (Kit_Key_Ref,
               "kit_root_record");
         begin
            Kit_Key_Field.Create (Ref, Field_Ref);
         end;
      end;
      declare
         Ref : constant Kit_Key_Reference := Kit_Key.Create ("kit_record",
            Kit_Key_Ref, False, 16);
      begin
         declare
            Field_Ref : constant Kit_Field_Reference :=
               Kit_Field.Get_By_Record_Field (Kit_Key_Ref, "kit_record");
         begin
            Kit_Key_Field.Create (Ref, Field_Ref);
         end;
      end;
      declare
         Ref : constant Kit_Key_Reference := Kit_Key.Create ("record_key",
            Kit_Key_Ref, True, 80);
      begin
         declare
            Field_Ref : constant Kit_Field_Reference :=
               Kit_Field.Get_By_Record_Field (Kit_Key_Ref, "kit_record");
         begin
            Kit_Key_Field.Create (Ref, Field_Ref);
         end;
         declare
            Field_Ref : constant Kit_Field_Reference :=
               Kit_Field.Get_By_Record_Field (Kit_Key_Ref, "name");
         begin
            Kit_Key_Field.Create (Ref, Field_Ref);
         end;
      end;
      Kit_Record_Base.Create
        (1,
         Db_Kit_Root_Record.Kit_Root_Record_Ref,
         Kit_Key_Ref);
   end Initialize;

   ----------
   -- Open --
   ----------

   procedure Open is
   begin
      Marlowe_Keys.T17_Top_Record_Ref := Marlowe_Keys.Handle.Get_Reference
         ("kit_key_top_record");
      Marlowe_Keys.T17_T1_Idx_Ref := Marlowe_Keys.Handle.Get_Reference
         ("kit_key_t1_idx");
      Marlowe_Keys.T17_Kit_Record_Ref := Marlowe_Keys.Handle.Get_Reference
         ("kit_key_kit_record");
      Marlowe_Keys.T17_Record_Key_Ref := Marlowe_Keys.Handle.Get_Reference
         ("kit_key_record_key");
   end Open;

end Kit.Db.Database.Db_Kit_Key;
