Parent Child Table (Part 2)

Our First difference will be in the Structure definition.


  Structure Galaxy
    ID.i                    ; Table Primary Key
    Name.s                  ; Name of Galaxy
    List SolarSystemCollection.SOLARSYSTEM::SolarSystem()
    List DeleteSolarSystemCollection.SOLARSYSTEM::SolarSystem()

    State.i
  EndStructure

As you can see, I've added in two Lists. The first: SolarSystemCollection which is a List of our Child Collection Records. And the second is called DeleteSolarSystemCollection which also is a List of our Child Collection Records.

Now EACH of our Parent Records can contain the Children linked to it. 

Now let us go through the Procedures and make the necessary changes to accommodate these Child Lists.

The First Change we'll have will be to the Init and Dispose Methods.


  Procedure Dispose(*Class.Galaxy)
    ;Dispose all Child Collections
    ForEach SolarSystemCollection()
      SOLARSYSTEM::Dispose(@SolarSystemCollection()) 
    Next 

    FreeList(SolarSystemCollection())
    FreeList(DeleteSolarSystemCollection())

    ClearStructure(*Class, Galaxy)
  EndProcedure

As you can see, the Initialization must instantiate the Child Lists, before they can be used.

Now the Dispose.

    
   Procedure Dispose(*Class.Galaxy)
    ;Dispose all Child Collections
    ForEach SolarSystemCollection()
      SOLARSYSTEM::Dispose(@SolarSystemCollection()) 
    Next 

    ClearStructure(*Class, Galaxy)
  EndProcedure

First, we iterate through the children calling their Dispose Methods, then we take care of the Parent. We should Free our Lists, Images, Fonts, etc. Then we call ClearStructure, on the Parent Structure itself.

There would be no changes to the Gadget load Procedures, these after all are ONLY listing the Parent data.

  • LoadComboBoxName() 
  • LoadListViewName()
  • LoadListIcon()
All of these Procedure are as before, each building its own SQL, and executing against the Database as before. I'll go ahead and show them to you.


  Procedure LoadComboName(cbData.i)
    Protected DBID, Sql$ 
      ComboBoxEx::ClearItems(cbData)
      DBID = DB::Open(DBTYPE::#Sqlite)
      Sql$ = " Select "
      Sql$ = Sql$ + " ID, "
      Sql$ = Sql$ + " Name "
      Sql$ = Sql$ + " FROM "
      Sql$ = Sql$ + " Galaxy "
      Sql$ = Sql$ + " ORDER BY "
      Sql$ = Sql$ + " Name "

      If DB::Query(DBID, Sql$)
        While NextDatabaseRow(DBID)
          ComboBoxEx::AddItem(cbData, -1, GetDatabaseString(DBID,DatabaseColumnIndex(DBID,"Name")))
          ComboBoxEx::SetItemData(cbData, ComboBoxEx::CountItems(cbData)-1, GetDatabaseLong(DBID, DatabaseColumnIndex(DBID,"ID")))
        Wend
      EndIf
      DB::Close(DBID)

  EndProcedure

  Procedure LoadListViewName(lvData.i)
    Protected DBID, Sql$
    If GadgetType(lvData) = #PB_GadgetType_ListView 
      ClearGadgetItems(lvData)
      DBID = DB::Open(DBTYPE::#Sqlite)

      Sql$ = " Select "
      Sql$ = Sql$ + " ID, "
      Sql$ = Sql$ + " Name"
      Sql$ = Sql$ + " FROM "
      Sql$ = Sql$ + " Galaxy "
      Sql$ = Sql$ + " ORDER BY "
      Sql$ = Sql$ + " Name"

      If DB::Query(DBID, Sql$)
        While NextDatabaseRow(DBID) 
        AddGadgetItem(lvData, -1, GetDatabaseString(DBID,DatabaseColumnIndex(DBID,"Name")))
          SetGadgetItemData(lvData, CountGadgetItems(lvData) -1, GetDatabaseLong(DBID, DatabaseColumnIndex(DBID,"ID")))
        Wend
      EndIf
      DB::Close(DBID)
    Else
      THEMEDMESSAGE::MessageBox("Program Error LoadListViewName" ,"Procedure called With wrong gadget type!")
    EndIf
  EndProcedure

  Procedure LoadListIcon(liData.i)
    Protected DBID, Sql$ 
    Protected Result 

      DBID = DB::Open(DBTYPE::#Sqlite)
      ListEx::DisableReDraw(liData, #True) 
      ListEx::ClearItems(liData)
      ListEx::RemoveColumn(liData, 0) ;Name

      ;===== Build Column Titles =====-
      ListEx::AddColumn(liData, 0, "Name", 175)
      ListEx::SetColumnAttribute(liData, 0, ListEx::#Align,  ListEx::#Left)

      Sql$ = " Select "
      Sql$ = Sql$ + "Name, "
      Sql$ = Sql$ + "ID "
      Sql$ = Sql$ + "FROM "
      Sql$ = Sql$ + "Galaxy "
      Sql$ = Sql$ + "ORDER BY "
      Sql$ = Sql$ + "Name "

      ;===== Populate Data Into Column =====
      Result = DB::Query(DBID, Sql$)
      If Result > 0
        While NextDatabaseRow(DBID)
          ListEx::AddItem(liData, -1,GetDatabaseString(DBID, DatabaseColumnIndex(DBID,"Name")) + #LF$ ) 
          ListEx::SetItemData(liData, ListEx::CountItems(liData) -1 , GetDatabaseLong(DBID, DatabaseColumnIndex(DBID,"ID")) ) 
        Wend
      EndIf

      DB::Close(DBID)

      ListEx::DisableReDraw(liData, #False) 
  EndProcedure

Now we will move on to the fun stuff!


















Comments

Popular posts from this blog

How to build this without writing Code

Connecting the Forms to the Data-Layer (part 9)

Connecting the Forms to the Data-Layer (part 8)