Commit 15675189 authored by 刘乐's avatar 刘乐

1, 代码重构

parent ab0f72c6
@echo **************************************************
@echo * *
@echo * System Driver *
@echo * *
@echo **************************************************
@rem 设置工程目录
@set Current_path = %cd%
@Set BtMon_Path = %cd%/Toshiba_System_Driver/BtMon_SystemDriver_0725
@Set RMService_path =
@Set TabSysSvc_path =
@Set tosrfec_path =
@Set TosSysHID_path =
@Set TVALZ_path =
@Set TosQnt_path =
@rem 设置工程目录
@Set VS2019_BuildTool_path = "D:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\Common7\IDE\devenv.exe"
@echo === Starting to Build System Driver for WinXP Win8 Win10 ===
@echo 1,开始编译 hydraulicModel工程
@%VS2019_BuildTool_path% hydraulicModel.sln /rebuild "release|x64" /out "compile.log"
@pause
\ No newline at end of file
......@@ -11,21 +11,28 @@ using namespace std;
int main(int argc, char* argv[])
{
const char* uri = "host=192.168.12.7 port=5432 dbname=2222 user=postgres password=admin";
const char* uri = "host=192.168.19.100 port=5432 dbname=JinXian user=postgres password=admin";
char* findUri = const_cast<char*>(uri);
char net[128];
char flag[128];
const char* file = "test.inp";
const char netName[128] = "쏵鳩彊제친謹";
const char netName[128] = "水力模型管网";
strcpy_s(net, "쏵鳩彊제친謹");
strcpy_s(net, "水力模型管网");
strcpy_s(flag, "HDY");
bool msg = simulation(findUri, net, flag);
std::cout << msg << std::endl;
//bool msg = simulation(findUri, net, flag);
// std::cout << msg << std::endl;
const char* date = "2020-06-19";
char* dates = const_cast<char*>(date);
// bool flag = getDataByInterval(findUri,'2020-06-18',)
const char* interval = "0";
char* intervals = const_cast<char*>(interval);
getDataByInterval(findUri, dates, intervals);
// std::cout << flag << std::endl;
return 0;
}
\ No newline at end of file
......@@ -15,8 +15,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "EPNAET2", "EPNAET2\EPNAET2.
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "funcDemo", "funcDemo\funcDemo.vcxproj", "{6A4F723D-C24B-4B16-A230-9C39A379F082}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "pandasUtils", "pandasUtils\pandasUtils.vcxproj", "{7786A13A-338A-4D91-B6BF-DC751E55F018}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -67,16 +65,6 @@ Global
{6A4F723D-C24B-4B16-A230-9C39A379F082}.Release|x64.Build.0 = Release|x64
{6A4F723D-C24B-4B16-A230-9C39A379F082}.Release|x86.ActiveCfg = Release|Win32
{6A4F723D-C24B-4B16-A230-9C39A379F082}.Release|x86.Build.0 = Release|Win32
{7786A13A-338A-4D91-B6BF-DC751E55F018}.Debug|Any CPU.ActiveCfg = Debug|Win32
{7786A13A-338A-4D91-B6BF-DC751E55F018}.Debug|x64.ActiveCfg = Debug|x64
{7786A13A-338A-4D91-B6BF-DC751E55F018}.Debug|x64.Build.0 = Debug|x64
{7786A13A-338A-4D91-B6BF-DC751E55F018}.Debug|x86.ActiveCfg = Debug|Win32
{7786A13A-338A-4D91-B6BF-DC751E55F018}.Debug|x86.Build.0 = Debug|Win32
{7786A13A-338A-4D91-B6BF-DC751E55F018}.Release|Any CPU.ActiveCfg = Release|Win32
{7786A13A-338A-4D91-B6BF-DC751E55F018}.Release|x64.ActiveCfg = Release|x64
{7786A13A-338A-4D91-B6BF-DC751E55F018}.Release|x64.Build.0 = Release|x64
{7786A13A-338A-4D91-B6BF-DC751E55F018}.Release|x86.ActiveCfg = Release|Win32
{7786A13A-338A-4D91-B6BF-DC751E55F018}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
......@@ -51,7 +51,7 @@ bool CivHydrCalc::hydrSimulation(char* fileName, char* rptFile, char* binOutFile
float dHStep;
ENGetVal(4, &dHStep);
hydrCompute(errcode);
bool isFirst = true;
// 计算
do
......@@ -83,17 +83,94 @@ bool CivHydrCalc::hydrSimulation(char* fileName, char* rptFile, char* binOutFile
getLinkResult(iTime);
iTime++;
} while (tstep > 0);
(void)mDbConn->updateNodeByInterval(CurrentDate, CurrentHour);
(void)mDbConn->updateLinkByInterval(CurrentDate,CurrentHour);
// 更新管网数据库数据
(void)mDbConn->updateNodeByInterval(CurrentDate, "0");
(void)mDbConn->updateLinkByInterval(CurrentDate,"0");
ENcloseH();
ENclose();
return true;
}
bool CivHydrCalc::qualitySimulation(char* inpFile, char* rptFile, char* binOutFile)
{
if (!hydrSimulation(inpFile, rptFile, binOutFile))
{
return false;
}
// 运行水质运算需要先进行水力运算,没有直接采用ENSolveH 是为了考虑减压阀的压力曲线设置
int errcode = 0;
// 进行水质分析
ENopenQ();
ENinitQ(1);
t = 0;
tstep = 0;
iTime = 0;
do
{
errcode = ENrunQ(&t); // errcode > 100 是错误
if (errcode > 100)
{
ENcloseQ();
ENclose();
return;
}
HydResultCache::GetInstance()->SetHydNodQualityResultTblAi(iTime);
HydResultCache::GetInstance()->SetHydLinQualityResultTblAi(iTime);
ENstepQ(&tstep);
iTime++;
} while (tstep > 0);
ENcloseQ();
ENclose();
return true;
}
bool CivHydrCalc::hydrCompute(int& errorcode)
{
bool isFirst = true;
// 计算
do
{
int nLinkCount;
ENgetcount(EN_LINKCOUNT, &nLinkCount);
/*for (int i = 1; i <= nLinkCount; i++)
{
int linkType;
char id[128] = "";
ENgetlinktype(i, &linkType);
ENgetlinkid(i, id);
}*/
if (ENrunH(&t) > 100) // errcode > 100 是错误
{
ENclose();
return false;
}
ENnextH(&tstep);
if (isFirst)
{
mDbConn->deleteByDate(NODERESULTTABLE, "日期", CurrentDate);
mDbConn->deleteByDate(PIPERESULTTABLE, "日期", CurrentDate);
isFirst = false;
}
// 获取当前节点和管段数据
getNodeResult(iTime);
getLinkResult(iTime);
iTime++;
} while (tstep > 0);
}
bool CivHydrCalc::exportInp(char* fileName)
{
//1 ,获取管网组件
......@@ -225,6 +302,7 @@ void CivHydrCalc::assemble(const std::string& table, CivInp& inp)
Civitems patternItems;
int total = comps.size();
std::set<Str> patterNams;
for (int i = 0; i < total; i++)
{
Componet comp = comps[i];
......@@ -252,21 +330,15 @@ void CivHydrCalc::assemble(const std::string& table, CivInp& inp)
nodeCoordItem.push_back(y_coord);
nodeCoordItems.push_back(nodeCoordItem);
if (pattern == "")
continue;
if (!pattern.empty())
{
patterNams.insert(pattern);
}
}
// 蓄水模式
Componets patterns = mDbConn->getComponets(PATTERNTABLE);
PatternFields fileds;
char where[128] = " where ";
strcat_s(where, fileds.NetName.c_str());
strcat_s(where, "=");
strcat_s(where, mNetname.c_str());
strcat_s(where, " and ");
strcat_s(where, fileds.Name.c_str());
strcat_s(where, "= ");
strcat_s(where, pattern.c_str());
Componets patterns = mDbConn->getComponetsByWhere(PATTERNTABLE, where);
size_t patternSize = patterns.size();
for (int i = 0; i < patternSize; i++)
{
......@@ -280,7 +352,6 @@ void CivHydrCalc::assemble(const std::string& table, CivInp& inp)
patternItems.push_back(item);
}
}
inp.writeComponet(jounItems,_JUNCTIONS);
inp.writeComponet(nodeCoordItems, _COORDS);
inp.writeComponet(patternItems, _PATTERNS);
......@@ -461,6 +532,9 @@ void CivHydrCalc::assemble(const std::string& table, CivInp& inp)
bool CivHydrCalc::getDataByInterval(char* date, char* interval)
{
if (!mDbConn->open())
return false;
if (!mDbConn->updateNodeByInterval(date, interval))
return false;
......
......@@ -36,6 +36,7 @@ public:
*@brief 导出inp文件
*/
bool exportInp(char* fileName);
private:
/**
*@brief 解析表字段名,按顺序写入
......@@ -43,6 +44,8 @@ private:
*@inp: inp文件对象
*/
void assemble(const std::string& table, CivInp& inp);
bool hydrCompute(int& errorcode);
/*
*@brief 获取节点计算结果
*@time: 时间
......
#include "CivInpExporter.h"
CivInpExporter::CivInpExporter()
{
}
CivInpExporter::~CivInpExporter()
{
}
\ No newline at end of file
#ifndef _CIVINPEXPOTER_H
#define _CIVINPEXPOTER_H
/**
* inpļ
*/
class CivInpExporter
{
public:
CivInpExporter();
~CivInpExporter();
private:
};
#endif // !_CIVINPEXPOTER_H
......@@ -157,12 +157,14 @@
<ClCompile Include="CivHydrCalc.cpp" />
<ClCompile Include="CivHydrFuncInter.cpp" />
<ClCompile Include="CivInp.cpp" />
<ClCompile Include="CivInpExporter.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="CivComponent.h" />
<ClInclude Include="CivHydrCalc.h" />
<ClInclude Include="CivHydrFuncInter.h" />
<ClInclude Include="CivInp.h" />
<ClInclude Include="CivInpExporter.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
......
......@@ -30,6 +30,9 @@
<ClCompile Include="..\pandaDbManager\CivCommonUtils.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CivInpExporter.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CivHydrCalc.h">
......@@ -44,5 +47,8 @@
<ClInclude Include="CivHydrFuncInter.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivInpExporter.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
This diff is collapsed.
#ifndef CIVDBCONN_H
#define CIVDBCONN
#include "CivTypes.h"
#include "CivTableTemp.h"
/**
数据库连接接口
*/
class CivDbConn
{
public:
CivDbConn();
virtual ~CivDbConn();
/**
*@brief 连接数据库,与具体的数据有关子类实现
*/
virtual bool open() = 0;
/**
*@brief 关闭数据库连接,与具体的数据有关子类实现
*/
virtual void close() = 0;
/**
*@brief 获取管网的所有表,与具体数据库相关,子类实现
*@netName: 管网名
*/
virtual Tables getTables(StrQuote netName, StrQuote schema) = 0;
/**
*@brief 判断某张表是否存在,判断方法跟具体的数据库有关,延迟到子类实现
*@tableName: 表名称
*/
virtual bool tableExist(StrQuote tableName) = 0;
/**
*@brief 创建表格
*@tableName: 表名
*@schema:
*@tableType:表类型,点结果,线结果
*/
bool createTable(StrQuote tableName, StrQuote schema, CivTableTemp::TableType tableType);
/**
*@brief 获取管网组件数据:管线,节点,水源等(必须组件)
*@tableName: 组件表
*/
Componets getComponets(StrQuote tableName);
Componets getComponetsByWhere(StrQuote tableName, StrQuote where);
/**
*@brief 水泵,阀门 (节点转换为管线)
*@tableName: 组件表
*/
Componets getLikelyPipes(StrQuote tableName);
/**
*@brief 根据字段值条件删除数据
*@table:表
*@filedName:字段名
*@fieldValue:字段值
*/
bool deleteByField(StrQuote table, StrQuote filedName, StrQuote fieldValue);
bool updateNodeByInterval(StrQuote currDate, StrQuote interval);
bool updateLinkByInterval(StrQuote currDate, StrQuote interval);
/**
*@brief 更新点表数据
*@nodeItems:需要更新的值
*/
bool updateNode(const NodeResultItems& nodeItems);
/**
*@brief 更新线表的数据
*@lineItems: 线数据
*/
bool updateLink(const LinkResultItems& lineItems);
// 添加过滤类型字段值
void addFilter(Str type, Str number);
// 获取过滤集
FiledFilter getFilter() const;
// 获取当前执行错误信息
Str getLastError() const;
bool isValid() const;
private:
/**
*@brief 根据code值寻数据
*@table: 线表
*@code: code
*/
Componets getValueByCode(StrQuote table, StrQuote code);
/**
*@brief 执行sql语句
*@sql: 需要执行的sql语句
*/
virtual bool execSql(Str sql) = 0;
/**
*@brief 清除查询结果集数据
*/
virtual void clearResult() = 0;
/**
*@brief 从查询集取出数据
*@result: 查询结果集
*@comp: 数据容器
*/
virtual void getData(Componets& comp) = 0;
protected:
bool mIsOpen = false; // 数据库连接信息
char* mLastError; // 执行sql的错误信息
private:
FiledFilter mFilter; // 表名:对应的编号
};
#endif // !CIVDBCONN_H
#include "CivDbConnection.h"
#include "CivCommonUtils.h"
#include "CivTableTemp.h"
#include<iostream>
#include <sstream>
......@@ -88,7 +87,7 @@ bool CivDbConnection::deleteByDate(StrQuote table, StrQuote filedName, StrQuote
if (!isValid())
return false;
char delSql[256] = "delete from public.\"";
char delSql[256] = "delete from \"";
strcat_s(delSql, table.c_str());
strcat_s(delSql, "\" where ");
strcat_s(delSql, filedName.c_str());
......@@ -110,7 +109,7 @@ bool CivDbConnection::updateNode(const NodeResultItems& nodeItems)
std::stringstream os;
NodeFieldName filedNames;
if (createTable(NODERESULTTABLE, "public", NODE_TABLE))
if (createTable(NODERESULTTABLE, "public", CivTableTemp::NODE_TABLE))
{
os << "INSERT INTO public."<< NODERESULTTABLE<<" ( ";
os << filedNames.Number<<",";
......@@ -174,7 +173,7 @@ bool CivDbConnection::updateLink(const LinkResultItems& lineItems)
std::stringstream os;
LinkFiledName filedNames;
std::string currData = CurrentDate;
if (createTable(PIPERESULTTABLE, "public", PIPE_TABLE))
if (createTable(PIPERESULTTABLE, "public", CivTableTemp::PIPE_TABLE))
{
os<< "INSERT INTO public."<< PIPERESULTTABLE <<"( ";
......@@ -306,7 +305,6 @@ bool CivDbConnection::updateLinkByInterval(StrQuote currDate, StrQuote interval)
<< TransUTFCoding(filedNames.dInterval) << "=" << interval;
std::string updateSql = os.str();
std::cout << updateSql << std::endl;
os.clear();
os.str("");
......@@ -316,7 +314,6 @@ bool CivDbConnection::updateLinkByInterval(StrQuote currDate, StrQuote interval)
return true;
}
Componets CivDbConnection::getComponets(StrQuote tableName)
{
if (!isValid())
......@@ -332,7 +329,7 @@ Componets CivDbConnection::getComponets(StrQuote tableName)
Componets nodes;
// 取数据到容器中
getData(mResult, nodes);
getData(nodes);
return nodes;
}
......@@ -344,7 +341,7 @@ Componets CivDbConnection::getComponetsByWhere(StrQuote tableName, StrQuote wher
std::string utf8Name = TransUTFCoding(tableName);
std::string utf8Where = TransUTFCoding(where);
char sql[128] = "select * from table ";
char sql[128] = "select * from public.";
strcat_s(sql, utf8Name.c_str());
strcat_s(sql, utf8Where.c_str());
......@@ -356,25 +353,25 @@ Componets CivDbConnection::getComponetsByWhere(StrQuote tableName, StrQuote wher
Componets comps;
// 取数据到容器中
getData(mResult, comps);
getData(comps);
return comps;
}
void CivDbConnection::getData(PGresult* result, Componets& comp)
void CivDbConnection::getData(Componets& comp)
{
if (result == NULL)
if (mResult == NULL)
return;
int rows = PQntuples(result);
int columns = PQnfields(result);
int rows = PQntuples(mResult);
int columns = PQnfields(mResult);
for (int i = 0; i < rows; i++)
{
Componet node;
for (int j = 0; j < columns; j++)
{
char* item = PQgetvalue(result, i, j);
char* fieldName = PQfname(result, j); // 获取列表字段名
char* item = PQgetvalue(mResult, i, j);
char* fieldName = PQfname(mResult, j); // 获取列表字段名
std::string itemStr = UTFTransCoding(item);
std::string fieldStr = UTFTransCoding(fieldName);
......@@ -383,7 +380,7 @@ void CivDbConnection::getData(PGresult* result, Componets& comp)
}
comp.push_back(node);
}
PQclear(result);
PQclear(mResult);
}
Componets CivDbConnection::getNodeByCode(StrQuote table, StrQuote code)
......@@ -406,7 +403,7 @@ Componets CivDbConnection::getNodeByCode(StrQuote table, StrQuote code)
Componets componets;
getData(mResult, componets);
getData(componets);
return componets;
}
......@@ -431,7 +428,7 @@ Componets CivDbConnection::getLineByCode(StrQuote table, StrQuote code)
Componets componets;
getData(mResult, componets);
getData(componets);
return componets;
}
......@@ -452,7 +449,7 @@ Componets CivDbConnection::getLikelyPipes(StrQuote tableName)
}
Componets nodes;
getData(mResult, nodes);
getData(nodes);
// 查询线表
......@@ -552,83 +549,16 @@ FiledFilter CivDbConnection::getFilter() const
return mFilter;
}
bool CivDbConnection::createTable(StrQuote tableName, StrQuote schema,TableType tableType)
bool CivDbConnection::createTable( StrQuote tableName, StrQuote schema, CivTableTemp::TableType tableType)
{
if (tableExist(tableName))
return true;
CivTableTemp tableTemp(tableName);
CivTableTemp tableTemp(tableName,tableType);
Str sql = tableTemp.createSql();
Str finalSql = TransUTFCoding(sql);
switch (tableType)
{
case NODE_TABLE:
{
NodeFieldName filedName;
tableTemp.addProperty(filedName.Number, CivTableTemp::FILED_VARCHAR);
tableTemp.addProperty(filedName.dPressure, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(filedName.dHead, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(filedName.dElevation, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(filedName.dDemand, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(filedName.dTankVolume, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(filedName.dTankMaxVolume, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(filedName.dTankLevel, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(filedName.dQuality, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(filedName.dDate, CivTableTemp::FILED_DATE);
tableTemp.addProperty(filedName.dInterval, CivTableTemp::FILED_INT);
tableTemp.addProperty(filedName.dModifyTime, CivTableTemp::FILED_DATETIME);
}
break;
case PIPE_TABLE:
{
LinkFiledName pipeFileds;
tableTemp.addProperty(pipeFileds.szNo, CivTableTemp::FILED_VARCHAR);
tableTemp.addProperty(pipeFileds.dFlow, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(pipeFileds.nFlowDirect, CivTableTemp::FILED_INT);
tableTemp.addProperty(pipeFileds.dVelocity, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(pipeFileds.dHeadloss, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(pipeFileds.dUnitHeadloss, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(pipeFileds.dFromNodHeadloss, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(pipeFileds.dToNodHeadloss, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(pipeFileds.dLocalHeadloss, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(pipeFileds.dFrictionHeadloss, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(pipeFileds.szStatus, CivTableTemp::FILED_VARCHAR);
tableTemp.addProperty(pipeFileds.dQuality, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(pipeFileds.dDate, CivTableTemp::FILED_DATE);
tableTemp.addProperty(pipeFileds.dInterval, CivTableTemp::FILED_INT);
tableTemp.addProperty(pipeFileds.dModifyTime, CivTableTemp::FILED_DATETIME);
}
break;
case PATTERN_TABLE:
{
PatternFields fileds;
tableTemp.addProperty(fileds.Name, CivTableTemp::FILED_VARCHAR);
tableTemp.addProperty(fileds.Value, CivTableTemp::FILED_VARCHAR64);
tableTemp.addProperty(fileds.NetName, CivTableTemp::FILED_VARCHAR);
}
break;
case CURVE_TABLE:
{
CurvesFields fileds;
tableTemp.addProperty(fileds.Label, CivTableTemp::FILED_VARCHAR);
tableTemp.addProperty(fileds.Type, CivTableTemp::FILED_VARCHAR);
tableTemp.addProperty(fileds.XCoord, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty(fileds.YCoord, CivTableTemp::FILED_DOUBLE);
}
break;
case PARAMETER_TABLE:
{
ParameterFileds fileds;
tableTemp.addProperty(fileds.name, CivTableTemp::FILED_VARCHAR);
tableTemp.addProperty(fileds.val, CivTableTemp::FILED_VARCHAR);
tableTemp.addProperty(fileds.type, CivTableTemp::FILED_VARCHAR);
tableTemp.addProperty(fileds.netName, CivTableTemp::FILED_VARCHAR64);
}
break;
default:
break;
}
std::string sql = tableTemp.createSql();
std::string finalSql = TransUTFCoding(sql);
return execSql(finalSql);
}
......
......@@ -3,11 +3,11 @@
#include <vector>
#include <set>
#include <libpq-fe.h>
#include "CivTableTemp.h"
#define DLLEXPORT __declspec(dllexport)
// 字段过滤
#define FiledFilter std::map<Str,std::set<Str>>
class CivDbConnection;
......@@ -17,16 +17,6 @@ class CivDbConnection;
class DLLEXPORT CivDbConnection
{
public:
// 表类型
enum TableType
{
NODE_TABLE, // 计算结果点表
PIPE_TABLE, // 计算结果线表
PATTERN_TABLE, // 模式表
CURVE_TABLE,
PARAMETER_TABLE // 参数表
};
CivDbConnection(char* uri);
~CivDbConnection();
......@@ -58,11 +48,12 @@ public:
*@schema:
*@tableType:表类型,点结果,线结果
*/
bool createTable(StrQuote tableName, StrQuote schema, TableType tableType);
bool createTable(StrQuote tableName, StrQuote schema,CivTableTemp::TableType tableType);
bool deleteByDate(StrQuote table, StrQuote filedName, StrQuote fieldValue);
bool updateNodeByInterval(StrQuote currDate, StrQuote interval);
bool updateLinkByInterval(StrQuote currDate, StrQuote interval);
/**
*@brief 更新点表数据
......@@ -108,7 +99,7 @@ private:
*@result: 查询结果集
*@comp: 数据容器
*/
void getData(PGresult* result, Componets& comp);
void getData( Componets& comp);
private:
PGconn* mConn;
......@@ -117,9 +108,5 @@ private:
char* mUri;
char* mLastError;
char mLinkParam[256];
char mNodeParam[256];
FiledFilter mFilter; // 表名:对应的编号
};
\ No newline at end of file
#include "CivPgConn.h"
#include "CivCommonUtils.h"
CivPgConn::CivPgConn(char* uri)
:mUri(uri), CivDbConn()
{
mConn = NULL;
mResult = NULL;
}
CivPgConn::~CivPgConn()
{
}
bool CivPgConn::open()
{
mConn = PQconnectdb(mUri);
if (PQstatus(mConn) == CONNECTION_BAD) {
mConn = NULL;
PQfinish(mConn);
return false;
}
mIsOpen = true;
return true;
}
void CivPgConn::close()
{
if (mConn != NULL)
PQfinish(mConn);
mIsOpen = false;
}
Tables CivPgConn::getTables(StrQuote netName, StrQuote schema)
{
if (!isValid())
return Tables();
std::string utf8NetName = TransUTFCoding(netName); // 中文乱码
std::string sql = "select \"name\" from public.__layer_info__ where net_code in(select code from public.__layer_net_info__ where \"name\" ='" + utf8NetName + "' and \"schema\" = '" + schema + "')";
if (!execSql(sql))
return Tables();
Tables tables;
int rows = PQntuples(mResult);
int columns = PQnfields(mResult);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
char* item = PQgetvalue(mResult, i, j);
std::string ss = UTFTransCoding(item);
tables.push_back(ss);
}
}
PQclear(mResult);
return tables;
}
bool CivPgConn::tableExist(StrQuote tableName)
{
char sql[256] = "select count(*) from pg_class where relname = '";
strcat_s(sql, tableName.c_str());
strcat_s(sql, "'");
if (!execSql(sql))
return false;
char* res = PQgetvalue(mResult, 0, 0);
PQclear(mResult);
int number = atoi(res);
return number > 0 ? true : false;
}
bool CivPgConn::execSql(Str sql)
{
if (sql.empty())
return false;
mResult = PQexec(mConn, sql.c_str());
char* error = PQresultErrorMessage(mResult);
std::string errorStr = error;
if (!errorStr.empty())
{
mLastError = error;
PQclear(mResult);
return false;
}
return true;
}
void CivPgConn::clearResult()
{
PQclear(mResult);
}
void CivPgConn::getData(Componets& comp)
{
if (mResult == NULL)
return;
int rows = PQntuples(mResult);
int columns = PQnfields(mResult);
for (int i = 0; i < rows; i++)
{
Componet node;
for (int j = 0; j < columns; j++)
{
char* item = PQgetvalue(mResult, i, j);
char* fieldName = PQfname(mResult, j); // 获取列表字段名
std::string itemStr = UTFTransCoding(item);
std::string fieldStr = UTFTransCoding(fieldName);
node.insert(std::pair<std::string, std::string>(fieldStr, itemStr));
}
comp.push_back(node);
}
PQclear(mResult);
}
\ No newline at end of file
#ifndef CIVPGCONN_H
#define CIVPGCONN_H
#include "CivDbConn.h"
#include <libpq-fe.h>
/**
postgress 数据库
*/
class CivPgConn: public CivDbConn
{
public:
CivPgConn(char* uri);
~CivPgConn();
private:
/**
*@brief 连接数据库,与具体的数据有关子类实现
*/
virtual bool open();
/**
*@brief 关闭数据库连接,与具体的数据有关子类实现
*/
virtual void close();
/**
*@brief 获取管网的所有表,与具体数据库相关,子类实现
*@netName: 管网名
*/
virtual Tables getTables(StrQuote netName, StrQuote schema);
/**
*@brief 判断某张表是否存在,判断方法跟具体的数据库有关,延迟到子类实现
*@tableName: 表名称
*/
virtual bool tableExist(StrQuote tableName);
/**
*@brief 执行sql语句
*@sql: 需要执行的sql语句
*/
virtual bool execSql(Str sql);
/**
*@brief 清除查询结果集数据
*/
virtual void clearResult();
/**
*@brief 从查询集取出数据
*@result: 查询结果集
*@comp: 数据容器
*/
virtual void getData(Componets& comp);
private:
PGconn* mConn;
PGresult* mResult;
char* mUri;
};
#endif // !CIVPGCONN_H
#include "CivTableTemp.h"
#include <fstream>
CivTableTemp::CivTableTemp(std::string tableName)
:mTableName(tableName)
CivTableTemp::CivTableTemp(Str tableName, TableType tableType)
:mTableName(tableName),mTableType(tableType)
{
mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_INT, "int"));
mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_INTEGER,"integer"));
......@@ -20,7 +19,7 @@ CivTableTemp::~CivTableTemp()
}
void CivTableTemp::addProperty(std::string fileName, FieldType filedType)
void CivTableTemp::addProperty(Str fileName, FieldType filedType)
{
FieldProperty prop = { fileName ,mFieldTyepMap.find(filedType)->second};
mPropertys.push_back(prop);
......@@ -31,16 +30,84 @@ void CivTableTemp::addPropertys(FiledPropertys props)
mPropertys.insert(mPropertys.end(), props.begin(), props.end());
}
std::string CivTableTemp::createSql()
Str CivTableTemp::createSql()
{
switch (mTableType)
{
case NODE_TABLE:
{
NodeFieldName filedName;
addProperty(filedName.Number, CivTableTemp::FILED_VARCHAR);
addProperty(filedName.dPressure, CivTableTemp::FILED_DOUBLE);
addProperty(filedName.dHead, CivTableTemp::FILED_DOUBLE);
addProperty(filedName.dElevation, CivTableTemp::FILED_DOUBLE);
addProperty(filedName.dDemand, CivTableTemp::FILED_DOUBLE);
addProperty(filedName.dTankVolume, CivTableTemp::FILED_DOUBLE);
addProperty(filedName.dTankMaxVolume, CivTableTemp::FILED_DOUBLE);
addProperty(filedName.dTankLevel, CivTableTemp::FILED_DOUBLE);
addProperty(filedName.dQuality, CivTableTemp::FILED_DOUBLE);
addProperty(filedName.dDate, CivTableTemp::FILED_DATE);
addProperty(filedName.dInterval, CivTableTemp::FILED_INT);
addProperty(filedName.dModifyTime, CivTableTemp::FILED_DATETIME);
}
break;
case PIPE_TABLE:
{
LinkFiledName pipeFileds;
addProperty(pipeFileds.szNo, CivTableTemp::FILED_VARCHAR);
addProperty(pipeFileds.dFlow, CivTableTemp::FILED_DOUBLE);
addProperty(pipeFileds.nFlowDirect, CivTableTemp::FILED_INT);
addProperty(pipeFileds.dVelocity, CivTableTemp::FILED_DOUBLE);
addProperty(pipeFileds.dHeadloss, CivTableTemp::FILED_DOUBLE);
addProperty(pipeFileds.dUnitHeadloss, CivTableTemp::FILED_DOUBLE);
addProperty(pipeFileds.dFromNodHeadloss, CivTableTemp::FILED_DOUBLE);
addProperty(pipeFileds.dToNodHeadloss, CivTableTemp::FILED_DOUBLE);
addProperty(pipeFileds.dLocalHeadloss, CivTableTemp::FILED_DOUBLE);
addProperty(pipeFileds.dFrictionHeadloss, CivTableTemp::FILED_DOUBLE);
addProperty(pipeFileds.szStatus, CivTableTemp::FILED_VARCHAR);
addProperty(pipeFileds.dQuality, CivTableTemp::FILED_DOUBLE);
addProperty(pipeFileds.dDate, CivTableTemp::FILED_DATE);
addProperty(pipeFileds.dInterval, CivTableTemp::FILED_INT);
addProperty(pipeFileds.dModifyTime, CivTableTemp::FILED_DATETIME);
}
break;
case PATTERN_TABLE:
{
PatternFields fileds;
addProperty(fileds.Name, CivTableTemp::FILED_VARCHAR);
addProperty(fileds.Value, CivTableTemp::FILED_VARCHAR64);
addProperty(fileds.NetName, CivTableTemp::FILED_VARCHAR);
}
break;
case CURVE_TABLE:
{
CurvesFields fileds;
addProperty(fileds.Label, CivTableTemp::FILED_VARCHAR);
addProperty(fileds.Type, CivTableTemp::FILED_VARCHAR);
addProperty(fileds.XCoord, CivTableTemp::FILED_DOUBLE);
addProperty(fileds.YCoord, CivTableTemp::FILED_DOUBLE);
}
break;
case PARAMETER_TABLE:
{
ParameterFileds fileds;
addProperty(fileds.name, CivTableTemp::FILED_VARCHAR);
addProperty(fileds.val, CivTableTemp::FILED_VARCHAR);
addProperty(fileds.type, CivTableTemp::FILED_VARCHAR);
addProperty(fileds.netName, CivTableTemp::FILED_VARCHAR64);
}
break;
default:
break;
}
if (mPropertys.size() <= 0)
return "";
return Str();
char sql[512] = "create table ";
strcat_s(sql, mTableName.c_str());
strcat_s(sql, "(id serial primary key");
int total = mPropertys.size();
size_t total = mPropertys.size();
for (int i = 0; i < total; i++)
{
FieldProperty property = mPropertys[i];
......
#pragma once
#ifndef CIVTABLETEMP_H
#define CIVTABLETEMP_H
#include<string>
#include<vector>
#include<map>
#include "CivTypes.h"
typedef struct FieldProperty
{
std::string mFiledName; //字段名
......@@ -11,14 +15,23 @@ typedef struct FieldProperty
#define FiledPropertys std::vector<FieldProperty>
/**
* 数据库表模板
*/
class CivTableTemp
{
public:
// 表类型
enum TableType
{
NODE_TABLE, // 计算结果点表
PIPE_TABLE, // 计算结果线表
PATTERN_TABLE, // 模式表
CURVE_TABLE, //曲线表
PARAMETER_TABLE // 参数表
};
// 字段类型
enum FieldType {
FILED_INT,
......@@ -32,7 +45,8 @@ public:
FILED_DATE
};
explicit CivTableTemp(std::string tableName);
explicit CivTableTemp(Str tableName, TableType tableType);
~CivTableTemp();
/**
......@@ -46,10 +60,17 @@ public:
void addPropertys(FiledPropertys props);
// 返回创建表的sql语句
std::string createSql();
Str createSql();
private:
std::string mTableName; // 表明
Str mTableName; // 表明
TableType mTableType; // 表类型
FiledPropertys mPropertys; // 字段和字段属性
std::map< FieldType, std::string> mFieldTyepMap;
std::map< FieldType, Str> mFieldTyepMap;
};
#endif // !CIVTABLETEMP_H
#pragma once
#ifndef CIVTYPES_H
#define CIVTYPES_H
#include <map>
#include<string>
#include <vector>
#include <set>
typedef std::string Str;
typedef const std::string& StrQuote;
......@@ -35,6 +39,9 @@ typedef const std::string& StrQuote;
#define Componet std::map<Str,Str>
#define Componets std::vector<Componet>
// 字段过滤
#define FiledFilter std::map<Str,std::set<Str>>
// 组件类型
enum SectType {
_TITLE, _JUNCTIONS, _RESERVOIRS, _TANKS, _PIPES, _PUMPS,
......@@ -55,7 +62,7 @@ enum ValvesType
GPV /* 常规阀门 */
};
// 水泵参数
// 水泵参数
enum PumpParameter
{
POWER, // 定速能量水泵的功率数值,hp(kW)
......@@ -125,14 +132,14 @@ struct ResivoirFields {
Str Flow = "流出流量";
};
//;ID Node1 Node2 Length Diameter Roughness MinorLoss Status
//;ID Node1 Node2 Length Diameter Roughness MinorLoss Status
struct PipeFields {
Str ID = "编号";
Str Node1 = Str("上点号");
Str Node2 = Str("本点号");
Str Length = Str("管长");
Str Diameter = Str("管径") ; // 管径
Str Roughness = Str("摩阻系数") ; //
Str Diameter = Str("管径"); // 管径
Str Roughness = Str("摩阻系数"); //
Str MinorLoss = Str("局部水头损");
Str Status = Str("状态");
};
......@@ -146,7 +153,7 @@ struct JunctionsFields {
Str X_Coord = "横坐标";
Str Y_Coord = "纵坐标";
Str InitQual = "初始水质";
} ;
};
// 模式表字段
struct PatternFields {
......@@ -200,12 +207,12 @@ struct NodeFieldName
Str Number = "本点号";
Str dFlow = "流量";
Str dHead = "水头";
Str dPressure="压力";
Str dElevation="标高";
Str dDemand="实际需水量";
Str dTankVolume="水池容量";
Str dTankMaxVolume="水池最大容量";
Str dTankLevel="液位高度";
Str dPressure = "压力";
Str dElevation = "标高";
Str dDemand = "实际需水量";
Str dTankVolume = "水池容量";
Str dTankMaxVolume = "水池最大容量";
Str dTankLevel = "液位高度";
Str dQuality = "水质";
Str dDate = "日期";
Str dInterval = "时段";
......@@ -217,14 +224,14 @@ struct LinkFiledName
Str szNo = "编号";
Str dFlow = "流量";
Str nFlowDirect = "流向";
Str dVelocity="流速";
Str dHeadloss="水头损失";
Str dUnitHeadloss="单位水头损";
Str dFromNodHeadloss="上点水头";
Str dToNodHeadloss="本点水头";
Str dLocalHeadloss="局部水头损";
Str dFrictionHeadloss="摩擦水头损";
Str szStatus="状态";
Str dVelocity = "流速";
Str dHeadloss = "水头损失";
Str dUnitHeadloss = "单位水头损";
Str dFromNodHeadloss = "上点水头";
Str dToNodHeadloss = "本点水头";
Str dLocalHeadloss = "局部水头损";
Str dFrictionHeadloss = "摩擦水头损";
Str szStatus = "状态";
Str dQuality = "水质";
Str dDate = "日期";
Str dInterval = "时段";
......@@ -267,3 +274,9 @@ struct LinkResultItem
#define NodeResultItems std::vector<NodeResultItem>
#define LinkResultItems std::vector<LinkResultItem>
#endif // !CIVTYPES_H
......@@ -156,13 +156,17 @@ copy CivTypes.h $(OutDir)..\include /y
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="CivCommonUtils.h" />
<ClInclude Include="CivDbConn.h" />
<ClInclude Include="CivDbConnection.h" />
<ClInclude Include="CivPgConn.h" />
<ClInclude Include="CivTableTemp.h" />
<ClInclude Include="CivTypes.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="CivCommonUtils.cpp" />
<ClCompile Include="CivDbConn.cpp" />
<ClCompile Include="CivDbConnection.cpp" />
<ClCompile Include="CivPgConn.cpp" />
<ClCompile Include="CivTableTemp.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
......
......@@ -27,6 +27,12 @@
<ClInclude Include="CivCommonUtils.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivDbConn.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivPgConn.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="CivDbConnection.cpp">
......@@ -38,5 +44,11 @@
<ClCompile Include="CivCommonUtils.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CivDbConn.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CivPgConn.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>
\ No newline at end of file
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