25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

139 satır
4.3 KiB

  1. #pragma once
  2. #include <string>
  3. #include <vector>
  4. #include <cppmariadb/config.h>
  5. namespace cppmariadb
  6. {
  7. /**
  8. * @brief Class to store a MYSQL statement. The variables
  9. * inside the statement are escaped automatically.
  10. */
  11. struct statement
  12. {
  13. public:
  14. static constexpr size_t npos = std::numeric_limits<size_t>::max();
  15. private:
  16. /**
  17. * @brief Parameter inside the statement.
  18. */
  19. struct parameter
  20. {
  21. bool has_value { false }; //!< The parameter has a value assigned (is not null).
  22. bool unescaped { false }; //!< The parameter should not be escaped (is unescaped).
  23. std::string value; //!< Value of the parameter.
  24. };
  25. private:
  26. mutable bool _changed; //!< The statement has changed (_query need to be recompiled).
  27. mutable std::string _query; //!< The compiled query.
  28. mutable const connection* _connection; //!< Connection the query was compiled for.
  29. std::vector<std::string> _code; //!< Code pars of the statement
  30. std::vector<std::pair<std::string, parameter>> _parameters; //!< Parameters of the statement
  31. public:
  32. /**
  33. * @brief Default constructor.
  34. */
  35. inline statement();
  36. /**
  37. * @brief Value constructor. Create a new statement of the passed query.
  38. *
  39. * @param[in] query Query to store in the new statement.
  40. */
  41. inline statement(const std::string& query);
  42. /**
  43. * @brief Assign a new query/statement to this object.
  44. */
  45. inline void assign(const std::string& query);
  46. /**
  47. * @brief Get the final query to pass to the server.
  48. *
  49. * @param[in] con Connection to use for escaping the values.
  50. *
  51. * @return Final query with escaped and filled values.
  52. */
  53. inline const std::string& query(const connection& con) const;
  54. /**
  55. * @brief Find a parameter by its name.
  56. *
  57. * @param[in] param Name of the parameter to find.
  58. *
  59. * @return Index of the parameter.
  60. */
  61. inline size_t find(const std::string& param);
  62. /**
  63. * @brief Set a parameter to null.
  64. *
  65. * @param[in] param Name of the parameter to set to null.
  66. */
  67. inline void set_null(const std::string& param);
  68. /**
  69. * @brief Set a parameter to null.
  70. *
  71. * @param[in] index Index of the parameter to set to null.
  72. */
  73. inline void set_null(size_t index);
  74. /**
  75. * @brief Check if the statement is empty.
  76. *
  77. * @retval true If this statement is empty.
  78. * @retval false If this statement has a query assigned.
  79. */
  80. inline bool empty() const;
  81. /**
  82. * @brief Set all variables of the statement to null.
  83. */
  84. inline void clear();
  85. /**
  86. * @brief Set the value of a parameter.
  87. *
  88. * @param[in] param Name of the parameter to set the value for.
  89. * @param[in] value Value to set for the parameter.
  90. */
  91. template<class T>
  92. inline void set(const std::string& param, const T& value);
  93. /**
  94. * @brief Set the value of a parameter.
  95. *
  96. * @param[in] index Index of the parameter to set the value for.
  97. * @param[in] value Value to set for the parameter.
  98. */
  99. template<class T>
  100. inline void set(size_t index, const T& value);
  101. private:
  102. /**
  103. * @brief Parse the passed query and split it into its code and parameters parts.
  104. *
  105. * The parsed parts of the query are stored alternately in the _code and _parameters vector,
  106. * starting with a code element.
  107. *
  108. * @param[in] query Query to parse.
  109. */
  110. void parse(const std::string& query);
  111. /**
  112. * @brief Build the final query using the passed conection to escape the values.
  113. * The query is cached in the _query variable of the object.
  114. */
  115. void build(const connection& con) const;
  116. };
  117. }