Single or Stand-alone table (part 7)
This brings up to the Delete Procedure, and the DeleteValidate Procedure. These we will explain in this post.
Just as the Save Procedure call a Validate Procedure, so the Delete Procedure call a DeleteValidate Procedure. In this manner you can easily insert code to ensure the record being deleted is not being "used" or referenced elsewhere in your application. The Delete itself is very straight forward.
Procedure.i Delete(*Class.FighterSize)
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$ + "FighterSize "
Sql$ = Sql$ + "WHERE "
Sql$ = Sql$ + "ID = ? "
Result = DB::Update(DBID, Sql$)
DB::Close(DBID)
ProcedureReturn Result
EndIf
If the Record is approved for deletion, we open the Database with a call to our DB Module, Create the Delete SQL. Then Call the Update query Procedure or our DB Module, Close the Database and return the Results of our SQL Query.
The DeleteValidation procedure is very Custom to the application. Only you know how the data layer is built and what records are in use where. So Here I will give you examples of how this module can be written.
Here is a default shell, which tests nothing, simply returns that the record in question is valid to be deleted.
Procedure.i DeleteValidate(*Class.FighterSize)
Protected DBID, Sql$, Cnt, PassFail, ErrMsg.s
PassFail.i = #True
ErrMsg.s = ""
;Validation SQL goes here
If PassFail= #False
THEMEDMESSAGE::MessageBox("Error", ErrMsg)
EndIf
ProcedureReturn PassFail
EndProcedure
In this example I'll make a check against another table to ensure that the Fighter in question has not already been assigned to a carrier. If has, then the record must first be removed from the Carrier Before it can be deleted.
Procedure.i DeleteValidate(*Class.FighterSize)
Protected DBID, Sql$, Cnt, PassFail, ErrMsg.s
PassFail.i = #True
ErrMsg.s = ""
DBID = DB::Open(DBTYPE::#Sqlite)
SetDatabaseLong(DBID, 0, *Class\ID)
Sql$ = "SELECT
Sql$ = Sql$ + "Name"
Sql$ = Sql$ + "FROM "
Sql$ = Sql$ + "Carrier "
Sql$ = Sql$ + "WHERE "
Sql$ = Sql$ + "FighterSizeID = ? "
If DB::Query(DBID, Sql$)
While NextDatabaseRow(DBID)
Cnt.c = GetDatabaseLong(DBID, 0)
If Cnt > 0
PassFail= #False
ErrMsg= "Fighter Size in use in Carrier: "+GetDatabaseString(DBID, DatabaseColumnIndex(DBID,"Name"))
EndIf
Wend
EndIf
DB::Close(DBID)
If PassFail= #False
THEMEDMESSAGE::MessageBox("Error", ErrMsg)
EndIf
ProcedureReturn PassFail
EndProcedure
The DeleteValidate Procedure has the Job to ensure that the record can be deleted, but it also has the job to report to the user why it cannot be deleted. this is simply achieved by displaying a message detailing the reason the record cannot be deleted.
If PassFail= #False
THEMEDMESSAGE::MessageBox("Error", ErrMsg)
EndIf
once this has been completed, he simply returns the result of his findings back to the Delete Procedure.
Comments
Post a Comment