3

I am looking for a c++ library that provides similar functionality to the c# SelectQueryBuilder library

http://www.codeproject.com/Articles/13419/SelectQueryBuilder-Building-complex-and-flexible-S

i.e. it allows one to get away from building horrible concatenated strings into order to form a dynamic SQL query, instead having a library that provides an interface where by you pass it the table, the elements you want to select from the table, etc, and it returns the SQL query as a string.

Any help much appreciated

Edit: Sample Query I am building....and we wont know the actual columns for selection until runtime e.g. don't know how many VAR1...VARx there will be and what exactly they will be.

SELECT * FROM 
    (
        SELECT 
            table_1.id, 
            table_2.name, 
            (select(COALESCE(sum(table_1.col_1 * 1.0) / NULLIF(sum(table_1.col_2 - table_1.col_3),0) * 100,0))) as VAR1, 
            (select(COALESCE(sum(table_1.col_4 * 1.0) / NULLIF(sum(table_1.col_5),0) * 100,0))) as VAR2, 
            sum(table_1.col_2) as VAR3 
        FROM table_1, table_2 
        WHERE table_1.id = table_2.id 
        GROUP BY table_1.id, table_2.name 
    ) VARIABLES 
WHERE VAR3 > 1000
D'Arcy Rittich
  • 167,292
  • 40
  • 290
  • 283
oracle3001
  • 1,090
  • 19
  • 31

1 Answers1

1

With QSqlQuery you can use placeholders and bind values to them:

QSqlQuery query;
query.prepare("INSERT INTO person (id, forename, surname) "
              "VALUES (:id, :forename, :surname)");
query.bindValue(":id", 1001);
query.bindValue(":forename", "Bart");
query.bindValue(":surname", "Simpson");
query.exec();

or

QSqlQuery query;
query.prepare("INSERT INTO person (id, forename, surname) "
              "VALUES (?, ?, ?)");
query.addBindValue(1001);
query.addBindValue("Bart");
query.addBindValue("Simpson");
query.exec();

http://qt-project.org/doc/qt-5.0/qtsql/qsqlquery.html#approaches-to-binding-values

headsvk
  • 2,726
  • 1
  • 19
  • 23
  • I forgot to ask if it possible nest the query / values and also add binds dynamically? i.e if I wanted a "select [ ] from table_1", where [] will be allocated at run time and is a list of values I would like to select? – oracle3001 Aug 22 '13 at 08:46
  • The value binding is done at runtime, I think you could use addBindValue in a for loop. If you posted some code I could help you further. – headsvk Aug 22 '13 at 09:00
  • I have added the sort of sample query I am working with (although the real examples select a lot longer list from the DB). I currently create them by concatenating strings together, but obviously I would prefer to be able to use the binding method you have suggested. – oracle3001 Aug 22 '13 at 09:55
  • Oh, I see, you need to not only bind values but also create variable queries. I haven't seen something like that in Qt. I checked the docs http://qt-project.org/doc/qt-5.0/qtsql/qsqlquery.html, but there's only the value binding approach, nothing about preparing long statements. – headsvk Aug 22 '13 at 10:10