Abdul Ahad Abdul Ahad - 4 months ago 96
SQL Question

How to detect SQL errors in c++ code at compile time

So, I want SQL errors to prevent c++ code from compiling via some cmake script or something. I know I could add some kind of runtime error checking but I don't want the following code to build sucessfully (not a question about pqxx, but SQL errors in c++ in general):

#include <iostream>
#include <pqxx/pqxx>

using namespace std;
using namespace pqxx;

int main() {
connection conn("dbname=d user=u");

conn.prepare("invalid_sql", "SELECT 1 FROM WHERE a = $1");

nontransaction txn(conn);

txn.prepared("invalid_sql")(1.0).exec();

cout << "the process already died due to invalid SQL" << endl;

return 0;
}


so I edited the question to display the prepared statements. What I'm going to do is put all the prepare statements in a init_sql() method and call that using boost/test/unit_test.hpp against a replicated db. I guess there's no way around it

Answer Source

Closing this question. The other answer is fine, but that's not what I did.

What I decided to do was put the prepared statements in a init_sql() method and call that using boost/test/unit_test.hpp against a replicated database with jenkins, which provides notifications for unit test failures.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download