Added a new `description` column to the `accounts` table in the database schema. Updated the model, DAO methods, and queries to handle the `description` field for both inserting and retrieving account data. This change ensures that account entries can now include descriptive information.
77 lines
2.7 KiB
C++
77 lines
2.7 KiB
C++
#include <QSqlQuery>
|
|
#include <QSqlError>
|
|
#include "AccountDAO.h"
|
|
|
|
#include "db/AccountInfo.h"
|
|
#include "DatabaseManager.h"
|
|
|
|
bool AccountDAO::upsertAccount(const AccountInfo &info) {
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
|
|
query.prepare(R"(
|
|
INSERT INTO accounts (device_id, app_name, card_name, amount, update_time, status, description)
|
|
VALUES (:device_id, :app_name, :card_name, :amount, :update_time, :status, :description)
|
|
ON CONFLICT(device_id, app_name, card_name)
|
|
DO UPDATE SET
|
|
amount = excluded.amount,
|
|
update_time = excluded.update_time,
|
|
status = excluded.status
|
|
)");
|
|
|
|
query.bindValue(":device_id", info.deviceId);
|
|
query.bindValue(":app_name", info.appName);
|
|
query.bindValue(":card_name", info.cardName);
|
|
query.bindValue(":amount", info.amount);
|
|
query.bindValue(":update_time", info.updateTime.toString(Qt::ISODate));
|
|
query.bindValue(":status", info.status);
|
|
query.bindValue(":description", info.description);
|
|
|
|
if (!query.exec()) {
|
|
qWarning() << "Ошибка при вставке/обновлении account:" << query.lastError().text();
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool AccountDAO::deleteAccount(const int id) {
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("DELETE FROM accounts WHERE id = :id");
|
|
query.bindValue(":id", id);
|
|
return query.exec();
|
|
}
|
|
|
|
QList<AccountInfo> AccountDAO::getAccounts(const QString &deviceId, const QString &appName) {
|
|
QList<AccountInfo> accounts;
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
|
|
query.prepare(R"(
|
|
SELECT id, device_id, app_name, card_name, amount, update_time, status, description
|
|
FROM accounts
|
|
WHERE device_id = :device_id AND app_name = :app_name
|
|
)");
|
|
|
|
query.bindValue(":device_id", deviceId);
|
|
query.bindValue(":app_name", appName);
|
|
|
|
if (!query.exec()) {
|
|
qWarning() << "Ошибка при получении accounts:" << query.lastError().text();
|
|
return accounts;
|
|
}
|
|
|
|
while (query.next()) {
|
|
AccountInfo acc;
|
|
acc.id = query.value("id").toInt();
|
|
acc.deviceId = query.value("device_id").toString();
|
|
acc.appName = query.value("app_name").toString();
|
|
acc.cardName = query.value("card_name").toString();
|
|
acc.amount = query.value("amount").toDouble();
|
|
acc.updateTime = QDateTime::fromString(query.value("update_time").toString(), Qt::ISODate);
|
|
acc.status = query.value("status").toString();
|
|
acc.description = query.value("description").toString();
|
|
accounts.append(acc);
|
|
}
|
|
|
|
return accounts;
|
|
}
|