Generic Database Manager(Wrapper)
AFAIK, we all must programming to database through database wrapper/manager such as sqliteman or CppSQLite.
But the database wrapper is specific to one type of database and this is not convenient for programmer cause require a lot of modification in case the database was cahnged.
Therefore, i would like to write a generic database wrapper that can interface with various kind of database.
I have create a class template like below.
template<typename T>
class ClsDatabaseManager
{
public:
// Pure Virtual Function
// All derived classes must implements it and forward the开发者_开发问答 call
// appropriate database wrapper
connect(string);
disconnect(string);
Execute(string);
CreateTable(string);
CreateDatabase(string);
private:
T m_table;
T m_database;
};
class sqliteManager : public ClsDatabaseManager<T>
{
// Implement all the function by forward the call
};
Therefore, i will use traits the get the information type based on the template argument supplied.
This template argument is a class type derived from this base class such as sqlite, postgresql and mysql.
So, any suggestion or recommendation to my design.
How to create a generic database interface and forward call to the specific database interface library.
EDIT:
What is the different between ODBC and C++ Database Access Library(Soci ) ?
Please help.
Thanks.
ODBC is the protocol. It is open database connectivity, which defines functions which a database should expose so that the user can use it in their C/C++ code. Normally the databases provides their own ODBC compliant driver. Soci is the library which does something that you want. It is a library, so it must be having it's own implementation which you can directly use.
1 connection for each table access, you would swap out your database network usage. Create Connection class, connection details, preferably a static implementation and use it as a component(Composition) of you class.
Keep your connection independent off your database transactions. If one transaction fails, atleast you can preempt the thread and use the same connection details for next transaction. You can use the same connection for multiple databse tries, rather than creating 1 connection/table access.
This is not actually an answer to your particular question, but what about using Soci? It does support sqlite, postgresql and mysql, as well as Oracle, Firebird and ODBC.
精彩评论