CppCrate 0.1
Public Types | Public Member Functions | List of all members
CppCrate::Query Class Reference

Public Types

enum  Type { SimpleType, ArgumentType, BulkArgumentType }
 

Public Member Functions

 Query (const std::string &sql)
 
 Query (const std::string &sql, const std::string &args)
 
 Query (const std::string &sql, const std::vector< std::string > &bulkArgs)
 
const std::string & arguments () const
 
const std::vector< std::string > & bulkArguments () const
 
bool hasArguments () const
 
bool hasBulkArguments () const
 
void setArguments (const std::string &args)
 
void setBulkArguments (const std::vector< std::string > &bulkArgs)
 
void setStatement (const std::string &sql)
 
const std::string & statement () const
 
Type type () const
 

Detailed Description

The class Query provides the possibility to conveniently define simple SQL statements, statements with parameter substitution, as well as statements for bulk operations. The actual type of a Query is accessible through type().

All three types can be defined either by using the corresponding constructor or by using the property based approach. To select all rows of a table called "players" write directly

Query q("SELECT * FROM players");

or set the appropriate properties individually

q.setStatement("SELECT * FROM players");

In order to prepare a statement with arguments (parameter substitution) use placeholders:

Query q("SELECT * FROM players WHERE age > ?", "[42]");
// or use numbered placeholders
Query q("SELECT * FROM players WHERE age > $1", "[42]");

For a bulk operation, finally, use:

Query q("INSERT INTO players (id, name) VALUES (?,?)");
std::vector<std::string> newPlayers;
newPlayers.push_back("[1, ’Calvin’]");
newPlayers.push_back("[2, ’Hobbes’]");
q.bulkArguments(newPlayers);

Member Enumeration Documentation

◆ Type

Describes the query's type.

Enumerator
SimpleType 

A simple SQL statement.

ArgumentType 

A SQL statement with arguments.

BulkArgumentType 

A bulk operation.

Constructor & Destructor Documentation

◆ Query() [1/3]

CppCrate::Query::Query ( const std::string &  sql)
explicit

Constructs a query with the SQL statement sql.

◆ Query() [2/3]

CppCrate::Query::Query ( const std::string &  sql,
const std::string &  args 
)

Constructs a query with the SQL statement sql and the parameters args.

Precondition
args must be a well-formed JSON array and should not contain a null character.

◆ Query() [3/3]

CppCrate::Query::Query ( const std::string &  sql,
const std::vector< std::string > &  bulkArgs 
)

Constructs a query with the SQL statement sql and the bulk arguments bulkArgs.

Precondition
The elements of bulkArgs must be well-formed JSON arrays and should not contain a null character.

Member Function Documentation

◆ arguments()

const std::string & CppCrate::Query::arguments ( ) const

Returns the parameters.

◆ bulkArguments()

const std::vector< std::string > & CppCrate::Query::bulkArguments ( ) const

Returns the bulk arguments.

◆ hasArguments()

bool CppCrate::Query::hasArguments ( ) const

Returns whether the query has parameters defined.

◆ hasBulkArguments()

bool CppCrate::Query::hasBulkArguments ( ) const

Returns whether the query has bulk arguments defined.

◆ setArguments()

void CppCrate::Query::setArguments ( const std::string &  args)

Sets the parameters to args.

Precondition
args must be a well-formed JSON array and should not contain a null character.
Note
The bulk arguments are implicitly cleared.

◆ setBulkArguments()

void CppCrate::Query::setBulkArguments ( const std::vector< std::string > &  bulkArgs)

Sets the bulk arguments to bulkArgs.

Precondition
The elements of bulkArgs must be well-formed JSON arrays and should not contain a null character.
Note
The simple arguments are implicitly cleared.

◆ setStatement()

void CppCrate::Query::setStatement ( const std::string &  sql)

Sets the SQL statement to sql.

◆ statement()

const std::string & CppCrate::Query::statement ( ) const

Returns the SQL statement.

◆ type()

Query::Type CppCrate::Query::type ( ) const

Returns the query's type.