#ifndef QUERY_CONNECTION_HPP #define QUERY_CONNECTION_HPP #include "matador/sql/abstract_sql_logger.hpp" #include "matador/sql/column_definition.hpp" #include "matador/sql/connection_info.hpp" #include "matador/sql/executor.hpp" #include "matador/sql/statement.hpp" #include namespace matador::sql { class schema; class connection_impl; /** * @brief The connection class represents a connection to a database. */ class connection final : public executor { public: /** * @brief Creates a database connection from a connection info data. * * @param info The database connection info data * @param sql_logger The logging handler */ explicit connection(connection_info info, const std::shared_ptr &sql_logger = std::make_shared()); /** * @brief Creates a database connection from a connection string. * * @param dns The database connection string * @param sql_logger The logging handler */ explicit connection(const std::string &dns, const std::shared_ptr &sql_logger = std::make_shared()); /** * Copies a given connection * * @param x The connection to copy */ connection(const connection &x); /** * Assigns from the given connection * * @param x The connection to assign * @return The reference to the assigned connection */ connection &operator=(const connection &x); /** * Copy moves a given connection * * @param x The connection to copy move */ connection(connection &&x) noexcept = default; /** * Assigns moves from the given connection * * @param x The connection to assign move * @return The reference to the assigned connection */ connection& operator=(connection &&x) noexcept; ~connection() override; /** * @brief Opens the database connection for the given dns. * * Opens the database connection. If database connection * couldn't be opened an exception is thrown. */ [[nodiscard]] utils::result open() const; /** * @brief Closes the database connection. * * Closes the database connection. */ [[nodiscard]] utils::result close() const; /** * @brief Returns true if database connection is open. * * Returns true if database connection is open * * @return True on open database connection. */ [[nodiscard]] utils::result is_open() const; /** * Returns the connection info data of the * current database connection. * * @return Returns the connection info data */ [[nodiscard]] const connection_info &info() const; /** * @brief Return the database type of the connection. * * Returns the database type of the connection which is * currently one of * - mssql * - mysql * - sqlite * - postgres * * @return The database type string */ [[nodiscard]] std::string type() const; /** * @brief Starts a transaction by calling the * underlying database backends transaction begin * statement. */ [[nodiscard]] utils::result begin() const; /** * @brief Commits a transaction by calling the * underlying database backends transaction commit * statement. */ [[nodiscard]] utils::result commit() const; /** * @brief Rollback a transaction by calling the * underlying database backends transaction rollback/abort * statement. */ [[nodiscard]] utils::result rollback() const; [[nodiscard]] utils::result, utils::error> describe(const std::string &table_name) const; [[nodiscard]] utils::result exists(const std::string &schema_name, const std::string &table_name) const; [[nodiscard]] utils::result exists(const std::string &table_name) const; [[nodiscard]] utils::result execute(const std::string &sql) const; [[nodiscard]] utils::result, utils::error> fetch(const query_context &ctx) const override; [[nodiscard]] utils::result execute(const query_context &ctx) const override; [[nodiscard]] utils::result prepare(const query_context &ctx) const override; [[nodiscard]] std::string str( const query_context& ctx ) const override; [[nodiscard]] const class dialect &dialect() const override; private: friend class fetchable_query; friend class session; connection_info connection_info_; std::unique_ptr connection_; std::shared_ptr logger_ = std::make_shared(); }; } #endif //QUERY_CONNECTION_HPP