matador::query< T > Class Template Reference

Creates a SQL query. More...

#include <query.hpp>

Inherits matador::detail::basic_query.

Public Member Functions

 query ()=default
 Create a new query.
 
querycreate (const std::string &table_name)
 
querycreate (const std::string &table_name, T obj)
 
querycreate (const std::string &table_name, std::initializer_list< std::shared_ptr< column > > column_list)
 Create a table with given name.
 
querydrop (const std::string &table_name)
 
template<class LocalType = T, typename = typename std::enable_if<!std::is_same<LocalType, row>::value>::type>
queryinsert (const std::string &table_name)
 
template<class LocalType = T, typename = typename std::enable_if<!std::is_same<LocalType, row>::value>::type>
queryinsert (const std::string &table_name, T &obj)
 
template<class LocalType = T, typename = typename std::enable_if<std::is_same<LocalType, row>::value>::type>
queryinsert (const std::string &table_name, const std::initializer_list< std::string > &column_names)
 Create an insert statement for given columns.
 
template<class LocalType = T, typename = typename std::enable_if<std::is_same<LocalType, row>::value>::type>
queryinsert (const std::string &table_name, const std::vector< std::string > &column_names)
 Create an insert statement for given columns.
 
template<class LocalType = T, typename = typename std::enable_if<std::is_same<LocalType, row>::value>::type>
queryvalues (const std::initializer_list< matador::any > &values)
 
template<class LocalType = T, typename = typename std::enable_if<!std::is_same<LocalType, row>::value>::type>
queryselect ()
 
queryselect (const std::initializer_list< std::string > &column_names)
 
queryselect (const std::vector< std::string > &column_names)
 
queryselect (matador::columns cols)
 Creates a select statement with the given columns.
 
template<class Type >
void prepare_select_prototype (Type &, const std::shared_ptr< columns > &)
 
void prepare_select_prototype (row &obj, const std::shared_ptr< columns > &cols)
 
template<class LocalType = T, typename = typename std::enable_if<!std::is_same<LocalType, row>::value>::type>
queryupdate (const std::string &table_name)
 
template<class LocalType = T, typename = typename std::enable_if<!std::is_same<LocalType, row>::value>::type>
queryupdate (const std::string &table_name, T &obj)
 
queryupdate (const std::string &table_name, const std::initializer_list< std::pair< std::string, matador::any > > &column_values)
 
queryfrom (const std::string &table_name)
 Specifies the from token of a query.
 
queryfrom (detail::basic_query &q)
 Specifies the from token of a query.
 
queryremove (const std::string &table_name)
 
template<class COND >
querywhere (const COND &c)
 
queryorder_by (const std::string &col)
 
queryasc ()
 
querydesc ()
 
querylimit (std::size_t l)
 
queryas (const std::string &alias)
 
querygroup_by (const std::string &col)
 
querycolumn (const std::string &column, data_type type, size_t index)
 
queryreset (t_query_command query_command)
 Resets the query.
 
result< T > execute (connection &conn)
 
statement< T > prepare (connection &conn)
 
- Public Member Functions inherited from matador::detail::basic_query
 basic_query ()
 
void reset_query (t_query_command query_command)
 
std::string str (bool prepared)
 
std::string str (connection &conn, bool prepared)
 
const sql & stmt () const
 
std::string tablename () const
 

Detailed Description

template<class T = row>
class matador::query< T >

Creates a SQL query.

The query class represents a SQL query for any database provided by the backend driver libraries.

With this class at hand you can create

  • create
  • drop
  • select
  • insert
  • update
  • delete

statements. These statements can be created manually by adding each part of the statement by hand or you can create them using serializable based class or instances.

The class uses a chain mechanism. Each method representing a part of a query returns the reference to itself. So you can concatenate your query parts by calling the methods in a chain (concatenated by dots).

Constructor & Destructor Documentation

◆ query()

template<class T = row>
matador::query< T >::query ( )
default

Create a new query.

Create a new query with unset default table name and unset default internal connection

Member Function Documentation

◆ as()

template<class T = row>
query & matador::query< T >::as ( const std::string &  alias)
inline

Specify an alias for the selection or the a table name.

Parameters
aliasAlias name
Returns
Query object reference

◆ asc()

template<class T = row>
query & matador::query< T >::asc ( )
inline

Order result of select query ascending.

Returns
A reference to the query.

◆ column()

template<class T = row>
query & matador::query< T >::column ( const std::string &  column,
data_type  type,
size_t  index 
)
inline

Adds a column to a select statement.

Parameters
columnThe name of the column.
typeThe datatype of the column.
indexThe index of the column.
Returns
A reference to the query.

◆ create() [1/3]

template<class T = row>
query & matador::query< T >::create ( const std::string &  table_name)
inline

Creates a create statement.

Returns
A reference to the query.

◆ create() [2/3]

template<class T = row>
query & matador::query< T >::create ( const std::string &  table_name,
std::initializer_list< std::shared_ptr< column > >  column_list 
)
inline

Create a table with given name.

Parameters
table_nameThe table name to be used for the statement
column_listThe columns to be created
Returns
A reference to the query.

◆ create() [3/3]

template<class T = row>
query & matador::query< T >::create ( const std::string &  table_name,
obj 
)
inline

Creates a create statement based on a given object pointer

Parameters
objThe serializable providing the column information.
Returns
A reference to the query.

◆ desc()

template<class T = row>
query & matador::query< T >::desc ( )
inline

Order result of select query descending.

Returns
A reference to the query.

◆ drop()

template<class T = row>
query & matador::query< T >::drop ( const std::string &  table_name)
inline

Creates a drop statement.

Returns
A reference to the query.

◆ execute()

template<class T = row>
result< T > matador::query< T >::execute ( connection conn)
inline

Executes the current query and returns a new result serializable.

Returns
The result serializable.

◆ from() [1/2]

template<class T = row>
query & matador::query< T >::from ( const std::string &  table_name)
inline

Specifies the from token of a query.

Specifies the from token of a query with a table name as argument.

Parameters
tableThe name of the table
Returns
A reference to the query.

◆ from() [2/2]

template<class T = row>
query & matador::query< T >::from ( detail::basic_query< T > &  q)
inline

Specifies the from token of a query.

Specifies the from token of a query with a sub query as argument.

Parameters
qThe sub query
Returns
A reference to the query.

◆ group_by()

template<class T = row>
query & matador::query< T >::group_by ( const std::string &  col)
inline

Adds a group by clause to a select statement.

Parameters
colThe group by clause.
Returns
A reference to the query.

◆ insert() [1/4]

template<class T = row>
template<class LocalType = T, typename = typename std::enable_if<!std::is_same<LocalType, row>::value>::type>
query & matador::query< T >::insert ( const std::string &  table_name)
inline

Creates an insert statement

Returns
A reference to the query.

◆ insert() [2/4]

template<class T = row>
template<class LocalType = T, typename = typename std::enable_if<std::is_same<LocalType, row>::value>::type>
query & matador::query< T >::insert ( const std::string &  table_name,
const std::initializer_list< std::string > &  column_names 
)
inline

Create an insert statement for given columns.

Parameters
column_namesList of column to insert
Returns
A reference to the query.

◆ insert() [3/4]

template<class T = row>
template<class LocalType = T, typename = typename std::enable_if<std::is_same<LocalType, row>::value>::type>
query & matador::query< T >::insert ( const std::string &  table_name,
const std::vector< std::string > &  column_names 
)
inline

Create an insert statement for given columns.

Parameters
column_namesList of column to insert
Returns
A reference to the query.

◆ insert() [4/4]

template<class T = row>
template<class LocalType = T, typename = typename std::enable_if<!std::is_same<LocalType, row>::value>::type>
query & matador::query< T >::insert ( const std::string &  table_name,
T &  obj 
)
inline

Creates an insert statement based on the given object.

Parameters
objThe serializable used for the insert statement.
Returns
A reference to the query.

◆ limit()

template<class T = row>
query & matador::query< T >::limit ( std::size_t  l)
inline

Adds a limit clause to a select statement.

Parameters
lThe limit clause.
Returns
A reference to the query.

◆ order_by()

template<class T = row>
query & matador::query< T >::order_by ( const std::string &  col)
inline

Adds an order by clause to a select statement.

Parameters
colThe column for the order by clause
Returns
A reference to the query.

◆ prepare()

template<class T = row>
statement< T > matador::query< T >::prepare ( connection conn)
inline

Creates and returns a prepared statement based on the current query.

Parameters
connThe connection.
Returns
The new prepared statement.

◆ remove()

template<class T = row>
query & matador::query< T >::remove ( const std::string &  table_name)
inline

Creates a delete statement.

Returns
A reference to the query.

◆ reset()

template<class T = row>
query & matador::query< T >::reset ( t_query_command  query_command)
inline

Resets the query.

Parameters
query_commandThe query command to which the query is reset
Returns
A reference to the query.

◆ select() [1/4]

template<class T = row>
template<class LocalType = T, typename = typename std::enable_if<!std::is_same<LocalType, row>::value>::type>
query & matador::query< T >::select ( )
inline

Creates a select statement.

Returns
A reference to the query.

◆ select() [2/4]

template<class T = row>
query & matador::query< T >::select ( const std::initializer_list< std::string > &  column_names)
inline

Creates a select statement for the given columns.

Parameters
column_namesA list of column names to select
Returns
A reference to the query.

◆ select() [3/4]

template<class T = row>
query & matador::query< T >::select ( const std::vector< std::string > &  column_names)
inline

Appends all columns from vector to a select statement.

Parameters
column_namesThe column list to select
Returns
A reference to the query.

◆ select() [4/4]

template<class T = row>
query & matador::query< T >::select ( matador::columns  cols)
inline

Creates a select statement with the given columns.

Parameters
colsThe columns to select
Returns
A reference to the query.

◆ update() [1/3]

template<class T = row>
template<class LocalType = T, typename = typename std::enable_if<!std::is_same<LocalType, row>::value>::type>
query & matador::query< T >::update ( const std::string &  table_name)
inline

Creates an update statement without any settings. Sets for all object attributes column and values of the internal object. To call this methods makes only sense if the query will be prepared afterwards.

Returns
A reference to the query.

◆ update() [2/3]

template<class T = row>
query & matador::query< T >::update ( const std::string &  table_name,
const std::initializer_list< std::pair< std::string, matador::any > > &  column_values 
)
inline

Creates an update statement without any settings. Sets for all column value pairs attributes column and values.

Parameters
column_valuesThe column name and value list
Returns
A reference to the query.

◆ update() [3/3]

template<class T = row>
template<class LocalType = T, typename = typename std::enable_if<!std::is_same<LocalType, row>::value>::type>
query & matador::query< T >::update ( const std::string &  table_name,
T &  obj 
)
inline

Creates an update statement without any settings. Sets for all object attributes column and values.

Parameters
objThe object to be updated.
Returns
A reference to the query.

◆ values()

template<class T = row>
template<class LocalType = T, typename = typename std::enable_if<std::is_same<LocalType, row>::value>::type>
query & matador::query< T >::values ( const std::initializer_list< matador::any > &  values)
inline

Initializes the values of an insert statement from the given initializer_list. The list consists of any objects representing the values.

Parameters
valuesThe values to set.
Returns
A reference to the query.

◆ where()

template<class T = row>
template<class COND >
query & matador::query< T >::where ( const COND c)
inline

Adds a where clause condition to the select or update statement. For any other query an exception is thrown.

Parameters
cThe condition.
Returns
A reference to the query.

The documentation for this class was generated from the following file:
  • matador/sql/query.hpp