Parent Child Table (part 6)

 So, as before, here is the whole Solar System Module, or Child Module:



XIncludeFile "../Utils/ThemedMessage.pbi"
XIncludeFile "DB.pbi"

DeclareModule SOLARSYSTEM
  Structure SolarSystem
    ID.i                    ; Table Primary Key
    GalaxyID.i              ; Foreign Key to Table Galaxy
    Name.s                  ; Name of SolarSystem
    GRow.i                  ; Galactic Row
    GCol.i                  ; Galactic Column

    State.i
  EndStructure


  Declare LoadComboName(cbData.i)
  Declare LoadListViewName(lvData.i)
  Declare LoadListIcon(liData.i)
  Declare CreateTable()
  Declare.i Init(*Class.SolarSystem)
  Declare.i Find(SolarSystemID.i, *Class.SolarSystem)
  Declare.i FindByName(Name$, *Class.SolarSystem)
  Declare.i Save(*Class.SolarSystem)
  Declare.i Delete(*Class.SolarSystem)
  Declare.i Validate(*Class.SolarSystem)
  Declare.i DeleteValidate(*Class.SolarSystem)

EndDeclareModule

Module SOLARSYSTEM
  EnableExplicit

  Declare Dispose(*Class.SolarSystem)

  Procedure CreateTable()
    Protected DBID, Sql$ 

      Sql$ = ""
      Sql$ = Sql$ + "DROP TABLE IF EXISTS SOLARSYSTEM ;"
      Sql$ = Sql$ + ""
      Sql$ = Sql$ + "CREATE TABLE SOLARSYSTEM"
      Sql$ = Sql$ + "("
      Sql$ = Sql$ + "	ID INTEGER NOT NULL PRIMARY KEY  AUTOINCREMENT,"
      Sql$ = Sql$ + "		GALAXYID INTEGER ,"
      Sql$ = Sql$ + "		NAME VARCHAR(100)  NOT NULL ,"
      Sql$ = Sql$ + "		GROW INTEGER ,"
      Sql$ = Sql$ + "		GCOL INTEGER ,"
      Sql$ = Sql$ + ""
      Sql$ = Sql$ + "		FOREIGN KEY (GALAXYID) REFERENCES GALAXY(ID)"
      Sql$ = Sql$ + ");"
      Sql$ = Sql$ + ""
      Sql$ = Sql$ + ""

    DBID = DB::open() 
    DB::update(DBID, Sql$)

  EndProcedure

  Procedure Init(*Class.SolarSystem)
    With *Class

    Dispose(*Class)

      \ID = 0 
      \GalaxyID = 0 
      \Name = ""
      \GRow = 0 
      \GCol = 0 

      \State = DB::#New

    EndWith
  EndProcedure

  Procedure Dispose(*Class.SolarSystem)
    ClearStructure(*Class, SolarSystem)
  EndProcedure

  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$ + " SolarSystem "
      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$ + " SolarSystem "
      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
      ListEx::RemoveColumn(liData, 0) ;GRow
      ListEx::RemoveColumn(liData, 0) ;GCol

      ;===== Build Column Titles =====-
      ListEx::AddColumn(liData, 0, "Name", 175)
      ListEx::AddColumn(liData, 1, "Row", 70)
      ListEx::AddColumn(liData, 2, "Col", 70)
      ListEx::SetColumnAttribute(liData, 0, ListEx::#Align,  ListEx::#Left)
      ListEx::SetColumnAttribute(liData, 1, ListEx::#Align,  ListEx::#Center)
      ListEx::SetColumnAttribute(liData, 2, ListEx::#Align,  ListEx::#Center)

      Sql$ = " Select "
      Sql$ = Sql$ + "Name, "
      Sql$ = Sql$ + "GRow, "
      Sql$ = Sql$ + "GCol, "
      Sql$ = Sql$ + "ID "
      Sql$ = Sql$ + "FROM "
      Sql$ = Sql$ + "SolarSystem "
      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$  + Str(GetDatabaseLong(DBID, DatabaseColumnIndex(DBID,"GRow"))) + #LF$  + Str(GetDatabaseLong(DBID, DatabaseColumnIndex(DBID,"GCol"))) + #LF$ ) 
          ListEx::SetItemData(liData, ListEx::CountItems(liData) -1 , GetDatabaseLong(DBID, DatabaseColumnIndex(DBID,"ID")) ) 
        Wend
      EndIf

      DB::Close(DBID)

      ListEx::DisableReDraw(liData, #False) 
  EndProcedure


  Procedure.i Find(SolarSystemID.i, *Class.SolarSystem)
    Protected Sql$, DBID, Result, PictureSize, *picture, BLOBSize
    Protected PassFail = #False 

    Init(*Class)

    DBID = DB::Open(DBTYPE::#Sqlite)
    SetDatabaseLong(DBID, 0, SolarSystemID)
    Sql$ = "SELECT "
    Sql$ =  Sql$ + "ID, "
    Sql$ =  Sql$ + "GalaxyID, "
    Sql$ =  Sql$ + "Name, "
    Sql$ =  Sql$ + "GRow, "
    Sql$ =  Sql$ + "GCol"
    Sql$ =  Sql$ + " FROM "
    Sql$ =  Sql$ + "SolarSystem "
    Sql$ =  Sql$ + " WHERE "
    Sql$ =  Sql$ + "ID = ? "

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

    DB::Close(DBID)


    ProcedureReturn PassFail 

  EndProcedure

  Procedure.i FindByName(Name$, *Class.SolarSystem)
    Protected Sql$, DBID, Result, PictureSize, *picture, BLOBSize
    Protected PassFail = #False 

    Init(*Class)

    DBID = DB::Open(DBTYPE::#Sqlite)
    SetDatabaseString(DBID, 0, Name$ )
    Sql$ = "SELECT "
    Sql$ =  Sql$ + "ID, "
    Sql$ =  Sql$ + "GalaxyID, "
    Sql$ =  Sql$ + "Name, "
    Sql$ =  Sql$ + "GRow, "
    Sql$ =  Sql$ + "GCol"
    Sql$ =  Sql$ + " FROM "
    Sql$ =  Sql$ + " SolarSystem "
    Sql$ =  Sql$ + " WHERE "
    Sql$ =  Sql$ + " GCol = ? "

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

    DB::Close(DBID)


    ProcedureReturn PassFail 

  EndProcedure


  Procedure.i Save(*Class.SolarSystem)
    Protected Sql$, Result, DBID, Length, *MemoryGraphicID 
    Protected PassFail = #False 

    If Validate(*Class) = #False
      ProcedureReturn #False
    Else 
      DBID = DB::Open() 
      With *Class
        SetDatabaseLong(DBID, 0, \GalaxyID)
        SetDatabaseString(DBID, 1, \Name)
        SetDatabaseLong(DBID, 2, \GRow)
        SetDatabaseLong(DBID, 3, \GCol)
      EndWith 

      If *Class\State = DB::#New 
        Sql$ = "INSERT "
        Sql$ = Sql$ + " into "
        Sql$ = Sql$ + "SolarSystem" 
        Sql$ = Sql$ + " ("
        Sql$ = Sql$ + "      GalaxyID, "
        Sql$ = Sql$ + "      Name, "
        Sql$ = Sql$ + "      GRow, "
        Sql$ = Sql$ + "      GCol"
        Sql$ = Sql$ + " )"
        Sql$ = Sql$ + " VALUES "
        Sql$ = Sql$ + " (?, ?, ?, ? )"

        PassFail = DB::Update(DBID, Sql$)

        If PassFail 
          *Class\State = DB::#Clean
          *Class\ID = DB::getLastID(DBID, "SolarSystem")
        EndIf 

    DB::Close(DBID)



      Else ;>>>>>> UPDATE <<<<<<

        SetDatabaseLong(DBID, 4, *Class\ID) 
        Sql$ = "UPDATE "
        Sql$ = Sql$ + "SolarSystem "
        Sql$ = Sql$ + "SET "
        Sql$ = Sql$ + "GalaxyID = ? , "
        Sql$ = Sql$ + "Name = ? , "
        Sql$ = Sql$ + "GRow = ? , "
        Sql$ = Sql$ + "GCol = ? "
        Sql$ = Sql$ + " WHERE "
        Sql$ = Sql$ + "ID = ? "

        PassFail = DB::Update(DBID, Sql$) 

        If PassFail 
          *Class\State = DB::#Clean
        EndIf 

        DB::Close(DBID) 

      EndIf 


    EndIf 

  ProcedureReturn PassFail  

  EndProcedure


  Procedure.i Delete(*Class.SolarSystem)
    Protected Result, DBID 
    Protected Sql$ 

    If DeleteValidate(*Class) = #False 
      ProcedureReturn #False 
    Else 

    DBID = DB::Open(DBTYPE::#Sqlite)
    SetDatabaseLong(DBID, 0, *Class\ID) 
    Sql$ = " DELETE "
    Sql$ = Sql$ + "FROM "
    Sql$ = Sql$ + "SolarSystem "
    Sql$ = Sql$ + "WHERE "
    Sql$ = Sql$ + "ID = ?  "
    Result = DB::Update(DBID, Sql$) 
    DB::Close(DBID) 
    ProcedureReturn Result 
  EndIf

  EndProcedure


  Procedure.i Validate(*Class.SolarSystem)

    Protected DBID, Sql$, PassFail.i = #True 
    Protected ErrMsg.s = ""


    If Len(*Class\Name) < 1 
      PassFail = #False
      ErrMsg = ErrMsg + "Name cannot be less than 1 characters in length!" + #LF$ 
    EndIf
    If Len(*Class\Name) > 100 
      PassFail = #False 
      ErrMsg = ErrMsg + "Name cannot exceed 100 characters!" + #LF$ 
    EndIf 

    ;Unique Check FIELD ==> SolarSystem
    If *Class\State = DB::#New  
      DBID = DB::Open(DBTYPE::#Sqlite)
      SetDatabaseString(DBID, 0, *Class\Name)  
      Sql$ = " Select "
      Sql$ = Sql$ + " * "
      Sql$ = Sql$ + " FROM "
      Sql$ = Sql$ + "SolarSystem "
      Sql$ = Sql$ + " WHERE "
      Sql$ = Sql$ + "Name = ? "

      If DatabaseQuery(DBID, Sql$)  
        While NextDatabaseRow(DBID) 
          PassFail = #False  
          ErrMsg = ErrMsg + "Name must be unique!" + #LF$  
        Wend  
       EndIf  
      DB::Close(DBID)  
    Else
      DBID = DB::Open(DBTYPE::#Sqlite)
      SetDatabaseString(DBID, 0, *Class\Name) 
      SetDatabaseLong(DBID, 1, *Class\ID) 
      Sql$ = "Select "
      Sql$ = Sql$ + " * "
      Sql$ = Sql$ + " FROM "
      Sql$ = Sql$ + "SolarSystem "
      Sql$ = Sql$ + " WHERE "
      Sql$ = Sql$ + "Name = ? "
      Sql$ = Sql$ + " And "
      Sql$ = Sql$ + "ID <> ? "
      If DatabaseQuery(DBID, Sql$) 
        While NextDatabaseRow(DBID) 
          PassFail = #False  
          ErrMsg = ErrMsg + "Name must be unique!" + #LF$  
        Wend 
      EndIf 
      DB::Close(DBID)  
    EndIf  


    If PassFail = #False 
      THEMEDMESSAGE::MessageBox("Validation Error",ErrMsg) 
    EndIf	

    ProcedureReturn PassFail 

  EndProcedure


  Procedure.i DeleteValidate(*Class.SolarSystem)
    Protected DBID, Sql$, Cnt, PassFail, ErrMsg.s 
    PassFail.i = #True 
    ErrMsg.s = ""
    ;DBID = DB::Open(DBTYPE::#Sqlite)

    ;Sql$ = "SELECT 
    ;Sql$ = Sql$ + "COUNT(*) As Count "
    ;Sql$ = Sql$ + "FROM "
    ;Sql$ = Sql$ + "DBField "
    ;Sql$ = Sql$ + "WHERE "
    ;Sql$ = Sql$ + "ClassID = ? "

    ;If DB::Query(DBID, Sql$) 
      ;While NextDatabaseRow(DBID)  
        ;Cnt.c =  GetDatabaseLong(DBID, 0)  
        ;If Cnt > 0  
          ;PassFail= #False  
          ;ErrMsg= "Project has Fields, Delete them first!"
        ;EndIf  
      ;Wend  
    ;EndIf
    ;DB::Close(DBID) 

    If PassFail= #False
      THEMEDMESSAGE::MessageBox("Error", ErrMsg) 
    EndIf

    ProcedureReturn PassFail 

  EndProcedure

EndModule





Comments

Popular posts from this blog

PAHLabs Blog goes Live

How to build this without writing Code

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