Module DB, the foundation of any Data Layer.
At the heart of any application is the database. Therefore, this segment of the application should be bulletproof. To prevent repeating code, it should start with the module I call DB. Module DB it the bottom most module, it is the actual module which interacts with the database. If later you decide to migrate your database from SQLite to say Postgres, then all you will need to do is tweak THIS Module.
This module will also contain the Enumeration of "STATE". By this I mean the current state of the record, is it:
- New - Created but not yest saved to the database.
- Clean - The Record is "Untouched". It is Identical to the record saved in the Database.
- Dirty - The record was loaded from the database, but it has been changed in some manner.
- DeleteRec - The record has been marked for deletion, but the user has yet to commit the delete to the database.
- NewDirty - The record is new, but the User is changing something, or the new record is being modified but has never been saved to the database.
These states will be used by the code behind the Form. That blog entry will be coming soon, for now know what these are, and know that they are located on the DB Module.
So, let's define what procedures are needed in the DB Module.
- Open Database - here we initialize our desired database and open it in preparation for whatever work needs to be performed. if desired this is where you will handle database encryption.
- Close Database - when database action is completed you must ALWAYS close the database. Never leave your database in an open state.
- Query - Executing queries, retrieving information from the database. This could entail retrieving a single field, such as a GetName Procedure, retrieving an entire record, of even thousands of records.
- Update - This entails changing information in the database, adding records, or deleting records.
- GetLastID - This is all about retrieving the ID or KEY field of the record you just ADDED (Inserted) to the database.
Declare Open(DBTYPEID.i = DBTYPE::#Sqlite)
Declare Close(id)
Declare Query(id,Sql.s)
Declare Update(id,Sql.s)
Declare getLastID(dbid.i,table.s)
;Sqlite = "Relative/Path/DBName.db"
;DDBC = "Relative/Path/To/Folder"
;Postgre = "host=localhost port=5432 dbname=test"
;MySQL = "host=localhost port=3306 dbname=test"
;MariaDB = "host=localhost port=3306 dbname=test"
#DB_Name = "DatabaseName.db"
#DB_ID = ""
#db_Password = ""
Enumeration
#New
#NewDirty
#Clean
#Dirty
#DeleteRec
EndEnumeration
EndDeclareModule
- #DB_Name - the fixed value of the Name of your Database
- #DB_ID - User ID used to connect with the database. Note for SQLite this must be "".
- #DB_Password - The User Password to connect to the database. For SQLite this must be "".
Select DBTYPEID
Case DBTYPE::#Sqlite
UseSQLiteDatabase()
Case DBTYPE::#Postgre
UsePostgreSQLDatabase()
Case DBTYPE::#MySQL
UseMySQLDatabase()
Case DBTYPE::#MariaDB
UseMySQLDatabase()
Case DBTYPE::#ODBC
UseODBCDatabase()
EndSelect
EndProcedure
Procedure Open(DBTYPEID.i = DBTYPE::#Sqlite)
Protected ProgramDataPath$
SetDBType(DBTYPEID)
ProgramDataPath$= GetUserDirectory(#PB_Directory_ProgramData)+"DatabasePath\"
; UseSQLiteDatabase("sqlcipher.dll")
; Protected id = OpenDatabase(#PB_Any,ProgramDataPath$ + #DB_PATH, #DB_ID, #DB_Password)
; update(id, "PRAGMA key = 'XXXXXXXXXX'")
Protected id = OpenDatabase(#PB_Any,ProgramDataPath$ + #DB_Name, #DB_ID, #db_Password)
If Not id
MessageRequester("Database Error","Cannot open database",#PB_MessageRequester_Error)
End
EndIf
ProcedureReturn id
EndProcedure
Procedure close(id)
If IsDatabase(id)
CloseDatabase(id)
EndIf
EndProcedure
Procedure query(id,Sql.s)
If Not DatabaseQuery(id,Sql)
MessageRequester("SQL ERROR",Sql + #LF$ + DatabaseError(),#PB_MessageRequester_Error)
ProcedureReturn #False
EndIf
ProcedureReturn #True
EndProcedure
Procedure update(id,Sql.s)
Protected valRet = DatabaseUpdate(id,Sql)
If Not valRet
MessageRequester("SQL ERROR",Sql + #LF$ + DatabaseError(),#PB_MessageRequester_Error)
EndIf
ProcedureReturn valRet
EndProcedure
Procedure getLastID(dbid.i, table.s)
Protected req.s,index
req = "Select last_insert_rowid(), ID from " + table
If DatabaseQuery(dbId, req)
While NextDatabaseRow(dbid)
index = GetDatabaseLong(dbId,DatabaseColumnIndex(DBID,"ID"))
Wend
EndIf
ProcedureReturn index
EndProcedure
br />
; **************************************************************
; Project : ClassBuilder V2
; Author : Pete Hollyer
; Module : DB.pbi
; **************************************************************
XIncludeFile "../Types/DBType.pbi"
DeclareModule DB
Declare Open(DBTYPEID.i = DBTYPE::#Sqlite)
Declare Close(id)
Declare Query(id,Sql.s)
Declare Update(id,Sql.s)
Declare getLastID(dbid.i,table.s)
;Sqlite = "Relative/Path/DBName.db"
;DDBC = "Relative/Path/To/Folder"
;Postgre = "host=localhost port=5432 dbname=test"
;MySQL = "host=localhost port=3306 dbname=test"
;MariaDB = "host=localhost port=3306 dbname=test"
#DB_Name = "DatabaseName.db"
#DB_ID = ""
#db_Password = ""
Enumeration
#New
#NewDirty
#Clean
#Dirty
#DeleteRec
EndEnumeration
EndDeclareModule
Module DB
EnableExplicit
Procedure SetDBType(DBTYPEID.i)
Select DBTYPEID
Case DBTYPE::#Sqlite
UseSQLiteDatabase()
Case DBTYPE::#Postgre
UsePostgreSQLDatabase()
Case DBTYPE::#MySQL
UseMySQLDatabase()
Case DBTYPE::#MariaDB
UseMySQLDatabase()
Case DBTYPE::#ODBC
UseODBCDatabase()
EndSelect
EndProcedure
Procedure Open(DBTYPEID.i = DBTYPE::#Sqlite)
Protected ProgramDataPath$
SetDBType(DBTYPEID)
ProgramDataPath$= GetUserDirectory(#PB_Directory_ProgramData)+"DatabasePath\"
; UseSQLiteDatabase("sqlcipher.dll")
; Protected id = OpenDatabase(#PB_Any,ProgramDataPath$ + #DB_PATH, #DB_ID, #DB_Password)
; update(id, "PRAGMA key = 'XXXXXXXXXX'")
Protected id = OpenDatabase(#PB_Any,ProgramDataPath$ + #DB_Name, #DB_ID, #db_Password)
If Not id
MessageRequester("Database Error","Cannot open database",#PB_MessageRequester_Error)
End
EndIf
ProcedureReturn id
EndProcedure
Procedure close(id)
If IsDatabase(id)
CloseDatabase(id)
EndIf
EndProcedure
Procedure query(id,Sql.s)
If Not DatabaseQuery(id,Sql)
MessageRequester("SQL ERROR",Sql + #LF$ + DatabaseError(),#PB_MessageRequester_Error)
ProcedureReturn #False
EndIf
ProcedureReturn #True
EndProcedure
Procedure update(id,Sql.s)
Protected valRet = DatabaseUpdate(id,Sql)
If Not valRet
MessageRequester("SQL ERROR",Sql + #LF$ + DatabaseError(),#PB_MessageRequester_Error)
EndIf
ProcedureReturn valRet
EndProcedure
Procedure getLastID(dbid.i, table.s)
Protected req.s,index
req = "Select last_insert_rowid(), ID from " + table
If DatabaseQuery(dbId, req)
While NextDatabaseRow(dbid)
index = GetDatabaseLong(dbId,DatabaseColumnIndex(DBID,"ID"))
Wend
EndIf
ProcedureReturn index
EndProcedure
EndModule
Comments
Post a Comment