samedi 28 mars 2015

sqlite3 bind variables syntax

I am trying to implement a C++11 wrapper to C API of SQLite, particularly to sqlite3_bind functions. I would like it to be something in the shape of this (example from my code):



sqlt3::exec<void>(
_db,
"INSERT OR REPLACE INTO window VALUES('position', ?3, ?4);\n"
"INSERT OR REPLACE INTO window VALUES('size', ?1, ?2);\n"
"INSERT OR REPLACE INTO window VALUES('maximized', ?5, '0');\n",
size.x,
size.y,
position.x,
position.y,
static_cast<int>(IsMaximized())
);


The problem here is that ? are shared between different statements. The exec function internally prepares subsequent SQL statement one by one. So I cannot determine which parameters should be bound to which statements, because there is no API to get which statement slots the statement contains. I can get the count of slots n (a max NNN from all ?NNNs) but I am unable to determine whether particular slots from zero to the n are actually occupied. It is because there can be single ? with no numbers and sqlite3_bind_parameter_name returns NULL for both the nameless parameter and the lack of parameter.


One solution that I came upon is to bind all parameters, in the order they are passed to the exec, to all of the statements but I didn't found in the documentation is it legal to bind to nonexistent parameter. Another drawback is that the complexity is v times s where v is number of variables to be bound and s is number of statements.


So, I came with another workaround to just parse whole string passed to exec and gather info about binding points (?, ?NNN, :VVV etc.). But, I think it isn't quite trivial task because that special symbol are not special in some contexts like string and quatations. So, what are another contexts in SQL statements, in which special tokens ?, ?NNN, etc. have not meaning of binding points?


Aucun commentaire:

Enregistrer un commentaire