86 lines
2.9 KiB
C++
86 lines
2.9 KiB
C++
#include "WifiConnectionDAO.h"
|
|
|
|
#include <QSqlQuery>
|
|
#include <QSqlError>
|
|
#include <QDateTime>
|
|
#include <QDebug>
|
|
|
|
#include "DatabaseManager.h"
|
|
|
|
namespace {
|
|
WifiConnection parseRow(const QSqlQuery &query) {
|
|
WifiConnection wc;
|
|
wc.androidId = query.value("android_id").toString();
|
|
wc.ip = query.value("ip").toString();
|
|
wc.port = query.value("port").toInt();
|
|
wc.code = query.value("code").toString();
|
|
wc.mac = query.value("mac").toString();
|
|
wc.updatedAt = query.value("updated_at").toString();
|
|
return wc;
|
|
}
|
|
} // namespace
|
|
|
|
bool WifiConnectionDAO::upsert(const QString &androidId, const QString &ip, int port,
|
|
const QString &code, const QString &mac) {
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare(R"(
|
|
INSERT INTO wifi_connections (android_id, ip, port, code, mac, updated_at)
|
|
VALUES (:android_id, :ip, :port, :code, :mac, :updated_at)
|
|
ON CONFLICT(android_id) DO UPDATE SET
|
|
ip = excluded.ip,
|
|
port = excluded.port,
|
|
code = excluded.code,
|
|
mac = excluded.mac,
|
|
updated_at = excluded.updated_at
|
|
)");
|
|
query.bindValue(":android_id", androidId);
|
|
query.bindValue(":ip", ip);
|
|
query.bindValue(":port", port);
|
|
query.bindValue(":code", code);
|
|
query.bindValue(":mac", mac);
|
|
query.bindValue(":updated_at", QDateTime::currentDateTimeUtc().toString(Qt::ISODate));
|
|
if (!query.exec()) {
|
|
qWarning() << "WifiConnectionDAO::upsert error:" << query.lastError().text();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
WifiConnection WifiConnectionDAO::getByAndroidId(const QString &androidId) {
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("SELECT * FROM wifi_connections WHERE android_id = :android_id LIMIT 1");
|
|
query.bindValue(":android_id", androidId);
|
|
if (!query.exec()) {
|
|
qWarning() << "WifiConnectionDAO::getByAndroidId error:" << query.lastError().text();
|
|
return {};
|
|
}
|
|
if (query.next()) {
|
|
return parseRow(query);
|
|
}
|
|
return {};
|
|
}
|
|
|
|
QList<WifiConnection> WifiConnectionDAO::getAll() {
|
|
QList<WifiConnection> result;
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
if (!query.exec("SELECT * FROM wifi_connections")) {
|
|
qWarning() << "WifiConnectionDAO::getAll error:" << query.lastError().text();
|
|
return result;
|
|
}
|
|
while (query.next()) {
|
|
result.append(parseRow(query));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
bool WifiConnectionDAO::remove(const QString &androidId) {
|
|
QSqlQuery query(DatabaseManager::instance().database());
|
|
query.prepare("DELETE FROM wifi_connections WHERE android_id = :android_id");
|
|
query.bindValue(":android_id", androidId);
|
|
if (!query.exec()) {
|
|
qWarning() << "WifiConnectionDAO::remove error:" << query.lastError().text();
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|