initial matador ng commit
This commit is contained in:
@@ -1,58 +1,153 @@
|
||||
#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/connection_impl.hpp"
|
||||
#include "matador/sql/dialect.hpp"
|
||||
#include "matador/sql/query.hpp"
|
||||
#include "matador/sql/query_context.hpp"
|
||||
#include "matador/sql/query_result.hpp"
|
||||
#include "matador/sql/record.hpp"
|
||||
#include "matador/sql/executor.hpp"
|
||||
#include "matador/sql/statement.hpp"
|
||||
|
||||
#include "matador/utils/logger.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace matador::sql {
|
||||
|
||||
class schema;
|
||||
class connection_impl;
|
||||
|
||||
class connection
|
||||
{
|
||||
/**
|
||||
* @brief The connection class represents a connection to a database.
|
||||
*/
|
||||
class connection final : public executor {
|
||||
public:
|
||||
explicit connection(connection_info info);
|
||||
explicit connection(const std::string& dns);
|
||||
/**
|
||||
* @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<abstract_sql_logger> &sql_logger = std::make_shared<null_sql_logger>());
|
||||
/**
|
||||
* @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<abstract_sql_logger> &sql_logger = std::make_shared<null_sql_logger>());
|
||||
/**
|
||||
* Copies a given connection
|
||||
*
|
||||
* @param x The connection to copy
|
||||
*/
|
||||
connection(const connection &x);
|
||||
connection& operator=(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;
|
||||
~connection();
|
||||
/**
|
||||
* 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;
|
||||
|
||||
void open();
|
||||
void close();
|
||||
[[nodiscard]] bool is_open() const;
|
||||
[[nodiscard]] const connection_info& info() const;
|
||||
~connection() override;
|
||||
|
||||
[[nodiscard]] std::vector<sql::column_definition> describe(const std::string &table_name) const;
|
||||
[[nodiscard]] bool exists(const std::string &schema_name, const std::string &table_name) const;
|
||||
[[nodiscard]] bool exists(const std::string &table_name) const;
|
||||
/**
|
||||
* @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<void, utils::error> open() const;
|
||||
/**
|
||||
* @brief Closes the database connection.
|
||||
*
|
||||
* Closes the database connection.
|
||||
*/
|
||||
[[nodiscard]] utils::result<void, utils::error> 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<bool, utils::error> is_open() const;
|
||||
|
||||
sql::query query(const sql::schema &schema) 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;
|
||||
|
||||
query_result<record> fetch(const query_context &q) const;
|
||||
[[nodiscard]] std::unique_ptr<query_result_impl> fetch(const std::string &sql) const;
|
||||
[[nodiscard]] size_t execute(const std::string &sql) const;
|
||||
/**
|
||||
* @brief Starts a transaction by calling the
|
||||
* underlying database backends transaction begin
|
||||
* statement.
|
||||
*/
|
||||
[[nodiscard]] utils::result<void, utils::error> begin() const;
|
||||
|
||||
statement prepare(query_context &&query) const;
|
||||
/**
|
||||
* @brief Commits a transaction by calling the
|
||||
* underlying database backends transaction commit
|
||||
* statement.
|
||||
*/
|
||||
[[nodiscard]] utils::result<void, utils::error> commit() const;
|
||||
|
||||
const class dialect& dialect() const;
|
||||
/**
|
||||
* @brief Rollback a transaction by calling the
|
||||
* underlying database backends transaction rollback/abort
|
||||
* statement.
|
||||
*/
|
||||
[[nodiscard]] utils::result<void, utils::error> rollback() const;
|
||||
|
||||
[[nodiscard]] utils::result<std::vector<column_definition>, utils::error> describe(const std::string &table_name) const;
|
||||
[[nodiscard]] utils::result<bool, utils::error> exists(const std::string &schema_name, const std::string &table_name) const;
|
||||
[[nodiscard]] utils::result<bool, utils::error> exists(const std::string &table_name) const;
|
||||
|
||||
[[nodiscard]] utils::result<size_t, utils::error> execute(const std::string &sql) const;
|
||||
|
||||
[[nodiscard]] utils::result<std::unique_ptr<query_result_impl>, utils::error> fetch(const query_context &ctx) const override;
|
||||
[[nodiscard]] utils::result<size_t, utils::error> execute(const query_context &ctx) const override;
|
||||
[[nodiscard]] utils::result<statement, utils::error> 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_impl> connection_;
|
||||
utils::logger logger_;
|
||||
const class dialect &dialect_;
|
||||
std::shared_ptr<abstract_sql_logger> logger_ = std::make_shared<null_sql_logger>();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //QUERY_CONNECTION_HPP
|
||||
|
||||
Reference in New Issue
Block a user