sqlite_modern_cpp icon indicating copy to clipboard operation
sqlite_modern_cpp copied to clipboard

Add binding mode option to database and database_binder

Open jimmyjxiao opened this issue 7 years ago • 1 comments

Allows the user to specify to use SQLite_static instead of transient all the time. In reference to @aminroosta comment about the interface on #167, I would argue this interface is better because it's more consistent with other things in c++ that use the << stream-like syntax. Iostream, for example, you can << an option and it'll affect things inputted in after.

For example:

std::cout  << "The number 42 in hex:     " << std::hex << 42 << '\n';

Output: The number 42 in hex: 2a

I think

db << "INSERT INTO foo values (?,?,?)" << binding_mode::do_not_copy << str1 << str2 << str3

Looks a lot cleaner than

db << "INSERT INTO foo values (?,?,?)" << binding_mode::do_not_copy(str1)<< binding_mode::do_not_copy(str2) << binding_mode::do_not_copy(str3)

At any rate, I decided to open a new pull request so we can discuss it here instead of on the old merged one.

jimmyjxiao avatar Jun 19 '18 17:06 jimmyjxiao

The modifier syntax looks quite nice when used inline, but they are dangerous when the statements are passed to different code. If a function accepts a prepared statement as argument, it can't know the current binding mode and it also can't change it without modifying the outer state.

You could add a getter to inspect the value, but bugs in functions which do not expect this are hard to find: The code looks right and it works in almost all circumstances, except in the unusual case where a user is in do_not_copy mode. Also the user has to guarantee the lifetime for every passed object, so adding an explicit marker ti every object makes sense.

zauguin avatar Jun 21 '18 10:06 zauguin