/** * * @file DbClientLockFree.h * @author An Tao * * Copyright 2018, An Tao. All rights reserved. * https://github.com/an-tao/drogon * Use of this source code is governed by a MIT license * that can be found in the License file. * * Drogon * */ #pragma once #include "DbConnection.h" #include #include #include #include #include #include #include #include #include namespace drogon { namespace orm { class DbClientLockFree : public DbClient, public std::enable_shared_from_this { public: DbClientLockFree(const std::string &connInfo, trantor::EventLoop *loop, ClientType type, #if LIBPQ_SUPPORTS_BATCH_MODE size_t connectionNumberPerLoop, bool autoBatch); #else size_t connectionNumberPerLoop); #endif ~DbClientLockFree() noexcept override; void execSql(const char *sql, size_t sqlLength, size_t paraNum, std::vector &¶meters, std::vector &&length, std::vector &&format, ResultCallback &&rcb, std::function &&exceptCallback) override; std::shared_ptr newTransaction( const std::function &commitCallback = std::function()) noexcept(false) override; void newTransactionAsync( const std::function &)> &callback) override; bool hasAvailableConnections() const noexcept override; void setTimeout(double timeout) override { timeout_ = timeout; } void closeAll() override; private: std::string connectionInfo_; trantor::EventLoop *loop_; DbConnectionPtr newConnection(); const size_t numberOfConnections_; std::vector connections_; std::vector connectionHolders_; std::unordered_set transSet_; std::deque> sqlCmdBuffer_; std::list &)>>> transCallbacks_; double timeout_{-1.0}; void makeTrans( const DbConnectionPtr &conn, std::function &)> &&callback); void execSqlWithTimeout( const char *sql, size_t sqlLength, size_t paraNum, std::vector &¶meters, std::vector &&length, std::vector &&format, ResultCallback &&rcb, std::function &&ecb); void handleNewTask(const DbConnectionPtr &conn); #if LIBPQ_SUPPORTS_BATCH_MODE size_t connectionPos_{0}; // Used for pg batch mode. bool autoBatch_{false}; #endif }; } // namespace orm } // namespace drogon