Go to the documentation of this file.
28 #if !defined(MYSQLPP_QUERY_H)
29 #define MYSQLPP_QUERY_H
53 # if defined(HAVE_STD_SLIST) || defined(HAVE_GLOBAL_SLIST)
60 #if !defined(DOXYGEN_IGNORE)
62 class MYSQLPP_EXPORT Connection;
63 class MYSQLPP_EXPORT Transaction;
121 class MYSQLPP_EXPORT Query :
123 public OptionalExceptions
128 #define MYSQLPP_DEFINE_INSERT_POLICY_TEMPLATES
130 #undef MYSQLPP_DEFINE_INSERT_POLICY_TEMPLATES
139 Query(Connection* c,
bool te =
true,
const char* qstr = 0);
148 Query(
const Query& q);
151 ulonglong affected_rows();
167 size_t escape_string(std::string* ps,
const char* original = 0,
168 size_t length = 0)
const;
191 size_t escape_string(
char* escaped,
const char* original,
192 size_t length)
const;
204 const char* error()
const;
220 ulonglong insert_id();
226 Query& operator=(
const Query& rhs);
246 operator void*()
const;
255 bool operator !()
const {
return !
operator void*(); }
283 std::string
str() {
return str(template_defaults); }
317 bool exec() {
return exec(str(template_defaults)); }
330 bool exec(
const std::string& str);
525 template <
typename Function>
546 template <
typename Function>
570 template <
class SSQLS,
typename Function>
573 std::string query(
"select * from `");
574 query += ssqls.table();
606 template <
class Sequence,
typename Function>
633 template <
class Sequence,
class SSQLS,
typename Function>
634 Function
store_if(Sequence& con,
const SSQLS& ssqls, Function fn)
636 std::string query(
"select * from `");
637 query += ssqls.table();
661 template <
class Sequence,
typename Function>
734 template <
class Sequence>
737 storein_sequence(con, str(template_defaults));
753 template <
class Sequence>
758 MYSQL_ROW d = result.fetch_raw_row();
760 Row row(d, &result, result.fetch_lengths(),
763 con.push_back(
typename Sequence::value_type(row));
766 else if (!result_empty()) {
774 if (throw_exceptions()) {
795 storein_sequence(con, str(p));
808 storein_set(con, str(template_defaults));
829 MYSQL_ROW d = result.fetch_raw_row();
831 Row row(d, &result, result.fetch_lengths(),
834 con.insert(
typename Set::value_type(row));
837 else if (!result_empty()) {
845 if (throw_exceptions()) {
866 storein_set(con, str(p));
887 template <
class Container>
890 storein(con, str(template_defaults));
902 storein(con, str(p));
909 storein_sequence(con, s);
916 storein_sequence(con, s);
923 storein_sequence(con, s);
926 #if defined(HAVE_EXT_SLIST)
932 storein_sequence(con, s);
934 #elif defined(HAVE_GLOBAL_SLIST)
942 void storein(slist<T>& con,
const SQLTypeAdapter& s)
944 storein_sequence(con, s);
946 #elif defined(HAVE_STD_SLIST)
953 void storein(std::slist<T>& con,
const SQLTypeAdapter& s)
955 storein_sequence(con, s);
991 MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
992 "UPDATE `" << o.table() <<
"` SET " << n.equal_list() <<
993 " WHERE " << o.equal_list(
" AND ", sql_use_compare);
1010 MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
1011 "INSERT INTO `" << v.table() <<
"` (" <<
1012 v.field_list() <<
") VALUES (" <<
1013 v.value_list() <<
')';
1030 template <
class Iter>
1035 if (first != last) {
1038 MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
1039 "INSERT INTO `" << first->table() <<
"` (" <<
1040 first->field_list() <<
") VALUES (" <<
1041 first->value_list() <<
')';
1047 while (++it != last) {
1048 MYSQLPP_QUERY_THISPTR <<
",(" << it->value_list() <<
')';
1067 template <
class Iter,
class InsertPolicy>
1070 bool success =
true;
1075 if (first == last) {
1079 typename InsertPolicy::access_controller ac(*conn_);
1081 for (Iter it = first; it != last; ++it) {
1082 if (policy.can_add(
int(tellp()), *it)) {
1084 MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
1085 "INSERT INTO `" << it->table() <<
"` (" <<
1086 it->field_list() <<
") VALUES (";
1089 MYSQLPP_QUERY_THISPTR <<
",(";
1092 MYSQLPP_QUERY_THISPTR << it->value_list() <<
')';
1108 if (policy.can_add(
int(tellp()), *it)) {
1109 MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
1110 "INSERT INTO `" << it->table() <<
"` (" <<
1111 it->field_list() <<
") VALUES (" <<
1112 it->value_list() <<
')';
1118 if (throw_exceptions()) {
1129 if (success && !empty && !exec()) {
1155 template <
class Iter,
class InsertPolicy>
1158 bool success =
true;
1163 if (first == last) {
1167 typename InsertPolicy::access_controller ac(*conn_);
1169 for (Iter it = first; it != last; ++it) {
1170 if (policy.can_add(
int(tellp()), *it)) {
1172 MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
1173 "REPLACE INTO `" << it->table() <<
"` (" <<
1174 it->field_list() <<
") VALUES (";
1177 MYSQLPP_QUERY_THISPTR <<
",(";
1180 MYSQLPP_QUERY_THISPTR << it->value_list() <<
')';
1196 if (policy.can_add(
int(tellp()), *it)) {
1197 MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
1198 "REPLACE INTO `" << it->table() <<
"` (" <<
1199 it->field_list() <<
") VALUES (" <<
1200 it->value_list() <<
')';
1206 if (throw_exceptions()) {
1217 if (success && !empty && !exec()) {
1245 MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
1246 "REPLACE INTO `" << v.table() <<
"` (" <<
1247 v.field_list() <<
") VALUES (" << v.value_list() <<
')';
1265 template <
class Iter>
1269 if (first != last) {
1272 MYSQLPP_QUERY_THISPTR << std::setprecision(16) <<
1273 "REPLACE INTO " << first->table() <<
" (" <<
1274 first->field_list() <<
") VALUES (" <<
1275 first->value_list() <<
')';
1281 while (++it != last) {
1282 MYSQLPP_QUERY_THISPTR <<
",(" << it->value_list() <<
')';
1289 #if !defined(DOXYGEN_IGNORE)
1294 mysql_query_define0(std::string, str)
1298 mysql_query_define1(storein_sequence)
1299 mysql_query_define1(storein_set)
1300 mysql_query_define1(storein)
1301 #endif // !defined(DOXYGEN_IGNORE)
1318 std::vector<SQLParseElement> parse_elems_;
1322 std::vector<std::string> parsed_names_;
1325 std::map<std::string, short int> parsed_nums_;
1328 std::stringbuf sbuffer_;
1340 inline std::ostream& operator <<(std::ostream& os,
Query& q)
1342 return os << q.
str();
1348 #endif // !defined(MYSQLPP_QUERY_H)
void reset()
Reset the query object so that it can be reused.
Definition: query.cpp:492
UseQueryResult use()
Execute a query that can return rows, with access to the rows in sequence.
Definition: query.cpp:649
Declares the MySQL++-specific exception classes.
int errnum() const
Get the last error number that was set.
Definition: query.cpp:120
ulonglong insert_id()
Get ID generated for an AUTO_INCREMENT column in the previous INSERT query.
Definition: query.cpp:254
void storein_sequence(Sequence &con, const SQLTypeAdapter &s)
Executes a query, storing the result rows in an STL sequence container.
Definition: query.h:754
Declares the Transaction class.
Declares the SQLTypeAdapter class.
Query & insert(Iter first, Iter last)
Insert multiple new rows.
Definition: query.h:1031
@ nr_error
problem retrieving next result
Definition: dbdriver.h:130
Function for_each(const SSQLS &ssqls, Function fn)
Run a functor for every row in a table.
Definition: query.h:571
Manages rows from a result set.
Definition: row.h:85
Manages the connection to the database server.
Definition: connection.h:81
void set_exceptions(bool e) const
Sets the exception state to a particular value.
Definition: noexceptions.h:141
bool result_empty()
Returns true if the most recent result set was empty.
Definition: query.cpp:504
StoreQueryResult set type for "use" queries.
Definition: result.h:320
size_type length() const
Return number of bytes in data buffer.
Definition: stadapter.cpp:487
Row fetch_row() const
Returns the next row in a "use" query's result set.
Definition: result.cpp:191
Declares the InsertPolicy classes.
Query & replace(Iter first, Iter last)
Insert multiple new rows, or replace existing ones if there are existing rows that match on key field...
Definition: query.h:1266
void storein_set(Set &con, SQLQueryParms &p)
Execute template query using given parameters, storing the results in a set type container.
Definition: query.h:864
void storein_sequence(Seq &con, SQLQueryParms &p)
Execute template query using given parameters, storing the results in a sequence type container.
Definition: query.h:793
Function store_if(Sequence &con, const SQLTypeAdapter &query, Function fn)
Execute a query, conditionally storing each row in a container.
Definition: query.h:607
Query & replacefrom(Iter first, Iter last, InsertPolicy &policy)
Replace multiple new rows using an insert policy to control how the REPLACE statements are created us...
Definition: query.h:1156
StoreQueryResult set type for "store" queries.
Definition: result.h:231
A class for building and executing SQL queries.
Definition: query.h:143
Query & update(const T &o, const T &n)
Replace an existing row's data with new data.
Definition: query.h:984
void parse()
Treat the contents of the query string as a template query.
Definition: query.cpp:291
Defines a class for building and executing SQL queries.
Converts many different data types to strings suitable for use in SQL queries.
Definition: stadapter.h:95
Disable exceptions in an object derived from OptionalExceptions.
Definition: noexceptions.h:140
StoreQueryResult store_next()
Return next result set, when processing a multi-query.
Definition: query.cpp:593
bool more_results()
Return whether more results are waiting for a multi-query or stored procedure response.
Definition: query.cpp:261
void storein(Container &con)
Execute a query, and store the entire result set in an STL container.
Definition: query.h:888
Declares the template query parameter-related stuff.
const char * error() const
Return error message for last error associated with this connection.
Definition: connection.cpp:185
Exception thrown when an insert policy is too strict to create a valid INSERT statement.
Definition: exceptions.h:478
MYSQL_RES * store_result()
Saves the results of the query just execute()d in memory and returns a pointer to the MySQL C API dat...
Definition: dbdriver.h:651
A template for setting a flag on a variable as long as the object that set it is in scope....
Definition: autoflag.h:59
const char * error() const
Get the last error message that was set.
Definition: query.cpp:127
SQLQueryParms template_defaults
The default template parameters.
Definition: query.h:1306
This class holds the parameter values for filling template queries.
Definition: qparms.h:70
SimpleResult execute()
Execute built-up query.
Definition: query.cpp:186
nr_code next_result()
Moves to the next result set from a multi-query.
Definition: dbdriver.h:465
Exception thrown when the database server encounters a problem while processing your query.
Definition: exceptions.h:307
void storein(std::deque< T > &con, const SQLTypeAdapter &s)
Specialization of storein_sequence() for std::deque.
Definition: query.h:914
ulonglong affected_rows()
Return the number of rows affected by the last query.
Definition: query.cpp:113
Query & insert(const T &v)
Insert a new row.
Definition: query.h:1006
bool result_empty()
Returns true if the most recent result set was empty.
Definition: dbdriver.h:546
static size_t escape_string_no_conn(char *to, const char *from, size_t length)
SQL-escapes the given string without reference to the character set of a database server.
Definition: dbdriver.h:310
void storein(std::list< T > &con, const SQLTypeAdapter &s)
Specialization of storein_sequence() for std::list.
Definition: query.h:921
Declares the DBDriver class.
Declares the classes for holding row data from a result set.
std::string query_info()
Returns information about the last executed query.
Definition: dbdriver.cpp:234
@ nr_more_results
success, with more results to come
Definition: dbdriver.h:128
Declares interface that allows exceptions to be optional.
ulonglong affected_rows()
Return the number of rows affected by the last query.
Definition: dbdriver.h:129
Function for_each(Function fn)
Execute the query, and call a functor for each returned row.
Definition: query.h:547
DBDriver * driver()
Returns a reference to the current database driver.
Definition: connection.h:211
bool more_results()
Returns true if there are unconsumed results from the most recent query.
Definition: dbdriver.h:446
Declares classes for holding information about SQL query results.
std::string str()
Get built query as a C++ string.
Definition: query.h:283
MYSQL_RES * use_result()
Returns a result set from the last-executed query which we can walk through in linear fashion,...
Definition: dbdriver.h:725
A special std::set derivative for holding MySQL data sets.
Definition: myset.h:93
void storein(std::multiset< T > &con, const SQLTypeAdapter &s)
Specialization of storein_set() for std::multiset.
Definition: query.h:968
Declares the Connection class.
ulonglong insert_id()
Get ID generated for an AUTO_INCREMENT column in the previous INSERT query.
Definition: dbdriver.h:423
Function store_if(Sequence &con, const SSQLS &ssqls, Function fn)
Pulls every row in a table, conditionally storing each one in a container.
Definition: query.h:634
This file includes top-level definitions for use both internal to the library, and outside it....
Query & insertfrom(Iter first, Iter last, InsertPolicy &policy)
Insert multiple new rows using an insert policy to control how the INSERT statements are created usin...
Definition: query.h:1068
StoreQueryResult store()
Execute a query that can return a result set.
Definition: query.cpp:511
void clear()
Clears the list.
Definition: qparms.h:122
size_t escape_string(std::string *ps, const char *original=0, size_t length=0) const
Return a SQL-escaped version of a character buffer.
Definition: query.cpp:134
void storein(T &con, SQLQueryParms &p)
Store template query results into a container.
Definition: query.h:900
Interface allowing a class to have optional exceptions.
Definition: noexceptions.h:92
void storein_set(Set &con)
Execute a query, storing the result set in an STL associative container.
Definition: query.h:806
void storein(std::set< T > &con, const SQLTypeAdapter &s)
Specialization of storein_set() for std::set.
Definition: query.h:961
Function for_each(const SQLTypeAdapter &query, Function fn)
Execute a query, and call a functor for each returned row.
Definition: query.h:526
nr_code
Result code returned by next_result()
Definition: dbdriver.h:105
size_t escape_string(char *to, const char *from, size_t length)
Return a SQL-escaped version of the given character buffer.
Definition: dbdriver.h:255
Function store_if(Sequence &con, Function fn)
Execute the query, conditionally storing each row in a container.
Definition: query.h:662
Defines a template for setting a flag within a given variable scope, and resetting it when exiting th...
void storein_sequence(Sequence &con)
Execute a query, storing the result set in an STL sequence container.
Definition: query.h:735
Exception thrown when something goes wrong in processing a "use" query.
Definition: exceptions.h:276
Defines a class for building quoted and escaped SQL text.
void storein_set(Set &con, const SQLTypeAdapter &s)
Executes a query, storing the result rows in an STL set-associative container.
Definition: query.h:825
std::string info()
Returns information about the most recently executed query.
Definition: query.cpp:247
Query & operator=(const Query &rhs)
Assign another query's state to this object.
Definition: query.cpp:268
int errnum()
Return last error number associated with this connection.
Definition: connection.cpp:178
Query & replace(const T &v)
Insert new row unless there is an existing row that matches on a unique index, in which case we repla...
Definition: query.h:1241
const char * data() const
Return pointer to raw data buffer.
Definition: stadapter.cpp:481
bool throw_exceptions() const
Returns true if exceptions are enabled.
Definition: noexceptions.h:134
bool execute(const char *qstr, size_t length)
Executes the given query string.
Definition: dbdriver.h:328
Query(Connection *c, bool te=true, const char *qstr=0)
Create a new query object attached to a connection.
Definition: query.cpp:65
Holds information about the result of queries that don't return rows.
Definition: result.h:70
bool exec()
Execute a built-up query.
Definition: query.h:317
void storein(std::vector< T > &con, const SQLTypeAdapter &s)
Specialization of storein_sequence() for std::vector.
Definition: query.h:907
std::string str(const SQLTypeAdapter &arg0)
Get built query as a C++ string with template query parameter substitution.
Definition: query.h:298