📄 aesqloperation.cpp
字号:
#include "AESQLOperation.h"
CAESQLOperation::CAESQLOperation(void)
{
}
CAESQLOperation::~CAESQLOperation(void)
{
}
// 初始化数据库连接
psInt32 CAESQLOperation::ConnectMySQL()
{
// 数据库已经连接则返回
if (db.connected == 1)
{
return SUCCESS;
}
// 连接数据库
otl_connect::otl_initialize();
try
{
db.rlogon("UID=root;PWD=123;DSN=mysql");
}
catch (otl_exception& p)
{
return ERR_CONNECT_SQL_FAIL;
}
return SUCCESS;
}
// 断开数据库连接
psVoid CAESQLOperation::DisconnectMySQL()
{
db.logoff();
}
// 供第一次启动时调用,创建数据表
psInt32 CAESQLOperation::CreateTheTable()
{
try
{ // 设置表的各个字段
char *sqlstr="create table aeinfo(EventNumber BIGINT unsigned, Remote char(1),ItemID int unsigned, Source char(255),"
"ComputerID smallint unsigned, ApplicationID smallint unsigned, UserID smallint unsigned, StampSec BIGINT,"
"StampMil smallint unsigned, StartSec BIGINT, StartMil smallint unsigned, EndSec BIGINT, EndMil smallint unsigned,"
"EventTopic smallint unsigned, EventCategory smallint unsigned, EventSubCategory tinyint unsigned, EventClass smallint unsigned,"
"EventString char(255), Quality tinyint unsigned, Acked tinyint unsigned, ActorID smallint unsigned, AckSec BIGINT, AckMil smallint unsigned,"
"NumberofSubstitutions tinyint unsigned, Substitutions char(255), PRIMARY KEY (EventNumber) )";
otl_cursor::direct_exec(
db,
sqlstr
);
}
catch (otl_exception& p)
{
// 数据库连接断开
if (db.connected == 0)
{
return ERR_CONNECT_SQL_FAIL;
}
// 数据表已经存在
if (p.code == 1050)
{
return SUCCESS;
}
return ERR_SQL_ERROR;
}
return SUCCESS;
}
// 向数据库中写入事件信息
psInt32 CAESQLOperation::InsertAEInfo(CAEInfo Info)
{
psUInt64 number = Info.GetEventNumber();
psBool remote = Info.GetRemote();
psUInt32 itemid = Info.GetItemID();
psChar source[SOURCE_LENGTH];
Info.GetSource(source);
psUInt16 computerid = Info.GetComputerID();
psUInt16 applicationid = Info.GetApplicationID();
psUInt16 userid = Info.GetUserID();
psInt64 stampsec = Info.GetTimestamp().tTime;
psUInt16 stampmil = Info.GetTimestamp().shMill;
psInt64 startsec = Info.GetStartTime().tTime;
psUInt16 startmil = Info.GetStartTime().shMill;
psInt64 endsec = Info.GetEndTime().tTime;
psUInt16 endmil = Info.GetEndTime().shMill;
psUInt16 eventtopic = Info.GetEventTopic();
psUInt16 eventcategory = Info.GetEventCategory();
psUInt8 eventsubcategory = Info.GetSubCategory();
psUInt16 eventclass = Info.GetEventClass();
psChar eventstring[EVENTSTRING_LENGTH];
Info.GetEventString(eventstring);
psUInt8 quality = Info.GetQuality();
psUInt8 acked = Info.GetAcked();
psUInt16 actorid = Info.GetActorID();
psInt64 acksec = Info.GetAckTime().tTime;
psUInt16 ackmil = Info.GetAckTime().shMill;
psUInt8 numberofsubstitutions = Info.GetNumberOfSubstitutions();
psChar substitutions[SUBSTITUTION_LENGTH];
Info.GetSubstitutions(substitutions);
psChar sqlstr[SQL_STR_LENGTH];
//生成SQL字符串
sprintf(
sqlstr,
"insert into aeinfo values(%llu, %d, %u, '%s', %u, %u, %u, %lld, %u,"
"%lld, %u, %lld, %u, %u, %u, %u, %u, '%s', %u, %u, %u, %lld, %u, %u, '%s')",
number,
remote,
itemid,
source,
computerid,
applicationid,
userid,
stampsec,
stampmil,
startsec,
startmil,
endsec,
endmil,
eventtopic,
eventcategory,
eventsubcategory,
eventclass,
eventstring,
quality,
acked,
actorid,
acksec,
ackmil,
numberofsubstitutions,
substitutions
);
try
{
otl_stream o(1,sqlstr,db);
}
catch (otl_exception& p)
{
// 数据库连接断开
if (db.connected == 0)
{
return ERR_CONNECT_SQL_FAIL;
}
// 数据表不存在
if (p.code == 1146)
{
return ERR_TABLE_NOT_EXIST;
}
// 关键字重复
if (p.code == 1062)
{
return ERR_DULPLICATE_KEY;
}
// 硬盘空间不足
if (p.code == 1021)
{
return ERR_DISKSPACE_FULL;
}
return ERR_SQL_ERROR;
}
return SUCCESS;
}
// SQL查询语句条件字符串的连接
psVoid CAESQLOperation::SqlCondition(psChar *sqlstr,CAEFilterInfo filter)
{
// 处理字符串的连接,在此没有处理区域查询
psChar sqlstm[SQL_STR_LENGTH] = "\0";
psChar temp[SQL_STR_LENGTH];
psChar *pConstStr = "select * from aeinfo";
psChar ucStartSec[40];
psChar ucStartMil[15];
psChar ucEndSec[40];
psChar ucEndMil[15];
psChar cSource[SOURCE_LENGTH];
psChar cEventString[EVENTSTRING_LENGTH];
psChar cSubstitution[SUBSTITUTION_LENGTH];
psInt8 cConditionCount = 0; //条件的计数
// 是否设置了开始时间查询条件
if (filter.GetStartTimeSet() == 1)
{
sprintf(ucStartSec, "%lld", filter.GetStartTime().tTime);
sprintf(ucStartMil, "%u", filter.GetStartTime().shMill);
sprintf(temp, "%s where (StampSec>%s or (StampSec=%s and StampMil>=%s))",
pConstStr, ucStartSec, ucStartSec, ucStartMil);
strcat(sqlstm, temp);
cConditionCount++;
}
// 是否设置了结束时间查询条件
if (filter.GetEndTimeSet() == 1)
{
sprintf(ucEndSec, "%lld", filter.GetEndTime().tTime);
sprintf(ucEndMil, "%u", filter.GetEndTime().shMill);
if (cConditionCount==0)
{
sprintf(temp, "%s where (StampSec<%s or (StampSec=%s and StampMil<=%s)) ",
pConstStr, ucEndSec, ucEndSec, ucEndMil);
strcat(sqlstm, temp);
}
else
{
sprintf(temp, " and (StampSec<%s or (StampSec=%s and StampMil<=%s))",
ucEndSec, ucEndSec, ucEndMil);
strcat(sqlstm, temp);
}
cConditionCount++;
}
// 是否设置了按事件源查询条件
if (filter.GetSourceSet() == 1)
{
filter.GetSource(cSource);
if (cConditionCount == 0)
{
sprintf(temp, "%s where Source like '%s'", pConstStr,cSource);
strcat(sqlstm, temp);
}
else
{
sprintf(temp, " and Source like '%s'", cSource);
strcat(sqlstm, temp);
}
cConditionCount++;
}
// 是否设置了按测点查询条件
if (filter.GetItemIDSet() == 1)
{
if (cConditionCount == 0)
{
sprintf(temp, "%s where ItemID=%u", pConstStr, filter.GetItemID());
strcat(sqlstm, temp);
}
else
{
sprintf(temp, " and ItemID=%u", filter.GetItemID());
strcat(sqlstm, temp);
}
cConditionCount++;
}
// 是否设置了按客户端查询条件
if (filter.GetComputerIDSet() == 1)
{
if (cConditionCount == 0)
{
sprintf(temp, "%s where ComputerID=%u", pConstStr, filter.GetComputerID());
strcat(sqlstm, temp);
}
else
{
sprintf(temp, " and ComputerID=%u", filter.GetComputerID());
strcat(sqlstm, temp);
}
cConditionCount++;
}
// 是否设置了按事件编号查询条件
if (filter.GetEventNumberSet() == 1)
{
if (cConditionCount==0)
{
sprintf(temp, "%s where EventNumber=%llu", pConstStr, filter.GetEventNumber());
strcat(sqlstm, temp);
}
else
{
sprintf(temp," and EventNumber=%llu", filter.GetEventNumber());
strcat(sqlstm, temp);
}
cConditionCount++;
}
// 是否设置了按事件主题查询条件
if (filter.GetEventTopicSet() == 1)
{
if (cConditionCount == 0)
{
sprintf(temp, "%s where EventTopic=%u", pConstStr, filter.GetEventTopic());
strcat(sqlstm, temp);
}
else
{
sprintf(temp, " and EventTopic=%u", filter.GetEventTopic());
strcat(sqlstm, temp);
}
cConditionCount++;
}
// 是否设置按报警类别查询条件
if (filter.GetEventCategorySet() == 1)
{
if (cConditionCount == 0)
{
sprintf(temp, "%s where EventCategory=%u", pConstStr, filter.GetEventCategory());
strcat(sqlstm, temp);
}
else
{
sprintf(temp, " and EventCategory=%u", filter.GetEventCategory());
strcat(sqlstm, temp);
}
cConditionCount++;
}
// 是否设置按应答查询条件
if (filter.GetAckedSet() == 1)
{
if (cConditionCount == 0)
{
sprintf(temp, "%s where Acked=%u", pConstStr, filter.GetAcked());
strcat(sqlstm, temp);
}
else
{
sprintf(temp, " and Acked=%u", filter.GetAcked());
strcat(sqlstm, temp);
}
cConditionCount++;
}
// 是否设置按应答者查询
if (filter.GetActorIDSet() == 1)
{
if (cConditionCount == 0)
{
sprintf(temp, "%s where ActorID=%u",pConstStr, filter.GetActorID());
strcat(sqlstm, temp);
}
else
{
sprintf(temp, " and ActorID=%u", filter.GetActorID());
strcat(sqlstm, temp);
}
cConditionCount++;
}
// 是否设置查询级别范围
if (filter.GetLowClassSet() == 1)
{
if (cConditionCount == 0)
{
sprintf(temp, "%s where EventClass>=%u", pConstStr, filter.GetLowClass());
strcat(sqlstm, temp);
}
else
{
sprintf(temp, " and EventClass>=%u", filter.GetLowClass());
strcat(sqlstm, temp);
}
cConditionCount++;
}
if (filter.GetHighClassSet() == 1)
{
if (cConditionCount == 0)
{
sprintf(temp, "%s where EventClass<=%u", pConstStr, filter.GetHighClass());
strcat(sqlstm, temp);
}
else
{
sprintf(temp, " and EventClass<=%u", filter.GetHighClass());
strcat(sqlstm, temp);
}
cConditionCount++;
}
// 设置查询事件内容
if (filter.GetEventStringSet() == 1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -