Heart 1.3.842.34c2ab5
Heart is base back end library for your c++ Qt projects.
setsinglevalue.cpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-2024 QuasarApp.
3 * Distributed under the lgplv3 software license, see the accompanying
4 * Everyone is permitted to copy and distribute verbatim copies
5 * of this license document, but changing it is not allowed.
6*/
7
8#include "setsinglevalue.h"
9#include "params.h"
10#include <QSqlError>
11#include <QSqlQuery>
12
13namespace QH {
14
15
16namespace PKG {
17
18
20 const QString& field,
21 const QVariant& value,
22 const QString &primaryKey) {
23 _primaryValue = address.id();
24 _table = address.table();
25 _field = field;
26 _value = value;
27 _primaryKey = primaryKey;
28}
29
31 return create<SetSingleValue>(dbAddress(), _field, _value);
32}
33
35 QString queryString = "UPDATE %0 SET %1=:%1 WHERE %2=:%2";
36
37 queryString = queryString.arg(table(), _field, primaryKey());
38
39 if (!q.prepare(queryString)) {
40
41 QuasarAppUtils::Params::log("Failed to prepare query: " + q.lastError().text(),
42 QuasarAppUtils::Error);
44 }
45
46 q.bindValue(":" + _field, _value);
47 q.bindValue(":" + _primaryKey, _primaryValue);
48
50}
51
52PrepareResult SetSingleValue::prepareInsertQuery(QSqlQuery &q, bool replace) const {
53
54 QString queryString = (replace)?
55 "REPLACE INTO %0 (%1, %2) VALUES (:%1, :%2)":
56 "INSERT INTO %0 (%1, %2) VALUES (:%1, :%2)";
57
58 queryString = queryString.arg(table(), primaryKey(), _field);
59
60 if (!q.prepare(queryString)) {
61
62 QuasarAppUtils::Params::log("Failed to prepare query: " + q.lastError().text(),
63 QuasarAppUtils::Error);
65 }
66
67 q.bindValue(":" + _primaryKey, _primaryValue);
68 q.bindValue(":" + _field, _value);
69
71}
72
73bool SetSingleValue::fromSqlRecord(const QSqlRecord &) {
74 return true;
75}
76
78 return false;
79}
80
81QString SetSingleValue::table() const {
82 return _table;
83}
84
86 return _primaryKey;
87}
88
90 return _primaryValue;
91}
92}
93}
The DbAddress class use to work with database addresses. Database Address it is structure with 2 valu...
Definition dbaddress.h:24
const QString & table() const
table This method return table name.
Definition dbaddress.cpp:58
const QVariant & id() const
id This method return id of object in table.
Definition dbaddress.cpp:67
The DBObject class- main class for work with data base.
Definition dbobject.h:94
DbAddress dbAddress() const
dbAddress This method return address of the database object. IF the object is not valid then this met...
Definition dbobject.cpp:242
QVariant primaryValue() const override
primaryValue This method is wraper of DBAddress::id. If This object do not contains a id value then r...
QString table() const override
table This method should be return name of the database table that should be contains objects with th...
DBObject * createDBObject() const override
createDBObject This method should be create a object with the some type as the object called this met...
bool isCached() const override
isCached This method sholud be return status of object. If this method return true then this object c...
SetSingleValue(const DbAddress &address, const QString &field, const QVariant &value, const QString &primaryKey="id")
SetSingleValue This is default constructor of the update query generator.
bool fromSqlRecord(const QSqlRecord &q) override
fromSqlRecord This method should be initialize this object from the executed sqlRecord....
QString primaryKey() const override
primaryKey This method must be return the name of primary key of this object table....
PrepareResult prepareUpdateQuery(QSqlQuery &q) const override
prepareUpdateQuery this method should be prepare a insert data query.
PrepareResult prepareInsertQuery(QSqlQuery &q, bool replace) const override
prepareInsertQuery This method should be prepare a query for insert object into database....
PrepareResult
The PrepareResult enum is result of work prepare sql query of dbobjects.
Definition dbobject.h:24
@ Success
prepare finished successful.
@ Fail
prepare is failed.
The QH namespace - QuasarApp Heart namespace. This namespace contains all classes of the Heart librar...
Definition heart.cpp:13