I am developing a large scale application in C++. The application also maintains a database (currently I am using MySQL), I use OTL for database connectivity. Now what i want to do is to provide support for using database from multiple vendors. E.g User A use MySQL and user B use PostGres. I think upon implementing it in C++ but didn’t come up with any possible solution due to lack of experience.
What I want to achieve, is something like that:
There would be a separate VC Project that deals with database and suppose it contains following files:
DataAccessLayer.cpp //This will main entry point of the project
Product.cpp //This deals with product table
Customer.cpp // This deals with Customer table
Orders.cpp //This deals with Orders table
. . . and many more // I want to have one cpp file per Database table`
And we will use the above project in our code like this
DataAccessLayer oDataAccessLayer;
oDataAccessLayer.Connect(); // This will connect to specified database, it might b some abstract class and have concrete class for each supported DB
oDataAccessLayer.Products.Search(//Some parameters here e.g prod id to b search);//I don't want to write search query again again for each database, This function will execute the query in specific database
oDataAccessLayer.Customers.Add(//Parameter)//Same is the case here I don't want to write ADd query for each supported database
oDataAccessLayer.Disconnect();
I don’t want the whole code I just need some sample code or related article to study.
Suppositions:
Suppose i have following two tables in my database
Docs
And currently i am supporting only following two databases:
1) Post Gres
2) Oracle
Implementation:
I implement this logic in following three phases
1) Data Base Adapter: Only this class is exposed to outer world and this contians the Pointer to Each QueryBuilder, these pointer will be initialized in the constructor of this class on the basis of DB type(which is passed as an argument to constructor)
2) DB Connector: This class will be responsible for DB handling (establish connection, disconnect etc.)
3) Query Builder: This section contains a basse class for each table in DB and also contains concrete calss for each each specific database
Sample Code (Synopsis Only)
DB Connector Section:
Query Builder Section: