tntdb 1.4
|
Helper class for building sql statements. More...
#include <sqlbuilder.h>
Public Member Functions | |
SqlBuilder () | |
The class is default constructable. | |
SqlBuilder (const std::string &sql_) | |
Create a sql builder with a string with optional placeholders. | |
SqlBuilder & | extendParam (const std::string &varname, unsigned count) |
Replace a sqlbuilder placeholder with a list of sql placeholders with index. | |
SqlBuilder & | replace (const std::string &varname, const std::string &value) |
Replace a placeholder with a value. | |
SqlBuilder & | replaceIf (bool condition, const std::string &varname, const std::string &value, const std::string &elsevalue=std::string()) |
Replace a placeholder conditionally with a value. | |
void | str (const std::string &s) |
sets the underlying string to the passed value | |
const std::string & | str () const |
returns the resulting sql string. | |
operator const std::string & () const | |
returns the resulting sql string implicitly. | |
Helper class for building sql statements.
Sql statments take placeholders, which can be filled with values for execution. Those placeholders are limited to take scalar values and only where scalar values are expected in the sql statement.
Sometimes the user needs to pass sets of values. Since it is not directly supported by sql the user has to build a list of placeholders into the sql statement. If the number of needed placeholders is not known at compile time, a sql statement needs to be created dynamically.
Anoter problem is, that placeholders are not allowed everywhere in the sql statment. To parametrise e.g. the table name, again the user has to replace the table name before passing the sql to the database.
This class has helpers for both cases.
Placeholders here start with a percent character followed by alphanumeric characters.
|
inline |
The class is default constructable.
|
inline |
Create a sql builder with a string with optional placeholders.
SqlBuilder & tntdb::SqlBuilder::extendParam | ( | const std::string & | varname, |
unsigned | count ) |
Replace a sqlbuilder placeholder with a list of sql placeholders with index.
The varname specifies the placeholder and the count the number of generated placeholders.
example:
This generates a sql statement with 3 sql placeholders and fills those with values from a std::set.
|
inline |
returns the resulting sql string implicitly.
SqlBuilder & tntdb::SqlBuilder::replace | ( | const std::string & | varname, |
const std::string & | value ) |
Replace a placeholder with a value.
This method replaces a sqlbuilder placeholder with a value. Note that no escaping is done so that passing user input may result in sql injection.
example:
|
inline |
Replace a placeholder conditionally with a value.
This adds a condition to the replace method. This helps when optional conditions are needed.
|
inline |
returns the resulting sql string.
|
inline |
sets the underlying string to the passed value