Generic Database Interface
This library aims to provide C++11 with a generic interface to manage databases via entity classes, in order to function it is built up based on 4 parts: Entity, EntityManager, Query Builder and Executor.
Via using the supported macros an entity can be declared as a class that represents a table in the database like:
ENTITY(MyEntity)
ADD(MyEntity, Id, int)
ADD(MyEntity, Number, double)
ADD(MyEntity, Name, std::string)
ADD_VAR(MyEntity, Phone, std::string, 20)
PRIMARY(MyEntity, "Id")
END
After that declaration MyEntity
class can be used regularly:
MyEntity entity;
entity.SetId(1);
entity.SetName("MyName");
entity.SetPhone("12345");
entity.SetNumber(3.14);
This class needs an Executor as a dependency. EntityManager manages entity objects and is used to look for, update, insert and delete entities:
Find(Entity e)
: This returns an entity from the database which primary key equalse
primary key.FindBy(Entity e, …)
: This returns a set of entity objects which have the samee
attributes values where…
are those attributes names.Insert(Entity e)
: Inserts the entitye
into the database.Update(Entity e)
: Updates a row in DB based on the values ofe
attributes.UpdateBy(Entity e, …)
: Updates DB rows which have samee
attributes values where…
are those attributes names.Delete(Entity e)
: Deletes a row from the DB based on the primary key.
This builder contains the query to be executed, and it builds queries like:
Select("Country").Count("CustomerID").
From("Customers").
GroupBy("Country").
Having("COUNT(CustomerID)", WhereCondition::GREATER_THAN, "5").
OrderBy("COUNT(CustomerID) DESC");
This class is to be implemented for each DBMS differently, here I have written 2 implementations one for MySQL and one for Postgres, it is used to execute the queries and to get the results as entity objects, it should implement 2 methods: Execute
and ExecuteQuery
.