Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pg more options #2

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion orm_lib/inc/drogon/orm/DbClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,10 @@ class DROGON_EXPORT DbClient : public trantor::NonCopyable
std::vector<const char *> &&parameters,
std::vector<int> &&length,
std::vector<int> &&format,
int resultFormat,
ResultCallback &&rcb,
std::function<void(const std::exception_ptr &)> &&exceptCallback) = 0;
std::function<void(const std::exception_ptr &)> &&exceptCallback,
bool usePreparedStmt) = 0;

protected:
ClientType type_;
Expand Down
57 changes: 57 additions & 0 deletions orm_lib/inc/drogon/orm/SqlBinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ enum class Mode
NonBlocking,
Blocking
};
enum class ResultFormat
{
Text = 0,
Binary = 1
};
enum class SqlOption
{
DisablePreparedStmt
};

namespace internal
{
Expand Down Expand Up @@ -323,6 +332,7 @@ class DROGON_EXPORT SqlBinder : public trantor::NonCopyable
parameters_(std::move(that.parameters_)),
lengths_(std::move(that.lengths_)),
formats_(std::move(that.formats_)),
resultFormat_(that.resultFormat_),
objs_(std::move(that.objs_)),
mode_(that.mode_),
callbackHolder_(std::move(that.callbackHolder_)),
Expand All @@ -331,6 +341,7 @@ class DROGON_EXPORT SqlBinder : public trantor::NonCopyable
execed_(that.execed_),
destructed_(that.destructed_),
isExceptionPtr_(that.isExceptionPtr_),
usePreparedStmt_(that.usePreparedStmt_),
type_(that.type_)
{
// set the execed_ to true to avoid the same sql being executed twice.
Expand Down Expand Up @@ -511,6 +522,50 @@ class DROGON_EXPORT SqlBinder : public trantor::NonCopyable
return *this;
}

self &operator<<(const ResultFormat &resultFormat)
{
resultFormat_ = resultFormat;
return *this;
}

self &operator<<(ResultFormat &resultFormat)
{
resultFormat_ = resultFormat;
return *this;
}

self &operator<<(ResultFormat &&resultFormat)
{
resultFormat_ = resultFormat;
return *this;
}

self &setSqlOption(SqlOption option)
{
switch (option)
{
case SqlOption::DisablePreparedStmt:
usePreparedStmt_ = false;
break;
}
return *this;
}

self &operator<<(const SqlOption &option)
{
return setSqlOption(option);
}

self &operator<<(SqlOption &option)
{
return setSqlOption(option);
}

self &operator<<(SqlOption &&option)
{
return setSqlOption(option);
}

template <typename T>
self &operator<<(const std::optional<T> &parameter)
{
Expand Down Expand Up @@ -590,6 +645,7 @@ class DROGON_EXPORT SqlBinder : public trantor::NonCopyable
std::vector<const char *> parameters_;
std::vector<int> lengths_;
std::vector<int> formats_;
ResultFormat resultFormat_{ResultFormat::Text};
std::vector<std::shared_ptr<void>> objs_;
Mode mode_{Mode::NonBlocking};
std::shared_ptr<CallbackHolderBase> callbackHolder_;
Expand All @@ -598,6 +654,7 @@ class DROGON_EXPORT SqlBinder : public trantor::NonCopyable
bool execed_{false};
bool destructed_{false};
bool isExceptionPtr_{false};
bool usePreparedStmt_{true};
ClientType type_;
};

Expand Down
36 changes: 26 additions & 10 deletions orm_lib/src/DbClientImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ void DbClientImpl::execSql(
std::vector<const char *> &&parameters,
std::vector<int> &&length,
std::vector<int> &&format,
int resultFormat,
ResultCallback &&rcb,
std::function<void(const std::exception_ptr &)> &&exceptCallback)
std::function<void(const std::exception_ptr &)> &&exceptCallback,
bool usePreparedStmt)
{
assert(paraNum == parameters.size());
assert(paraNum == length.size());
Expand All @@ -138,16 +140,18 @@ void DbClientImpl::execSql(
std::move(parameters),
std::move(length),
std::move(format),
resultFormat,
std::move(rcb),
std::move(exceptCallback));
std::move(exceptCallback),
usePreparedStmt);
return;
}
DbConnectionPtr conn;
bool busy = false;
{
std::lock_guard<std::mutex> guard(connectionsMutex_);

if (readyConnections_.size() == 0)
if (readyConnections_.empty())
{
if (sqlCmdBuffer_.size() > 200000)
{
Expand All @@ -163,8 +167,10 @@ void DbClientImpl::execSql(
std::move(parameters),
std::move(length),
std::move(format),
resultFormat,
std::move(rcb),
std::move(exceptCallback));
std::move(exceptCallback),
usePreparedStmt);
sqlCmdBuffer_.push_back(std::move(cmd));
}
}
Expand All @@ -183,8 +189,10 @@ void DbClientImpl::execSql(
std::move(parameters),
std::move(length),
std::move(format),
resultFormat,
std::move(rcb),
std::move(exceptCallback));
std::move(exceptCallback),
usePreparedStmt);
return;
}
if (busy)
Expand Down Expand Up @@ -368,8 +376,10 @@ void DbClientImpl::handleNewTask(const DbConnectionPtr &connPtr)
std::move(cmd->parameters_),
std::move(cmd->lengths_),
std::move(cmd->formats_),
cmd->resultFormat_,
std::move(cmd->callback_),
std::move(cmd->exceptionCallback_));
std::move(cmd->exceptionCallback_),
cmd->usePreparedStmt_);
return;
}
}
Expand Down Expand Up @@ -487,8 +497,10 @@ void DbClientImpl::execSqlWithTimeout(
std::vector<const char *> &&parameters,
std::vector<int> &&length,
std::vector<int> &&format,
int resultFormat,
ResultCallback &&rcb,
std::function<void(const std::exception_ptr &)> &&ecb)
std::function<void(const std::exception_ptr &)> &&ecb,
bool usePreparedStmt)
{
DbConnectionPtr conn;
assert(timeout_ > 0.0);
Expand Down Expand Up @@ -536,7 +548,7 @@ void DbClientImpl::execSqlWithTimeout(
{
std::lock_guard<std::mutex> guard(connectionsMutex_);

if (readyConnections_.size() == 0)
if (readyConnections_.empty())
{
if (sqlCmdBuffer_.size() > 200000)
{
Expand All @@ -552,8 +564,10 @@ void DbClientImpl::execSqlWithTimeout(
std::move(parameters),
std::move(length),
std::move(format),
resultFormat,
std::move(resultCallback),
std::move(exceptionCallback));
std::move(exceptionCallback),
usePreparedStmt);
sqlCmdBuffer_.emplace_back(command);
*cmd = command;
}
Expand All @@ -573,8 +587,10 @@ void DbClientImpl::execSqlWithTimeout(
std::move(parameters),
std::move(length),
std::move(format),
resultFormat,
std::move(resultCallback),
std::move(exceptionCallback));
std::move(exceptionCallback),
usePreparedStmt);
timeoutFlagPtr->runTimer();
return;
}
Expand Down
24 changes: 14 additions & 10 deletions orm_lib/src/DbClientImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,17 @@ class DbClientImpl : public DbClient,
ClientType type);
#endif
~DbClientImpl() noexcept override;
void execSql(const char *sql,
size_t sqlLength,
size_t paraNum,
std::vector<const char *> &&parameters,
std::vector<int> &&length,
std::vector<int> &&format,
ResultCallback &&rcb,
std::function<void(const std::exception_ptr &)>
&&exceptCallback) override;
void execSql(
const char *sql,
size_t sqlLength,
size_t paraNum,
std::vector<const char *> &&parameters,
std::vector<int> &&length,
std::vector<int> &&format,
int resultFormat,
ResultCallback &&rcb,
std::function<void(const std::exception_ptr &)> &&exceptCallback,
bool usePreparedStmt) override;
std::shared_ptr<Transaction> newTransaction(
const std::function<void(bool)> &commitCallback =
std::function<void(bool)>()) noexcept(false) override;
Expand Down Expand Up @@ -99,8 +101,10 @@ class DbClientImpl : public DbClient,
std::vector<const char *> &&parameters,
std::vector<int> &&length,
std::vector<int> &&format,
int resultFormat,
ResultCallback &&rcb,
std::function<void(const std::exception_ptr &)> &&exceptCallback);
std::function<void(const std::exception_ptr &)> &&exceptCallback,
bool usePreparedStmt);
};

} // namespace orm
Expand Down
Loading