You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
392 lines
13 KiB
C++
392 lines
13 KiB
C++
#include "DetectDataDB.h"
|
|
#include "InfoFile.h"
|
|
#include "gensql.h"
|
|
|
|
#define _MD_WARNINGTABLE "warningtable"
|
|
/*myself */
|
|
#define _MD_PRIMARY_KEY "uid"
|
|
#define _MD_TABLE_FORMS_MANE "wftable" //数据表名称
|
|
|
|
#define _MD_MODEL "model" //model 模型名称
|
|
#define _MD_DIAMETER "diameter" //直径
|
|
#define _MD_HIGHT "hight" //高度
|
|
#define _MD_CORRELATE "correlate" //相似度
|
|
#define _MD_CHANNEL "channel" //通道
|
|
#define _MD_PICPATH "picpath" //图片路径
|
|
#define _MD_ROTATE "rotate"//辐条个数
|
|
#define _MD_UPDATE_TIME "time" //最后更新的时间
|
|
#define _MD_PIC "pic" //图片
|
|
|
|
|
|
#define _MD_TIMETABLE "wftimetable_2"
|
|
#define _MD_TYPE "type"
|
|
#define _MD_STARTTIME "starttime"
|
|
#define _MD_ENDTIME "endtime"
|
|
#define _MD_TIMETYPY_S "startType"
|
|
#define _MD_TIMETYPY_E "EndType"
|
|
|
|
#define _MD_WARNINGTABLE "warningtable"
|
|
#define _MD_MESSAGE "message"
|
|
#define _MD_CLASS "class"
|
|
#define _INDEX_FORTABLE "CREATE INDEX idx_wftable ON wftable (time COLLATE BINARY ASC); "
|
|
DetectDataDB::DetectDataDB(const QString& dbName,const QString &dbType) :DataBaseSql(dbName,dbType)
|
|
{
|
|
// db = QSqlDatabase::addDatabase("QSQLITE");
|
|
// db.setDatabaseName(dbName);
|
|
// if (!dbUser.isEmpty()){
|
|
// db.setUserName(dbUser);
|
|
// db.setPassword(dbPwb);
|
|
// }
|
|
}
|
|
|
|
DetectDataDB::~DetectDataDB()
|
|
{
|
|
|
|
}
|
|
//m_value.insert("time1", "");//起始时间
|
|
//m_value.insert("time2", "");//结束时间
|
|
//m_value.insert("name", "");//模型名字
|
|
//m_value.insert("Type", "");//查询类型
|
|
QString DataBaseSql::genCheckStr(QVariantMap m_value)
|
|
{
|
|
int nType = m_value.value(_CHECK_TYPE_).toInt();
|
|
QString select_sql;
|
|
switch (nType)
|
|
{
|
|
case EMT_CHECK_BY_TIME:
|
|
{
|
|
QString time1 = m_value.value(_CHECK_TIME1_).toString();
|
|
QString strFifter = QString(" where time ='%1'").arg(time1);
|
|
int IsCount = m_value.value(_CHECK_COUNT_).toInt(0);
|
|
if (IsCount==1)
|
|
select_sql = QString("select count(*) from ") + _MD_TABLE_FORMS_MANE + strFifter;
|
|
else
|
|
select_sql = QString("select * from ") + _MD_TABLE_FORMS_MANE + strFifter;
|
|
}
|
|
break;
|
|
case EMT_CHECK_BY_NAME:
|
|
{
|
|
QString time1 = m_value.value(_CHECK_TIME1_).toString();
|
|
QString time2 = m_value.value(_CHECK_TIME2_).toString();
|
|
QString modelname = m_value.value(_CHECK_NAME_).toString();
|
|
QString strFifter = QString(" where time >'%1' and time < '%2' and model='%3'").arg(time1).arg(time2).arg(modelname);
|
|
int IsCount = m_value.value(_CHECK_COUNT_).toInt(0);
|
|
if (IsCount == 1)
|
|
select_sql = QString("select count(*) from ") + _MD_TABLE_FORMS_MANE + strFifter;
|
|
else
|
|
select_sql = QString("select * from ") + _MD_TABLE_FORMS_MANE + strFifter;
|
|
}
|
|
break;
|
|
case EMT_CHECK_BY_SAE:
|
|
{
|
|
QString time1 = m_value.value(_CHECK_TIME1_).toString();
|
|
QString time2 = m_value.value(_CHECK_TIME2_).toString();
|
|
QString checktypeStr = m_value.value("CheckType").toString();
|
|
QString strFifter = QString(" where time >'%1' and time < '%2'").arg(time1).arg(time2);
|
|
int IsCount = m_value.value(_CHECK_COUNT_).toInt(0);
|
|
if (IsCount == 1)
|
|
select_sql = QString("select count(*) from ") + _MD_TABLE_FORMS_MANE + strFifter;
|
|
else{
|
|
if (checktypeStr == "Count")
|
|
select_sql = QString("select %1 from ").arg(_MD_MODEL) + _MD_TABLE_FORMS_MANE + strFifter;
|
|
else
|
|
select_sql = QString("select * from ") + _MD_TABLE_FORMS_MANE + strFifter;
|
|
}
|
|
}
|
|
break;
|
|
case EMT_CHECK_BY_LOG:
|
|
{
|
|
QString time1 = m_value.value(_CHECK_TIME1_).toString();
|
|
QString time2 = m_value.value(_CHECK_TIME2_).toString();
|
|
int logType = m_value.value(_CHECK_LOGTYPE).toInt();
|
|
QString strFifter = QString(" where time >'%1' and time < '%2' and class = '%3' order by time desc ").arg(time1).arg(time2).arg(logType);
|
|
int IsCount = m_value.value(_CHECK_COUNT_).toInt(0);
|
|
if (IsCount == 1)
|
|
select_sql = QString("select count(*) from ") + _MD_WARNINGTABLE + strFifter;
|
|
else
|
|
select_sql = QString("select * from ") + _MD_WARNINGTABLE + strFifter;
|
|
}
|
|
break;
|
|
case EMT_CHECK_BY_COUNT:
|
|
{
|
|
QString time1 = m_value.value(_CHECK_TIME1_).toString();
|
|
QString time2 = m_value.value(_CHECK_TIME2_).toString();
|
|
QString checktypeStr = m_value.value("CheckType").toString();
|
|
QString strFifter = QString(" where time >'%1' and time < '%2'").arg(time1).arg(time2);
|
|
int IsCount = m_value.value(_CHECK_COUNT_).toInt(0);
|
|
if (IsCount == 1)
|
|
select_sql = QString("select count(*) from ") + _MD_TABLE_FORMS_MANE + strFifter;
|
|
else{
|
|
QString strGroup = QString(" GROUP BY %1 ORDER BY %2").arg(_MD_MODEL).arg(_MD_MODEL);
|
|
if (checktypeStr == "Count")
|
|
select_sql = QString("select %1 from ").arg(_MD_MODEL).arg(_MD_MODEL) + _MD_TABLE_FORMS_MANE + strFifter;// +strGroup;
|
|
else
|
|
select_sql = QString("select * from ") + _MD_TABLE_FORMS_MANE + strFifter;
|
|
}
|
|
}
|
|
break;
|
|
case EMT_CHECK_BY_UID:
|
|
{
|
|
QString uid = m_value.value(_CHECK_UID_).toString();
|
|
QString strFifter = QString(" where uid = %1 ").arg(uid);
|
|
int IsCount = m_value.value(_CHECK_COUNT_).toInt(0);
|
|
if (IsCount == 1)
|
|
select_sql = QString("select count(*) from ") + _MD_TABLE_FORMS_MANE + strFifter;
|
|
else
|
|
select_sql = QString("select * from ") + _MD_TABLE_FORMS_MANE + strFifter;
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
return select_sql;
|
|
}
|
|
|
|
bool DetectDataDB::checkoutData(QVariantMap m_value, QSqlQuery &sql)
|
|
{
|
|
int nType = m_value.value(_CHECK_TYPE_).toInt();
|
|
QString select_sql;
|
|
switch (nType)
|
|
{
|
|
case EMT_CHECK_BY_TIME:
|
|
{
|
|
QString time1 = m_value.value(_CHECK_TIME1_).toString();
|
|
QString strFifter = QString(" where time ='%1'").arg(time1);
|
|
select_sql = QString("select * from ") + _MD_TABLE_FORMS_MANE + strFifter;
|
|
}
|
|
break;
|
|
case EMT_CHECK_BY_NAME:
|
|
{
|
|
QString time1 = m_value.value(_CHECK_TIME1_).toString();
|
|
QString time2 = m_value.value(_CHECK_TIME2_).toString();
|
|
QString modelname = m_value.value(_CHECK_NAME_).toString();
|
|
QString strFifter = QString(" where time >'%1' and time < '%2' and model='%3'").arg(time1).arg(time2).arg(modelname);
|
|
select_sql = QString("select * from ") + _MD_TABLE_FORMS_MANE + strFifter;
|
|
}
|
|
break;
|
|
case EMT_CHECK_BY_SAE:
|
|
{
|
|
QString time1 = m_value.value(_CHECK_TIME1_).toString();
|
|
QString time2 = m_value.value(_CHECK_TIME2_).toString();
|
|
QString strFifter = QString(" where time >'%1' and time < '%2'").arg(time1).arg(time2);
|
|
select_sql = QString("select * from ") + _MD_TABLE_FORMS_MANE + strFifter;
|
|
}
|
|
break;
|
|
case EMT_CHECK_BY_LOG:
|
|
{
|
|
QString time1 = m_value.value(_CHECK_TIME1_).toString();
|
|
QString time2 = m_value.value(_CHECK_TIME2_).toString();
|
|
int logType = m_value.value(_CHECK_LOGTYPE).toInt();
|
|
QString strFifter = QString(" where time >'%1' and time < '%2' and class = '%3' order by time desc ").arg(time1).arg(time2).arg(logType);
|
|
select_sql = QString("select * from ") + _MD_WARNINGTABLE + strFifter;
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
sql = db->exec(select_sql);
|
|
return true;
|
|
}
|
|
|
|
bool DetectDataDB::checkoutData(QString selectStr, QSqlQuery &sql)
|
|
{
|
|
sql = db->exec(selectStr);
|
|
return true;
|
|
}
|
|
// bool DetectDataDB::checkoutData(QString selectStr, QSqlQuery &sql)
|
|
// {
|
|
// Q
|
|
// }
|
|
bool DetectDataDB::DelDatasByTime(QString minTime)
|
|
{
|
|
QString StrTime = QString("time <'%1'").arg(minTime);
|
|
QString select_sql = QString("DELETE FROM %1 WHERE %2").arg(_MD_TABLE_FORMS_MANE).arg(StrTime);
|
|
db->exec(select_sql);
|
|
return true;
|
|
}
|
|
|
|
bool DetectDataDB::DelAllDatas()
|
|
{
|
|
QString strSqll = QString("delete from %1 ").arg(_MD_TABLE_FORMS_MANE);
|
|
db->exec(strSqll);
|
|
strSqll = QString("delete from %1 ").arg("warningtable");
|
|
db->exec(strSqll);
|
|
return true;
|
|
}
|
|
|
|
bool DetectDataDB::DelWarnDataByCount(int model /*=1*/, int nCount/*=100000*/)
|
|
{
|
|
QString strLimit1 = QString("select count(uid) from %1 where class = '%3'").arg("warningtable").arg(model);
|
|
QString strLimit2 = QString("uid in(select uid from %1 where class = '%2' order by time desc limit(select count(uid) from %3 where class = '%4') offset %5)").arg("warningtable").arg(model).arg("warningtable").arg(model).arg(nCount);
|
|
QString strSqll = QString("delete from %1 where (%2)>%3 and %4").arg("warningtable").arg(strLimit1).arg(nCount).arg(strLimit2);
|
|
QSqlQuery sql = db->exec(strSqll);
|
|
return true;
|
|
}
|
|
|
|
bool DetectDataDB::InitDatabase()
|
|
{
|
|
if (!openDB())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
QString strCheckTable = QString("select name from sqlite_master where type='table' order by name;");
|
|
QSqlQuery sql = db->exec(strCheckTable);//查询数据库中所有的数据表
|
|
QStringList tablenamelist;
|
|
while (sql.next())
|
|
{
|
|
QString strname = sql.value(0).toString();
|
|
tablenamelist.append(strname);
|
|
}
|
|
if (!tablenamelist.contains(_MD_TABLE_FORMS_MANE))//是否包含 table 否则创建新表
|
|
{
|
|
QVariantMap vDataMap;
|
|
vDataMap.insert(_MD_MODEL, "VARCHAR(32)");
|
|
vDataMap.insert(_MD_DIAMETER, "INT(100)");//直径
|
|
vDataMap.insert(_MD_HIGHT, "INT(100)");//高度
|
|
vDataMap.insert(_MD_CORRELATE, "INT(100)");//相似度
|
|
vDataMap.insert(_MD_CHANNEL, "INT(100)");
|
|
vDataMap.insert("detecttime", "VARCHAR(32)");
|
|
vDataMap.insert(_MD_UPDATE_TIME, "DATETIME(32)");
|
|
vDataMap.insert(_MD_PIC, "BLOB");
|
|
if (!CreatTable(_MD_TABLE_FORMS_MANE, _MD_PRIMARY_KEY, vDataMap))
|
|
{
|
|
//return false;
|
|
}
|
|
else
|
|
{
|
|
QString strIndex = QString("CREATE INDEX idx_wftable ON wftable (time COLLATE BINARY ASC); ");
|
|
db->exec(strIndex);//创建索引
|
|
}
|
|
}
|
|
|
|
if (!tablenamelist.contains(_MD_TIMETABLE))
|
|
{
|
|
QVariantMap vTimeMap;//保存班次时间
|
|
vTimeMap.insert(_MD_TYPE, "VARCHAR(32)");
|
|
vTimeMap.insert(_MD_STARTTIME, "VARCHAR(32)");
|
|
vTimeMap.insert(_MD_ENDTIME, "VARCHAR(32)");
|
|
vTimeMap.insert(_MD_TIMETYPY_S, "INT(100)");
|
|
vTimeMap.insert(_MD_TIMETYPY_E, "INT(100)");
|
|
if (!CreatTable(_MD_TIMETABLE, _MD_PRIMARY_KEY, vTimeMap))
|
|
{
|
|
//return false;
|
|
}
|
|
else{
|
|
|
|
}
|
|
}
|
|
|
|
if (!tablenamelist.contains(_MD_WARNINGTABLE))
|
|
{
|
|
QVariantMap vWarningMap;//报警信息
|
|
vWarningMap.insert(_MD_MESSAGE, "VARCHAR(256)");
|
|
vWarningMap.insert(_MD_CLASS, "VARCHAR(32)");
|
|
vWarningMap.insert(_MD_UPDATE_TIME, "DATETIME(32)");
|
|
if (!CreatTable(_MD_WARNINGTABLE, _MD_PRIMARY_KEY, vWarningMap))
|
|
{
|
|
//return false;
|
|
|
|
}
|
|
else{
|
|
QString strIndex = QString("CREATE INDEX idx_warningtable ON warningtable (time COLLATE BINARY ASC); ");
|
|
db->exec(strIndex);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool DetectDataDB::ReadOutTimeData(QMap<QString, TimeStruct> &m_MapTimeMatch)
|
|
{
|
|
QString select_sql = QString("select * from ") + _MD_TIMETABLE;
|
|
QSqlQuery sql = db->exec(select_sql);
|
|
QSqlError err = sql.lastError();
|
|
int t = err.type();
|
|
if (t != QSqlError::NoError)
|
|
{
|
|
return false;
|
|
}
|
|
while (sql.next())
|
|
{
|
|
int nIndex = sql.value("uid").toInt();
|
|
QString type = sql.value(_MD_TYPE).toString();
|
|
QTime start = sql.value(_MD_STARTTIME).toTime();
|
|
QTime end = sql.value(_MD_ENDTIME).toTime();
|
|
int nSt = sql.value(_MD_TIMETYPY_S).toInt();
|
|
int nEd = sql.value(_MD_TIMETYPY_E).toInt();
|
|
TimeStruct m_timeStruct;
|
|
m_timeStruct.m_Index = nIndex;
|
|
m_timeStruct.m_name = type;
|
|
m_timeStruct.m_startTime = start;
|
|
m_timeStruct.m_endTime = end;
|
|
m_timeStruct.time_start = (AMPMType)nSt;
|
|
m_timeStruct.time_end = (AMPMType)nEd;
|
|
m_MapTimeMatch.insert(type, m_timeStruct);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool DetectDataDB::AddOneWarningMessage(WarnMessage m_messageInfo)
|
|
{
|
|
QVariantMap m_map;
|
|
m_map.insert(_MD_MESSAGE, m_messageInfo.m_Message);
|
|
m_map.insert(_MD_UPDATE_TIME, m_messageInfo.m_Date);
|
|
m_map.insert(_MD_CLASS, m_messageInfo.m_class);
|
|
return InsertOneData(_MD_WARNINGTABLE, m_map);
|
|
}
|
|
bool DetectDataDB::AddOneTime(TimeStruct &m_timestruct)
|
|
{
|
|
QVariantMap m_map;
|
|
m_map.insert(_MD_TYPE, m_timestruct.m_name);
|
|
m_map.insert(_MD_STARTTIME, m_timestruct.m_startTime);
|
|
m_map.insert(_MD_ENDTIME, m_timestruct.m_endTime);
|
|
m_map.insert(_MD_TIMETYPY_S, (int)m_timestruct.time_start);
|
|
m_map.insert(_MD_TIMETYPY_E, (int)m_timestruct.time_end);
|
|
return InsertOneData(_MD_TIMETABLE, m_map);
|
|
}
|
|
bool DetectDataDB::DelOneTime(TimeStruct &m_timestruct)
|
|
{
|
|
QString m_map = gensql::genClass(_MD_TYPE, m_timestruct.m_name);//!>生成where条件语句
|
|
QString strSql = gensql::genDeleteData(_MD_TIMETABLE, m_map);
|
|
db->exec(strSql);
|
|
if (db->lastError().isValid())
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool DetectDataDB::AddOneData(QVariantMap m_map)
|
|
{
|
|
/*该部分数据是保存检测结果+结果图片*/
|
|
|
|
QString strInsert = "INSERT INTO %1(%2) VALUES(%3)";
|
|
QString strHeader;//
|
|
QString strValue;//
|
|
|
|
QStringList strKeys = m_map.keys();
|
|
for (int i = 0; i < strKeys.size(); i++) {
|
|
strHeader += strKeys[i];
|
|
if (i + 1 < strKeys.size())
|
|
strHeader += ",";
|
|
strValue += ":" + strKeys[i] + "";
|
|
|
|
if (i + 1 < strKeys.size())
|
|
strValue += ",";
|
|
}
|
|
strInsert = strInsert.arg(_MD_TABLE_FORMS_MANE, strHeader, strValue);
|
|
//return strInsert;
|
|
QSqlQuery query = db->exec();
|
|
query.prepare(strInsert);
|
|
for (int i = 0; i < strKeys.size(); i++)
|
|
{
|
|
QString m_keys = strKeys.at(i);
|
|
QString str = ":" + m_keys;
|
|
query.bindValue(str, m_map.value(m_keys));
|
|
}
|
|
query.exec();
|
|
return true;
|
|
}
|