GrpcPrint/PrintS/Config/dao/FixPointDao.cpp
2024-03-19 17:45:12 +08:00

120 lines
3.0 KiB
C++

#include "FixPointDao.h"
FixPointDao::FixPointDao(SQLite::Database* pdb) :BaseDao(pdb)
{
}
FixPointDao::~FixPointDao()
{
}
void FixPointDao::CreateTable()
{
if (m_pDB->tableExists(FixPointCfg::TABLE_NAME))return;
char buffer[1024];
memset(buffer, '\0', sizeof(buffer));
string createsql = "CREATE TABLE %s(%s INTEGER PRIMARY KEY AUTOINCREMENT,%s INTEGER,%s FLOAT,%s FLOAT,%s INTEGER)";
sprintf_s(buffer, sizeof(buffer), createsql.c_str(),
FixPointCfg::TABLE_NAME.c_str(),
FixPointCfg::FIELD_ID.c_str(),
FixPointCfg::FIELD_CNO.c_str(),
FixPointCfg::FIELD_POINT_X.c_str(),
FixPointCfg::FIELD_POINT_Y.c_str(),
FixPointCfg::FIELD_DURATION.c_str()
);
m_pDB->exec(buffer);
}
void FixPointDao::Find(int cno,FixPointWrapper& fpw)
{
string findstr = "SELECT * FROM %s WHERE %s=%d";
char buffer[256];
sprintf_s(buffer, sizeof(buffer), findstr.c_str(),
FixPointCfg::TABLE_NAME.c_str(),
FixPointCfg::FIELD_CNO.c_str(),cno);
SQLite::Statement query(*m_pDB, buffer);
while (query.executeStep()) {
FixPointCfg* cfg = new FixPointCfg();
cfg->m_Id = query.getColumn(FixPointCfg::FIELD_ID.c_str()).getInt();
cfg->m_Cno = query.getColumn(FixPointCfg::FIELD_CNO.c_str()).getInt();
cfg->m_PointX = (float)query.getColumn(FixPointCfg::FIELD_POINT_X.c_str()).getDouble();
cfg->m_PointY = (float)query.getColumn(FixPointCfg::FIELD_POINT_Y.c_str()).getDouble();
cfg->m_Duration = query.getColumn(FixPointCfg::FIELD_DURATION.c_str()).getUInt();
//fpw.Lock();
fpw.m_Cfgs.push_back(cfg);
//fpw.Unlock();
}
}
void FixPointDao::Save(int cno, FixPointWrapper& fpw)
{
for (list<FixPointCfg*>::iterator it = fpw.m_Cfgs.begin();it!=fpw.m_Cfgs.end(); it++) {
FixPointCfg* pcfg = (*it);
if (pcfg == NULL)continue;
m_pDB->exec(pcfg->GetUpdateSql());
}
}
bool FixPointDao::Add(FixPointCfg* cfg)
{
if (!cfg)return false;
bool rel = true;
char buffer[256];
string strsql = "INSERT INTO %s(%s,%s,%s,%s) VALUES(%d,%.3f,%.3f,%u)";
sprintf_s(buffer, sizeof(buffer), strsql.c_str(),
FixPointCfg::TABLE_NAME.c_str(),
FixPointCfg::FIELD_CNO.c_str(),
FixPointCfg::FIELD_POINT_X.c_str(),
FixPointCfg::FIELD_POINT_Y.c_str(),
FixPointCfg::FIELD_DURATION.c_str(),
cfg->m_Cno,cfg->m_PointX,cfg->m_PointY,cfg->m_Duration);
try
{
if (m_pDB->exec(buffer))
{
sprintf_s(buffer, sizeof(buffer), "SELECT last_insert_rowid() FROM %s", FixPointCfg::TABLE_NAME.c_str());
SQLite::Statement query(*m_pDB, string(buffer));
if (query.executeStep()) {
cfg->m_Id=query.getColumn(0).getInt();
}
}
else {
rel = false;
}
}
catch (SQLite::Exception &e)
{
(void)e;
rel = false;
}
return rel;
}
bool FixPointDao::Del(FixPointCfg* cfg)
{
if (!cfg)return false;
bool rel = true;
char buffer[256];
string strsql = "DELETE FROM %s WHERE %s=%d";
sprintf_s(buffer, sizeof(buffer), strsql.c_str(),
FixPointCfg::TABLE_NAME.c_str(),
FixPointCfg::FIELD_ID.c_str(), cfg->m_Id);
try
{
if (!m_pDB->exec(buffer))
{
rel = false;
}
}
catch (SQLite::Exception &e)
{
(void)e;
rel = false;
}
return rel;
}