EasyQtSql
Easy SQL data access helper for QtSql
UpdateQuery Class Reference

QSqlQuery wrapper for UPDATE ... SET ... WHERE ... query execution. More...

#include <EasyQtSql_UpdateQuery.h>

Public Member Functions

 UpdateQuery (const QString &table, const QSqlDatabase &db)
 
UpdateQueryset (const QString &field, const QVariant &value)
 Sets table field to value More...
 
UpdateQueryset (const QVariantMap &map)
 Sets table fields to values from map More...
 
NonQueryResult where (const QString &expr)
 Executes update query with expr condition. More...
 
NonQueryResult where (const QString &expr, const QVariant &last)
 Executes update query with expr condition and last parameter binding. More...
 
template<typename... Rest>
NonQueryResult where (const QString &expr, const QVariant &first, const Rest &... rest)
 
NonQueryResult exec ()
 Executes UPDATE query without conditions. More...
 

Detailed Description

QSqlQuery wrapper for UPDATE ... SET ... WHERE ... query execution.

Constructor & Destructor Documentation

◆ UpdateQuery()

UpdateQuery::UpdateQuery ( const QString &  table,
const QSqlDatabase &  db 
)
inline

Member Function Documentation

◆ exec()

NonQueryResult UpdateQuery::exec ( )
inline

Executes UPDATE query without conditions.

◆ set() [1/2]

UpdateQuery& UpdateQuery::set ( const QString &  field,
const QVariant &  value 
)
inline

Sets table field to value

Parameters
fieldTable field name
valueValue to set
Returns
UpdateQuery reference

The method returns an UpdateQuery reference so its call can be chained:

//UPDATE table SET a=111, b=222
t.update("table")
.set("a", 111) //method chaining
.set("b", 222)
.exec();

◆ set() [2/2]

UpdateQuery& UpdateQuery::set ( const QVariantMap &  map)
inline

Sets table fields to values from map

Parameters
mapValues map. Map key is field name, map value is field value.

The method returns an UpdateQuery reference so its calls can be chained.

//UPDATE table SET a=111, b=222
t.update("table")
.set(QVariantMap{ {"a", 111}, {"b", 222} })
.exec();

◆ where() [1/3]

NonQueryResult UpdateQuery::where ( const QString &  expr)
inline

Executes update query with expr condition.

Parameters
exprWHERE condition
//UPDATE table SET a=111, b=222 WHERE a=1 AND b=2
t.update("table")
.set(QVariantMap{ {"a", 111}, {"b", 222} })
.where("a=1 AND b=2");

◆ where() [2/3]

NonQueryResult UpdateQuery::where ( const QString &  expr,
const QVariant &  last 
)
inline

Executes update query with expr condition and last parameter binding.

Parameters
exprWHERE condition
lastParameter to bind on WHERE expression

The method supports variable count of QVariant parameters. Parameters are bound with QSqlQuery::addBindValue.

//UPDATE table SET a=111, b=222 WHERE a=1 AND b=2
t.update("table")
.set(QVariantMap{ {"a", 111}, {"b", 222} })
.where("a=? AND b=?", 1, 2);

◆ where() [3/3]

template<typename... Rest>
NonQueryResult UpdateQuery::where ( const QString &  expr,
const QVariant &  first,
const Rest &...  rest 
)
inline

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