| 1 |
/*! \page query Fetching Query Results |
| 2 |
|
| 3 |
First read about \ref stat "how to prepare" statements |
| 4 |
|
| 5 |
\section query_fetch Fetching a Results |
| 6 |
|
| 7 |
The result is represented by cppdb::result class. It stores the query result. The prepared statement result can be fetched |
| 8 |
using \ref cppdb::statement::query() "statement::query()" function. |
| 9 |
|
| 10 |
\code |
| 11 |
cppdb::statement st = sql << "SELECT name,age FROM students"; |
| 12 |
cppdb::result r = st.query(); |
| 13 |
\endcode |
| 14 |
|
| 15 |
\note \ref cppdb::statement "statement" can be converted to \ref cppdb::result "result" automatically that actually causes calling \ref cppdb::statement::query() "query()" function. |
| 16 |
|
| 17 |
So the same code above can be written using syntactic sugar as following: |
| 18 |
|
| 19 |
\code |
| 20 |
cppdb::result r = sql << "SELECT name,age FROM students"; |
| 21 |
\endcode |
| 22 |
|
| 23 |
\section query_meta Fetching Meta-data on the Result: |
| 24 |
|
| 25 |
Following meta-data can be fetched about the result: |
| 26 |
|
| 27 |
- Number of columns using \ref cppdb::result::cols() "cols()" member function. |
| 28 |
- The name of columns using \ref cppdb::result::name() "name()" member function. These names can be used |
| 29 |
for retrieval of the data using \ref cppdb::result "fetch()" member functions. |
| 30 |
|
| 31 |
\section query_iter Getting the Result Data |
| 32 |
|
| 33 |
In order to iterate over rows of the result you should use \ref cppdb::result::next() "next()" function that |
| 34 |
returns true if the next row exits and false otherwise. Once \ref cppdb::result::next() "next()" returned true |
| 35 |
you can fetch the values using \ref cppdb::result "fetch()" family of functions. |
| 36 |
|
| 37 |
There are 3 kinds of prototypes for \c fetch() functions. |
| 38 |
|
| 39 |
-# <tt>bool fetch(int column,type &value)</tt> - fetch the value from column (index starts from 0) returning false if the value in NULL. |
| 40 |
-# <tt>bool fetch(std::string const &column_name,type &value)</tt> - fetch the value from column using its name returning false if the value in NULL. |
| 41 |
-# <tt>bool fetch(type &value)</tt> - fetch the value from the next column in current row (starting from 0) returning false if the value in NULL. |
| 42 |
|
| 43 |
Where type is one of C++ numeric types, \c std::string for text, \c std::tm for date-time types and \c std::ostream for Blob types. Fetching |
| 44 |
a value would try to do the best in casting between result type and the type you provide, for example fetching numeric or date-time types |
| 45 |
would convert them to string representation, it would try to do the casting between string and std::tm and numeric types if possible. |
| 46 |
|
| 47 |
If conversion fails, cppdb::bad_value_cast is thrown. |
| 48 |
|
| 49 |
For example: |
| 50 |
|
| 51 |
\code |
| 52 |
cppdb::result r = sql << "SELECT name,age FROM users WHERE age > ?" << 18.0; |
| 53 |
while(r.next()) { |
| 54 |
std::string name; |
| 55 |
double age; |
| 56 |
r.fetch(0,name); |
| 57 |
r.fetch(1,age); |
| 58 |
std::cout << name << " is " << age << " years old" << std::endl; |
| 59 |
} |
| 60 |
\endcode |
| 61 |
|
| 62 |
Another way to get values directly is by using cppdb::result::get(int) or cppdb::result::get(std::string const &) template member functions |
| 63 |
that allow to fetch values from columns directly using column index or column name: |
| 64 |
|
| 65 |
\code |
| 66 |
cppdb::result r = sql << "SELECT name,age FROM users WHERE age > ?" << 18.0; |
| 67 |
while(r.next()) { |
| 68 |
std::cout << r.get<std::string>("name") << " is " << r.get<double>("age") << " years old" << std::endl; |
| 69 |
} |
| 70 |
\endcode |
| 71 |
|
| 72 |
Unlike \c fetch() function, \c get() functions throw cppdb::null_value_fetch if the value was null. |
| 73 |
|
| 74 |
|
| 75 |
\section query_syntacx Syntactic Sugar |
| 76 |
|
| 77 |
There is also overloaded operator ">>" that provide syntactic sugar for fetching data and the example above an be written as: |
| 78 |
|
| 79 |
\code |
| 80 |
cppdb::result r = sql << "SELECT name,age FROM users WHERE age > ?" << 18.0; |
| 81 |
while(r.next()) { |
| 82 |
std::string name; |
| 83 |
double age; |
| 84 |
r >> name >> age; |
| 85 |
std::cout << name << " is " << age << " years old" << std::endl; |
| 86 |
} |
| 87 |
\endcode |
| 88 |
|
| 89 |
\note Operators \c >> function remain variable unchanged when the result is NULL value. |
| 90 |
|
| 91 |
In order to fetch both meta-data and the value you can use \ref cppdb::into "into()" manipulator |
| 92 |
that passes both reference to the value and a tag. For example |
| 93 |
|
| 94 |
\code |
| 95 |
cppdb::null_tag_type age_tag,name_tag; |
| 96 |
std::string name; |
| 97 |
double age; |
| 98 |
r >> cppdb::into(name,name_tag) >> cppdb::into(age,age_tag); |
| 99 |
\endcode |
| 100 |
|
| 101 |
\section query_row Fetching a Single Row |
| 102 |
|
| 103 |
Sometimes it is useful to fetch a single row of data and not iterate over it. This can be done using cppdb::statement::row() |
| 104 |
function that works like \ref cppdb::statement::query() "query()" but also calls \ref cppdb::result::next() "next()" first time |
| 105 |
and checks if more then one rows had been fetched (in which case it throws \ref cppdb::multiple_rows_query "multiple_rows_query" exception). |
| 106 |
|
| 107 |
For example: |
| 108 |
|
| 109 |
\code |
| 110 |
cppdb::statement st = sql<<"SELECT password WHERE username=?" << user |
| 111 |
cppdb::result r = st.row(); |
| 112 |
|
| 113 |
if(!r.empty()) { |
| 114 |
if(pass == r.get<std::string>(0)) { |
| 115 |
// ok |
| 116 |
} |
| 117 |
else { |
| 118 |
// wrong password |
| 119 |
} |
| 120 |
} |
| 121 |
else { |
| 122 |
// no such user |
| 123 |
} |
| 124 |
\endcode |
| 125 |
|
| 126 |
You can also use cppdb::row manipulator to make it shorter: |
| 127 |
|
| 128 |
\code |
| 129 |
cppdb::result r = sql<<"SELECT password WHERE username=?" << user << cppdb::row |
| 130 |
if(!r.empty()) |
| 131 |
... |
| 132 |
else |
| 133 |
... |
| 134 |
\endcode |
| 135 |
|
| 136 |
You can also use operator \c >> right after \ref cppdb::row "row" manipulator, but in case |
| 137 |
the empty result was fetched a \ref cppdb::empty_row_access "empty_row_access" exception would be thrown. |
| 138 |
|
| 139 |
\code |
| 140 |
double age; |
| 141 |
sql<<"SELECT age WHERE username=?" << user << cppdb::row >> age; |
| 142 |
\endcode |
| 143 |
|
| 144 |
|
| 145 |
*/ |
| 146 |
|