#include <query.h>
Inheritance diagram for mysqlpp::Query:
Public Member Functions | |
Query (Connection *c, bool te=true, const char *qstr=0) | |
Create a new query object attached to a connection. | |
Query (const Query &q) | |
Create a new query object as a copy of another. | |
ulonglong | affected_rows () |
Return the number of rows affected by the last query. | |
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. | |
size_t | escape_string (char *escaped, const char *original, size_t length) const |
Return a SQL-escaped version of the given character buffer. | |
int | errnum () const |
Get the last error number that was set. | |
const char * | error () const |
Get the last error message that was set. | |
std::string | info () |
Returns information about the most recently executed query. | |
ulonglong | insert_id () |
Get ID generated for an AUTO_INCREMENT column in the previous INSERT query. | |
Query & | operator= (const Query &rhs) |
Assign another query's state to this object. | |
operator void * () const | |
Test whether the object has experienced an error condition. | |
void | parse () |
Treat the contents of the query string as a template query. | |
void | reset () |
Reset the query object so that it can be reused. | |
std::string | str () |
Get built query as a C++ string. | |
std::string | str (const SQLTypeAdapter &arg0) |
Get built query as a C++ string with template query parameter substitution. | |
std::string | str (SQLQueryParms &p) |
Get built query as a null-terminated C++ string. | |
bool | exec () |
Execute a built-up query. | |
bool | exec (const std::string &str) |
Execute a query. | |
SimpleResult | execute () |
Execute built-up query. | |
SimpleResult | execute (SQLQueryParms &p) |
Execute template query using given parameters. | |
SimpleResult | execute (const SQLTypeAdapter &str) |
Execute a query that returns no rows. | |
SimpleResult | execute (const char *str, size_t len) |
Execute query in a known-length string of characters. This can include null characters. | |
UseQueryResult | use () |
Execute a query that can return rows, with access to the rows in sequence. | |
UseQueryResult | use (SQLQueryParms &p) |
Execute a template query that can return rows, with access to the rows in sequence. | |
UseQueryResult | use (const SQLTypeAdapter &str) |
Execute a query that can return rows, with access to the rows in sequence. | |
UseQueryResult | use (const char *str, size_t len) |
Execute a query that can return rows, with access to the rows in sequence. | |
StoreQueryResult | store () |
Execute a query that can return a result set. | |
StoreQueryResult | store (SQLQueryParms &p) |
Store results from a template query using given parameters. | |
StoreQueryResult | store (const SQLTypeAdapter &str) |
Execute a query that can return rows, returning all of the rows in a random-access container. | |
StoreQueryResult | store (const char *str, size_t len) |
Execute a query that can return rows, returning all of the rows in a random-access container. | |
template<typename Function> | |
Function | for_each (const SQLTypeAdapter &query, Function fn) |
Execute a query, and call a functor for each returned row. | |
template<typename Function> | |
Function | for_each (Function fn) |
Execute the query, and call a functor for each returned row. | |
template<class SSQLS, typename Function> | |
Function | for_each (const SSQLS &ssqls, Function fn) |
Run a functor for every row in a table. | |
template<class Sequence, typename Function> | |
Function | store_if (Sequence &con, const SQLTypeAdapter &query, Function fn) |
Execute a query, conditionally storing each row in a container. | |
template<class Sequence, class SSQLS, typename Function> | |
Function | store_if (Sequence &con, const SSQLS &ssqls, Function fn) |
Pulls every row in a table, conditionally storing each one in a container. | |
template<class Sequence, typename Function> | |
Function | store_if (Sequence &con, Function fn) |
Execute the query, conditionally storing each row in a container. | |
StoreQueryResult | store_next () |
Return next result set, when processing a multi-query. | |
bool | more_results () |
Return whether more results are waiting for a multi-query or stored procedure response. | |
template<class Sequence> | |
void | storein_sequence (Sequence &con) |
Execute a query, storing the result set in an STL sequence container. | |
template<class Sequence> | |
void | storein_sequence (Sequence &con, const SQLTypeAdapter &s) |
Executes a query, storing the result rows in an STL sequence container. | |
template<class Seq> | |
void | storein_sequence (Seq &con, SQLQueryParms &p) |
Execute template query using given parameters, storing the results in a sequence type container. | |
template<class Set> | |
void | storein_set (Set &con) |
Execute a query, storing the result set in an STL associative container. | |
template<class Set> | |
void | storein_set (Set &con, const SQLTypeAdapter &s) |
Executes a query, storing the result rows in an STL set-associative container. | |
template<class Set> | |
void | storein_set (Set &con, SQLQueryParms &p) |
Execute template query using given parameters, storing the results in a set type container. | |
template<class Container> | |
void | storein (Container &con) |
Execute a query, and store the entire result set in an STL container. | |
template<class T> | |
void | storein (std::vector< T > &con, const SQLTypeAdapter &s) |
Specialization of storein_sequence() for std::vector . | |
template<class T> | |
void | storein (std::deque< T > &con, const SQLTypeAdapter &s) |
Specialization of storein_sequence() for std::deque . | |
template<class T> | |
void | storein (std::list< T > &con, const SQLTypeAdapter &s) |
Specialization of storein_sequence() for std::list . | |
template<class T> | |
void | storein (std::set< T > &con, const SQLTypeAdapter &s) |
Specialization of storein_set() for std::set . | |
template<class T> | |
void | storein (std::multiset< T > &con, const SQLTypeAdapter &s) |
Specialization of storein_set() for std::multiset . | |
template<class T> | |
Query & | update (const T &o, const T &n) |
Replace an existing row's data with new data. | |
template<class T> | |
Query & | insert (const T &v) |
Insert a new row. | |
template<class Iter> | |
Query & | insert (Iter first, Iter last) |
Insert multiple new rows. | |
template<class T> | |
Query & | replace (const T &v) |
Insert new row unless there is an existing row that matches on a unique index, in which case we replace it. | |
Public Attributes | |
SQLQueryParms | template_defaults |
The default template parameters. | |
Friends | |
class | SQLQueryParms |
One does not generally create Query objects directly. Instead, call mysqlpp::Connection::query() to get one tied to that connection.
There are several ways to build and execute SQL queries with this class.
The way most like other database libraries is to pass a SQL statement in either the form of a C or C++ string to one of the exec*(), store*(), or use() methods. The query is executed immediately, and any results returned.
For more complicated queries, it's often more convenient to build up the query string over several C++ statements using Query's stream interface. It works like any other C++ stream (std::cout
, std::ostringstream
, etc.) in that you can just insert things into the stream, building the query up piece by piece. When the query string is complete, you call the overloaded version of exec*(), store*(), or use() takes no parameters, which executes the built query and returns any results.
If you are using the library's Specialized SQL Structures feature, Query has several special functions for generating common SQL queries from those structures. For instance, it offers the insert() method, which builds an INSERT query to add the contents of the SSQLS to the database. As with the stream interface, these methods only build the query string; call one of the parameterless methods mentioned previously to actually execute the query.
Finally, you can build "template queries". This is something like C's printf()
function, in that you insert a specially-formatted query string into the object which contains placeholders for data. You call the parse() method to tell the Query object that the query string contains placeholders. Having done that, you call one of the the many exec*(), store*(), or use() overloads that take SQLTypeAdapter objects. There are 25 of each by default, differing only in the number of STA objects they take. (See lib/querydef
.pl if you need to change the limit, or examples/tquery2
.cpp for a way around it that doesn't require changing the library.) Only the version taking a single STA object is documented below, as to document all of them would just be repetitive. For each Query method that takes a single STA object, there's a good chance there's a set of undocumented overloads that take more of them for the purpose of filling out a template query.
See the user manual for more details about these options.
|
Create a new query object attached to a connection. This is the constructor used by mysqlpp::Connection::query().
|
|
Create a new query object as a copy of another. This is not a traditional copy ctor! Its only purpose is to make it possible to assign the return of Connection::query() to an empty Query object. In particular, the stream buffer and template query stuff will be empty in the copy, regardless of what values they have in the original. |
|
Get the last error number that was set. This just delegates to Connection::errnum(). Query has nothing extra to say, so use either, as makes sense in your program. |
|
Get the last error message that was set. This just delegates to Connection::error(). Query has nothing extra to say, so use either, as makes sense in your program. |
|
Return a SQL-escaped version of the given character buffer.
|
|
Return a SQL-escaped version of a character buffer.
There's a degenerate fourth mode, where ps is zero: simply returns 0, because there is nowhere to store the result. Note that if original is 0, we always ignore the length parameter even if it is nonzero. Length always comes from ps->length() in this case. ps is a pointer because if it were a reference, the other overload would be impossible to call: the compiler would complain that the two overloads are ambiguous because std::string has a char* conversion ctor. A nice bonus is that pointer syntax makes it clearer that the first parameter is an "out" parameter.
|
|
Execute a query.
Same as execute(), except that it only returns a flag indicating whether the query succeeded or not. It is basically a thin wrapper around the C API function
|
|
Execute a built-up query. Same as exec(), except that it uses the query string built up within the query object already instead of accepting a query string from the caller.
|
|
Execute query in a known-length string of characters. This can include null characters. Executes the query immediately, and returns the results. |
|
Execute a query that returns no rows.
To support template queries, there many more overloads of this type (25 total, by default; see |
|
Execute template query using given parameters. This method should only be used by code that doesn't know, at compile time, how many parameters it will have. This is useful within the library, and also for code that builds template queries dynamically, at run time.
|
|
Execute built-up query. Use one of the execute() overloads if you don't expect the server to return a result set. For instance, a DELETE query. The returned SimpleResult object contains status information from the server, such as whether the query succeeded, and if so how many rows were affected. This overloaded version of execute() simply executes the query that you have built up in the object in some way. (For instance, via the insert() method, or by using the object's stream interface.)
|
|
Run a functor for every row in a table. Just like for_each(Function), except that it builds a "select * from TABLE" query using the SQL table name from the SSQLS instance you pass.
|
|
Execute the query, and call a functor for each returned row. Just like for_each(const SQLTypeAdapter&, Function), but it uses the query string held by the Query object already
|
|
Execute a query, and call a functor for each returned row. This method wraps a use() query, calling the given functor for every returned row. It is analogous to STL's for_each() algorithm, but instead of iterating over some range within a container, it iterates over a result set produced by a query.
|
|
Insert multiple new rows. Builds an INSERT SQL query using items from a range within an STL container. Insert the entire contents of the container by using the begin() and end() iterators of the container as parameters to this function.
|
|
Insert a new row. This function builds an INSERT SQL query. One uses it with MySQL++'s Specialized SQL Structures mechanism.
|
|
Get ID generated for an AUTO_INCREMENT column in the previous INSERT query.
|
|
Return whether more results are waiting for a multi-query or stored procedure response. If this function returns true, you must call store_next() to fetch the next result set before you can execute more queries. Wraps mysql_more_results() in the MySQL C API. That function only exists in MySQL v4.1 and higher. Therefore, this function always returns false when built against older API libraries.
|
|
Test whether the object has experienced an error condition. Allows for code constructs like this:
Query q = conn.query(); .... use query object if (q) { ... no problems in using query object } else { ... an error has occurred } This method returns false if either the Query object or its associated Connection object has seen an error condition since the last operation. |
|
Assign another query's state to this object. The same caveats apply to this operator as apply to the copy ctor. |
|
Treat the contents of the query string as a template query. This method sets up the internal structures used by all of the other members that accept template query parameters. See the "Template Queries" chapter in the user manual for more information. |
|
Insert new row unless there is an existing row that matches on a unique index, in which case we replace it. This function builds a REPLACE SQL query. One uses it with MySQL++'s Specialized SQL Structures mechanism.
|
|
Reset the query object so that it can be reused. As of v3.0, Query objects auto-reset upon query execution unless you've set it up for making template queries. (It can't auto-reset in that situation, because it would forget the template info.) Therefore, the only time you must call this is if you have a Query object set up for making template queries, then want to build queries using one of the other methods. (Static strings, SSQLS, or the stream interface.) |
|
Execute a query that can return rows, returning all of the rows in a random-access container. This overload is for situations where you have the query in a C string and have its length already. If you want to execute a query in a null-terminated C string or have the query string in some other form, you probably want to call store(const SQLTypeAdapter&) instead. SQLTypeAdapter converts from plain C strings and other useful data types implicitly. |
|
Execute a query that can return rows, returning all of the rows in a random-access container.
To support template queries, there many more overloads of this type (25 total, by default; see |
|
Store results from a template query using given parameters. This method should only be used by code that doesn't know, at compile time, how many parameters it will have. This is useful within the library, and also for code that builds template queries dynamically, at run time.
|
|
Execute a query that can return a result set. Use one of the store() overloads to execute a query and retrieve the entire result set into memory. This is useful if you actually need all of the records at once, but if not, consider using one of the use() methods instead, which returns the results one at a time, so they don't allocate as much memory as store().
You must use store(), storein() or use() for
The name of this method comes from the MySQL C API function it is implemented in terms of, This function has the same set of overloads as execute().
|
|
Execute the query, conditionally storing each row in a container. Just like store_if(Sequence&, const SQLTypeAdapter&, Function), but it uses the query string held by the Query object already
|
|
Pulls every row in a table, conditionally storing each one in a container. Just like store_if(Sequence&, const SQLTypeAdapter&, Function), but it uses the SSQLS instance to construct a "select * from TABLE" query, using the table name field in the SSQLS.
|
|
Execute a query, conditionally storing each row in a container. This method wraps a use() query, calling the given functor for every returned row, and storing the results in the given sequence container if the functor returns true. This is analogous to the STL copy_if() algorithm, except that the source rows come from a database query instead of another container. (copy_if() isn't a standard STL algorithm, but only due to an oversight by the standardization committee.) This fact may help you to remember the order of the parameters: the container is the destination, the query is the source, and the functor is the predicate; it's just like an STL algorithm.
|
|
Return next result set, when processing a multi-query. There are two cases where you'd use this function instead of the regular store() functions. First, when handling the result of executing multiple queries at once. (See this page in the MySQL documentation for details.) Second, when calling a stored procedure, MySQL can return the result as a set of results. In either case, you must consume all results before making another MySQL query, even if you don't care about the remaining results or result sets. As the MySQL documentation points out, you must set the MYSQL_OPTION_MULTI_STATEMENTS_ON flag on the connection in order to use this feature. See Connection::set_option(). Multi-queries only exist in MySQL v4.1 and higher. Therefore, this function just wraps store() when built against older API libraries.
|
|
Execute a query, and store the entire result set in an STL container.
This is a set of specialized template functions that call either storein_sequence() or storein_set(), depending on the type of container you pass it. It understands Like the functions it wraps, this is actually an overloaded set of functions. See the other functions' documentation for details. Use this function if you think you might someday switch your program from using a set-associative container to a sequence container for storing result sets, or vice versa. See exec(), execute(), store(), and use() for alternative query execution mechanisms. |
|
Execute template query using given parameters, storing the results in a sequence type container. This method should only be used by code that doesn't know, at compile time, how many parameters it will have. This is useful within the library, and also for code that builds template queries dynamically, at run time.
|
|
Executes a query, storing the result rows in an STL sequence container.
lib/querydef .pl), each taking one more SQLTypeAdapter object than the previous one. See the template query overview above for more about this topic. |
|
Execute a query, storing the result set in an STL sequence container. This function works much like store() from the caller's perspective, because it returns the entire result set at once. It's actually implemented in terms of use(), however, so that memory for the result set doesn't need to be allocated twice. There are many overloads for this function, pretty much the same as for execute(), except that there is a Container parameter at the front of the list. So, you can pass a container and a query string, or a container and template query parameters.
|
|
Execute template query using given parameters, storing the results in a set type container. This method should only be used by code that doesn't know, at compile time, how many parameters it will have. This is useful within the library, and also for code that builds template queries dynamically, at run time.
|
|
Executes a query, storing the result rows in an STL set-associative container.
lib/querydef .pl), each taking one more SQLTypeAdapter object than the previous one. See the template query overview above for more about this topic. |
|
Execute a query, storing the result set in an STL associative container.
The same thing as storein_sequence(), except that it's used with associative STL containers, such as |
|
Get built query as a null-terminated C++ string.
|
|
Get built query as a C++ string with template query parameter substitution.
lib/querydef .pl), each taking one more SQLTypeAdapter object than the previous one. See the template query overview above for more about this topic. |
|
Replace an existing row's data with new data. This function builds an UPDATE SQL query using the new row data for the SET clause, and the old row data for the WHERE clause. One uses it with MySQL++'s Specialized SQL Structures mechanism.
|
|
Execute a query that can return rows, with access to the rows in sequence. This overload is for situations where you have the query in a C string and have its length already. If you want to execute a query in a null-terminated C string or have the query string in some other form, you probably want to call use(const SQLTypeAdapter&) instead. SQLTypeAdapter converts from plain C strings and other useful data types implicitly. |
|
Execute a query that can return rows, with access to the rows in sequence.
To support template queries, there many more overloads of this type (25 total, by default; see |
|
Execute a template query that can return rows, with access to the rows in sequence. This method should only be used by code that doesn't know, at compile time, how many parameters it will have. This is useful within the library, and also for code that builds template queries dynamically, at run time.
|
|
Execute a query that can return rows, with access to the rows in sequence. Use one of the use() overloads if memory efficiency is important. They return an object that can walk through the result records one by one, without fetching the entire result set from the server. This is superior to store() when there are a large number of results; store() would have to allocate a large block of memory to hold all those records, which could cause problems. A potential downside of this method is that MySQL database resources are tied up until the result set is completely consumed. Do your best to walk through the result set as expeditiously as possible.
The name of this method comes from the MySQL C API function that initiates the retrieval process, This function has the same set of overloads as execute().
|
|
The default template parameters. Used for filling in parameterized queries. |