Hazelcast C++ Client
Hazelcast C++ Client Library
sql_service.h
1 /*
2  * Copyright (c) 2008-2022, Hazelcast, Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include "hazelcast/util/export.h"
19 #include "hazelcast/client/sql/sql_result.h"
20 #include "hazelcast/client/sql/sql_statement.h"
21 #include "hazelcast/client/sql/hazelcast_sql_exception.h"
22 
23 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
24 #pragma warning(push)
25 #pragma warning(disable : 4251) // for dll export
26 #endif
27 
28 namespace hazelcast {
29 namespace client {
30 namespace impl {
31 class hazelcast_client_instance_impl;
32 }
33 class hazelcast_client;
34 namespace sql {
35 
88 class HAZELCAST_API sql_service
89 {
90 public:
109  template<typename... Params>
110  boost::future<std::shared_ptr<sql_result>> execute(const std::string& query,
111  const Params&... params)
112  {
113  sql_statement s{ client_context_, query };
114  int _[] = { 0, (s.add_parameter(params), 0)... };
115  (void)_;
116  return execute(s);
117  }
118 
127  boost::future<std::shared_ptr<sql_result>> execute(
128  const sql_statement& statement);
129 
130 private:
131  friend client::impl::hazelcast_client_instance_impl;
132  friend sql_result;
133 
134  client::spi::ClientContext& client_context_;
135 
136  struct sql_execute_response_parameters
137  {
138  int64_t update_count;
139  std::shared_ptr<sql_row_metadata> row_metadata;
140  std::shared_ptr<sql_page> first_page;
141  boost::optional<impl::sql_error> error;
142  bool is_infinite_rows = false;
143  bool is_infinite_rows_exist = false;
144  };
145 
146  struct sql_fetch_response_parameters
147  {
148  boost::optional<std::shared_ptr<sql_page>> page;
149  boost::optional<impl::sql_error> error;
150  };
151 
152  explicit sql_service(client::spi::ClientContext& context);
153 
154  std::shared_ptr<connection::Connection> query_connection();
155 
156  void rethrow(const std::exception& exc_ptr);
157  void rethrow(const std::exception& cause_ptr,
158  const std::shared_ptr<connection::Connection>& connection);
159 
160  boost::uuids::uuid client_id();
161 
162  std::shared_ptr<sql_result> handle_execute_response(
163  protocol::ClientMessage& msg,
164  std::shared_ptr<connection::Connection> connection,
165  impl::query_id id,
166  int32_t cursor_buffer_size);
167 
168  static sql_execute_response_parameters decode_execute_response(
169  protocol::ClientMessage& msg);
170 
171  impl::query_id create_query_id(
172  const std::shared_ptr<connection::Connection>& query_conn);
173 
174  boost::future<std::shared_ptr<sql_page>> fetch_page(
175  const impl::query_id& q_id,
176  int32_t cursor_buffer_size,
177  const std::shared_ptr<connection::Connection>& connection);
178 
179  static sql_fetch_response_parameters decode_fetch_response(
180  protocol::ClientMessage message);
181 
182  static void handle_fetch_response_error(
183  boost::optional<impl::sql_error> error);
184 
191  boost::future<void> close(
192  const std::shared_ptr<connection::Connection>& connection,
193  impl::query_id id);
194 };
195 } // namespace sql
196 } // namespace client
197 } // namespace hazelcast
198 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
199 #pragma warning(pop)
200 #endif
A service to execute SQL statements.
Definition: sql_service.h:89
boost::future< std::shared_ptr< sql_result > > execute(const std::string &query, const Params &... params)
Convenient method to execute a distributed query with the given parameter values.
Definition: sql_service.h:110
Definition of an SQL statement.
Definition: sql_statement.h:42