Hazelcast C++ Client
Hazelcast C++ Client Library
sql_statement.h
1 /*
2  * Copyright (c) 2008-2021, 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 <chrono>
19 #include <cstddef>
20 #include <string>
21 
22 #include <boost/optional/optional.hpp>
23 
24 #include "hazelcast/util/export.h"
25 #include "hazelcast/client/serialization/pimpl/data.h"
26 #include "hazelcast/client/serialization/serialization.h"
27 #include "hazelcast/client/sql/sql_expected_result_type.h"
28 
29 namespace hazelcast {
30 namespace client {
31 namespace sql {
32 class sql_service;
33 
41 class HAZELCAST_API sql_statement
42 {
43 public:
48  static constexpr std::chrono::milliseconds TIMEOUT_NOT_SET{ -1 };
49 
54  static constexpr std::chrono::milliseconds TIMEOUT_DISABLED{ 0 };
55 
57  static constexpr std::chrono::milliseconds DEFAULT_TIMEOUT =
58  TIMEOUT_NOT_SET;
59 
61  static constexpr int32_t DEFAULT_CURSOR_BUFFER_SIZE = 4096;
62 
68  sql_statement(hazelcast_client& client, std::string query);
69 
74  const std::string& sql() const;
75 
86  sql_statement& sql(std::string sql_string);
87 
103  template<typename... Param>
104  sql_statement& set_parameters(Param... params);
105 
115  template<typename Param>
116  sql_statement& add_parameter(const Param& value);
117 
126  sql_statement& clear_parameters();
127 
133  int32_t cursor_buffer_size() const;
134 
156  sql_statement& cursor_buffer_size(int32_t size);
157 
163  std::chrono::milliseconds timeout() const;
164 
180  sql_statement& timeout(std::chrono::milliseconds timeout);
181 
187  sql_expected_result_type expected_result_type() const;
188 
195  sql_statement& expected_result_type(sql_expected_result_type type);
196 
203  const boost::optional<std::string>& schema() const;
204 
219  sql_statement& schema(boost::optional<std::string> schema);
220 
221 private:
222  using data = serialization::pimpl::data;
223  using serialization_service = serialization::pimpl::SerializationService;
224 
225  sql_statement(spi::ClientContext& client_context, std::string query);
226 
227  std::string sql_;
228  std::vector<data> serialized_parameters_;
229  int32_t cursor_buffer_size_;
230  std::chrono::milliseconds timeout_;
231  sql::sql_expected_result_type expected_result_type_;
232  boost::optional<std::string> schema_;
233 
234  serialization_service& serialization_service_;
235 
237 };
238 
239 template<typename Param>
241 sql_statement::add_parameter(const Param& param)
242 {
243  serialized_parameters_.emplace_back(serialization_service_.to_data(param));
244 
245  return *this;
246 }
247 
248 template<typename... Param>
251 {
252  int _[] = { 0, ((void)add_parameter(params), 0)... };
253  (void)_;
254  return *this;
255 }
256 
257 } // namespace sql
258 } // namespace client
259 } // namespace hazelcast
A service to execute SQL statements.
Definition: sql_service.h:89
Definition of an SQL statement.
Definition: sql_statement.h:42
sql_statement & set_parameters(Param... params)
Sets the values for statement parameters.
sql_statement & add_parameter(const Param &value)
Adds a single parameter value to the end of the parameter values list.
sql_statement & expected_result_type(sql_expected_result_type type)
Sets the expected result type.