executable.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License, version 2.0, as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is also distributed with certain software (including
  9. * but not limited to OpenSSL) that is licensed under separate terms,
  10. * as designated in a particular file or component or in included license
  11. * documentation. The authors of MySQL hereby grant you an
  12. * additional permission to link the program and your derivative works
  13. * with the separately licensed software that they have included with
  14. * MySQL.
  15. *
  16. * Without limiting anything contained in the foregoing, this file,
  17. * which is part of MySQL Connector/C++, is also subject to the
  18. * Universal FOSS Exception, version 1.0, a copy of which can be found at
  19. * http://oss.oracle.com/licenses/universal-foss-exception.
  20. *
  21. * This program is distributed in the hope that it will be useful, but
  22. * WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  24. * See the GNU General Public License, version 2.0, for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software Foundation, Inc.,
  28. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  29. */
  30. #ifndef MYSQLX_EXECUTABLE_H
  31. #define MYSQLX_EXECUTABLE_H
  32. /**
  33. @file
  34. Class representing executable statements.
  35. */
  36. #include "common.h"
  37. #include "result.h"
  38. #include "../common/op_if.h"
  39. namespace mysqlx {
  40. using std::ostream;
  41. /**
  42. Represents an operation that can be executed.
  43. Creating an operation does not involve any communication with the server.
  44. Only when `execute()` method is called operation is sent to the server
  45. for execution.
  46. The template parameter `Res` is the type of result that
  47. is returned by `execute()` method.
  48. A derived class must create an implementation object for the operation and
  49. set it using reset() method. Such implementation object should implement
  50. common::Executable_if interface.
  51. */
  52. template <class Res, class Op>
  53. class Executable
  54. {
  55. protected:
  56. using Impl = common::Executable_if;
  57. std::shared_ptr<Impl> m_impl;
  58. Executable() = default;
  59. void reset(Impl *impl)
  60. {
  61. m_impl.reset(impl);
  62. }
  63. /*
  64. Copy semantics implementation: the current state of the other executable
  65. object (of its implementation, to be precise) is copied to this new
  66. instance. After that the two executables are independent objects describing
  67. the same operation.
  68. */
  69. void reset(const Executable &other)
  70. {
  71. m_impl.reset(other.m_impl->clone());
  72. }
  73. void reset(const Executable &&other)
  74. {
  75. m_impl = std::move(other.m_impl);
  76. }
  77. void check_if_valid()
  78. {
  79. if (!m_impl)
  80. throw Error("Attempt to use invalid operation");
  81. }
  82. Impl* get_impl()
  83. {
  84. check_if_valid();
  85. return m_impl.get();
  86. }
  87. public:
  88. Executable(const Executable &other)
  89. {
  90. operator=(other);
  91. }
  92. Executable(Executable &&other)
  93. {
  94. operator=(std::move(other));
  95. }
  96. virtual ~Executable() {}
  97. Executable& operator=(const Executable &other)
  98. {
  99. try {
  100. reset(other);
  101. return *this;
  102. }
  103. CATCH_AND_WRAP
  104. }
  105. Executable& operator=(Executable &&other)
  106. {
  107. try {
  108. reset(std::move(other));
  109. return *this;
  110. }
  111. CATCH_AND_WRAP
  112. }
  113. /// Execute given operation and return its result.
  114. virtual Res execute()
  115. {
  116. try {
  117. check_if_valid();
  118. /*
  119. Note: The implementation object's execute() methods returns a reference
  120. to a common::Result_init object which provides information about
  121. the session and pending server reply. The returned Res instance should
  122. be constructible from such Result_init reference.
  123. */
  124. return m_impl->execute();
  125. }
  126. CATCH_AND_WRAP
  127. }
  128. struct Access;
  129. friend Access;
  130. };
  131. } // mysqlx
  132. #endif