#include "DatabaseManager.h" #include "Constant.h" #include "Log.h" #include #include #include #include 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, name, year, month, day) values (?, ?, ?, ?, ?)"); query.bindValue(0, file.channel); query.bindValue(1, file.time); query.bindValue(2, file.year); query.bindValue(3, file.month); query.bindValue(4, file.day); 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::get(QVariantMap params) { QList 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(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(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::get(DatabaseManager::Channel chn) { QList 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(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::getTopTwo() { QList 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(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 根据通道和文件名删除记录 * @param id * @return */ bool DatabaseManager::remove(DatabaseManager::Channel chn, QString name) { QSqlQuery query; query.prepare("delet from file where channel = ? and name = ?"); query.bindValue(0, chn); query.bindValue(1, name); if (query.exec()) { return true; } else { Log::error("delete one record from database failed, channel = {}, name = {}, reason: {}", (int)chn, name.toStdString(), query.lastError().databaseText().toStdString()); return false; } }