RecordControlApplication/DatabaseManager.cpp
2024-08-22 22:34:58 -07:00

314 lines
9.1 KiB
C++
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
query.prepare("insert into file (channel, datetime, filename) values (?, ?, ?)");
query.addBindValue(file.channel);
query.addBindValue(file.datetime);
query.addBindValue(file.filename);
if (query.exec()) {
return true;
} else {
Log::error("insert one record into database failed, reason: {}",
query.lastError().databaseText().toStdString() + "," + query.lastError().driverText().toStdString());
return false;
}
}
/**
* @brief 查询数据
* @param params 参数
* @return
*/
QList<DatabaseManager::File> DatabaseManager::get(QVariantMap params)
{
QList<DatabaseManager::File> result;
QSqlQuery query;
QString sql = "select * from file where channel=? and datetime like \'%1\'";
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;
}
query.prepare(sql);
query.bindValue(0, chn);
query.bindValue(1, QString("%1-%2-%3%"));
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());
file.datetime = query.value("datetime").toString();
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());
file.datetime = query.value("datetime").toString();
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());
file.datetime = query.value("datetime").toString();
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;
query.prepare(R"(
select distinct strftime('%Y', datetime) as year
from file
order by year
)");
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;
query.prepare(R"(
select distinct strftime('%m', datetime) as month
from file
where channel = ? and strftime('%Y', datetime) = '?'
order by month
)");
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;
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
)");
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
* @param id
* @return
*/
bool DatabaseManager::remove(DatabaseManager::Channel chn, QString name)
{
int id = -1;
// 找到对应记录
QSqlQuery query;
query.prepare("select * from file where channel = ? and filename = ?");
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());
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());
return false;
}
}