Commit 7eeb415e authored by 刘乐's avatar 刘乐

1, 模型改进

parent 49d41dc4
...@@ -48,18 +48,20 @@ bool CivHydrCalc::hydrSimulation(char* fileName, char* rptFile, char* binOutFile ...@@ -48,18 +48,20 @@ bool CivHydrCalc::hydrSimulation(char* fileName, char* rptFile, char* binOutFile
return false; return false;
} }
float dHStep;
ENGetVal(4, &dHStep);
// 计算 // 计算
do do
{ {
int nLinkCount; int nLinkCount;
ENgetcount(EN_LINKCOUNT, &nLinkCount); ENgetcount(EN_LINKCOUNT, &nLinkCount);
for (int i = 1; i <= nLinkCount; i++) /*for (int i = 1; i <= nLinkCount; i++)
{ {
int linkType; int linkType;
char id[128] = ""; char id[128] = "";
ENgetlinktype(i, &linkType); ENgetlinktype(i, &linkType);
ENgetlinkid(i, id); ENgetlinkid(i, id);
} }*/
if (ENrunH(&t) > 100) // errcode > 100 是错误 if (ENrunH(&t) > 100) // errcode > 100 是错误
{ {
...@@ -67,15 +69,13 @@ bool CivHydrCalc::hydrSimulation(char* fileName, char* rptFile, char* binOutFile ...@@ -67,15 +69,13 @@ bool CivHydrCalc::hydrSimulation(char* fileName, char* rptFile, char* binOutFile
return false; return false;
} }
ENnextH(&tstep); ENnextH(&tstep);
iTime++;
// 获取当前节点和管段数据 // 获取当前节点和管段数据
getNodeResult(t); getNodeResult(iTime);
getLinkResult(t); getLinkResult(iTime);
iTime++;
} while (tstep > 0); } while (tstep > 0);
// 获取计算结果
return true; return true;
} }
...@@ -142,8 +142,6 @@ bool CivHydrCalc::exportInp(char* fileName) ...@@ -142,8 +142,6 @@ bool CivHydrCalc::exportInp(char* fileName)
void CivHydrCalc::assemble(const std::string& table, CivInp& inp) void CivHydrCalc::assemble(const std::string& table, CivInp& inp)
{ {
// 根据组建类型解析字段 // 根据组建类型解析字段
if (PIPELINE == table) if (PIPELINE == table)
{ {
...@@ -307,7 +305,7 @@ void CivHydrCalc::assemble(const std::string& table, CivInp& inp) ...@@ -307,7 +305,7 @@ void CivHydrCalc::assemble(const std::string& table, CivInp& inp)
// 水池数据 // 水池数据
CivItem tankNode; CivItem tankNode;
tankNode.push_back("P"+id); tankNode.push_back(id);
tankNode.push_back(node1); tankNode.push_back(node1);
tankNode.push_back(node2); tankNode.push_back(node2);
...@@ -391,7 +389,7 @@ void CivHydrCalc::assemble(const std::string& table, CivInp& inp) ...@@ -391,7 +389,7 @@ void CivHydrCalc::assemble(const std::string& table, CivInp& inp)
// 阀门数据 // 阀门数据
CivItem vavleNode; CivItem vavleNode;
vavleNode.push_back("V"+id); vavleNode.push_back(id);
vavleNode.push_back(node1); vavleNode.push_back(node1);
vavleNode.push_back(node2); vavleNode.push_back(node2);
vavleNode.push_back(diametor); vavleNode.push_back(diametor);
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include "CivCommonUtils.h" #include "CivCommonUtils.h"
#include "CivTableTemp.h" #include "CivTableTemp.h"
#include<iostream> #include<iostream>
#include <sstream>
#define MAX_BUFFER 16384 #define MAX_BUFFER 16384
...@@ -22,21 +23,14 @@ bool CivDbConnection::tableExist(StrQuote tableName) ...@@ -22,21 +23,14 @@ bool CivDbConnection::tableExist(StrQuote tableName)
strcat_s(sql, tableName.c_str()); strcat_s(sql, tableName.c_str());
strcat_s(sql, "'"); strcat_s(sql, "'");
PGresult* result = PQexec(mConn, sql); if (!execSql(sql))
if (result == NULL)
return false; return false;
/* if (PQstatus(mConn) != PGRES_COMMAND_OK)
{
mLastError = PQresultErrorMessage(result);
PQclear(result);
return false;
}*/
char* res = PQgetvalue(result, 0, 0); char* res = PQgetvalue(mResult, 0, 0);
PQclear(mResult);
int number = atoi(res); int number = atoi(res);
PQclear(result);
bool flag = number > 0 ? true : false; return number > 0 ? true : false;
return flag;
} }
bool CivDbConnection::open() bool CivDbConnection::open()
...@@ -62,7 +56,7 @@ void CivDbConnection::close() ...@@ -62,7 +56,7 @@ void CivDbConnection::close()
mIsOpen = false; mIsOpen = false;
} }
Tables CivDbConnection::getTables(StrQuote netName,StrQuote schema) const Tables CivDbConnection::getTables(StrQuote netName,StrQuote schema)
{ {
if (!isValid()) if (!isValid())
...@@ -100,72 +94,67 @@ bool CivDbConnection::updateNode(const NodeResultItems& nodeItems) ...@@ -100,72 +94,67 @@ bool CivDbConnection::updateNode(const NodeResultItems& nodeItems)
if (createTable("__node_result__", "public", NODE_TABLE)) if (createTable("__node_result__", "public", NODE_TABLE))
{ {
NodeFieldName filedNames; NodeFieldName filedNames;
char sql[MAX_BUFFER] = "INSERT INTO public.__node_result__ ( ";
strcat_s(sql, filedNames.Number.c_str()); std::stringstream os;
strcat_s(sql, ","); os << "INSERT INTO public.__node_result__ ( ";
strcat_s(sql, filedNames.dDemand.c_str()); os << filedNames.Number;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, filedNames.dFlow.c_str()); os<< filedNames.dDemand;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, filedNames.dHead.c_str()); os<< filedNames.dHead;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, filedNames.dElevation.c_str()); os<< filedNames.dElevation;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, filedNames.dPressure.c_str()); os<< filedNames.dPressure;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, filedNames.dTankLevel.c_str()); os<< filedNames.dTankLevel;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, filedNames.dTankMaxVolume.c_str()); os<< filedNames.dTankMaxVolume;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, filedNames.dTankVolume.c_str()); os<< filedNames.dTankVolume;
strcat_s(sql, ","); /* os<< ",";
strcat_s(sql, filedNames.lTime.c_str()); os<< filedNames.lTime;*/
strcat_s(sql, ") VALUES("); os<< ") VALUES";
// 2, 参数值 // 2, 参数值
int total = nodeItems.size(); size_t total = nodeItems.size();
for (int i = 0; i < total; i++) for (int i = 0; i < total; i++)
{ {
NodeResultItem item = nodeItems[i]; NodeResultItem item = nodeItems[i];
strcat_s(sql, item.szNo); os << "('" << item.szNo << "',";
strcat_s(sql, ","); os << item.dDemand;
strcat_s(sql, std::to_string(item.dFlow).c_str()); os << ",";
strcat_s(sql, ","); os << item.dHead;
strcat_s(sql, std::to_string(item.dHead).c_str()); os << ",";
strcat_s(sql, ","); os << item.dElevation;
strcat_s(sql, std::to_string(item.dElevation).c_str()); os << ",";
strcat_s(sql, ","); os << item.dPressure;
strcat_s(sql, std::to_string(item.dPressure).c_str()); os << ",";
strcat_s(sql, ","); os << item.dTankLevel;
strcat_s(sql, std::to_string(item.dTankLevel).c_str()); os << ",";
strcat_s(sql, ","); os << item.dTankMaxVolume;
strcat_s(sql, std::to_string(item.dTankMaxVolume).c_str()); os << ",";
strcat_s(sql, ","); os << item.dTankVolume;
strcat_s(sql, std::to_string(item.dTankVolume).c_str()); /* os << ",";
strcat_s(sql, ","); os << item.lTime; */
strcat_s(sql, std::to_string(item.lTime).c_str()); if (i == (total - 1))
if(i==(total-1))
strcat_s(sql, ")");
else
strcat_s(sql, "),");
}
std::string sqlStr(sql, sql + strlen(sql));
std::string utf8Str = CivCommonUtils::string_To_UTF8(sqlStr);
PGresult* result = PQexec(mConn, utf8Str.c_str());
if (PQstatus(mConn) != PGRES_COMMAND_OK) // or conn==NULL
{ {
mLastError = PQresultErrorMessage(result); os << ")";
PQclear(result);
return false;
}
PQclear(result);
} }
else else
{ {
os << "),";
}
}
std::string sql = os.str();
os.clear();
if (!execSql(sql))
return false;
}
// 2, 参数值 // 2, 参数值
int total = nodeItems.size(); size_t total = nodeItems.size();
for (int i = 0; i < total; i++) for (int i = 0; i < total; i++)
{ {
NodeResultItem item = nodeItems[i]; NodeResultItem item = nodeItems[i];
...@@ -192,19 +181,10 @@ bool CivDbConnection::updateNode(const NodeResultItems& nodeItems) ...@@ -192,19 +181,10 @@ bool CivDbConnection::updateNode(const NodeResultItems& nodeItems)
strcat_s(sql, item.szNo); strcat_s(sql, item.szNo);
strcat_s(sql, "'"); strcat_s(sql, "'");
std::string sqlStr(sql, sql + strlen(sql)); if (!execSql(sql))
std::string utf8Str = CivCommonUtils::string_To_UTF8(sqlStr); return false;
PGresult* result = PQexec(mConn, utf8Str.c_str());
if (PQstatus(mConn) != PGRES_COMMAND_OK) // or conn==NULL
{
mLastError = PQresultErrorMessage(result);
continue;
} }
PQclear(result);
}
}
return true; return true;
} }
...@@ -214,111 +194,96 @@ bool CivDbConnection::updateLink(const LinkResultItems& lineItems) ...@@ -214,111 +194,96 @@ bool CivDbConnection::updateLink(const LinkResultItems& lineItems)
if (!isValid()) if (!isValid())
return false; return false;
if (createTable("__pipe_result__", "public", NODE_TABLE)) std::stringstream os;
if (createTable("__pipe_result__", "public", PIPE_TABLE))
{ {
LinkFiledName filedNames; LinkFiledName filedNames;
char sql[MAX_BUFFER] = "INSERT INTO public.__pipe_result__ ( "; os<< "INSERT INTO public.__pipe_result__( ";
strcat_s(sql, filedNames.szNo.c_str()); os<< filedNames.szNo;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, filedNames.dFlow.c_str()); os<< filedNames.dFlow;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, filedNames.nFlowDirect.c_str()); /* os<< filedNames.nFlowDirect;
strcat_s(sql, ","); os<< ",";*/
strcat_s(sql, filedNames.dVelocity.c_str()); os<< filedNames.dVelocity;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, filedNames.dHeadloss.c_str()); os<< filedNames.dHeadloss;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, filedNames.dUnitHeadloss.c_str()); os<< filedNames.dUnitHeadloss;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, filedNames.dFromNodHeadloss.c_str()); os<< filedNames.dFromNodHeadloss;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, filedNames.dToNodHeadloss.c_str()); os<< filedNames.dToNodHeadloss;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, filedNames.dLocalHeadloss.c_str()); os<< filedNames.dLocalHeadloss;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, filedNames.dFrictionHeadloss.c_str()); os<< filedNames.dFrictionHeadloss;
strcat_s(sql, ","); /*os<< ",";
strcat_s(sql, filedNames.dLocalHeadloss.c_str()); os<< filedNames.szStatus;*/
strcat_s(sql, ","); /*os<< ",";
strcat_s(sql, filedNames.szStatus.c_str()); os<< filedNames.lTime;*/
strcat_s(sql, ","); os<< ") VALUES";
strcat_s(sql, filedNames.lTime.c_str());
strcat_s(sql, ") VALUES(");
// 2, 参数值 // 2, 参数值
int total = lineItems.size(); size_t total = lineItems.size();
for (int i = 0; i < total; i++) for (int i = 0; i < total; i++)
{ {
LinkResultItem item = lineItems[i]; LinkResultItem item = lineItems[i];
os<<"('" <<item.szNo<<"'";
strcat_s(sql, item.szNo); os<< ",";
strcat_s(sql, ","); os<< item.dFlow;
strcat_s(sql, std::to_string(item.dFlow).c_str()); //os<< ",";
strcat_s(sql, ","); /* int direct = item.nFlowDirect;
strcat_s(sql, std::to_string(item.nFlowDirect).c_str()); os<< direct;*/
strcat_s(sql, ","); os<< ",";
strcat_s(sql, std::to_string(item.dVelocity).c_str()); os<< item.dVelocity;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, std::to_string(item.dHeadloss).c_str()); os<< item.dHeadloss;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, std::to_string(item.dUnitHeadloss).c_str()); os<< item.dUnitHeadloss;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, std::to_string(item.dFromNodHeadloss).c_str()); os<< item.dFromNodHeadloss;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, std::to_string(item.dToNodHeadloss).c_str()); os<< item.dToNodHeadloss;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, std::to_string(item.dLocalHeadloss).c_str()); os<< item.dLocalHeadloss;
strcat_s(sql, ","); os<< ",";
strcat_s(sql, std::to_string(item.dFrictionHeadloss).c_str()); os<< item.dFrictionHeadloss;
strcat_s(sql, ","); /*os<< ",";
strcat_s(sql, std::to_string(item.dLocalHeadloss).c_str()); os<< "'"<<item.szStatus<<"'";*/
strcat_s(sql, ","); /*os<< ",";
strcat_s(sql, item.szStatus); os<< item.lTime;*/
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.lTime).c_str());
// 拼接值
strcat_s(sql, std::to_string(item.dFlow).c_str());
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.dVelocity).c_str());
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.dUnitHeadloss).c_str());
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.dHeadloss).c_str());
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.dFrictionHeadloss).c_str());
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.dFromNodHeadloss).c_str());
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.dToNodHeadloss).c_str());
strcat_s(sql, ",");
strcat_s(sql, std::to_string(item.dLocalHeadloss).c_str());
if (i == (total - 1)) if (i == (total - 1))
strcat_s(sql, ")"); os << ")";
else else
strcat_s(sql, "),"); os << "),";
} }
std::string sqlStr(sql, sql + strlen(sql)); std::string sql = os.str();
std::string utf8Str = CivCommonUtils::string_To_UTF8(sqlStr); os.clear();
PGresult* result = PQexec(mConn, utf8Str.c_str()); if (!execSql(sql))
if (PQstatus(mConn) != PGRES_COMMAND_OK)
{
mLastError = PQresultErrorMessage(result);
return false; return false;
} }
PQclear(result);
}
else
{ {
// 2, 参数值 // 2, 参数值
int total = lineItems.size(); size_t total = lineItems.size();
/*
update
表一
set
表一的字段 = 表2的字段
from
表一,
表二
where
表一.Id = 表二.Id
*/
std::string sql = "update public.管段 as tb1 set(";
for (int i = 0; i < total; i++) for (int i = 0; i < total; i++)
{ {
LinkResultItem item = lineItems[i]; LinkResultItem item = lineItems[i];
char sql[512] = "update public."; char sql[512] = "update public.管段 as tb1 set(";
strcat_s(sql, "管段");
strcat_s(sql, " as tb1 set(");
strcat_s(sql, "流量"); strcat_s(sql, "流量");
strcat_s(sql, ",流速"); strcat_s(sql, ",流速");
strcat_s(sql, ",单位水头损"); strcat_s(sql, ",单位水头损");
...@@ -352,44 +317,37 @@ bool CivDbConnection::updateLink(const LinkResultItems& lineItems) ...@@ -352,44 +317,37 @@ bool CivDbConnection::updateLink(const LinkResultItems& lineItems)
strcat_s(sql, item.szNo); strcat_s(sql, item.szNo);
strcat_s(sql, "'"); strcat_s(sql, "'");
std::string sqlStr(sql, sql + strlen(sql)); if (!execSql(sql))
std::string utf8Str = CivCommonUtils::string_To_UTF8(sqlStr); return false;
PGresult* result = PQexec(mConn, utf8Str.c_str());
PQstatus(mConn);
if (PQstatus(mConn) != PGRES_COMMAND_OK) // or conn==NULL
{
mLastError = PQresultErrorMessage(result);
continue;
}
PQclear(result);
} }
} }
return true; return true;
} }
Componets CivDbConnection::getComponets(StrQuote tableName) const Componets CivDbConnection::getComponets(StrQuote tableName)
{ {
if (!isValid()) if (!isValid())
return Nodes(); return Nodes();
std::string sql = "select * from public.\"" + tableName + "\""; std::string sql = "select * from public.\"" + tableName + "\"";
std::string utf8Str = CivCommonUtils::string_To_UTF8(sql); if (!execSql(sql))
PGresult* result = PQexec(mConn, utf8Str.c_str());
if (result == NULL)
{ {
return Nodes(); return Nodes();
} }
Componets nodes; Componets nodes;
// 取数据到容器中 // 取数据到容器中
getData(result, nodes); getData(mResult, nodes);
PQclear(result);
return nodes; return nodes;
} }
void CivDbConnection::getData(PGresult* result, Componets& comp) const void CivDbConnection::getData(PGresult* result, Componets& comp)
{ {
if (result == NULL)
return;
int rows = PQntuples(result); int rows = PQntuples(result);
int columns = PQnfields(result); int columns = PQnfields(result);
for (int i = 0; i < rows; i++) for (int i = 0; i < rows; i++)
...@@ -407,10 +365,10 @@ void CivDbConnection::getData(PGresult* result, Componets& comp) const ...@@ -407,10 +365,10 @@ void CivDbConnection::getData(PGresult* result, Componets& comp) const
} }
comp.push_back(node); comp.push_back(node);
} }
PQclear(result);
} }
Componets CivDbConnection::getNodeByCode(StrQuote table, StrQuote code) const Componets CivDbConnection::getNodeByCode(StrQuote table, StrQuote code)
{ {
if (!isValid()) if (!isValid())
return Nodes(); return Nodes();
...@@ -422,22 +380,18 @@ Componets CivDbConnection::getNodeByCode(StrQuote table, StrQuote code) const ...@@ -422,22 +380,18 @@ Componets CivDbConnection::getNodeByCode(StrQuote table, StrQuote code) const
strcat_s(sql, code.c_str()); strcat_s(sql, code.c_str());
strcat_s(sql, "'"); strcat_s(sql, "'");
std::string utfStr = CivCommonUtils::string_To_UTF8(sql); if (!execSql(sql))
PGresult* result = PQexec(mConn, utfStr.c_str());
if (result == NULL)
{ {
return Nodes(); return Nodes();
} }
Componets componets; Componets componets;
getData(result, componets); getData(mResult, componets);
PQclear(result);
return componets; return componets;
} }
Componets CivDbConnection::getLineByCode(StrQuote table, StrQuote code) const Componets CivDbConnection::getLineByCode(StrQuote table, StrQuote code)
{ {
if (!isValid()) if (!isValid())
return Nodes(); return Nodes();
...@@ -451,18 +405,15 @@ Componets CivDbConnection::getLineByCode(StrQuote table, StrQuote code) const ...@@ -451,18 +405,15 @@ Componets CivDbConnection::getLineByCode(StrQuote table, StrQuote code) const
strcat_s(sql, code.c_str()); strcat_s(sql, code.c_str());
strcat_s(sql, "'"); strcat_s(sql, "'");
std::string utfStr = CivCommonUtils::string_To_UTF8(sql); if (!execSql(sql))
PGresult* result = PQexec(mConn, utfStr.c_str());
if (result == NULL)
{ {
return Nodes(); return Nodes();
} }
Componets componets; Componets componets;
getData(result, componets); getData(mResult, componets);
PQclear(result);
return componets; return componets;
} }
...@@ -476,16 +427,14 @@ Componets CivDbConnection::getLikelyPipes(StrQuote tableName) ...@@ -476,16 +427,14 @@ Componets CivDbConnection::getLikelyPipes(StrQuote tableName)
strcat_s(sql, tableName.c_str()); strcat_s(sql, tableName.c_str());
strcat_s(sql,"\""); strcat_s(sql,"\"");
std::string utfStr = CivCommonUtils::string_To_UTF8(sql); if (!execSql(sql))
PGresult* result = PQexec(mConn, utfStr.c_str());
if (result == NULL)
{ {
return Componets(); return Componets();
} }
Componets nodes; Componets nodes;
getData(result, nodes); getData(mResult, nodes);
PQclear(result);
// 查询线表 // 查询线表
if (nodes.size() <= 0) if (nodes.size() <= 0)
...@@ -525,15 +474,28 @@ Componets CivDbConnection::getLikelyPipes(StrQuote tableName) ...@@ -525,15 +474,28 @@ Componets CivDbConnection::getLikelyPipes(StrQuote tableName)
// 根据点数据查询起点和终点,管径 // 根据点数据查询起点和终点,管径
int total = nodes.size(); size_t total = nodes.size();
for (int i = 0; i < total; i++) for (int i = 0; i < total; i++)
{ {
Componet componet = nodes[i]; Componet componet = nodes[i];
nodes[i]["编号"] = componet.find("id")->second; Str bendianhao = componet.find("本点号")->second;
if (bendianhao.empty() || bendianhao == "")
{
Str ID = componet.find("id")->second;
Str type = mValvesMap.find(tableName)->second;
Str newbendian = type + ID;
std::string upSql = "Update "+ tableName +" Set 本点号 ='"+ newbendian +"' Where id ='"+ID+"'";
execSql(upSql);
nodes[i]["编号"] = newbendian;
}
else {
nodes[i]["编号"] = bendianhao;
}
Str code = componet.find("code")->second; Str code = componet.find("code")->second;
Componets lines = getLineByCode(PIPELINE, code); Componets lines = getLineByCode(PIPELINE, code);
int lTotal = lines.size(); size_t lTotal = lines.size();
for (int j = 0; j < lTotal; j++) for (int j = 0; j < lTotal; j++)
{ {
...@@ -546,7 +508,7 @@ Componets CivDbConnection::getLikelyPipes(StrQuote tableName) ...@@ -546,7 +508,7 @@ Componets CivDbConnection::getLikelyPipes(StrQuote tableName)
if (fromCode == code) if (fromCode == code)
{ {
int size = tables.size(); size_t size = tables.size();
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
{ {
codeNodes = getNodeByCode(tables[i], toCode); codeNodes = getNodeByCode(tables[i], toCode);
...@@ -563,7 +525,7 @@ Componets CivDbConnection::getLikelyPipes(StrQuote tableName) ...@@ -563,7 +525,7 @@ Componets CivDbConnection::getLikelyPipes(StrQuote tableName)
} }
else if (toCode == code) else if (toCode == code)
{ {
int size = tables.size(); size_t size = tables.size();
Str currTable; Str currTable;
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
{ {
...@@ -632,7 +594,7 @@ bool CivDbConnection::createTable(StrQuote tableName, StrQuote schema,TableType ...@@ -632,7 +594,7 @@ bool CivDbConnection::createTable(StrQuote tableName, StrQuote schema,TableType
return true; return true;
CivTableTemp tableTemp(tableName); CivTableTemp tableTemp(tableName);
LinkFiledName pipeFileds;
switch (tableType) switch (tableType)
{ {
case NODE_TABLE: case NODE_TABLE:
...@@ -647,40 +609,52 @@ bool CivDbConnection::createTable(StrQuote tableName, StrQuote schema,TableType ...@@ -647,40 +609,52 @@ bool CivDbConnection::createTable(StrQuote tableName, StrQuote schema,TableType
tableTemp.addProperty("液位高度", CivTableTemp::FILED_DOUBLE); tableTemp.addProperty("液位高度", CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty("水质", CivTableTemp::FILED_DOUBLE); tableTemp.addProperty("水质", CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty("时间", CivTableTemp::FILED_DATETIME); tableTemp.addProperty("时间", CivTableTemp::FILED_DATETIME);
break; break;
case PIPE_TABLE: case PIPE_TABLE:
tableTemp.addProperty("编号", CivTableTemp::FILED_VARCHAR);
tableTemp.addProperty("流量", CivTableTemp::FILED_DOUBLE); tableTemp.addProperty(pipeFileds.szNo, CivTableTemp::FILED_VARCHAR);
tableTemp.addProperty("流向", CivTableTemp::FILED_INT); tableTemp.addProperty(pipeFileds.dFlow, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty("流速", CivTableTemp::FILED_DOUBLE); tableTemp.addProperty(pipeFileds.nFlowDirect, CivTableTemp::FILED_INT);
tableTemp.addProperty("水头损失", CivTableTemp::FILED_DOUBLE); tableTemp.addProperty(pipeFileds.dVelocity, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty("单位水头损失", CivTableTemp::FILED_DOUBLE); tableTemp.addProperty(pipeFileds.dHeadloss , CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty("上点水头", CivTableTemp::FILED_DOUBLE); tableTemp.addProperty(pipeFileds.dUnitHeadloss, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty("本点水头", CivTableTemp::FILED_DOUBLE); tableTemp.addProperty(pipeFileds.dFromNodHeadloss, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty("局部水头损失", CivTableTemp::FILED_DOUBLE); tableTemp.addProperty(pipeFileds.dToNodHeadloss, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty("摩擦水头损失", CivTableTemp::FILED_DOUBLE); tableTemp.addProperty(pipeFileds.dLocalHeadloss, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty("状态", CivTableTemp::FILED_VARCHAR); tableTemp.addProperty(pipeFileds.dFrictionHeadloss, CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty("水质", CivTableTemp::FILED_DOUBLE); tableTemp.addProperty(pipeFileds.szStatus, CivTableTemp::FILED_VARCHAR);
tableTemp.addProperty( "水质", CivTableTemp::FILED_DOUBLE);
tableTemp.addProperty("时间", CivTableTemp::FILED_DATETIME); tableTemp.addProperty("时间", CivTableTemp::FILED_DATETIME);
break; break;
default: default:
break; break;
} }
std::string sql = tableTemp.createSql(); std::string sql = tableTemp.createSql();
return execSql(sql);
}
bool CivDbConnection::execSql(std::string sql)
{
if (sql == "" || sql.empty()) if (sql == "" || sql.empty())
return false; return false;
std::string sqlStr = CivCommonUtils::string_To_UTF8(sql); std::string sqlStr = CivCommonUtils::string_To_UTF8(sql);
const char* finalSql = sqlStr.c_str(); const char* finalSql = sqlStr.c_str();
PGresult* result = PQexec(mConn, finalSql); mResult = PQexec(mConn, finalSql);
if (PQstatus(mConn) != PGRES_COMMAND_OK)
if (PQstatus(mConn) == PGRES_BAD_RESPONSE)
{ {
mLastError = PQresultErrorMessage(result); mLastError = PQresultErrorMessage(mResult);
std::cout << mLastError << std::endl; PQclear(mResult);
PQclear(result);
return false; return false;
} }
PQclear(result); mLastError = PQresultErrorMessage(mResult);
return true; return true;
} }
void CivDbConnection::clearResult()
{
PQclear(mResult);
}
\ No newline at end of file
...@@ -30,13 +30,13 @@ public: ...@@ -30,13 +30,13 @@ public:
*@brief 获取管网的所有表 *@brief 获取管网的所有表
*@netName: 管网名 *@netName: 管网名
*/ */
Tables getTables(StrQuote netName, StrQuote schema = "public") const; Tables getTables(StrQuote netName, StrQuote schema = "public");
/** /**
*@brief 获取管网组件数据:管线,节点,水源等(必须组件) *@brief 获取管网组件数据:管线,节点,水源等(必须组件)
*@tableName: 组件表 *@tableName: 组件表
*/ */
Componets getComponets(StrQuote tableName) const; Componets getComponets(StrQuote tableName);
/** /**
*@brief 水泵,阀门 (节点转换为管线) *@brief 水泵,阀门 (节点转换为管线)
...@@ -81,19 +81,28 @@ private: ...@@ -81,19 +81,28 @@ private:
*@table: 线表 *@table: 线表
*@code: *@code:
*/ */
Componets getLineByCode(StrQuote table, StrQuote code) const; Componets getLineByCode(StrQuote table, StrQuote code);
Componets getNodeByCode(StrQuote table, StrQuote code) const; Componets getNodeByCode(StrQuote table, StrQuote code);
/**
*@brief 执行sql语句
*@sql: 需要执行的sql语句
*/
bool execSql(std::string sql);
void clearResult();
/** /**
*@brief 从查询集取出数据 *@brief 从查询集取出数据
*@result: 查询结果集 *@result: 查询结果集
*@comp: 数据容器 *@comp: 数据容器
*/ */
void getData(PGresult* result, Componets& comp) const; void getData(PGresult* result, Componets& comp);
private: private:
PGconn* mConn; PGconn* mConn;
PGresult* mResult;
bool mIsOpen = false; bool mIsOpen = false;
char* mUri; char* mUri;
......
#include "CivTableTemp.h" #include "CivTableTemp.h"
#include <fstream>
CivTableTemp::CivTableTemp(std::string tableName) CivTableTemp::CivTableTemp(std::string tableName)
:mTableName(tableName) :mTableName(tableName)
...@@ -32,6 +33,7 @@ std::string CivTableTemp::createSql() ...@@ -32,6 +33,7 @@ std::string CivTableTemp::createSql()
if (mPropertys.size() <= 0) if (mPropertys.size() <= 0)
return ""; return "";
char sql[512] = "create table "; char sql[512] = "create table ";
strcat_s(sql, mTableName.c_str()); strcat_s(sql, mTableName.c_str());
strcat_s(sql, "(id serial primary key"); strcat_s(sql, "(id serial primary key");
......
...@@ -179,11 +179,11 @@ struct LinkFiledName ...@@ -179,11 +179,11 @@ struct LinkFiledName
Str nFlowDirect = "流向"; Str nFlowDirect = "流向";
Str dVelocity="流速"; Str dVelocity="流速";
Str dHeadloss="水头损失"; Str dHeadloss="水头损失";
Str dUnitHeadloss="单位水头损"; Str dUnitHeadloss="单位水头损";
Str dFromNodHeadloss="上点水头"; Str dFromNodHeadloss="上点水头";
Str dToNodHeadloss="本点水头"; Str dToNodHeadloss="本点水头";
Str dLocalHeadloss="局部水头损"; Str dLocalHeadloss="局部水头损";
Str dFrictionHeadloss="摩擦水头损"; Str dFrictionHeadloss="摩擦水头损";
Str szStatus="状态"; Str szStatus="状态";
Str lTime = "时间"; Str lTime = "时间";
}; };
......
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