prepared_statement.hxx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /** Helper classes for defining and executing prepared statements.
  2. *
  3. * See the connection_base hierarchy for more about prepared statements.
  4. *
  5. * Copyright (c) 2000-2019, Jeroen T. Vermeulen.
  6. *
  7. * See COPYING for copyright license. If you did not receive a file called
  8. * COPYING with this source code, please notify the distributor of this mistake,
  9. * or contact the author.
  10. */
  11. #ifndef PQXX_H_PREPARED_STATEMENT
  12. #define PQXX_H_PREPARED_STATEMENT
  13. #include "pqxx/compiler-public.hxx"
  14. #include "pqxx/compiler-internal-pre.hxx"
  15. #include "pqxx/types.hxx"
  16. #include "pqxx/internal/statement_parameters.hxx"
  17. namespace pqxx
  18. {
  19. /// Dedicated namespace for helper types related to prepared statements.
  20. namespace prepare
  21. {
  22. /// Pass a number of statement parameters only known at runtime.
  23. /** When you call any of the @c exec_params functions, the number of arguments
  24. * is normally known at compile time. This helper function supports the case
  25. * where it is not.
  26. *
  27. * Use this function to pass a variable number of parameters, based on a
  28. * sequence ranging from @c begin to @c end exclusively.
  29. *
  30. * The technique combines with the regular static parameters. You can use it
  31. * to insert dynamic parameter lists in any place, or places, among the call's
  32. * parameters. You can even insert multiple dynamic sequences.
  33. *
  34. * @param begin A pointer or iterator for iterating parameters.
  35. * @param end A pointer or iterator for iterating parameters.
  36. * @return An object representing the parameters.
  37. */
  38. template<typename IT> inline pqxx::internal::dynamic_params<IT>
  39. make_dynamic_params(IT begin, IT end)
  40. {
  41. return pqxx::internal::dynamic_params<IT>(begin, end);
  42. }
  43. /// Pass a number of statement parameters only known at runtime.
  44. /** When you call any of the @c exec_params functions, the number of arguments
  45. * is normally known at compile time. This helper function supports the case
  46. * where it is not.
  47. *
  48. * Use this function to pass a variable number of parameters, based on a
  49. * container of parameter values.
  50. *
  51. * The technique combines with the regular static parameters. You can use it
  52. * to insert dynamic parameter lists in any place, or places, among the call's
  53. * parameters. You can even insert multiple dynamic containers.
  54. *
  55. * @param container A container of parameter values.
  56. * @return An object representing the parameters.
  57. */
  58. template<typename C>
  59. inline pqxx::internal::dynamic_params<typename C::const_iterator>
  60. make_dynamic_params(const C &container)
  61. {
  62. return pqxx::internal::dynamic_params<typename C::const_iterator>(container);
  63. }
  64. } // namespace prepare
  65. } // namespace pqxx
  66. namespace pqxx
  67. {
  68. namespace prepare
  69. {
  70. /// Helper class for passing parameters to, and executing, prepared statements
  71. /** @deprecated As of 6.0, use @c transaction_base::exec_prepared and friends.
  72. */
  73. class PQXX_LIBEXPORT invocation : internal::statement_parameters
  74. {
  75. public:
  76. PQXX_DEPRECATED invocation(transaction_base &, const std::string &statement);
  77. invocation &operator=(const invocation &) =delete;
  78. /// Execute!
  79. result exec() const;
  80. /// Execute and return result in binary format
  81. result exec_binary() const;
  82. /// Has a statement of this name been defined?
  83. bool exists() const;
  84. /// Pass null parameter.
  85. invocation &operator()() { add_param(); return *this; }
  86. /// Pass parameter value.
  87. /**
  88. * @param v parameter value; will be represented as a string internally.
  89. */
  90. template<typename T> invocation &operator()(const T &v)
  91. { add_param(v, true); return *this; }
  92. /// Pass binary parameter value for a BYTEA field.
  93. /**
  94. * @param v binary string; will be passed on directly in binary form.
  95. */
  96. invocation &operator()(const binarystring &v)
  97. { add_binary_param(v, true); return *this; }
  98. /// Pass parameter value.
  99. /**
  100. * @param v parameter value (will be represented as a string internally).
  101. * @param nonnull replaces value with null if set to false.
  102. */
  103. template<typename T> invocation &operator()(const T &v, bool nonnull)
  104. { add_param(v, nonnull); return *this; }
  105. /// Pass binary parameter value for a BYTEA field.
  106. /**
  107. * @param v binary string; will be passed on directly in binary form.
  108. * @param nonnull determines whether to pass a real value, or nullptr.
  109. */
  110. invocation &operator()(const binarystring &v, bool nonnull)
  111. { add_binary_param(v, nonnull); return *this; }
  112. /// Pass C-style parameter string, or null if pointer is null.
  113. /**
  114. * This version is for passing C-style strings; it's a template, so any
  115. * pointer type that @c to_string accepts will do.
  116. *
  117. * @param v parameter value (will be represented as a C++ string internally)
  118. * @param nonnull replaces value with null if set to @c false
  119. */
  120. template<typename T> invocation &operator()(T *v, bool nonnull=true)
  121. { add_param(v, nonnull); return *this; }
  122. /// Pass C-style string parameter, or null if pointer is null.
  123. /** This duplicates the pointer-to-template-argument-type version of the
  124. * operator, but helps compilers with less advanced template implementations
  125. * disambiguate calls where C-style strings are passed.
  126. */
  127. invocation &operator()(const char *v, bool nonnull=true)
  128. { add_param(v, nonnull); return *this; }
  129. private:
  130. transaction_base &m_home;
  131. const std::string m_statement;
  132. invocation &setparam(const std::string &, bool nonnull);
  133. result internal_exec(result_format format) const;
  134. };
  135. namespace internal
  136. {
  137. /// Internal representation of a prepared statement definition.
  138. struct PQXX_LIBEXPORT prepared_def
  139. {
  140. /// Text of prepared query.
  141. std::string definition;
  142. /// Has this prepared statement been prepared in the current session?
  143. bool registered = false;
  144. prepared_def() =default;
  145. explicit prepared_def(const std::string &);
  146. };
  147. } // namespace pqxx::prepare::internal
  148. } // namespace pqxx::prepare
  149. } // namespace pqxx
  150. #include "pqxx/compiler-internal-post.hxx"
  151. #endif