tntdb 1.4
tntdb::SqlBuilder Class Reference

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.
 
SqlBuilderextendParam (const std::string &varname, unsigned count)
 Replace a sqlbuilder placeholder with a list of sql placeholders with index.
 
SqlBuilderreplace (const std::string &varname, const std::string &value)
 Replace a placeholder with a value.
 
SqlBuilderreplaceIf (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.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ SqlBuilder() [1/2]

tntdb::SqlBuilder::SqlBuilder ( )
inline

The class is default constructable.

◆ SqlBuilder() [2/2]

tntdb::SqlBuilder::SqlBuilder ( const std::string & sql_)
inline

Create a sql builder with a string with optional placeholders.

Member Function Documentation

◆ extendParam()

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:

std::set<int> myValues;
myValues.insert(2);
myValues.insert(3);
myValues.insert(6);
tntdb::Statement st = conn.prepare(
"select foo from bar where baz in (%set)")
.extendParam("set", myValues.size())
);
st.set("set", s);
// => select foo from bar where baz in (:set1,:set2,:set3)
Helper class for building sql statements.
Definition sqlbuilder.h:58
SqlBuilder & extendParam(const std::string &varname, unsigned count)
Replace a sqlbuilder placeholder with a list of sql placeholders with index.
This class represents an SQL statement.
Definition statement.h:73
Statement & set(const std::string &col, const T &data)
Set the host variable with the given name to the passed value.
Definition statement.h:492

This generates a sql statement with 3 sql placeholders and fills those with values from a std::set.

◆ operator const std::string &()

tntdb::SqlBuilder::operator const std::string & ( ) const
inline

returns the resulting sql string implicitly.

◆ replace()

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:

std::string myTablename = "bar";
tntdb::Statement st = conn.prepare(
"select foo from %tab")
.replace("tab", myTablename)
);
// => select foo from bar
SqlBuilder & replace(const std::string &varname, const std::string &value)
Replace a placeholder with a value.

◆ replaceIf()

SqlBuilder & tntdb::SqlBuilder::replaceIf ( bool condition,
const std::string & varname,
const std::string & value,
const std::string & elsevalue = std::string() )
inline

Replace a placeholder conditionally with a value.

This adds a condition to the replace method. This helps when optional conditions are needed.

std::string searchString = "";
tntdb::Statement st1 = conn.prepare(
"select foo from mytable where bar = 17 %cond"
.replaceIf(!searchString.empty(), "cond", "and bar like :s")
);
// => select foo from mytable where bar = 17
searchString = "foo%";
tntdb::Statement st2 = conn.prepare(
"select foo from mytable where bar = 17 %cond"
.replaceIf(!searchString.empty(), "cond", "and bar like :s")
);
// => select foo from mytable where bar = 17 and bar like :s
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.
Definition sqlbuilder.h:144

◆ str() [1/2]

const std::string & tntdb::SqlBuilder::str ( ) const
inline

returns the resulting sql string.

◆ str() [2/2]

void tntdb::SqlBuilder::str ( const std::string & s)
inline

sets the underlying string to the passed value


The documentation for this class was generated from the following file: