Convenience class to help composing select, insert and update statements.
More...
#include <bpssqlvalues.h>
Convenience class to help composing select, insert and update statements.
Empty strings are handled like NULL in context of inserts and updates. In select conditions the database column is compared to both, NULL or '', if the string value is empty. This special handling is necessary because for Oracle NULL and '' are the same, however for PostgreSQL they are not.
The special type BpsTypeLocaltimestamp may be used for localtimestamp in the query.
q.prepare(
);
q.prepare(
bStr(
"update t_table set %1 where c_key=?").arg(vals.
pairs())
);
q.execute(vals.
values() << 456);
q.prepare(
bStr(
"insert into t_table (%1) values (%2)")
);
#define bStr(aStr)
Encapsulation for string literals.
Definition: bpsglobals.h:125
Convenience class to help composing select, insert and update statements.
Definition: bpssqlvalues.h:54
QVariantList values() const
QString columns(const QString &aPrefix=QString()) const
QString condPairs(const QString &aSeparator, const QString &aPrefix=QString()) const
Get list of pairs as used for select conditions (depreciated).
QString pairs(const QString &aPrefix=QString()) const
Get list of pairs as used for update statements.
Dummy type representing the pseudo-value "localcaltimestamp" for SQL operations.
Definition: bpstypes.h:28
◆ Condition
Conditon pair building modes.
Enumerator |
---|
CondEqualAnd | Compare if equal and combine the pairs with AND: c_aaa=? and c_bbb=? ...
|
CondUnequalAnd | Compare if not equal and combine the pairs with AND: c_aaa<>? and c_bbb<>? ...
|
CondEqualOr | Compare if equal and combine the pairs with OR: c_aaa=? or c_bbb=? ...
|
CondUnequalOr | Compare if not equal and combine the pairs with OR: c_aaa<>? or c_bbb<>? ...
|
◆ BpsSqlValues() [1/3]
Copy constructor.
- Parameters
-
aOther | The other object to clone |
◆ BpsSqlValues() [2/3]
BpsSqlValues::BpsSqlValues |
( |
const QVariantMap & |
aMap | ) |
|
Create object from a variant map.
- Parameters
-
◆ BpsSqlValues() [3/3]
BpsSqlValues::BpsSqlValues |
( |
const QSqlRecord & |
aRec | ) |
|
Create object from a sql record.
- Parameters
-
◆ columns()
- Parameters
-
aPrefix | A prefix to put before each column name, for example "x." or "y_". |
- Returns
- All column names as comma separated list.
◆ condPairs() [1/2]
Get list of pairs as used for select conditions.
This is an enhanced version with more flexibility replaceing the depreciated old version.
Takes care of NULL values when necessary, so two NULL values compare as "equal". Also handles the quirk that an empty string is handeled as NULL by some databases.
- Parameters
-
aCondition | Condition for comparing and combining the pairs. |
aPrefix | A prefix to put before each column name, for example "x." or "y_". |
- Returns
- Pairs of "column=?", "column is null" or "(column is null or column='')" respectively as separated list.
◆ condPairs() [2/2]
Get list of pairs as used for select conditions (depreciated).
- Parameters
-
aSeparator | Separator to be used between the pairs. |
aPrefix | A prefix to put before each column name, for example "x." or "y_". |
- Returns
- Pairs of "column=?", "column is null" or "(column is null or column='')" respectively as separated list.
◆ insert() [1/2]
Inserts (merges) all key/value pairs from the other BpsSqlValues object.
- Parameters
-
◆ insert() [2/2]
void BpsSqlValues::insert |
( |
const QString & |
aKey, |
|
|
const QVariant & |
aValue |
|
) |
| |
Insert a key/value pair.
- Parameters
-
aKey | The key to insert |
aValue | The value to insert |
◆ isEmpty()
bool BpsSqlValues::isEmpty |
( |
| ) |
const |
- Returns
- True if there are no columns.
◆ keys()
- Parameters
-
aPrefix | A prefix to put before each column name, for example "x." or "y_". |
- Returns
- List of all keys (= column names).
◆ map()
QVariantMap BpsSqlValues::map |
( |
| ) |
const |
- Returns
- The internal variant map.
◆ operator QVariant()
BpsSqlValues::operator QVariant |
( |
| ) |
const |
|
inline |
◆ operator=() [1/3]
Assign other sql values to this object.
- Parameters
-
aOther | The other values to copy. |
- Returns
- Reference to the current object.
◆ operator=() [2/3]
Assign values of a sql record to this object.
- Parameters
-
aOther | The other sql record to copy from. |
- Returns
- Reference to the current object.
◆ operator=() [3/3]
BpsSqlValues& BpsSqlValues::operator= |
( |
const QVariantMap & |
aOther | ) |
|
Assign other variant map to this object.
- Parameters
-
aOther | The other variant map to copy from. |
- Returns
- Reference to the current object.
◆ operator[]() [1/2]
Get the value associated with the key aName as a modifiable reference.
If the values contain no such item, the function inserts a default-constructed value and returns a reference to it.
- See also
- insert(), value()
- Parameters
-
aName | Name of the column to get the value for. |
- Returns
- The value associated with the key key as a modifiable reference.
◆ operator[]() [2/2]
Get the named columns value.
Same as value().
- Parameters
-
aName | Name of the column to get the value for. |
- Returns
- The value, or QVariant() in case no such column name is found.
◆ pairs()
Get list of pairs as used for update statements.
- Parameters
-
aPrefix | A prefix to put before each column name, for example "x." or "y_". |
- Returns
- Pairs of "column=?" or "column=null" respectively as comma separated list.
◆ remove()
int BpsSqlValues::remove |
( |
const QString & |
aKey | ) |
|
Removes all the items that have the key aKey from the map.
- Parameters
-
aKey | The key of the pair to remove. |
- Returns
- Number of items removed which is usually 1 but will be 0 if the key isn't in the map.
◆ setMap()
void BpsSqlValues::setMap |
( |
const QVariantMap & |
aMap | ) |
|
Set the internal map.
- Parameters
-
◆ tags()
QString BpsSqlValues::tags |
( |
| ) |
const |
- Returns
- The value placeholders or nulls as comma separated list.
◆ value()
Get the named columns value.
- Parameters
-
aName | Name of the column to get the value for. |
- Returns
- The value, or QVariant() in case no such column name is found.
◆ values()
QVariantList BpsSqlValues::values |
( |
| ) |
const |
- Returns
- List of all non-null values.
The documentation for this class was generated from the following file: