313 lines
9.4 KiB
C++
Executable File
313 lines
9.4 KiB
C++
Executable File
#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, year, month, day, time, filename) values (?, ?, ?, ?, ?, ?)");
|
||
query.bindValue(0, file.channel);
|
||
query.bindValue(1, file.year);
|
||
query.bindValue(2, file.month);
|
||
query.bindValue(3, file.day);
|
||
query.bindValue(4, file.time);
|
||
query.bindValue(5, file.filename);
|
||
if (query.exec()) {
|
||
return true;
|
||
} else {
|
||
Log::error("insert one record into database failed, reason: {}",
|
||
query.lastError().databaseText().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 year = ? and month = ? and day = ?";
|
||
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());
|
||
query.prepare(sql);
|
||
query.bindValue(0, chn);
|
||
query.bindValue(1, year);
|
||
query.bindValue(2, month);
|
||
query.bindValue(3, day);
|
||
if (year.isEmpty() || month.isEmpty() || day.isEmpty() || (chn != MainChannel && chn != SecondaryChannel)) {
|
||
Log::error("select from database error, params error");
|
||
return result;
|
||
}
|
||
if (query.exec()) {
|
||
while (query.next()) {
|
||
DatabaseManager::File file;
|
||
file.id = query.value("id").toInt();
|
||
file.year = query.value("year").toString();
|
||
file.month = query.value("month").toString();
|
||
file.day = query.value("day").toString();
|
||
file.channel = static_cast<DatabaseManager::Channel>(query.value("channel").toInt());
|
||
file.time = query.value("time").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.year = query.value("year").toString();
|
||
file.month = query.value("month").toString();
|
||
file.day = query.value("day").toString();
|
||
file.channel = static_cast<DatabaseManager::Channel>(query.value("channel").toInt());
|
||
file.time = query.value("time").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.year = query.value("year").toString();
|
||
file.month = query.value("month").toString();
|
||
file.day = query.value("day").toString();
|
||
file.channel = static_cast<DatabaseManager::Channel>(query.value("channel").toInt());
|
||
file.time = query.value("time").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("select distinct year from file");
|
||
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("select distinct month from file where channel = ? and year=?");
|
||
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("select distinct day from file where channel = ? and year=? and month=?");
|
||
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;
|
||
}
|
||
}
|