Commit 6d82f512 authored by 刘乐's avatar 刘乐

1,追踪分析接口优化

parent 7e05214c
......@@ -25,7 +25,7 @@ void CivConTrackTest::test(char* uri)
char sn[32];
strcpy_s(sn, "JD00000016");
int succ = trackingSimulation(uri, sn, 5, result);
int succ = trackingSimulation(uri, sn, 12, result);
std::cout << result << std::endl;
std::cout << "测试结果:" << succ << std::endl;
......
......@@ -24,11 +24,11 @@ int main(int argc, char* argv[])
context.contextTest(findUri);*/
// 测试追踪分析
/*CivTestContext context(new CivConTrackTest());
context.contextTest(findUri);*/
CivTestContext context(new CivConTrackTest());
context.contextTest(findUri);
// 测试根据时段获取值
/* CivTestContext context(new CivConGetDataTest());
/*CivTestContext context(new CivConGetDataTest());
context.contextTest(findUri);*/
// 测试根据
......@@ -36,8 +36,8 @@ int main(int argc, char* argv[])
context.contextTest(findUri);*/
// 上游追踪
CivTestContext context(new CivUpstreamTrackingTest());
context.contextTest(findUri);
/*CivTestContext context(new CivUpstreamTrackingTest());
context.contextTest(findUri);*/
// 下游追踪
/*CivTestContext context(new CivDownStreamTrackingTest());
......
......@@ -6,7 +6,7 @@
#include "CivHydrSimulation.h"
#include "CivTrackingAnalysis.h"
bool __declspec(dllexport) getDataByInterval(char* uri, char* date, char* interval)
bool FUNEXPORT getDataByInterval(char* uri, char* date, char* interval)
{
CivDbConn* dbConn = new CivPgConn(uri);
if (!dbConn->open())
......@@ -31,7 +31,7 @@ bool __declspec(dllexport) getDataByInterval(char* uri, char* date, char* interv
return true;
}
bool __declspec(dllexport) hdyrSimulation(char* uri)
bool FUNEXPORT hdyrSimulation(char* uri)
{
// 创建数据库连接
CivDbConn* pgConn = new CivPgConn(uri);
......@@ -48,7 +48,7 @@ bool __declspec(dllexport) hdyrSimulation(char* uri)
return flag;
}
bool __declspec(dllexport) qualitySimulation(char* uri)
bool FUNEXPORT qualitySimulation(char* uri)
{
// 创建数据库连接
CivDbConn* pgConn = new CivPgConn(uri);
......@@ -66,7 +66,7 @@ bool __declspec(dllexport) qualitySimulation(char* uri)
return flag;
}
bool __declspec(dllexport) trackingSimulation(char* uri, char* sN, int hours, char* result)
bool FUNEXPORT trackingSimulation(char* uri, char* sN, int hours, char* result)
{
// 创建数据库连接
......@@ -92,7 +92,7 @@ bool __declspec(dllexport) trackingSimulation(char* uri, char* sN, int hours, ch
return flag;
}
bool __declspec(dllexport) getRptByCondition(char* uri, char* type, char* condition, char* result)
bool FUNEXPORT getRptByCondition(char* uri, char* type, char* condition, char* result)
{
CivDbConn* dbConn = new CivPgConn(uri);
if (!dbConn->open())
......@@ -176,7 +176,7 @@ bool __declspec(dllexport) getRptByCondition(char* uri, char* type, char* condit
return true;
}
bool __declspec(dllexport) upstreamTracking(char* uri, char* sn, char* result)
bool FUNEXPORT upstreamTracking(char* uri, char* sn, char* result)
{
// 创建数据库连接
CivDbConn* pgConn = new CivPgConn(uri);
......@@ -199,7 +199,7 @@ bool __declspec(dllexport) upstreamTracking(char* uri, char* sn, char* result)
return true;
}
bool __declspec(dllexport) downstreamTracking(char* uri, char* sn, char* result)
bool FUNEXPORT downstreamTracking(char* uri, char* sn, char* result)
{
// 创建数据库连接
CivDbConn* pgConn = new CivPgConn(uri);
......@@ -222,7 +222,7 @@ bool __declspec(dllexport) downstreamTracking(char* uri, char* sn, char* result)
return true;
}
bool __declspec(dllexport) waterSupplyScopeAnalysis(char* uri, char* sn, char* result)
bool FUNEXPORT waterSupplyScopeAnalysis(char* uri, char* sn, char* result)
{
// 创建数据库连接
CivDbConn* pgConn = new CivPgConn(uri);
......
#ifndef CIVHYDRFUNCINTER_H
#define CIVHYDRFUNCINTER_H
#define FUNEXPORT extern "C" __declspec(dllexport)
#define FUNEXPORT __declspec(dllexport)
#if defined(__cplusplus)
extern "C" {
......@@ -13,7 +13,7 @@ extern "C" {
*@param sn 追踪节点本点号
*@param result 追踪结果
*/
bool __declspec(dllexport) upstreamTracking(char* uri, char* sn, char* result);
bool FUNEXPORT upstreamTracking(char* uri, char* sn, char* result);
/**
*@brief 下游追踪
......@@ -21,7 +21,7 @@ extern "C" {
*@param sn 追踪节点本点号
*@param result 追踪结果
*/
bool __declspec(dllexport) downstreamTracking(char* uri, char* sn, char* result);
bool FUNEXPORT downstreamTracking(char* uri, char* sn, char* result);
/**
*@brief 供水范围追踪
......@@ -29,7 +29,7 @@ extern "C" {
*@param sn 追踪节点本点号
*@param result 追踪结果
*/
bool __declspec(dllexport) waterSupplyScopeAnalysis(char* uri, char* sn, char* result);
bool FUNEXPORT waterSupplyScopeAnalysis(char* uri, char* sn, char* result);
/**
*@brief 获取当前日期和时段的结果值
......@@ -39,21 +39,21 @@ extern "C" {
*@param result 获取的结果值
*@return 1: 成功,其他值失败
*/
bool __declspec(dllexport) getRptByCondition(char* uri, char* type, char* condition, char* result);
bool FUNEXPORT getRptByCondition(char* uri, char* type, char* condition, char* result);
/**
*@brief 水力计算服务
*@param uri:数据库连接地址
*@return 1:成功,其他值失败
*/
bool __declspec(dllexport) hdyrSimulation(char* uri);
bool FUNEXPORT hdyrSimulation(char* uri);
/**
*@brief 水质计算
*@param uri:数据库连接地址
*@return 1:成功,其他值失败
*/
bool __declspec(dllexport) qualitySimulation(char* uri);
bool FUNEXPORT qualitySimulation(char* uri);
/**
*@brief 水质追踪分析,扩散分析
......@@ -63,7 +63,7 @@ extern "C" {
*@param result:输出结果:json字符串
*@return 1:成功,其他值失败
*/
bool __declspec(dllexport) trackingSimulation(char* uri, char* sN, int hours,char* result);
bool FUNEXPORT trackingSimulation(char* uri, char* sN, int hours,char* result);
/**
*@brief 获取管网计算特定时段的结果
......@@ -72,7 +72,7 @@ extern "C" {
*@param interval: 时段,整数
*@return 成功,失败
*/
bool __declspec(dllexport) getDataByInterval(char* uri, char* date, char* interval);
bool FUNEXPORT getDataByInterval(char* uri, char* date, char* interval);
#if defined(__cplusplus)
}
......
......@@ -224,7 +224,6 @@ bool CivHydrSimulation::hdyrInstantSimulation()
return true;
}
bool CivHydrSimulation::hdyrSimulation()
{
......@@ -277,7 +276,7 @@ bool CivHydrSimulation::brushingResult()
}
// 更新管网数据库数据
updateLastestDataToGisNet(mDbConn, "0");
// updateLastestDataToGisNet(mDbConn, "0");
return true;
......@@ -299,15 +298,15 @@ bool CivHydrSimulation::updateLastestDataToGisNet(CivDbConn* dbConn, const std::
if (dbConn == nullptr)
return false;
dbConn->updateNodeByInterval(CurrentDate, time);
dbConn->updateLinkByInterval(CurrentDate, time);
dbConn->updateNodeByInterval(CurrentDate, time);
return true;
}
bool CivHydrSimulation::qualitySimulation()
{
CivSysLog::getInstance()->error("开始水质分析计算", "qualitySimulation", __FUNCTION__);
CivSysLog::getInstance()->info("开始水质分析计算", "qualitySimulation", __FUNCTION__);
if (!convertInp(ANALYSIS_AGE, ""))
{
CivSysLog::getInstance()->error("写入inp文件失败", "qualitySimulation", __FUNCTION__);
......@@ -328,6 +327,7 @@ bool CivHydrSimulation::qualitySimulation()
CivSysLog::getInstance()->error("水质分析结果存储失败", "CivHydrCalc", __FUNCTION__);
return false;
}
return true;
}
......@@ -348,16 +348,7 @@ bool CivHydrSimulation::trackingSimulation(char* snNode, int hour)
CivSysLog::getInstance()->error("追踪分析计算失败", "CivHydrCalc", __FUNCTION__);
return false;
}
// 扩散分析不存储数据
//模拟数据刷新
if (!brushingResult())
{
CivSysLog::getInstance()->error("追踪分析计算失败", "CivHydrCalc", __FUNCTION__);
return false;
}
return true;
}
......@@ -415,15 +406,19 @@ bool CivHydrSimulation::qualityCompute()
ENclose();
return false;
}
// 获取当前节点和管段数据
getNodeResult(iTime);
getLinkResult(iTime);
ENnextH(&tstep);
iTime++;
if (mQuaLityType == ANALYSIS_TRACE)
continue;
// 获取当前节点和管段数据
getNodeResult(iTime);
getLinkResult(iTime);
} while (tstep > 0);
ENcloseH();
......@@ -463,101 +458,83 @@ bool CivHydrSimulation::qualityCompute()
return true;
}
void CivHydrSimulation::getTrackingResult(int hours, std::string& newresult)
void CivHydrSimulation::getTrackingResult(int hours, std::string& jsonResult)
{
std::string result;
result.append("[");
// 缓存节点和管段空间信息
CivTrackingResultCache trackingCache;
trackingCache.cacheInfoFromDb(mDbConn);
jsonResult.append("[");
std::stringstream stream;
for (int i = 0; i < hours; i++)
{
NodeResultItems nodeItemsMap;
LinkResultItems linkItemsMap;
mResultCache.getResultByTime(i, nodeItemsMap, linkItemsMap);
// 查找节点追踪的节点编号
std::string nodeTempText;
nodeTempText.append("{\"type\":\"污染源\",\"location\":[");
auto nodeIter = nodeItemsMap.begin();
size_t nodeTotal = nodeItemsMap.size();
while (nodeIter != nodeItemsMap.end())
{
if (nodeIter->second.dQuality > 0)
{
nodeTempText.append("{\"value\":[");
std::string sn = nodeIter->first;
Junctions junction;
trackingCache.getNodeInfoBySn(sn, junction);
nodeTempText.append(junction.xCoord);
nodeTempText.append(",");
nodeTempText.append(junction.yCoord);
nodeTempText.append("],");
nodeTempText.append("\"code\":");
nodeTempText.append(sn.c_str());
nodeTempText.append("},");
}
nodeIter++;
}
std::string newNodeTempText = nodeTempText.substr(0, nodeTempText.length() - 1);
newNodeTempText.append("]}");
// 追踪管段编号
auto linkIter = linkItemsMap.begin();
size_t total = linkItemsMap.size();
int index = 1;
std::string linkTempText;
linkTempText.append("{\"type\":\"污染管段\",\"location\":[");
while (linkIter != linkItemsMap.end())
{
if (linkIter->second.dQuality > 0)
{
std::string sn = linkIter->first;
linkTempText.append("{\"coords\":[");
Pipes pipe;
trackingCache.getPipeInfoBySn(sn, pipe);
linkTempText.append("[");
linkTempText.append(pipe.startX);
linkTempText.append(",");
linkTempText.append(pipe.startY);
linkTempText.append("],");
linkTempText.append("[");
linkTempText.append(pipe.endX);
linkTempText.append(",");
linkTempText.append(pipe.endY);
linkTempText.append("]");
linkTempText.append("]},");
}
linkIter++;
}
std::string newLinkTempText = linkTempText.substr(0, linkTempText.length() - 1);
newLinkTempText.append("]}");
result.append("[");
result.append(newNodeTempText);
result.append(",");
result.append(newLinkTempText);
result.append("],");
auto iter = mTrackLinkSns.find(i);
if (iter == mTrackLinkSns.end())
{
continue;
}
jsonResult.append("[");
std::vector<std::string> vecSns = iter->second;
for (int j = 0; j < vecSns.size(); j++)
{
Pipes pipe;
std::string sn = vecSns[j];
trackingCache.getPipeInfoBySn(sn, pipe);
jsonResult.append("[[");
jsonResult.append(pipe.startX);
jsonResult.append(",");
jsonResult.append(pipe.startY);
jsonResult.append("],[");
jsonResult.append(pipe.endX);
jsonResult.append(",");
jsonResult.append(pipe.endY);
jsonResult.append("]],");
}
jsonResult = jsonResult.substr(0, jsonResult.length() - 1);
jsonResult.append("],");
}
newresult = result.substr(0, result.length() -1 );
newresult.append("]");
//std::stringstream stream;
//for (int i = 0; i < hours; i++)
//{
// NodeResultItems nodeItemsMap;
// LinkResultItems linkItemsMap;
// mResultCache.getResultByTime(i, nodeItemsMap, linkItemsMap);
//
// // 追踪管段编号
// auto linkIter = linkItemsMap.begin();
// size_t total = linkItemsMap.size();
// int index = 1;
// jsonResult.append("[");
// while (linkIter != linkItemsMap.end())
// {
// if (linkIter->second.dQuality > 0)
// {
// std::string sn = linkIter->first;
//
// Pipes pipe;
// trackingCache.getPipeInfoBySn(sn, pipe);
// jsonResult.append("[[");
// jsonResult.append(pipe.startX);
// jsonResult.append(",");
// jsonResult.append(pipe.startY);
// jsonResult.append("],[");
// jsonResult.append(pipe.endX);
// jsonResult.append(",");
// jsonResult.append(pipe.endY);
// jsonResult.append("]],");
// }
// linkIter++;
// }
// jsonResult = jsonResult.substr(0, jsonResult.length() - 1);
// jsonResult.append("],");
//}
jsonResult = jsonResult.substr(0, jsonResult.length() - 1);
jsonResult.append("]");
}
void CivHydrSimulation::getNodeResult(short time)
......@@ -661,27 +638,29 @@ void CivHydrSimulation::getLinkResult(short time)
void CivHydrSimulation::getNodeQuality(short time)
{
if (mQuaLityType == ANALYSIS_TRACE)
{
return;
}
int nNodeCount;
ENgetcount(EN_NODECOUNT, &nNodeCount);
float* quality = (float*)malloc(sizeof(float));
for (int i = 1; i <= nNodeCount; i++)
{
char No[35];
// 编号
ENgetnodeid(i, No);
float* quality = (float*)malloc(sizeof(float));
// 水质
ENgetnodevalue(i, EN_QUALITY, quality);
int trueQuality = *quality;
if (mQuaLityType == ANALYSIS_TRACE)
trueQuality *= 100;
mResultCache.addNodeQuality(trueQuality, time, No);
free(quality);
mResultCache.addNodeQuality(*quality, time, No);
}
free(quality);
}
void CivHydrSimulation::getLinkQuality(short time)
......@@ -705,11 +684,17 @@ void CivHydrSimulation::getLinkQuality(short time)
// 水质
ENgetlinkvalue(i, EN_LINKQUAL, quality);
int trueQuality = *quality;
if (mQuaLityType == ANALYSIS_TRACE)
trueQuality *= 100;
if (mQuaLityType != ANALYSIS_TRACE )
{
mResultCache.addLinkQuality(*quality, time, lnkNo);
continue;
}
mResultCache.addLinkQuality(trueQuality, time, lnkNo);
if (*quality > 0)
{
mTrackLinkSns[time].push_back(lnkNo);
}
}
free(typeCode);
free(quality);
......
......@@ -109,6 +109,8 @@ private:
CivDbConn* mDbConn;
CivSimulResultCache mResultCache; // 存储模拟结果缓存类
QualityAnalyType mQuaLityType = ANALYSIS_RESERVE;
std::map<int,std::vector<std::string>> mTrackLinkSns;
std::string mInpFile; // inp文件名
std::string mRptFile; // 报告文件名
std::string mBinFile;// 二进制文件名
......
......@@ -326,8 +326,10 @@ void CivNewInp::setQuality(const CivQuality& quality)
{
writeString("1");
continue;
}
else {
writeString("0");
}
writeString("0");
}
else
{
......@@ -461,6 +463,7 @@ void CivNewInp::setTimes(const CivParameter& params)
{
CivParameter::ParamTable table = *iter;
writeStringFormat(table.name);
if (mDuration >-1 && table.name == "Duration")
{
writeString(std::to_string(mDuration));
......
#include "CivProjManager.h"
CivProjManager::CivProjManager()
{
}
CivProjManager::~CivProjManager()
{
}
\ No newline at end of file
#pragma once
/**
*/
class CivProjManager
{
public:
explicit CivProjManager();
~CivProjManager();
};
\ No newline at end of file
#include "CivProject.h"
CivProject::CivProject()
{
}
CivProject::~CivProject()
{
}
\ No newline at end of file
#pragma once
/*
*/
class CivProject
{
public:
explicit CivProject();
~CivProject();
};
......@@ -16,6 +16,20 @@ void CivSimulResultCache::addNodeQuality(float quality, int interval, Str sNo)
bool CivSimulResultCache::updateToDb(CivDbConn* dbConn)
{
if (dbConn == nullptr)
return false;
// 更行节点
for (auto iter = mNodeItemsMap.begin();iter!=mNodeItemsMap.end();iter++)
{
dbConn->updateNode(iter->second);
}
for (auto iter = mLinkItemsMap.begin(); iter != mLinkItemsMap.end(); iter++)
{
dbConn->updateLink(iter->second);
}
return true;
}
......
......@@ -58,6 +58,7 @@ bool CivTrackingAnalysis::createGraphFrom(CivDbConn* dbConn)
std::list<CivPipe::PipesTable> pipesTableLis = civPipe.mTables;
std::list<CivPipe::PipesTable>::iterator pIter;
for (pIter = pipesTableLis.begin(); pIter != pipesTableLis.end(); pIter++)
{
CivPipe::PipesTable pipeTable = *pIter;
......@@ -78,7 +79,15 @@ bool CivTrackingAnalysis::createGraphFrom(CivDbConn* dbConn)
CivGraphEdage edAge(pipeTable.ID, pipeTable.Length, { sStartjuction.getXCoord(),sStartjuction.getYCoord(),endJunction.getXCoord(),endJunction.getYCoord() });
mGraph->insertAEdge(sStartjuction, endJunction, edAge);
}
else if (sStartjuction.getHead() == endJunction.getHead())
{
CivGraphEdage edAge1(pipeTable.ID, pipeTable.Length, { sStartjuction.getXCoord(),sStartjuction.getYCoord(),endJunction.getXCoord(),endJunction.getYCoord() });
mGraph->insertAEdge(sStartjuction, endJunction, edAge1);
CivGraphEdage edAge2(pipeTable.ID, pipeTable.Length, { endJunction.getXCoord(),endJunction.getYCoord(), sStartjuction.getXCoord(),sStartjuction.getYCoord() });
mGraph->insertAEdge(endJunction, sStartjuction, edAge2);
}
else
{
CivGraphEdage edAge(pipeTable.ID, pipeTable.Length, { endJunction.getXCoord(),endJunction.getYCoord(), sStartjuction.getXCoord(),sStartjuction.getYCoord() });
......
......@@ -160,6 +160,8 @@
<ClCompile Include="CivBuilder.cpp" />
<ClCompile Include="CivInpDirector.cpp" />
<ClCompile Include="CivNewInp.cpp" />
<ClCompile Include="CivProject.cpp" />
<ClCompile Include="CivProjManager.cpp" />
<ClCompile Include="CivSimulResultCache.cpp" />
<ClCompile Include="CivTrackingAnalysis.cpp" />
</ItemGroup>
......@@ -172,6 +174,8 @@
<ClInclude Include="CivBuilder.h" />
<ClInclude Include="CivInpDirector.h" />
<ClInclude Include="CivNewInp.h" />
<ClInclude Include="CivProject.h" />
<ClInclude Include="CivProjManager.h" />
<ClInclude Include="CivSimulResultCache.h" />
<ClInclude Include="CivTrackingAnalysis.h" />
</ItemGroup>
......
......@@ -45,6 +45,12 @@
<ClCompile Include="CivGraphFactory.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CivProjManager.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CivProject.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CivHydrFuncInter.h">
......@@ -77,5 +83,11 @@
<ClInclude Include="CivGraphFactory.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivProjManager.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivProject.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
#include "CivConnection.h"
CivConnection::CivConnection()
:mValid(false)
{
}
......@@ -11,9 +10,107 @@ CivConnection::~CivConnection()
}
bool CivConnection::isValid() const
bool CivConnection::update(const std::string& table, const std::map<std::string, std::string>& result, const std::string& where)
{
return mValid;
std::string sql = "update " + table + "set ";
std::map<std::string, std::string>::const_iterator iter = result.begin();
for (; iter != result.end(); iter++)
{
std::string filedName = iter->first;
std::string fieldVallue = iter->second;
sql.append(filedName + "=" + fieldVallue);
sql.append(",");
}
sql = sql.substr(0, sql.length() - 1);
if (!execSql(sql))
return false;
return true;
}
bool CivConnection::del(const std::string& table, const std::string& where)
{
if (table.empty() || where.empty())
return false;
std::string sql = "delete from "+table +"where "+where;
if (!execSql(sql))
return false;
return true;
}
bool CivConnection::insert(const std::string& table, const std::map<std::string, std::string>& result)
{
if (table.empty() || result.size() <= 0)
return false;
// 拼接sql语句
std::string sql = "INSERT INTO \""+table+"\"(";
std::string vals = "(";
for (auto iter = result.begin(); iter != result.end(); iter++)
{
std::string field = iter->first;
std::string fieldValue = iter->second;
sql.append(field);
sql.append(",");
vals.append(fieldValue);
vals.append(",");
}
sql = sql.substr(0, sql.length() - 1);
vals = vals.substr(0, vals.length() - 1);
sql.append(") VALUES");
sql.append(vals);
//2 执行插入操作
if (!execSql(sql))
return false;
return true;
}
bool CivConnection::query(const std::string& table, const std::vector<std::string>& fields, std::vector<std::map<std::string,std::string>>& result, const std::string& where)
{
//1 拼接sql语句
if (fields.size() <= 0 || table.empty())
return false;
std::string sql = "select ";
int total = fields.size();
for (int i = 0; i < total; i++)
{
sql.append(fields[i]);
sql.append(",");
}
sql = sql.substr(0, sql.length() - 1);
sql.append("from ");
sql.append(table);
if (!where.empty())
{
sql.append("where ");
sql.append(where);
}
// 2 执行sql语句
if (!execSql(sql))
{
return false;
}
// 3 获取执行结果集
queryResult(result);
return true;
}
std::string CivConnection::getLastError() const
......
#pragma once
#include <string>
#include <map>
#include <vector>
/**
数据库连接类
......@@ -10,38 +13,65 @@ public:
CivConnection();
virtual ~CivConnection();
virtual bool connect(const std::string url) = 0;
/**
*@brief 建立数据,与具体的数据有关子类实现
*/
virtual bool open() = 0;
virtual bool connect(const std::string url) = 0;
/**
*@brief 关闭数据,与具体的数据有关子类实现
*/
virtual bool close() = 0;
virtual bool disconnect() = 0;
/**
*@brief 执行sql, 非查询操作,与具体的数据有关子类实现
*@param 非查询类,sql语句
*@brief 执行查询操作,与具体的数据有关子类实现
*@param sql 执行sql语句
*/
virtual bool execNoQquery(const std::string& sql) = 0;
virtual bool execSql(const std::string& sql) = 0;
/**
*@brief 执行查询操作,与具体的数据有关子类实现
*@param sql 查询sql语句
*@brief 获取查询结果
*@param sql 结果集
*/
virtual bool execQuery(const std::string& sql) = 0;
virtual bool queryResult(std::vector<std::map<std::string, std::string>>& result) = 0;
// 检查连接
bool isValid() const;
/**
*@brief 删除数据
*@param table 表名
*@param result 需要更新的字段和字段值映射
*@param where 条件
*/
bool update(const std::string& table, const std::map<std::string, std::string>& result, const std::string& where = "1=1");
/**
*@brief 删除数据
*@param table 表名
*@param where 删除条件
*/
bool del(const std::string& table, const std::string& where = "1=1");
/**
*@brief 往数据库表插入数据
*@param table 表名
*@param result 需要添加的数据,字段和字段值的
*/
bool insert(const std::string& table, const std::map<std::string, std::string>& result);
/**
*@brief 执行查询操作,与具体的数据有关子类实现
*@param table 待查询的表名
*@param fields 需要查询的字段
*@param result 查询结果
*@param where 查询条件语句,默认
*/
bool query(const std::string& table,
const std::vector<std::string>&fields,
std::vector<std::map <std::string,std::string>>& result ,
const std::string& where = "1=1");
// 获取信息
std::string getLastError() const;
private:
bool mValid;
protected:
std::string mLastError;
};
#include "CivDbUtils.h"
CivDbUtils::CivDbUtils()
{
}
\ No newline at end of file
#pragma once
class CivDbUtils
{
public:
CivDbUtils();
private:
};
This diff is collapsed.
#pragma once
#include "CivAssembly.h"
#include <memory>
class CivConnection;
/**
从数据库获取inp文件相关的类
*/
class CivInpDbHelper
{
public:
explicit CivInpDbHelper(CivConnection* mDbConn);
~CivInpDbHelper();
bool getNode(CivNode& node);
bool getPipe(CivPipe& pipe) ;
bool getTank(CivTank& tank) ;
bool getValve(CivValve& valve) ;
bool getPumps(CivPumps& pump) ;
bool getReservoirs(CivReservoirs& reservoirs) ;
bool getEmitters(CivEmitters& emitter) ;
bool getCoordinates(CivCoordinates& coord) ;
bool getMixing(CivMixing& mixing) ;
bool getParameter(std::vector<CivParameter>& param) ;
bool getPatterns(CivPatterns& patterns) ;
bool getCurves(CivCurves& curves) ;
bool getDemands(CivDemands& demands) ;
bool getSources(CivSources& sources) ;
bool getStatus(CivStatus& status) ;
bool getQuality(CivQuality& quality) ;
bool getLabels(CivLabels& labels) ;
bool getTags(CivTags& tags) ;
private:
CivConnection* mDbConn;
std::map<std::string, std::map<std::string, std::string>> mCacheMap;
};
......@@ -70,6 +70,7 @@ public:
bool deleteByField(StrQuote table, StrQuote filedName, StrQuote fieldValue) override;
bool updateNodeByInterval(StrQuote currDate, StrQuote interval) override;
bool updateLinkByInterval(StrQuote currDate, StrQuote interval) override;
/**
*@brief 更新点表数据
*@param nodeItems:需要更新的值
......
#include "CivPgDbConnection.h"
CivPgDbConnection::CivPgDbConnection()
:CivConnection(),mConn(nullptr),mResult(nullptr)
{
}
CivPgDbConnection::~CivPgDbConnection()
{
disconnect();
}
bool CivPgDbConnection::connect(const std::string url)
{
disconnect();
mConn = PQconnectdb(url.c_str());
if (PQstatus(mConn) == CONNECTION_BAD) {
mConn = NULL;
PQfinish(mConn);
return false;
}
return true;
}
bool CivPgDbConnection::disconnect()
{
if (mConn != NULL)
{
PQfinish(mConn);
}
return true;
}
bool CivPgDbConnection::queryResult(std::vector<std::map<std::string,std::string>>& result)
{
int rows = PQntuples(mResult);
int columns = PQnfields(mResult);
for (int i = 0; i < rows; i++)
{
std::map<std::string, std::string> map;
for (int j = 0; j < columns; j++)
{
std::string filedName = PQfname(mResult, j);
std::string filedValue = PQgetvalue(mResult, i, 0);
map.insert(std::pair<std::string, std::string>(filedName,filedValue));
}
result.push_back(map);
}
PQclear(mResult);
}
bool CivPgDbConnection::execSql(const std::string& sql)
{
if (mConn)
return false;
mResult = PQexec(mConn, sql.c_str());
std::string errorStr = PQresultErrorMessage(mResult);
if (!errorStr.empty())
{
mLastError = errorStr;
PQclear(mResult);
return false;
}
return true;
}
#pragma once
#include "CivConnection.h"
#include <vector>
#include <libpq-fe.h>
#define SAFEDELETE(a) if((a)!=nullptr){delete a;a=nullptr;}
/**
postgress 连接类
*/
class CivPgDbConnection : public CivConnection
{
public:
explicit CivPgDbConnection();
~CivPgDbConnection();
/**
*@brief 建立数据,与具体的数据有关子类实现
*/
virtual bool connect(const std::string url);
/**
*@brief 关闭数据,与具体的数据有关子类实现
*/
virtual bool disconnect();
/**
*@brief 执行查询操作,与具体的数据有关子类实现
*@param sql 执行sql语句
*/
virtual bool execSql(const std::string& sql);
virtual bool queryResult(std::vector<std::map<std::string, std::string>>& result);
private:
PGconn* mConn; // 连接对象
PGresult* mResult; // 查询结果集
};
......@@ -161,9 +161,10 @@ copy CivDbConn.h $(OutDir)..\include /y
<ClInclude Include="CivCommonUtils.h" />
<ClInclude Include="CivConnection.h" />
<ClInclude Include="CivDbConn.h" />
<ClInclude Include="CivDbUtils.h" />
<ClInclude Include="CivInpDbHelper.h" />
<ClInclude Include="CivMaintainModel.h" />
<ClInclude Include="CivPgConn.h" />
<ClInclude Include="CivPgDbConnection.h" />
<ClInclude Include="CivTableTemp.h" />
<ClInclude Include="CivTrackingResultCache.h" />
<ClInclude Include="CivTypes.h" />
......@@ -173,9 +174,10 @@ copy CivDbConn.h $(OutDir)..\include /y
<ClCompile Include="CivCommonUtils.cpp" />
<ClCompile Include="CivConnection.cpp" />
<ClCompile Include="CivDbConn.cpp" />
<ClCompile Include="CivDbUtils.cpp" />
<ClCompile Include="CivInpDbHelper.cpp" />
<ClCompile Include="CivMaintainModel.cpp" />
<ClCompile Include="CivPgConn.cpp" />
<ClCompile Include="CivPgDbConnection.cpp" />
<ClCompile Include="CivTableTemp.cpp" />
<ClCompile Include="CivTrackingResultCache.cpp" />
</ItemGroup>
......
......@@ -36,15 +36,18 @@
<ClInclude Include="CivTrackingResultCache.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivDbUtils.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivConnection.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivMaintainModel.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivPgDbConnection.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivInpDbHelper.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="CivTableTemp.cpp">
......@@ -71,7 +74,10 @@
<ClCompile Include="CivMaintainModel.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CivDbUtils.cpp">
<ClCompile Include="CivPgDbConnection.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CivInpDbHelper.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
......
......@@ -116,7 +116,7 @@ std::string CivSysLog::getCurrentTime2()
timeinfo = localtime(&rawtime);
time_t tick = mktime(timeinfo);
strftime(s, sizeof(s), "%Y%m%d%H%M%S", timeinfo);
strftime(s, sizeof(s), "%Y%m%d", timeinfo);
return s;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment