The Single or stand-alone table (part 3)

Welcome Back!

In my previous entry we defined the needed Procedures in a Stand-Alone table data layer Module.  In this blog entry we will begin to build out these Procedures. The first step in building the FighterSize Module is to create our public interface, or Public Declares.

  Declare LoadComboName(cbData.i)
  Declare LoadListViewName(lvData.i)
  Declare LoadListIcon(liData.i)
  Declare.i Init(*Class.FighterSize)
  Declare.i Find(FighterSizeID.i, *Class.FighterSize)
  Declare.i FindByName(FighterName.s, *Class.FighterSize)
  Declare.i Save(*Class.FighterSize)
  Declare.i Delete(*Class.FighterSize)
  Declare.i Validate(*Class.FighterSize)
  Declare.i DeleteValidate(*Class.FighterSize)

Before we dive into creating our public procedures there are a couple of housekeeping things we need to address first. Our first Procedures to create will be Private Procedures.  We are going to build an Initialize and a Dispose Procedure. These will be called by our Public Procedures to ensure that our variables will be properly created and properly disposed of.


  Declare Dispose(*Class.FighterSize)
  Declare Init(*Class.FighterSize)


So, here is our Dispose Procedure, if our structure included an Image or Allocated Memory then the freeing of those would occur here. 

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


And here is our Initialize Procedure. Note the Initialization of our State variable ensuring he is set to DB::#New.


  Procedure Init(*Class.FighterSize)
    With *Class

    Dispose(*Class)

      \ID = 0 
      \Name = ""
      \UnitDescription = ""
      \UnitTypeID = 0 
      \VisibleWithTechnologyLevelID = 0 
      \IsHidden = #False 
      \GameImageID = 0 
      \Space = 0 
      \ProdCost = 0 
      \ProdTime = 0 
      \Attack = 0 
      \Defense = 0 

      \State = DB::#New

    EndWith
  EndProcedure


So, now with those out of the way we can move on to our first three Procedures. These being the data query loads of the primary form Gadgets used in any PureBasic Form. These are the ComboBox Gadget, the ListView Gadget, and ListIconView Gadgets. Remember we are suffixing the Specific field to be listed in the ComboBox and ListView Procedures. I'll start with the ComboBox Gadget:


  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$ + " FighterSize "
      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

As you can see I, personally loath the default ComboBox Gadget, and prefer to use the Custom Gadget developed by Thorsten Hoeppner.  His ComboBox offers All the same features as the default, plus a host of superior features. 

Once your form is instantiated, or created by the OpenWindow command, all of your Gadget Constants are now handles to the controls they represent. And as handles they can be passed about and manipulated by any code in your application.  Here is an example of what I am saying in Code:

  OpenWindow(#Window_1,0,0,450,170,"AI Configure Tech",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)
  TextGadget(#Gadget_label1,15,10,175,25,"Fighter Size: ",#PB_Text_Right)
  ComboBoxGadget(#Gadget_cbFighterSize,195,10,220,25)

  FIGHTERSIZE::LoadComboName(#Gadget_cbFighterSize) ;<== Call to our Procedure
  If ComboBoxEx::CountItems(#Gadget_cbFighterSize) > 0
    ComboboxEx::SetState(#Gadget_cbFighterSize, 0)
  EndIf  

The call to our FighterSize Module passes the handle of the ComboBox to our Procedure.

Procedure LoadComboName(cbData.i)

Here in the LoadComboName Procedure it receives a ComboBox Gadget Handle in the Procedure Call. The local Variable cbData IS handle to the ComboBox on the Opened Window. 

First thing the procedure does is Clear the Gadget Items, in preparation to load in new items from the database. 

ComboBoxEx::ClearItems(cbData)

Next, we call our DB Module to open the database and we capture the database handle in the variable DBID.

DBID = DB::Open(DBTYPE::#Sqlite)

Now we build our SQL code selecting the ID and Name of the FighterSize.

Then we once again call our DB Module Query Procedure to execute the Query.

If DB::Query(DBID, Sql$)

If the Query is Successful, then we can interrogate the results. We loop through the results and load each returned record into our ComboBox, setting the Item Data to the ID of the Record Retrieved.

  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

And finally, we once again call our DB Module to close the database.

  DB::Close(DBID)

This completes the ComboBox Load Procedure.


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)