I'm working on an application which needs to open a database file. There are 2 "versions" of this database: one of them is more general data storage, and the other contains "less" information. That being said, database table structures are different, so I need to issue different queries to get "same" information from each of them.
My first thought is to create a query factory abstract interface, which will only have pure virtual methods returning the queries:
class IQueryFactory
{
public:
virtual QString getNames() const = 0;
virtual QString getSurnames() const = 0;
// ...
};
and have this interface implemented for both "versions" of database:
class GeneralQueryFactory : public IQueryFactory
{
public:
QString getNames() const override
{
return "SELECT DISTINCT Name FROM People;";
}
QString getSurnames() const override
{
return "SELECT DISTINCT Surname FROM People;";
}
// ...
}
class SpecificQueryFactory : public IQueryFactory
{
public:
QString getNames() const override
{
return "SELECT DISTINCT FirstName FROM Employees;";
}
QString getSurnames() const override
{
return "SELECT DISTINCT LastName FROM Employees;";
}
// ...
}
I'm creating an instance of GeneralQueryFactory
and SpecificQueryFactory
at program startup, then when a database is to be loaded, I check if some table in database, and based on that store the corresponding query factory pointer with the database name in map:
GeneralQueryFactory generalFactory;
SpecificQueryFactory specificFactory;
// ...
if (...)
dbInfo.add(dbPath, &app.generalFactory);
else
dbInfo.add(dbPath, &app.specificFactory);
Later I use the query factory as follows (in another function):
auto queryFct = dbInfo.getQueryFactory(dbPath);
QSqlQuery sqlQuery(queryFct->getNames());
sqlQuery.exec();
// ...
I wanna know if this is done correctly. Is the usage of this design pattern correct for this problem? What can I improve? Thanks.
struct Queries{QString getNames, getSurnames;}
.dbInfo
acts as repository, it has bothadd
andget
methods for query strategy. what else should be different?