Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL request helper for C++

I'm looking for a library that will simplify working with SQL in C++. I'm aware that there are different ORM solutions (like those listed here), and I had a quick look over them, but I'm probably looking for something different (not exactly ORM), so if you could help...

I want something that won't encapsulate access to the database etc, but will act more like request builder and binder, and I run the requests myself. I expect that this would allow me to simplify SQL handling and at the same time have access to run custom queries, without necessarily staying within the object model.

For the moment the closest thing I've found is this Database Template Library, which allows to define binding like this:

   boundIOs["INT_VALUE"]    == rowbuf.exampleInt;
   boundIOs["STRING_VALUE"] == rowbuf.exampleStr;

(column INT_VALUE is bound to exampleInt field of the object) and then run queries using such bindings, which I find quite convenient and flexible enough at the same time.

I'll definitely take a closer look to this library, but probably you could also suggest some other library/framework that uses similar ideas, and is more popular/mature/supported etc, or share your experience with this DTL library? Thanks.

like image 668
Roman L Avatar asked Jan 31 '26 19:01

Roman L


1 Answers

OTL has a prety nice interface that doesn't have an ORM model.

It uses streams to set/get bind variables. A simple example is shown below

otl_stream i(50, // buffer size
              "select * from test_tab where f1>=:f<int>" // SELECT statement,
              db // connect object
             ); 
i<<8; // assigning :f = 8

//execute query
while(!i.eof()){ // while not end-of-data
    i>>f1>>f2;
    cout<<"f1="<<f1<<", f2="<<f2<<endl;
}

You can see more examples here

Edit, showing how binding multiple variables is trivial

otl_stream i(50, // buffer size
              "select * from test_tab where f1>=:f<int> AND f2=:g<int> and f3=:h<int> and f4=:i<int>" // SELECT statement,
              db // connect object
             ); 
i << 8 << 10 << 12 << 42;

//execute query
while(!i.eof()){ // while not end-of-data
    i>>f1>>f2;
    cout<<"f1="<<f1<<", f2="<<f2<<endl;
}

As you can see, you define your placeholders and their types once f1>=:f<int> AND f2=:g<int>. You then stream your values into the query (which is typesafe) before executing it.

like image 50
Glen Avatar answered Feb 02 '26 09:02

Glen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!