Warning, /sdk/codevis/thirdparty/soci/docs/queries.md is written in an unsupported language. File is not indexed.
0001 # Queries
0002
0003 ## Simple SQL statements
0004
0005 In many cases, the SQL query is intended to be executed only once, which means that statement parsing and execution can go together. The `session` class provides a special `once` member, which triggers parsing and execution of such one-time statements:
0006
0007 ```cpp
0008 sql.once << "drop table persons";
0009 ```
0010
0011 For shorter syntax, the following form is also allowed:
0012
0013 ```cpp
0014 sql << "drop table persons";
0015 ```
0016
0017 The IOStream-like interface is exactly what it looks like, so that the statement text can be composed of many parts, involving anything that is *streamable* (including custom classes, if they have appropriate `operator<<`):
0018
0019 ```cpp
0020 string tableName = "persons";
0021 sql << "drop table " << tableName;
0022
0023 int id = 123;
0024 sql << "delete from companies where id = " << id;
0025 ```
0026
0027 ## Query transformation
0028
0029 In SOCI 3.2.0, query transformation mechanism was introduced.
0030
0031 Query transformation is specified as user-defined unary function or callable function object with input parameter of type `std::string` which returns object of type `std::string` as well.
0032
0033 The query transformation function is registered for current database session using dedicated `session::set_query_transformation` method. Then, the transformation function is called with query string as argument just before the query is sent to database backend for execution or for preparation.
0034
0035 For one-time statements, query transformation is performed before each execution of statement. For prepared statements, query is transformed only once, before preparation, regardless how many times it is executed.
0036
0037 A few short examples how to use query transformation:
0038
0039 * defined as free function:
0040
0041 ```cpp
0042 std::string less_than_ten(std::string query)
0043 {
0044 return query + " WHERE price < 10";
0045 }
0046
0047 session sql(postgresql, "dbname=mydb");
0048 sql.set_query_transformation(less_than_ten);
0049 sql << "DELETE FROM item";
0050 ```
0051
0052 * defined as function object:
0053
0054 ```cpp
0055 struct order
0056 {
0057 order(std::string const& by) : by_(by) {}
0058
0059 std::string operator()(std::string const& query) const
0060 {
0061 return query + " ORDER BY " + by_;
0062 }
0063
0064 std::string by_;
0065 };
0066
0067 char const* query = "SELECT * FROM product";
0068 sql.set_query_transformation(order("price"));
0069 sql << query;
0070 sql.set_query_transformation(order("id"));
0071 sql << query;
0072 ```
0073
0074 * defined as lambda function (since C++11):
0075
0076 ```cpp
0077 std::string dep = "sales";
0078 sql.set_query_transformation(
0079 [&dep](std::string const& query) {
0080 return query + " WHERE department = '" + dep + "'";
0081 });
0082 sql << "SELECT * FROM employee";
0083 ```
0084
0085 Query transformations enable users with simple mechanism to apply extra requirements to or interact with SQL statement being executed and that is without changing the SQL statement itself which may be passed from different
0086 parts of application.
0087
0088 For example, the query transformation may be used to:
0089
0090 * modify or add clauses of SQL statements (i.e. `WHERE` clause with new condition)
0091 * prefix table names with new schema to allow namespaces switch
0092 * validate SQL statements
0093 * perform sanitization checking for any unverified input
0094 * apply database-specific features like add optimization hints to SQL statements (i.e. `SELECT /*+RULE*/ A FROM C` in Oracle 9)