Use shorter stmt names in postgresql connections(#326)

This commit is contained in:
An Tao 2020-01-04 09:36:14 +08:00 committed by GitHub
parent cb95960e2d
commit f07d9e03e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 3 deletions

View File

@ -46,7 +46,9 @@ bool checkSql(const string_view &sql_)
sql.find("delete") != std::string::npos || sql.find("delete") != std::string::npos ||
sql.find("drop") != std::string::npos || sql.find("drop") != std::string::npos ||
sql.find("truncate") != std::string::npos || sql.find("truncate") != std::string::npos ||
sql.find("lock") != std::string::npos); sql.find("lock") != std::string::npos ||
sql.find("create") != std::string::npos ||
sql.find("alter") != std::string::npos);
} }
} // namespace orm } // namespace orm
@ -273,7 +275,7 @@ void PgConnection::sendBatchedSql()
auto iter = preparedStatementsMap_.find(cmd->sql_); auto iter = preparedStatementsMap_.find(cmd->sql_);
if (iter == preparedStatementsMap_.end()) if (iter == preparedStatementsMap_.end())
{ {
statName = utils::getUuid(); statName = newStmtName();
if (PQsendPrepare(connectionPtr_.get(), if (PQsendPrepare(connectionPtr_.get(),
statName.c_str(), statName.c_str(),
cmd->sql_.data(), cmd->sql_.data(),

View File

@ -252,7 +252,7 @@ void PgConnection::execSqlInLoop(
else else
{ {
isRreparingStatement_ = true; isRreparingStatement_ = true;
statementName_ = utils::getUuid(); statementName_ = newStmtName();
if (PQsendPrepare(connectionPtr_.get(), if (PQsendPrepare(connectionPtr_.get(),
statementName_.c_str(), statementName_.c_str(),
sql_.data(), sql_.data(),

View File

@ -91,6 +91,12 @@ class PgConnection : public DbConnection,
std::shared_ptr<PGconn> connectionPtr_; std::shared_ptr<PGconn> connectionPtr_;
trantor::Channel channel_; trantor::Channel channel_;
bool isRreparingStatement_{false}; bool isRreparingStatement_{false};
size_t preparedStatementsID_{0};
std::string newStmtName()
{
loop_->assertInLoopThread();
return std::to_string(++preparedStatementsID_);
}
void handleRead(); void handleRead();
void pgPoll(); void pgPoll();
void handleClosed(); void handleClosed();