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;
128 #define MYSQLPP_DEFINE_INSERT_POLICY_TEMPLATES
130 #undef MYSQLPP_DEFINE_INSERT_POLICY_TEMPLATES
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();
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)
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();
Exception thrown when an insert policy is too strict to create a valid INSERT statement.
Definition: exceptions.h:457
Manages the connection to the database server.
Definition: connection.h:60
Disable exceptions in an object derived from OptionalExceptions.
Definition: noexceptions.h:120
Interface allowing a class to have optional exceptions.
Definition: noexceptions.h:72
A class for building and executing SQL queries.
Definition: query.h:124
void storein(std::deque< T > &con, const SQLTypeAdapter &s)
Specialization of storein_sequence() for std::deque.
Definition: query.h:914
Function for_each(Function fn)
Execute the query, and call a functor for each returned row.
Definition: query.h:547
std::string str()
Get built query as a C++ string.
Definition: query.h:283
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
bool exec()
Execute a built-up query.
Definition: query.h:317
Function for_each(const SQLTypeAdapter &query, Function fn)
Execute a query, and call a functor for each returned row.
Definition: query.h:526
Query & insert(Iter first, Iter last)
Insert multiple new rows.
Definition: query.h:1031
void storein_sequence(Sequence &con, const SQLTypeAdapter &s)
Executes a query, storing the result rows in an STL sequence container.
Definition: query.h:754
void storein(T &con, SQLQueryParms &p)
Store template query results into a container.
Definition: query.h:900
SQLQueryParms template_defaults
The default template parameters.
Definition: query.h:1306
Query & update(const T &o, const T &n)
Replace an existing row's data with new data.
Definition: query.h:984
Function store_if(Sequence &con, Function fn)
Execute the query, conditionally storing each row in a container.
Definition: query.h:662
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
std::string str(const SQLTypeAdapter &arg0)
Get built query as a C++ string with template query parameter substitution.
Definition: query.h:298
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
void storein(std::list< T > &con, const SQLTypeAdapter &s)
Specialization of storein_sequence() for std::list.
Definition: query.h:921
void storein(Container &con)
Execute a query, and store the entire result set in an STL container.
Definition: query.h:888
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
void storein_sequence(Sequence &con)
Execute a query, storing the result set in an STL sequence container.
Definition: query.h:735
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
Query & insert(const T &v)
Insert a new row.
Definition: query.h:1006
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(std::multiset< T > &con, const SQLTypeAdapter &s)
Specialization of storein_set() for std::multiset.
Definition: query.h:968
Function for_each(const SSQLS &ssqls, Function fn)
Run a functor for every row in a table.
Definition: query.h:571
Function store_if(Sequence &con, const SQLTypeAdapter &query, Function fn)
Execute a query, conditionally storing each row in a container.
Definition: query.h:607
void storein(std::vector< T > &con, const SQLTypeAdapter &s)
Specialization of storein_sequence() for std::vector.
Definition: query.h:907
void storein_set(Set &con)
Execute a query, storing the result set in an STL associative container.
Definition: query.h:806
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(std::set< T > &con, const SQLTypeAdapter &s)
Specialization of storein_set() for std::set.
Definition: query.h:961
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
Manages rows from a result set.
Definition: row.h:64
This class holds the parameter values for filling template queries.
Definition: qparms.h:49
Converts many different data types to strings suitable for use in SQL queries.
Definition: stadapter.h:74
A special std::set derivative for holding MySQL data sets.
Definition: myset.h:72
Holds information about the result of queries that don't return rows.
Definition: result.h:49
StoreQueryResult set type for "store" queries.
Definition: result.h:212
Exception thrown when something goes wrong in processing a "use" query.
Definition: exceptions.h:255
StoreQueryResult set type for "use" queries.
Definition: result.h:299
Row fetch_row() const
Returns the next row in a "use" query's result set.
Definition: result.cpp:172
This file includes top-level definitions for use both internal to the library, and outside it....
Declares the MySQL++-specific exception classes.
Declares the InsertPolicy classes.
Declares interface that allows exceptions to be optional.
Declares the template query parameter-related stuff.
Declares classes for holding information about SQL query results.
Declares the classes for holding row data from a result set.
Defines a class for building quoted and escaped SQL text.
Declares the SQLTypeAdapter class.
Declares the Transaction class.