|
- #pragma once
-
- #include <string>
- #include <vector>
-
- #include <cppmariadb/config.h>
-
- namespace cppmariadb
- {
-
- /**
- * @brief Class to store a MYSQL statement. The variables
- * inside the statement are escaped automatically.
- */
- struct statement
- {
- public:
- static constexpr size_t npos = std::numeric_limits<size_t>::max();
-
- private:
- /**
- * @brief Parameter inside the statement.
- */
- struct parameter
- {
- bool has_value { false }; //!< The parameter has a value assigned (is not null).
- bool unescaped { false }; //!< The parameter should not be escaped (is unescaped).
- std::string value; //!< Value of the parameter.
- };
-
- private:
- mutable bool _changed; //!< The statement has changed (_query need to be recompiled).
- mutable std::string _query; //!< The compiled query.
- mutable const connection* _connection; //!< Connection the query was compiled for.
-
- std::vector<std::string> _code; //!< Code pars of the statement
- std::vector<std::pair<std::string, parameter>> _parameters; //!< Parameters of the statement
-
- public:
- /**
- * @brief Default constructor.
- */
- inline statement();
-
- /**
- * @brief Value constructor. Create a new statement of the passed query.
- *
- * @param[in] query Query to store in the new statement.
- */
- inline statement(const std::string& query);
-
- /**
- * @brief Assign a new query/statement to this object.
- */
- inline void assign(const std::string& query);
-
- /**
- * @brief Get the final query to pass to the server.
- *
- * @param[in] con Connection to use for escaping the values.
- *
- * @return Final query with escaped and filled values.
- */
- inline const std::string& query(const connection& con) const;
-
- /**
- * @brief Find a parameter by its name.
- *
- * @param[in] param Name of the parameter to find.
- *
- * @return Index of the parameter.
- */
- inline size_t find(const std::string& param);
-
- /**
- * @brief Set a parameter to null.
- *
- * @param[in] param Name of the parameter to set to null.
- */
- inline void set_null(const std::string& param);
-
- /**
- * @brief Set a parameter to null.
- *
- * @param[in] index Index of the parameter to set to null.
- */
- inline void set_null(size_t index);
-
- /**
- * @brief Check if the statement is empty.
- *
- * @retval true If this statement is empty.
- * @retval false If this statement has a query assigned.
- */
- inline bool empty() const;
-
- /**
- * @brief Set all variables of the statement to null.
- */
- inline void clear();
-
- /**
- * @brief Set the value of a parameter.
- *
- * @param[in] param Name of the parameter to set the value for.
- * @param[in] value Value to set for the parameter.
- */
- template<class T>
- inline void set(const std::string& param, const T& value);
-
- /**
- * @brief Set the value of a parameter.
- *
- * @param[in] index Index of the parameter to set the value for.
- * @param[in] value Value to set for the parameter.
- */
- template<class T>
- inline void set(size_t index, const T& value);
-
- private:
- /**
- * @brief Parse the passed query and split it into its code and parameters parts.
- *
- * The parsed parts of the query are stored alternately in the _code and _parameters vector,
- * starting with a code element.
- *
- * @param[in] query Query to parse.
- */
- void parse(const std::string& query);
-
- /**
- * @brief Build the final query using the passed conection to escape the values.
- * The query is cached in the _query variable of the object.
- */
- void build(const connection& con) const;
- };
-
- }
|