Looking for a good SQL DSL
-
I am working on a project where I use SQLite to store the application data. I use the built in "QSql..." classes to access it. As I did not like to embedd the queries as strings in my code, I started to wrap around the in build classes, creating query builders.
This is an example of a select:
@
SQLSelect select{"rules"};
select.field("id");
select.field("column");
select.field("value");
select.field("category");
select.field("type");
select.where(QString("country = %2").arg(_country));
@I also try to reason about the structure of the database, so I created the TableStructure class:
@
TableStructure rules{"rules"};
rules.addField("id", SQLType::Integer().PK().NotNull());
rules.addField("column", SQLType::Integer().NotNull());
rules.addField("value", SQLType::Text().NotNull());
rules.addField("category", SQLType::Integer().NotNull());
rules.addField("type", SQLType::Integer().NotNull());
@and the DataBaseSchema class:
@
DataBaseSchema schema;
schema.addTable(rulse);TableStructure countries{"countries"}; countries.addField("ID", SQLType::Integer().PK()); countries.addField("Code", SQLType::Text()); schema.addTable(countries);
@
Database schemas are comperable, so it is really convinient to check from the application if the database is exactly what was expacted, or an update is needed, or someone accidently opened somethig totally unrelated SQLite database.
Now, all these work good enough, but I keep thinking that this must have been implemented a few times before, and I am making a research for similar libraries.
I have found similar things for C#, Java:
http://www.codeproject.com/Articles/13419/SelectQueryBuilder-Building-complex-and-flexible-S
http://sqlorm.sourceforge.net/dynamic_query_builder_doc.htmlBut sadly not much for C++. Someone else also asked it on stackowerflow:
http://stackoverflow.com/questions/18346357/c-sql-query-building-library
The answer is good, but sadly not quite what I Am looking for. Am I maybe using the wrong terminology?So if anyone has any ideas about this, know some libraries (GPL compatible preffered!), I would be happy to hear about it. I also would like to hear your opinion in general about that you find this a good approach, or you would do something else in my place.
Every idea is appreciated!
-
Have you tried sqlate? It's a typesafe sql query builder that uses expression templates for doing as much work as possible at compile time. It's freely available from