Hazelcast C++ Client
Hazelcast C++ Client Library
sql_result.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 <boost/thread/future.hpp>
19 
20 #include "hazelcast/util/export.h"
21 #include "hazelcast/client/connection/Connection.h"
22 #include "hazelcast/client/sql/sql_page.h"
23 #include "hazelcast/client/sql/sql_row_metadata.h"
24 
25 namespace hazelcast {
26 namespace client {
27 namespace sql {
28 class sql_service;
29 
30 // This class is NOT thread-safe. Do NOT use simultaneously from multiple
31 // threads.
64 class HAZELCAST_API sql_result : public std::enable_shared_from_this<sql_result>
65 {
66 public:
67  class HAZELCAST_API page_iterator
68  {
69  public:
70  page_iterator(std::shared_ptr<sql_result> result,
71  std::shared_ptr<sql_page> first_page);
72 
73  page_iterator(const page_iterator&) = delete;
74  page_iterator(page_iterator&&) = default;
75  page_iterator& operator=(const page_iterator&) = delete;
76  page_iterator& operator=(page_iterator&&) = default;
77 
86  boost::future<std::shared_ptr<sql_page>> next();
87 
91  bool has_next() const;
92 
93  private:
94 
95  std::shared_ptr<std::atomic<bool>> in_progress_;
96  std::shared_ptr<std::atomic<bool>> last_;
97  std::shared_ptr<sql_row_metadata> row_metadata_;
98  serialization::pimpl::SerializationService* serialization_;
99  std::shared_ptr<sql_result> result_;
100  std::shared_ptr<sql_page> first_page_;
101  };
102 
106  virtual ~sql_result();
107 
112  bool row_set() const;
113 
122  const sql_row_metadata& row_metadata() const;
123 
129  int64_t update_count() const;
130 
140  boost::future<void> close();
141 
154  page_iterator iterator();
155 
156 private:
157  friend class sql_service;
158 
159  spi::ClientContext* client_context_;
160  sql_service* service_;
161  std::shared_ptr<connection::Connection> connection_;
162  impl::query_id query_id_;
163  int64_t update_count_;
164  std::shared_ptr<sql_row_metadata> row_metadata_;
165  std::shared_ptr<sql_page> first_page_;
166 
167  bool iterator_requested_;
168 
171  std::atomic<bool> closed_;
172  std::mutex mtx_;
173 
174  int32_t cursor_buffer_size_;
175 
188  sql_result(
189  spi::ClientContext* client_context,
190  sql_service* service,
191  std::shared_ptr<connection::Connection> connection,
192  impl::query_id id,
193  int64_t update_count,
194  std::shared_ptr<sql_row_metadata> row_metadata,
195  std::shared_ptr<sql_page> first_page,
196  int32_t cursor_buffer_size);
197 
198 private:
199  boost::future<std::shared_ptr<sql_page>> fetch_page();
200 
201  template<typename T>
202  boost::optional<T> to_object(serialization::pimpl::data data)
203  {
204  return client_context_->get_serialization_service().to_object<T>(data);
205  }
206  void check_closed() const;
207 };
208 
209 } // namespace sql
210 } // namespace client
211 } // namespace hazelcast
A service to execute SQL statements.
Definition: sql_service.h:89