views:

59

answers:

3

Hello to all, 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.

A: 

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.

DumbCoder
How about Sqlite which does not require network connection ?
peterwkc
Don't initialize the connection object for Sqlite. I assumed you have to access databases on the network, but still having multiple connections from a single for every table access is time consuming(setting up connections) and waste of resources and may overwhelm the connection limits, stopping others from connecting.
DumbCoder
Sqllite is a database, not the wrapper library.
Manoj R
@peterwkc: actually sqlite might require a network connection if it's on a mounted drive (slow... but possible).
Matthieu M.
Why a downvote ? Some bugger has just downvoted 5 of my answers for no reason.
DumbCoder
Thanks for all comment.
peterwkc
I just mistaken click it. I have changed it.
peterwkc
A: 

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.

usta
How about OTL ? Is it similar to Soci ? Thanks. I find it quite useful but still got a lot of to resolve.
peterwkc
I think my class can jsut wrapps the Soci library.
peterwkc
@peterwkc: Never heard about OTL before. Let me see what it has to offer...
usta
@peterwkc: OK, now I had a brief look at OTL, and it looks quite solid overall. However it seemed less intuitive to me than Soci, maybe it's just because I'm used to Soci. Another downside for me was the lack of native back-end to Sqlite, but going through ODBC for that instead. Kinda defeats the whole purpose of using Sqlite. And the corresponding example included a 'logon to db' call which makes little sense for Sqlite, but is still needed because of ODBC. Having said that the OTL team still seems to have done a great job, but perhaps not that useful for me.
usta
I try to use soci with sqlite but i counld not find the backend for sqlite. I have download the latest version of soci and install on linux.
peterwkc
@peterwkc: I guess you downloaded 3.0.0? Then it is expected. Get it from git repository instead (`git clone git://soci.git.sourceforge.net/gitroot/soci/soci`), then you'll have all the backends. The soon-to-be released 3.1 version will include those in the release proper I think.
usta
A: 

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.

Manoj R
Thanks for your comment.
peterwkc