Merge pull request #71 from an-tao/dev

Modify the PgConnection class
This commit is contained in:
An Tao 2019-03-08 17:06:08 +08:00 committed by GitHub
commit 8575951052
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 46 deletions

View File

@ -3,7 +3,7 @@
GIT_VER=$(git log|grep ^commit|wc -l|sed -e "s/^ *//")
MD5=$(git log|head -1|awk '{printf $2}')
TMP_FILE=/tmp/version
echo "#define VERSION \"0.9.29.$GIT_VER\"" > ${TMP_FILE}
echo "#define VERSION \"0.9.30.$GIT_VER\"" > ${TMP_FILE}
echo "#define VERSION_MD5 \"$MD5\"" >> ${TMP_FILE}
if [ ! -f $1 ];then
mv -f ${TMP_FILE} $1

View File

@ -233,8 +233,8 @@ void PgConnection::execSqlInLoop(std::string &&sql,
else
{
_isRreparingStatement = true;
auto statementName = getuuid();
if (PQsendPrepare(_connPtr.get(), statementName.c_str(), _sql.c_str(), paraNum, NULL) == 0)
_statementName = getuuid();
if (PQsendPrepare(_connPtr.get(), _statementName.c_str(), _sql.c_str(), paraNum, NULL) == 0)
{
LOG_ERROR << "send query error: " << PQerrorMessage(_connPtr.get());
if (_isWorking)
@ -255,46 +255,10 @@ void PgConnection::execSqlInLoop(std::string &&sql,
}
return;
}
std::weak_ptr<PgConnection> weakPtr = shared_from_this();
_preparingCallback = [weakPtr,
statementName,
paraNum,
parameters = std::move(parameters),
length = std::move(length),
format = std::move(format)]() {
auto thisPtr = weakPtr.lock();
if (!thisPtr)
return;
thisPtr->_isRreparingStatement = false;
thisPtr->_preparedStatementMap[thisPtr->_sql] = statementName;
if (PQsendQueryPrepared(thisPtr->_connPtr.get(),
statementName.c_str(),
paraNum,
parameters.data(),
length.data(),
format.data(),
0) == 0)
{
LOG_ERROR << "send query error: " << PQerrorMessage(thisPtr->_connPtr.get());
if (thisPtr->_isWorking)
{
thisPtr->_isWorking = false;
try
{
throw Failure(PQerrorMessage(thisPtr->_connPtr.get()));
}
catch (...)
{
auto exceptPtr = std::current_exception();
thisPtr->_exceptCb(exceptPtr);
thisPtr->_exceptCb = nullptr;
}
thisPtr->_cb = nullptr;
thisPtr->_idleCb();
}
return;
}
};
_paraNum = paraNum;
_parameters = std::move(parameters);
_length = std::move(length);
_format = std::move(format);
}
}
pgPoll();
@ -374,8 +338,7 @@ void PgConnection::handleRead()
{
if (_isRreparingStatement)
{
_preparingCallback();
_preparingCallback = nullptr;
doAfterPreparing();
}
else
{
@ -385,3 +348,36 @@ void PgConnection::handleRead()
}
}
}
void PgConnection::doAfterPreparing()
{
_isRreparingStatement = false;
_preparedStatementMap[_sql] = _statementName;
if (PQsendQueryPrepared(_connPtr.get(),
_statementName.c_str(),
_paraNum,
_parameters.data(),
_length.data(),
_format.data(),
0) == 0)
{
LOG_ERROR << "send query error: " << PQerrorMessage(_connPtr.get());
if (_isWorking)
{
_isWorking = false;
try
{
throw Failure(PQerrorMessage(_connPtr.get()));
}
catch (...)
{
auto exceptPtr = std::current_exception();
_exceptCb(exceptPtr);
_exceptCb = nullptr;
}
_cb = nullptr;
_idleCb();
}
return;
}
}

View File

@ -95,7 +95,13 @@ class PgConnection : public DbConnection, public std::enable_shared_from_this<Pg
std::vector<int> &&format,
ResultCallback &&rcb,
std::function<void(const std::exception_ptr &)> &&exceptCallback);
std::function<void()> _preparingCallback;
//std::function<void()> _preparingCallback;
void doAfterPreparing();
std::string _statementName;
int _paraNum;
std::vector<const char *> _parameters;
std::vector<int> _length;
std::vector<int> _format;
};
} // namespace orm