Single or stand-alone table (part 8)

 The Final post in this series on the Stand-alone table Module is the Validate Procedure. As with the DeleteValidate Procedure this procedure has two functions, first validating the Structure of Object, but it also has the job to report All issues it found back to the user.

The following list of validations should be performed for the FighterSize Table.

  • All String Field Lengths to ensure they are within the size limits defined in our database.
  • Not Null, for every field defined as not null we should perform a not null validation.
  • Unique Test. All fields which need to be Unique are, in fact Unique.
  • In addition to these any other req2uirements you need should be tested here.
The following is the Validation Procedure of my FighterSize Table

  Procedure.i Validate(*Class.FighterSize)
    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) > 50 
      PassFail = #False 
      ErrMsg = ErrMsg + "Name cannot exceed 50 characters!" + #LF$ 
    EndIf 
    ;Unique Check FIELD ==> FighterSize
    If *Class\State = DB::#New  
      DBID = DB::Open(DBTYPE::#Sqlite)
      SetDatabaseString(DBID, 0, *Class\Name)  
      Sql$ = " Select "
      Sql$ = Sql$ + " * "
      Sql$ = Sql$ + " FROM "
      Sql$ = Sql$ + "FighterSize "
      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$ + "FighterSize "
      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

So, by this point you should be able to simply read through the code and identify the tests being performed.  For the Unique Validation, two tests must be performed, on if the record is being inserted New, another is the Record is being Updated.
Like the DeleteValidate after ALL tests are performed, and the database is closed, the MessageBox is displayed to the User explaining the reasons why their record cannot be saved. 

    If PassFail = #False 
      THEMEDMESSAGE::MessageBox("Validation Error",ErrMsg) 
    EndIf	
That Concludes the posts on building a Single Stand-Alone Table Database-Layer Module. 

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)