345 lines
11 KiB
C++
345 lines
11 KiB
C++
#include "sqlControler.h"
|
|
#include <FrontendBackendBridge.h>
|
|
std::vector<std::string> getFutureDates()
|
|
{
|
|
std::vector<std::string> futureDates;
|
|
|
|
// 获取当前时间点
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
for (int i = 0; i < 30; i++) {
|
|
// 将当前时间点加上指定天数
|
|
auto future = now + std::chrono::hours(24 * i);
|
|
|
|
// 将时间转换为时间结构体
|
|
std::time_t futureTime = std::chrono::system_clock::to_time_t(future);
|
|
|
|
// 将时间结构体格式化为 "年月日" 的字符串
|
|
std::tm* futureTm = std::localtime(&futureTime);
|
|
std::ostringstream oss;
|
|
oss << futureTm->tm_year + 1900 << '-' << std::setw(2) << std::setfill('0') << futureTm->tm_mon + 1
|
|
<< '-' << std::setw(2) << std::setfill('0') << futureTm->tm_mday;
|
|
|
|
futureDates.push_back(oss.str());
|
|
}
|
|
|
|
return futureDates;
|
|
}
|
|
int callback(void* data, int argc, char** argv, char** azColName) {
|
|
std::vector<sqlDatas>* result = reinterpret_cast<std::vector<sqlDatas>*>(data);
|
|
|
|
for (int i = 0; i < argc; i++) {
|
|
std::string columnName = azColName[i];
|
|
std::string columnValue = argv[i] ? argv[i] : "NULL";
|
|
sqlDatas data;
|
|
data.dataName = columnName;
|
|
data.valueText = columnValue;
|
|
result->push_back(data);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
void sqlController::initPara()
|
|
{
|
|
|
|
{//每次启动备份文件
|
|
QFile sourceFile(QString::fromStdString(dataBasePath));
|
|
if (!sourceFile.open(QIODevice::ReadOnly)) {
|
|
qDebug() << "无法打开源文件: " << sourceFile.errorString();
|
|
}
|
|
else
|
|
{
|
|
QString destinationFilePath = QString::fromStdString(dataBasePath);
|
|
destinationFilePath.insert(destinationFilePath.lastIndexOf(".db"), QString::fromStdString(getCurrentTableName()));
|
|
QFile destinationFile(destinationFilePath);
|
|
if (!destinationFile.open(QIODevice::WriteOnly)) {
|
|
qDebug() << "无法打开目标文件: " << destinationFile.errorString();
|
|
}
|
|
else
|
|
{
|
|
QByteArray fileData = sourceFile.readAll();
|
|
destinationFile.write(fileData);
|
|
sourceFile.close();
|
|
destinationFile.close();
|
|
}
|
|
}
|
|
//qDebug() << "文件备份成功: " << destinationFile ;
|
|
}//打开数据库文件
|
|
int rc = sqlite3_open(dataBasePath.c_str(), &db); // 打开内存数据库,也可以指定文件路径打开实际数据库
|
|
|
|
if (rc != SQLITE_OK) {
|
|
std::cout << "无法打开数据库: " << sqlite3_errmsg(db) << "path:" << dataBasePath;
|
|
return;
|
|
}
|
|
std::vector<std::string>tables = getFutureDates();
|
|
for (auto table : tables)//创建未来30天的表格
|
|
{
|
|
/*"Camera1Result REAL, "
|
|
"Camera2Result REAL, "
|
|
"Camera3pResult REAL, "
|
|
"Camera3nResult REAL, "
|
|
"poleHeigth REAL, "
|
|
"batteryHeight REAL, "
|
|
"poleWidth REAL, "
|
|
"poleConcentricity REAL, "
|
|
"rubberBandWidth REAL, "
|
|
"rubberConcentricity REAL,";*/
|
|
std::string tableName = "\'" + table + "\'";
|
|
std::string datas("SN TEXT PRIMARY KEY, timeCode TEXT, ");
|
|
for (auto data : bridge.back.json.summaryData.datas)
|
|
datas += (data.first.toStdString() + " REAL,");
|
|
datas+="updateTime TEXT, send BOOL); ";
|
|
|
|
std::string createTableQuery =
|
|
"CREATE TABLE IF NOT EXISTS "
|
|
+ tableName +
|
|
" (" + datas + ");";
|
|
|
|
sqlite3_exec(db, createTableQuery.c_str(), 0, 0, 0);
|
|
}
|
|
for (auto table : tables)//创建未来30天的表格
|
|
{
|
|
for (auto data : bridge.back.json.summaryData.datas)
|
|
{
|
|
checkCol(db, table, data.first.toStdString());
|
|
}
|
|
}
|
|
}
|
|
void sqlController::checkCol(sqlite3* db, std::string tableName, std::string columnName)
|
|
{
|
|
try
|
|
{
|
|
// 执行查询,检查表是否存在并且包含指定的列
|
|
const char* query = "SELECT sql FROM sqlite_master WHERE type='table' AND name=?";
|
|
sqlite3_stmt* stmt;
|
|
int rc = sqlite3_prepare_v2(db, query, -1, &stmt, nullptr);
|
|
|
|
if (rc != SQLITE_OK) {
|
|
std::cerr << "无法准备查询: " << sqlite3_errmsg(db) << std::endl;
|
|
}
|
|
sqlite3_bind_text(stmt, 1, tableName.c_str(), -1, SQLITE_STATIC);
|
|
|
|
if (sqlite3_step(stmt) == SQLITE_ROW) {
|
|
// 表存在,现在检查是否包含指定的列
|
|
const char* tableDefinition = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
|
|
if (sqlite3_strlike(columnName.c_str(), tableDefinition, 0)) {
|
|
//std::cout << "table '" << tableName << "' exist and true '" << columnName << "'" << std::endl;
|
|
}
|
|
else {
|
|
std::cout << "table '" << tableName << "' exist ,but not true '" << columnName << "'" << std::endl;
|
|
}
|
|
}
|
|
|
|
// 清理资源
|
|
sqlite3_finalize(stmt);
|
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
|
std::cerr << "SQLite异常: " << e.what() << std::endl;
|
|
}
|
|
|
|
}
|
|
bool sqlController::addData(sqlDataStruct data)
|
|
{
|
|
if (data.SN.empty())//SN码为空,将停止处理
|
|
{
|
|
qDebug() << "[INFO] " << " SN empty!";
|
|
return false;
|
|
}
|
|
|
|
std::string query = "SELECT COUNT(*) FROM \'" + getCurrentTableName() + "\' WHERE sn = '" + data.SN + "'; ";
|
|
sqlite3_stmt* statement;
|
|
|
|
int result = sqlite3_prepare_v2(db, query.c_str(), -1, &statement, nullptr);//执行语句
|
|
|
|
if (result != SQLITE_OK)
|
|
return false;
|
|
if (sqlite3_step(statement) != SQLITE_ROW)
|
|
return false;
|
|
if (sqlite3_column_int(statement, 0) > 0)//查询出来的行数大于0
|
|
{
|
|
//拼接数据
|
|
std::string updateQuery = "UPDATE \'" + getCurrentTableName() + "\' SET send=1,";
|
|
for (auto d : data.dataVec)
|
|
{
|
|
updateQuery += d.dataName + " = ";
|
|
if (d.valueDouble != 0)
|
|
updateQuery += std::to_string(d.valueDouble) + ",";
|
|
else if (d.valueInt != 0)
|
|
updateQuery += std::to_string(d.valueInt) + ",";
|
|
else//TEXT
|
|
updateQuery += "\'" + d.valueText + "\',";
|
|
}
|
|
updateQuery.replace(updateQuery.find_last_of(","), 1, " WHERE SN = \'" + data.SN + "\';");
|
|
int rc = sqlite3_exec(db, updateQuery.c_str(), nullptr, nullptr, nullptr);
|
|
if (rc != SQLITE_OK) {
|
|
std::cout << "更新操作失败: " << sqlite3_errmsg(db) << ";" << updateQuery;
|
|
sqlite3_finalize(statement);
|
|
return false;
|
|
}
|
|
}
|
|
else {
|
|
std::string query = "SELECT COUNT(*) FROM \'" + getPreviousDate() + "\' WHERE sn = '" + data.SN + "'; ";
|
|
sqlite3_stmt* statement;
|
|
int result = sqlite3_prepare_v2(db, query.c_str(), -1, &statement, nullptr);//执行语句
|
|
if (result != SQLITE_OK)
|
|
return false;
|
|
if (sqlite3_step(statement) != SQLITE_ROW)
|
|
return false;
|
|
if (sqlite3_column_int(statement, 0) > 0)
|
|
{
|
|
//拼接数据
|
|
std::string updateQuery = "UPDATE \'" + getPreviousDate() + "\' SET send=1,";
|
|
for (auto d : data.dataVec)
|
|
{
|
|
updateQuery += d.dataName + " = ";
|
|
if (d.valueDouble != 0)
|
|
updateQuery += std::to_string(d.valueDouble) + ",";
|
|
else if (d.valueInt != 0)
|
|
updateQuery += std::to_string(d.valueInt) + ",";
|
|
else//TEXT
|
|
updateQuery += "\'" + d.valueText + "\',";
|
|
}
|
|
updateQuery.replace(updateQuery.find_last_of(","), 1, " WHERE SN = \'" + data.SN + "\';");
|
|
int rc = sqlite3_exec(db, updateQuery.c_str(), nullptr, nullptr, nullptr);
|
|
if (rc != SQLITE_OK) {
|
|
std::cout << "更新操作失败: " << sqlite3_errmsg(db) << ";" << updateQuery;
|
|
sqlite3_finalize(statement);
|
|
return false;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
std::string insertQuery = "INSERT INTO \'" + getCurrentTableName() + "\' (send,SN,";
|
|
for (auto d : data.dataVec)
|
|
insertQuery += d.dataName + ",";
|
|
insertQuery.replace(insertQuery.find_last_of(","), 1, ") VALUES (1,\'" + data.SN + "\',");
|
|
for (auto d : data.dataVec)
|
|
{
|
|
if (d.valueDouble != 0)
|
|
insertQuery += std::to_string(d.valueDouble) + ",";
|
|
else if (d.valueInt != 0)
|
|
insertQuery += std::to_string(d.valueInt) + ",";
|
|
else//TEXT
|
|
insertQuery += "\'" + d.valueText + "\',";
|
|
}
|
|
insertQuery.replace(insertQuery.find_last_of(","), 1, " );");
|
|
int rc = sqlite3_exec(db, insertQuery.c_str(), nullptr, nullptr, nullptr);
|
|
if (rc != SQLITE_OK) {
|
|
std::cout << "插入操作失败: " << sqlite3_errmsg(db) << ";" << insertQuery;
|
|
sqlite3_finalize(statement);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
sqlite3_finalize(statement);
|
|
return true;
|
|
}
|
|
int sqlController::checktSn(std::string SN)
|
|
{
|
|
return 0;
|
|
std::string query = "SELECT COUNT(*) FROM \'" + getCurrentTableName() + "\' WHERE sn LIKE '" + SN + "%'; ";
|
|
sqlite3_stmt* statement;
|
|
int result = sqlite3_prepare_v2(db, query.c_str(), -1, &statement, nullptr);
|
|
int resultReturn = -1;
|
|
int num = 0;
|
|
if (result == SQLITE_OK)
|
|
if (sqlite3_step(statement) == SQLITE_ROW)
|
|
num = sqlite3_column_int(statement, 0);
|
|
if (num > 0)
|
|
{
|
|
query = "SELECT send FROM \'" + getCurrentTableName() + "\' WHERE sn =\'" + SN + "-" + std::to_string(num - 1) + "\' ; ";
|
|
std::vector<sqlDatas> rowData;
|
|
int rc = sqlite3_exec(db, query.c_str(), callback, &rowData, 0);
|
|
for (int i = 0; i < rowData.size(); i++)
|
|
{
|
|
if (!rowData[i].valueText.compare("1"))
|
|
{
|
|
num -= 1;
|
|
}
|
|
}
|
|
}
|
|
sqlite3_finalize(statement);
|
|
return num;
|
|
}
|
|
#if 0
|
|
sqlDataStruct sqlController::selectData(std::string SN, int snCountIndex, int send)
|
|
{
|
|
std::string query;
|
|
int rc;
|
|
//send设true
|
|
{
|
|
query = "UPDATE \'" + getCurrentTableName() + "\' SET send = " + std::to_string(send) + " WHERE sn = \'" + SN + "-" + std::to_string(snCountIndex) + "\' ; ";
|
|
if (debugMode == true)
|
|
{
|
|
std::cout << query << endl;
|
|
qDebug() << "sql query:" << QString::fromStdString(query);
|
|
}
|
|
rc = sqlite3_exec(db, query.c_str(), nullptr, nullptr, nullptr);
|
|
}
|
|
|
|
//////////////////////////////
|
|
if (snCountIndex > 0)
|
|
query = "SELECT * FROM \'" + getCurrentTableName() + "\' WHERE sn =\'" + SN + "-" + std::to_string(snCountIndex) + "\' ; ";
|
|
else
|
|
query = "SELECT * FROM \'" + getCurrentTableName() + "\' WHERE sn =\'" + SN + "-0\' ; ";
|
|
|
|
if (debugMode == true)
|
|
{
|
|
std::cout << query << endl;
|
|
qDebug() << "sql query:" << QString::fromStdString(query);
|
|
}
|
|
|
|
std::vector<sqlDatas> rowData;
|
|
sqlDataStruct Respone;
|
|
rc = sqlite3_exec(db, query.c_str(), callback, &rowData, 0);
|
|
if (rc != SQLITE_OK) {
|
|
std::cerr << "Failed to execute query: " << sqlite3_errmsg(db) << std::endl;
|
|
}
|
|
else
|
|
{
|
|
Respone.SN = SN;
|
|
Respone.dataVec.swap(rowData);
|
|
Respone.tableName = getCurrentTableName();
|
|
}
|
|
return Respone;
|
|
}
|
|
#endif
|
|
sqlDataStruct sqlController::selectData(std::string SN, int send)
|
|
{
|
|
std::string query;
|
|
int rc;
|
|
//send设true
|
|
query = "UPDATE \'" + getCurrentTableName() + "\' SET send = " + std::to_string(send) + " WHERE sn = \'" + SN + "\' ; ";
|
|
rc = sqlite3_exec(db, query.c_str(), nullptr, nullptr, nullptr);
|
|
query = "SELECT * FROM \'" + getCurrentTableName() + "\' WHERE sn =\'" + SN + "\' ; ";
|
|
std::vector<sqlDatas> rowData;
|
|
sqlDataStruct Respone;
|
|
rc = sqlite3_exec(db, query.c_str(), callback, &rowData, 0);
|
|
if (rc != SQLITE_OK) {
|
|
std::cerr << "Failed to execute query: " << sqlite3_errmsg(db) << std::endl;
|
|
return sqlDataStruct();
|
|
}
|
|
else if (rowData.size() == 0)
|
|
{
|
|
query = "UPDATE \'" + getPreviousDate() + "\' SET send = " + std::to_string(send) + " WHERE sn = \'" + SN + "\' ; ";
|
|
rc = sqlite3_exec(db, query.c_str(), nullptr, nullptr, nullptr);
|
|
query = "SELECT * FROM \'" + getPreviousDate() + "\' WHERE sn =\'" + SN + "\' ; ";
|
|
rc = sqlite3_exec(db, query.c_str(), callback, &rowData, 0);
|
|
}
|
|
|
|
Respone.SN = SN;
|
|
Respone.dataVec.swap(rowData);
|
|
Respone.tableName = getCurrentTableName();
|
|
|
|
return Respone;
|
|
}
|
|
void sqlController::uninitlize()
|
|
{
|
|
sqlite3_close(db);
|
|
}
|
|
|