Hazelcast C++ Client
Hazelcast C++ Client Library
Loading...
Searching...
No Matches
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 <iterator>
19#include <chrono>
20
21#include <boost/thread/future.hpp>
22
23#include "hazelcast/util/export.h"
24#include "hazelcast/client/connection/Connection.h"
25#include "hazelcast/client/sql/sql_page.h"
26#include "hazelcast/client/sql/sql_row_metadata.h"
27
28namespace hazelcast {
29namespace client {
30namespace sql {
31class sql_service;
32
33// This class is NOT thread-safe. Do NOT use simultaneously from multiple
34// threads.
67class HAZELCAST_API sql_result : public std::enable_shared_from_this<sql_result>
68{
69public:
70
74 class HAZELCAST_API page_iterator
75 {
76 public:
77 page_iterator(std::shared_ptr<sql_result> result,
78 std::shared_ptr<sql_page> first_page);
79
80 page_iterator(const page_iterator&) = delete;
81 page_iterator(page_iterator&&) = default;
82 page_iterator& operator=(const page_iterator&) = delete;
83 page_iterator& operator=(page_iterator&&) = default;
84
93 boost::future<std::shared_ptr<sql_page>> next();
94
98 bool has_next() const;
99
100 private:
101
102 std::shared_ptr<std::atomic<bool>> in_progress_;
103 std::shared_ptr<std::atomic<bool>> last_;
104 std::shared_ptr<sql_row_metadata> row_metadata_;
105 serialization::pimpl::SerializationService* serialization_;
106 std::shared_ptr<sql_result> result_;
107 std::shared_ptr<sql_page> first_page_;
108 };
109
113 class HAZELCAST_API page_iterator_sync
114 {
115 public:
116 using difference_type = void;
117 using value_type = std::shared_ptr<sql_page>;
118 using pointer = std::shared_ptr<sql_page>;
119 using reference = std::shared_ptr<sql_page>&;
120 using iterator_category = std::input_iterator_tag;
121
125 void set_timeout(std::chrono::milliseconds);
126
130 std::chrono::milliseconds timeout() const;
131
132 friend HAZELCAST_API bool operator==(const page_iterator_sync&,
133 const page_iterator_sync&);
134 friend HAZELCAST_API bool operator!=(const page_iterator_sync&,
135 const page_iterator_sync&);
136
137
143 std::shared_ptr<sql_page> operator*() const;
144
150 std::shared_ptr<sql_page> operator->() const;
151
155 page_iterator_sync operator++(int) = delete;
156
162 page_iterator_sync& operator++();
163
164 private:
165
166 friend class sql_result;
167 page_iterator_sync(page_iterator&&, std::chrono::milliseconds timeout);
168 page_iterator_sync() = default;
169
170 struct non_copyables
171 {
172 explicit non_copyables(page_iterator&&);
173
174 boost::future<std::shared_ptr<sql_page>> preloaded_page_;
175 page_iterator iter_;
176 };
177
178 std::shared_ptr<non_copyables> block_;
179 std::shared_ptr<sql_page> current_;
180 std::chrono::milliseconds timeout_;
181 };
182
186 class HAZELCAST_API row_iterator_sync
187 {
188 public:
189 using difference_type = void;
190 using value_type = sql_page::sql_row;
191 using pointer = sql_page::sql_row*;
192 using reference = const sql_page::sql_row&;
193 using iterator_category = std::input_iterator_tag;
194
198 void set_timeout(std::chrono::milliseconds);
199
203 std::chrono::milliseconds timeout() const;
204
205 friend HAZELCAST_API bool operator==(const row_iterator_sync&,
206 const row_iterator_sync&);
207 friend HAZELCAST_API bool operator!=(const row_iterator_sync&,
208 const row_iterator_sync&);
209
215 const sql_page::sql_row& operator*() const;
216
217
223 const sql_page::sql_row* operator->() const;
224
228 row_iterator_sync operator++(int) = delete;
229
235 row_iterator_sync& operator++();
236
237 private:
238
239 friend class sql_result;
240 row_iterator_sync(page_iterator_sync&&);
241 row_iterator_sync() = default;
242
243 mutable page_iterator_sync iterator_;
244 std::size_t row_idx_;
245 };
246
250 virtual ~sql_result();
251
256 bool row_set() const;
257
266 const sql_row_metadata& row_metadata() const;
267
273 int64_t update_count() const;
274
284 boost::future<void> close();
285
299
300 page_iterator_sync pbegin(
301 std::chrono::milliseconds timeout = std::chrono::milliseconds{ -1 });
302 page_iterator_sync pend();
303 row_iterator_sync begin(
304 std::chrono::milliseconds timeout = std::chrono::milliseconds{ -1 });
305 row_iterator_sync end();
306
307private:
308 friend class sql_service;
309
310 spi::ClientContext* client_context_;
311 sql_service* service_;
312 std::shared_ptr<connection::Connection> connection_;
313 impl::query_id query_id_;
314 int64_t update_count_;
315 std::shared_ptr<sql_row_metadata> row_metadata_;
316 std::shared_ptr<sql_page> first_page_;
317
318 bool iterator_requested_;
319
322 std::atomic<bool> closed_;
323 std::mutex mtx_;
324
325 int32_t cursor_buffer_size_;
326
339 sql_result(
340 spi::ClientContext* client_context,
341 sql_service* service,
342 std::shared_ptr<connection::Connection> connection,
343 impl::query_id id,
344 int64_t update_count,
345 std::shared_ptr<sql_row_metadata> row_metadata,
346 std::shared_ptr<sql_page> first_page,
347 int32_t cursor_buffer_size);
348
349private:
350 boost::future<std::shared_ptr<sql_page>> fetch_page();
351
352 template<typename T>
353 boost::optional<T> to_object(serialization::pimpl::data data)
354 {
355 return client_context_->get_serialization_service().to_object<T>(data);
356 }
357 void check_closed() const;
358};
359
360} // namespace sql
361} // namespace client
362} // namespace hazelcast
Copy is allowed for convenience but it does shallow copy so it should be avoided.
Definition sql_result.h:114
std::shared_ptr< sql_page > operator*() const
Dereferences current page.
Definition sql.cpp:1137
std::chrono::milliseconds timeout() const
Retrieves the timeout.
Definition sql.cpp:1085
page_iterator_sync operator++(int)=delete
Post increment operator is deleted.
void set_timeout(std::chrono::milliseconds)
Sets timeout for page fetch operation.
Definition sql.cpp:1079
std::shared_ptr< sql_page > operator->() const
Dereferences current page.
Definition sql.cpp:1148
Copy is allowed for convenience but it does shallow copy so it should be avoided.
Definition sql_result.h:75
bool has_next() const
Tells whether there are pages to be retrieved.
Definition sql.cpp:1053
boost::future< std::shared_ptr< sql_page > > next()
Fetches the new page.
Definition sql.cpp:982
void set_timeout(std::chrono::milliseconds)
Sets timeout for page fetch operation.
Definition sql.cpp:845
row_iterator_sync operator++(int)=delete
Post increment operator is deleted because copy is discouraged.
const sql_page::sql_row & operator*() const
Returns current row.
Definition sql.cpp:857
const sql_page::sql_row * operator->() const
Returns current row.
Definition sql.cpp:867
std::chrono::milliseconds timeout() const
Retrieves the timeout.
Definition sql.cpp:851
bool row_set() const
Return whether this result has rows to iterate using the iterator() method.
Definition sql.cpp:777
const sql_row_metadata & row_metadata() const
Gets the row metadata.
Definition sql.cpp:945
page_iterator iterator()
Returns an iterator over the result pages.
Definition sql.cpp:783
int64_t update_count() const
Returns the number of rows updated by the statement or -1 if this result is a row set.
Definition sql.cpp:771
boost::future< void > close()
Release the resources associated with the query result.
Definition sql.cpp:901
A service to execute SQL statements.
Definition sql_service.h:90