Parent Child Table (Part 3)

 Moving on to the Find Methods.  Here again we have some changes. After we load up our Parent Record, we then need to load up its children.  I'll focus in on the in the code segment below:

  
  PassFail = DB::Query(DBID, Sql$)
    If PassFail 
      While NextDatabaseRow(DBID)
        With *Class
          \ID = GetDatabaseLong(DBID, DatabaseColumnIndex(DBID,"ID")) 
          \Name = GetDatabaseString(DBID, DatabaseColumnIndex(DBID,"Name")) 
          \State = DB::#Clean 
        EndWith 
      Wend
    EndIf

    DB::Close(DBID)

    ClearList(*Class\SolarSystemCollection.SOLARSYSTEM::SolarSystem()) 
    ;-----------------------------------------------
    LoadSolarSystemCollection(*Class)
    ;-----------------------------------------------

    ProcedureReturn PassFail 

Here you can see After the Parent Record is loaded, and the database Closed, we then Clear the List in preparation for loading it, then we call a new Procedure to Load the Child Collection. 

Let's take a look at two new Procedures to our Data-Layer Parent Class.

The Load Child Collection:

  Procedure LoadSolarSystemCollection(*Class.Galaxy)
    Protected DBID, Sql$
    DBID = DB::Open(DBTYPE::#Sqlite)
    SetDatabaseLong(DBID, 0, *Class\ID)

    Sql$ = " SELECT ID "
    Sql$ = Sql$ + " FROM SolarSystem "
    Sql$ = Sql$ + " WHERE GalaxyID = ? "

    NewList FID()
    If DB::Query(DBID, Sql$)
      While NextDatabaseRow(DBID) 
        AddElement(FID())
        FID()=GetDatabaseLong(DBID, 0)
      Wend

      DB::Close(DBID)

      ForEach FID()
        AddElement(*Class\SolarSystemCollection())
        SOLARSYSTEM::Find(FID(), *Class\SolarSystemCollection())
      Next
      FreeList(FID())
    EndIf

  EndProcedure

Remember we have ONE Database Connection. So, we must gather up the ID's of the Child Records and load them into a List. Then Close the Database. Then Iterate the list of Child ID's and by calling the Child's own find record we can the load our collection of children. When complete, we clear and dispose the list.


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)