| 1 |
/*! \page stat Preparing and Executing Statements |
| 2 |
|
| 3 |
\section stat_prep Creating a Statement |
| 4 |
|
| 5 |
The cppdb::statement objects are usually created by calling cppdb::session::prepare() function or by using |
| 6 |
cppdb::session::operator<<(). Prepare statements are usually cached for future reuse. Non-prepared statements |
| 7 |
can be also created by calling cppdb::session::create_statement() and prepared statements can be created without |
| 8 |
caching by calling cppdb::session::create_prepared_uncached_statement(). It is useful when |
| 9 |
such statements are rarely executed or executed only once. |
| 10 |
|
| 11 |
For example: |
| 12 |
|
| 13 |
\code |
| 14 |
cppdb::statement st=sql.prepare("DELETE FROM users"); |
| 15 |
\endcode |
| 16 |
|
| 17 |
\section stat_bind Binding Parameters |
| 18 |
|
| 19 |
The statement may contain placeholders marked with "?" for parameters that should be binded. The |
| 20 |
values for placeholders can be binded by their order using cppdb::statement::operator<<() or \ref cppdb::statement "bind()" |
| 21 |
functions. The placeholder order can be also specified explicitly starting from 1. |
| 22 |
|
| 23 |
For example: |
| 24 |
\code |
| 25 |
cppdb::statement st=sql.prepare("DELETE FROM users WHERE age<? AND role<>?"); |
| 26 |
st.bind(1,13); |
| 27 |
st.bind(2,"moderator"); |
| 28 |
\endcode |
| 29 |
|
| 30 |
Of course syntactic sugar may be used as well: |
| 31 |
\code |
| 32 |
cppdb::statement st=sql << "DELETE FROM users WHERE age<? AND role<>?" << 13 << "moderator"; |
| 33 |
\endcode |
| 34 |
|
| 35 |
|
| 36 |
\section stat_null Binding NULL values. |
| 37 |
|
| 38 |
- You can use cppdb::statement::bind_null() functions. |
| 39 |
\n |
| 40 |
\code |
| 41 |
cppdb::statement st=sql << "INSERT into f(x) values(?)"; |
| 42 |
st.bind_null(1); |
| 43 |
\endcode |
| 44 |
- You can use manipulator cppdb::null |
| 45 |
\n |
| 46 |
\code |
| 47 |
sql << "INSERT into f(x) values(?)" << cppdb::null << cppdb::exec; |
| 48 |
\endcode |
| 49 |
- If you have an information about wether the value is null or not only at run time you can use a cppdb::use() manipulator |
| 50 |
which receives a value and a parameter of cppdb::null_tag_type. |
| 51 |
\n |
| 52 |
\code |
| 53 |
cppdb::null_tag_type tag = value_is_null ? cppdb::null_value : cppdb::not_null_value; |
| 54 |
sql << "INSERT into f(x) values(?)" << cppdb::use(value,tag) << cppdb::exec; |
| 55 |
\endcode |
| 56 |
|
| 57 |
|
| 58 |
\section stat_exec Executing Statements |
| 59 |
|
| 60 |
Statements are executed by calling cppdb::statement::exec(). Also manipulator cppdb::exec provided |
| 61 |
for execution of a statement that allows writing complex queries in one line. |
| 62 |
\code |
| 63 |
sql << "DELETE FROM users WHERE age<? AND role<>?" << 13 << "moderator" <<cppdb::exec; |
| 64 |
\endcode |
| 65 |
|
| 66 |
\section stat_meta Fetching Meta-data |
| 67 |
|
| 68 |
Meta-data about recently executed statement can fetched using following functions: |
| 69 |
|
| 70 |
- cppdb::statement::affected() - the number of rows affected by the statement |
| 71 |
- cppdb::statement::last_insert_id() - the last generated row id. |
| 72 |
- cppdb::statement::sequence_last() - the last generated sequence number (using named sequences). |
| 73 |
|
| 74 |
\section stat_reset Reusing Statement |
| 75 |
|
| 76 |
The same prepared statement can be reused multiple times. For this purpose after each call of ppdb::statement::exec() or ppdb::statement::query(), ppdb::statement::reset() should be called that would clear all bindings and allow executing it once again: |
| 77 |
|
| 78 |
\code |
| 79 |
cppdb::statement st = sql << "INSERT INTO students(id,name) values(?,?)"; |
| 80 |
for(i=0;i<students.size();i++) { |
| 81 |
st.bind(students[i].id); |
| 82 |
st.bind(students[i].name); |
| 83 |
st.exec(); |
| 84 |
st.reset(); |
| 85 |
} |
| 86 |
\endcode |
| 87 |
|
| 88 |
*/ |
| 89 |
|