RecordControlApplication/DatabaseManager.cpp

314 lines
9.1 KiB
C++
Raw Normal View History

2024-08-12 11:26:42 +08:00
#include "DatabaseManager.h"
#include "Constant.h"
#include "Log.h"
#include <QCoreApplication>
#include <QMutex>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
DatabaseManager* DatabaseManager::instance = nullptr;
DatabaseManager::DatabaseManager()
{
if (QSqlDatabase::contains("qt_sql_default_connection")) {
db = QSqlDatabase::database("qt_sql_default_connection");
} else {
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(Constant::DatabasePath);
}
}
DatabaseManager::~DatabaseManager()
{
db.close();
}
DatabaseManager* DatabaseManager::getInstace()
{
if (instance == nullptr) {
static QMutex mutex;
QMutexLocker locker(&mutex);
if (instance == nullptr) {
instance = new DatabaseManager();
}
}
return instance;
}
/**
* @brief
* @return
*/
bool DatabaseManager::open()
{
if (!db.open()) {
Log::error("database open failed");
qDebug() << db.lastError();
return false;
}
Log::info("database open success");
return true;
}
/**
* @brief
*/
void DatabaseManager::close()
{
if (db.isOpen())
db.close();
}
/**
* @brief
* @param file
*/
bool DatabaseManager::insert(File file)
{
QSqlQuery query;
2024-08-23 13:34:58 +08:00
query.prepare("insert into file (channel, datetime, filename) values (?, ?, ?)");
query.addBindValue(file.channel);
query.addBindValue(file.datetime);
query.addBindValue(file.filename);
2024-08-12 11:26:42 +08:00
if (query.exec()) {
return true;
} else {
Log::error("insert one record into database failed, reason: {}",
2024-08-23 13:34:58 +08:00
query.lastError().databaseText().toStdString() + "," + query.lastError().driverText().toStdString());
2024-08-12 11:26:42 +08:00
return false;
}
}
/**
* @brief
* @param params
* @return
*/
QList<DatabaseManager::File> DatabaseManager::get(QVariantMap params)
{
QList<DatabaseManager::File> result;
QSqlQuery query;
2024-08-23 13:34:58 +08:00
QString sql = "select * from file where channel=? and datetime like \'%1\'";
2024-08-12 11:26:42 +08:00
QString year = params.value("year").toString();
QString month = params.value("month").toString();
QString day = params.value("day").toString();
Channel chn = static_cast<Channel>(params.value("channel").toInt());
if (year.isEmpty() || month.isEmpty() || day.isEmpty() || (chn != MainChannel && chn != SecondaryChannel)) {
Log::error("select from database error, params error");
return result;
}
2024-08-23 13:34:58 +08:00
query.prepare(sql);
query.bindValue(0, chn);
query.bindValue(1, QString("%1-%2-%3%"));
2024-08-12 11:26:42 +08:00
if (query.exec()) {
while (query.next()) {
DatabaseManager::File file;
file.id = query.value("id").toInt();
file.channel = static_cast<DatabaseManager::Channel>(query.value("channel").toInt());
2024-08-23 13:34:58 +08:00
file.datetime = query.value("datetime").toString();
2024-08-12 11:26:42 +08:00
file.filename = query.value("filename").toString();
result.push_back(file);
}
} else {
Log::error("select from database failed, reason: {}",
query.lastError().databaseText().toStdString());
}
Log::info("record of one day: {}", result.length());
return result;
}
/**
* @brief
* @param chn
* @return
*/
QList<DatabaseManager::File> DatabaseManager::get(DatabaseManager::Channel chn)
{
QList<DatabaseManager::File> result;
QSqlQuery query;
QString sql = "select * from file where channel=?";
query.prepare(sql);
query.bindValue(0, chn);
if (query.exec()) {
while (query.next()) {
DatabaseManager::File file;
file.id = query.value("id").toInt();
file.channel = static_cast<DatabaseManager::Channel>(query.value("channel").toInt());
2024-08-23 13:34:58 +08:00
file.datetime = query.value("datetime").toString();
2024-08-12 11:26:42 +08:00
file.filename = query.value("filename").toString();
result.push_back(file);
}
} else {
Log::error("select from database failed, reason: {}",
query.lastError().databaseText().toStdString());
}
return result;
}
/**
* @brief
*/
QList<DatabaseManager::File> DatabaseManager::getTopTwo()
{
QList<DatabaseManager::File> result;
QSqlQuery query;
query.prepare("select * from file limit 0,2");
if (query.exec()) {
while (query.next()) {
DatabaseManager::File file;
file.id = query.value("id").toInt();
file.channel = static_cast<DatabaseManager::Channel>(query.value("channel").toInt());
2024-08-23 13:34:58 +08:00
file.datetime = query.value("datetime").toString();
2024-08-12 11:26:42 +08:00
file.filename = query.value("filename").toString();
result.push_back(file);
}
} else {
Log::error("select top two records from database failed, reason: {}",
query.lastError().databaseText().toStdString());
}
return result;
}
/**
* @brief
* @return
*/
QStringList DatabaseManager::getAllYears(Channel chn)
{
QStringList result;
QSqlQuery query;
2024-08-23 13:34:58 +08:00
query.prepare(R"(
select distinct strftime('%Y', datetime) as year
from file
order by year
)");
2024-08-12 11:26:42 +08:00
if (query.exec()) {
while (query.next()) {
QString year = query.value(0).toString();
result.push_back(year);
}
} else {
Log::error("select all years from database failed, reason: {}",
query.lastError().databaseText().toStdString());
}
Log::info("number of year: {}", result.length());
return result;
}
/**
* @brief
* @param year
* @return
*/
QStringList DatabaseManager::getAllMonths(Channel chn, QString year)
{
QStringList result;
QSqlQuery query;
2024-08-23 13:34:58 +08:00
query.prepare(R"(
select distinct strftime('%m', datetime) as month
from file
where channel = ? and strftime('%Y', datetime) = '?'
order by month
)");
2024-08-12 11:26:42 +08:00
query.bindValue(0, chn);
query.bindValue(1, year);
if (query.exec()) {
while (query.next()) {
QString month = query.value(0).toString();
result.push_back(month);
}
} else {
Log::error("select all months of one year from database failed, reason: {}",
query.lastError().databaseText().toStdString());
}
Log::info("number of month: {}", result.length());
return result;
}
/**
* @brief
* @param year
* @param month
* @return
*/
QStringList DatabaseManager::getAllDays(Channel chn, QString year, QString month)
{
QStringList result;
QSqlQuery query;
2024-08-23 13:34:58 +08:00
query.prepare(R"(
select distinct strftime('%d', datetime) as day
from file
where channel = ?
and strftime('%Y', datetime) = '?'
and strftime('%m', datetime) = '?'
order by day
)");
2024-08-12 11:26:42 +08:00
query.bindValue(0, chn);
query.bindValue(1, year);
query.bindValue(2, month);
if (query.exec()) {
while (query.next()) {
QString day = query.value(0).toString();
result.push_back(day);
}
} else {
Log::error("select all days of one month from database failed, reason: {}",
query.lastError().databaseText().toStdString());
}
Log::info("number of day: {}", result.length());
return result;
}
/**
* @brief yyyyMMddhhmmss
2024-08-12 11:26:42 +08:00
* @param id
* @return
*/
bool DatabaseManager::remove(DatabaseManager::Channel chn, QString name)
{
int id = -1;
// 找到对应记录
2024-08-12 11:26:42 +08:00
QSqlQuery query;
query.prepare("select * from file where channel = ? and filename = ?");
2024-08-12 11:26:42 +08:00
query.bindValue(0, chn);
query.bindValue(1, name);
if (query.exec()) {
while (query.next()) {
id = query.value(0).toInt();
break;
}
} else {
Log::info("cannot find the record, channel = {}, name = {}",
int(chn),
name.toStdString());
return false;
}
// 未找到
if (id == -1) {
Log::info("cannot find the record, channel = {}, name = {}",
int(chn),
name.toStdString());
return false;
}
Log::info("find the record, channel = {} , name = {}, and the id = {}",
int(chn),
name.toStdString(),
id);
query.clear();
// 删除文件
query.prepare("delete from file where id = ?");
query.bindValue(0, id);
if (query.exec()) {
Log::info("delete one record from database, id = {}, channel = {}, name = {}",
id,
int(chn),
name.toStdString());
2024-08-12 11:26:42 +08:00
return true;
} else {
Log::error("delete one record from database failed, channel = {}, name = {}, reason: {}",
(int)chn,
name.toStdString(),
query.lastError().driverText().toStdString() + ", " + query.lastError().databaseText().toStdString());
2024-08-12 11:26:42 +08:00
return false;
}
}