Hazelcast C++ Client
Hazelcast C++ Client Library
sql_page.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 <vector>
19 #include <boost/any.hpp>
20 #include <boost/optional.hpp>
21 #include <boost/enable_shared_from_this.hpp>
22 
23 #include "hazelcast/util/export.h"
24 #include "hazelcast/client/sql/sql_column_type.h"
25 #include "hazelcast/client/sql/sql_row_metadata.h"
26 #include "hazelcast/client/serialization/serialization.h"
27 
28 namespace hazelcast {
29 namespace client {
30 namespace protocol {
31 namespace codec {
32 namespace builtin {
33 class sql_page_codec;
34 }
35 } // namespace codec
36 } // namespace protocol
37 namespace sql {
38 
42 class HAZELCAST_API sql_page
43 {
44  using column = std::vector<boost::any>;
45  struct HAZELCAST_API page_data;
46 
47 public:
48  class HAZELCAST_API sql_row
49  {
50  public:
51  sql_row(size_t row_index, std::shared_ptr<page_data> page);
52 
70  template<typename T>
71  boost::optional<T> get_object(std::size_t column_index) const
72  {
73  check_index(column_index);
74 
75  return page_data_->get_column_value<T>(column_index, row_index_);
76  }
77 
99  template<typename T>
100  boost::optional<T> get_object(const std::string& column_name) const
101  {
102  auto column_index = resolve_index(column_name);
103  return page_data_->get_column_value<T>(column_index, row_index_);
104  }
105 
113  const sql_row_metadata& row_metadata() const;
114 
115  private:
116  std::size_t row_index_;
117  std::shared_ptr<page_data> page_data_;
118 
119  std::size_t resolve_index(const std::string& column_name) const;
120 
121  void check_index(size_t index) const;
122  };
123 
132  sql_page(std::vector<sql_column_type> column_types,
133  std::vector<column> columns,
134  bool last,
135  std::shared_ptr<sql_row_metadata> row_metadata = nullptr);
136 
141  const std::vector<sql_column_type>& column_types() const;
142 
147  bool last() const;
148 
153  std::size_t column_count() const;
154 
159  std::size_t row_count() const;
160 
165  const std::vector<sql_row>& rows() const;
166 
167 private:
168  friend class sql_result;
169  friend class protocol::codec::builtin::sql_page_codec;
170 
171  std::shared_ptr<page_data> page_data_;
172  std::vector<sql_row> rows_;
173  bool last_;
174 
179  void row_metadata(std::shared_ptr<sql_row_metadata> row_metadata);
180 
185  void serialization_service(serialization::pimpl::SerializationService* ss);
186 
187  void construct_rows();
188 
189  struct HAZELCAST_API page_data
190  {
191  std::vector<sql_column_type> column_types_;
192  std::vector<column> columns_;
193  std::shared_ptr<sql_row_metadata> row_metadata_;
194  serialization::pimpl::SerializationService* serialization_service_;
195 
196  template<typename T>
197  boost::optional<T> get_column_value(std::size_t column_index,
198  std::size_t row_index) const
199  {
200  assert(column_index < column_count());
201  assert(row_index < row_count());
202 
203  auto& any_value = columns_[column_index][row_index];
204  if (any_value.empty()) {
205  return boost::none;
206  }
207 
208  if (column_types_[column_index] != sql_column_type::object) {
209  return boost::any_cast<T>(any_value);
210  }
211 
212  // this is the object type, hence the value is `data`
213  // and we need to de-serialize it
214  return serialization_service_->to_object<T>(
215  boost::any_cast<serialization::pimpl::data>(any_value));
216  }
217 
218  std::size_t column_count() const;
219  std::size_t row_count() const;
220  };
221 };
222 
223 } // namespace sql
224 } // namespace client
225 } // namespace hazelcast
boost::optional< T > get_object(std::size_t column_index) const
Gets the value of the column by index.
Definition: sql_page.h:71
boost::optional< T > get_object(const std::string &column_name) const
Gets the value of the column by column name.
Definition: sql_page.h:100
A finite set of rows returned to the client.
Definition: sql_page.h:43