tntdb::SqlBuilder Class Reference

Helper class for building sql statements. More...

#include <tntdb/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

tntdb::SqlBuilder::SqlBuilder ( )
inline

The class is default constructable.

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

Create a sql builder with a string with optional placeholders.

Member Function Documentation

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);
"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)

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

tntdb::SqlBuilder::operator const std::string & ( ) const
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:

std::string myTablename = "bar";
"select foo from %tab")
.replace("tab", myTablename)
);
// => select foo from bar
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 = "";
"select foo from mytable where bar = 17 %cond"
.replaceIf(!searchString.empty(), "s", "and bar like %s")
);
// => select foo from mytable where bar = 17
searchString = "foo%";
"select foo from mytable where bar = 17 %cond"
.replaceIf(!searchString.empty(), "s", "and bar like :s")
);
// => select foo from mytable where bar = 17 and bar like :s
void tntdb::SqlBuilder::str ( const std::string &  s)
inline

sets the underlying string to the passed value

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

returns the resulting sql string.


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