|
- #pragma once
-
- #include <memory>
-
- #include <cppmariadb/config.h>
- #include <cppmariadb/enums.h>
- #include <cppmariadb/impl/handle.h>
-
- namespace cppmariadb
- {
-
- /**
- * @brief MariaDB connection.
- */
- struct connection
- : public __impl::handle<MYSQL*>
- {
- private:
- using result_t = ::cppmariadb::result;
- using result_ptr_u = std::unique_ptr<result_t>;
-
- result_ptr_u _result; //!< REsult of the last query.
-
- public:
- /**
- * @breif Default constructor.
- */
- inline connection();
-
- /**
- * @brief Value constructor. Creates a new connection using the mariadb handle.
- */
- inline connection(MYSQL* h);
-
- /**
- * @brief Move constructor.
- */
- inline connection(connection&& other);
-
- /**
- * @brief Copy constructor.
- */
- inline connection(const connection& other) = delete;
-
- /**
- * @brief Destructor.
- */
- inline ~connection();
-
- /**
- * @brief Move assignment constructor.
- */
- inline connection& operator =(connection&& other);
-
- /**
- * @brief Copy assignment constructor.
- */
- inline connection& operator =(const connection& other) = delete;
-
- public: /* misc */
- /**
- * @brief Connect to the database.
- *
- * @param[in] host Host to connect to.
- * @param[in] port Port to connect to.
- * @param[in] user User to use for authentication.
- * @param[in] password Password to use for authentication.
- * @param[in] database Default database to connect to.
- * @param[in] flags Connection flags.
- */
- inline void connect(
- const std::string& host,
- const uint& port,
- const std::string& user,
- const std::string& password,
- const std::string& database,
- const client_flags& flags);
-
- /**
- * @brief Close this connection.
- */
- inline void close();
-
- /**
- * @brief Get the result of the last executed query.
- */
- inline result_t* result() const;
-
- /**
- * @brief Get the field count of the last executed query.
- */
- inline uint fieldcount() const;
-
- /**
- * @brief Get the error code of the last executed operation.
- */
- inline error_code error_code() const;
-
- /**
- * @brief Get the error code of the last executed operation.
- */
- inline std::string error_msg() const;
-
- /**
- * @brief Escape the passed string.
- *
- * @param[in] value String to escape.
- *
- * @return Escaped string.
- */
- inline std::string escape(const std::string& value) const;
-
- public: /* execute query */
- /**
- * @brief Execute the passed query.
- *
- * @param[in] cmd Query to execute.
- */
- inline void execute(const std::string& cmd);
-
- /**
- * @brief Execute the passed query.
- *
- * @param[in] cmd Query to execute.
- *
- * @return ID of the inserted/updated/effected row.
- */
- inline unsigned long long execute_id(const std::string& cmd);
-
- /**
- * @brief Execute the passed query.
- *
- * @param[in] cmd Query to execute.
- *
- * @return Number of affected rows.
- */
- inline unsigned long long execute_rows(const std::string& cmd);
-
- /**
- * @brief Execute the passed query.
- *
- * @param[in] cmd Query to execute.
- *
- * @return Stored result or nullptr if the query could not be executed.
- */
- inline result_stored* execute_stored(const std::string& cmd);
-
- /**
- * @brief Execute the passed query.
- *
- * @param[in] cmd Query to execute.
- *
- * @return Used result or nullptr if the query could not be executed.
- */
- inline result_used* execute_used(const std::string& cmd);
-
- public: /* execute statement */
- /**
- * @brief Render and execute the passed statement.
- *
- * @param[in] s Statement to render and execute.
- */
- inline void execute(const statement& s);
-
- /**
- * @brief Render and execute the passed statement.
- *
- * @param[in] s Statement to render and execute.
- *
- * @return ID of the inserted/updated/effected row.
- */
- inline unsigned long long execute_id(const statement& s);
-
- /**
- * @brief Render and execute the passed statement.
- *
- * @param[in] s Statement to render and execute.
- *
- * @return Number of affected rows.
- */
- inline unsigned long long execute_rows(const statement& s);
-
- /**
- * @brief Render and execute the passed statement.
- *
- * @param[in] s Statement to render and execute.
- *
- * @return Stored result of nullptr if the query could not be executed.
- */
- inline result_stored* execute_stored(const statement& s);
-
- /**
- * @brief Render and execute the passed statement.
- *
- * @param[in] s Statement to render and execute.
- *
- * @return Used result of nullptr if the query could not be executed.
- */
- inline result_used* execute_used(const statement& s);
-
- private:
- /**
- * @brief Execute the passed query and return the suitable result type.
- *
- * @tparam T Traits type that is used to execute the query and contains the result type.
- *
- * @param[in] cmd Query to execute.
- */
- template<class T>
- typename T::result_type* execute_internal(const std::string& cmd);
- };
-
- }
|