/** * * @file DbClientImpl.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 namespace drogon { namespace orm { class DbClientImpl : public DbClient, public std::enable_shared_from_this { public: DbClientImpl(const std::string &connInfo, size_t connNum, #if LIBPQ_SUPPORTS_BATCH_MODE ClientType type, bool autoBatch); #else ClientType type); #endif ~DbClientImpl() 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 init(); void closeAll() override; private: size_t numberOfConnections_; trantor::EventLoopThreadPool loops_; std::shared_ptr sharedMutexPtr_; double timeout_{-1.0}; #if LIBPQ_SUPPORTS_BATCH_MODE bool autoBatch_{false}; #endif DbConnectionPtr newConnection(trantor::EventLoop *loop); void makeTrans( const DbConnectionPtr &conn, std::function &)> &&callback); mutable std::mutex connectionsMutex_; std::unordered_set connections_; std::unordered_set readyConnections_; std::unordered_set busyConnections_; std::list &)>>> transCallbacks_; std::deque> sqlCmdBuffer_; void handleNewTask(const DbConnectionPtr &connPtr); void execSqlWithTimeout( const char *sql, size_t sqlLength, size_t paraNum, std::vector &¶meters, std::vector &&length, std::vector &&format, ResultCallback &&rcb, std::function &&exceptCallback); }; } // namespace orm } // namespace drogon