Database Interface
Overview
This page documents the proposal for the structure and content of pkg/database to achieve the following goals:
-
Reduce the friction when adding support to a new DBMS(Database Management System).
-
Enable overriding the default read/write operations for a specific DBMS.
-
Expose the filters (
WHEREclauses) and sorters (ORDER BYclauses) separately from the rest of theSQLstatements so they can be plugged in and out of the different queries. -
Reduce the maintenance of the codebase.
-
Increase the readability of the codebase.
Proposal
The two pillars of this proposal are:
-
Definition of interfaces that need to be implementing when adding support to a new DBMS.
-
Usage of a SQL Builder.
Database Interfaces
To add a new DBMS the following interfaces must be implemented:
DBInterface-
Include the methods that should be used by the rest of the KubeArchive application. Encapsulates the business logic related to the different read/write database operations:
QueryResources,WriteResources,WriteUrls,QueryLogUrls, etc. DBCreator-
Include the methods
GetDriverName()andGetConnectionString(). They are used for establishing the database connection in theDBInterfaceconstructor. DBSelector-
Encapsulate the different
SELECTSQL statements without theWHEREandORDER BYclauses. DBFilter-
Encapsulate the different
WHERESQL clauses that can be used inSELECT,DELETEorUPDATEstatements. DBSorter-
Encapsulate the different
ORDER BYclauses. DBInserter-
Encapsulate the different
INSERTSQL statements. DBDeleter-
Encapsulate the different
DELETESQL statement without theWHEREclauses.
Additional requirements for new DBMS
-
Add a
DBInterfaceconstructor function to theRegisteredDatabasesmap assigned to a key with the driver name. The function receive no parameters. -
Add a
DBCreatorconstructor function. The function receives a map parameter with all the environment variables. These are meant to be used in theGetConnectionString()method.
SQL Builder
The go-sqlbuilder library is used to combine the different SQL pieces into one.
This library supports:
-
Building parametrized queries based on different database flavors.
-
Adding custom SQL functions that can be specific of a DBMS.
SQL Builders in Database Interfaces
go-sqlbuilder expose different sqlbuilders:
-
SelectBuilder -
InsertBuilder -
DeleteBuilder
Combined with the Database Interfaces the following guidelines should be applied:
-
All the exposed
DBSelectorfunctions should return aSelectBuilder -
All the exposed
DBInserterfunctions should return aInsertBuilder -
All the exposed
DBDeleterfunctions should return aDeleteBuilder
ORDER BY clauses are only allowed in SELECT statements so:
-
All the exposed
DBSorterfunctions should receive aSelectBuilderand return it properly modified.
WHERE clauses are allowed in several sqlbuilders but all of them include a Cond object that keeps
track of the different arguments and expressions involved so:
-
All the exposed
DBFilterfunctions should receive aCondobject of asqlbuilderand should be used to create the returned expression as a string.
Common code
Using go-sqlbuilder allows the creation of a lot of SQL statements in the same way for different DBMS.
To prevent having duplicated code, along with some of the interfaces a total or a partial implementation of the methods that don’t depend on specific DBMS functions will be provided.
Then the DBMS implementation can inherit from the Partials and skip some of the method implementations unless overwriting them is required.