GridGain C++
query_sql.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 GridGain Systems, Inc. and Contributors.
3  *
4  * Licensed under the GridGain Community Edition License (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  * https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license
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 
22 #ifndef _IGNITE_CACHE_QUERY_QUERY_SQL
23 #define _IGNITE_CACHE_QUERY_QUERY_SQL
24 
25 #include <stdint.h>
26 #include <string>
27 #include <vector>
28 
29 #include <ignite/impl/writable_object.h>
31 
32 namespace ignite
33 {
34  namespace cache
35  {
36  namespace query
37  {
43  class SqlQuery
44  {
45  public:
52  SqlQuery(const std::string& type, const std::string& sql) :
53  type(type),
54  sql(sql),
55  pageSize(1024),
56  loc(false),
57  distributedJoins(false),
58  args()
59  {
60  // No-op.
61  }
62 
68  SqlQuery(const SqlQuery& other) :
69  type(other.type),
70  sql(other.sql),
71  pageSize(other.pageSize),
72  loc(other.loc),
73  distributedJoins(other.distributedJoins),
74  args()
75  {
76  args.reserve(other.args.size());
77 
78  typedef std::vector<impl::WritableObjectBase*>::const_iterator Iter;
79 
80  for (Iter i = other.args.begin(); i != other.args.end(); ++i)
81  args.push_back((*i)->Copy());
82  }
83 
89  SqlQuery& operator=(const SqlQuery& other)
90  {
91  if (this != &other)
92  {
93  SqlQuery tmp(other);
94 
95  Swap(tmp);
96  }
97 
98  return *this;
99  }
100 
105  {
106  typedef std::vector<impl::WritableObjectBase*>::const_iterator Iter;
107 
108  for (Iter it = args.begin(); it != args.end(); ++it)
109  delete *it;
110  }
111 
117  void Swap(SqlQuery& other)
118  {
119  if (this != &other)
120  {
121  std::swap(type, other.type);
122  std::swap(sql, other.sql);
123  std::swap(pageSize, other.pageSize);
124  std::swap(loc, other.loc);
125  std::swap(distributedJoins, other.distributedJoins);
126  std::swap(args, other.args);
127  }
128  }
129 
135  const std::string& GetType() const
136  {
137  return type;
138  }
139 
145  void SetType(const std::string& type)
146  {
147  this->type = type;
148  }
149 
155  const std::string& GetSql() const
156  {
157  return sql;
158  }
159 
165  void SetSql(const std::string& sql)
166  {
167  this->sql = sql;
168  }
169 
175  int32_t GetPageSize() const
176  {
177  return pageSize;
178  }
179 
185  void SetPageSize(int32_t pageSize)
186  {
187  this->pageSize = pageSize;
188  }
189 
195  bool IsLocal() const
196  {
197  return loc;
198  }
199 
207  void SetLocal(bool loc)
208  {
209  this->loc = loc;
210  }
211 
217  bool IsDistributedJoins() const
218  {
219  return distributedJoins;
220  }
221 
230  void SetDistributedJoins(bool enabled)
231  {
232  distributedJoins = enabled;
233  }
234 
244  template<typename T>
245  void AddArgument(const T& arg)
246  {
247  args.push_back(new impl::WritableObject<T>(arg));
248  }
249 
254  {
255  std::vector<impl::WritableObjectBase*>::iterator iter;
256  for (iter = args.begin(); iter != args.end(); ++iter)
257  delete *iter;
258 
259  args.clear();
260  }
261 
267  void Write(binary::BinaryRawWriter& writer) const
268  {
269  writer.WriteBool(loc);
270  writer.WriteString(sql);
271  writer.WriteString(type);
272  writer.WriteInt32(pageSize);
273 
274  writer.WriteInt32(static_cast<int32_t>(args.size()));
275 
276  std::vector<impl::WritableObjectBase*>::const_iterator it;
277 
278  for (it = args.begin(); it != args.end(); ++it)
279  (*it)->Write(writer);
280 
281  writer.WriteBool(distributedJoins);
282  writer.WriteInt32(0); // Timeout, ms
283  writer.WriteBool(false); // ReplicatedOnly
284  }
285 
286  private:
288  std::string type;
289 
291  std::string sql;
292 
294  int32_t pageSize;
295 
297  bool loc;
298 
300  bool distributedJoins;
301 
303  std::vector<impl::WritableObjectBase*> args;
304  };
305  }
306  }
307 }
308 
309 #endif //_IGNITE_CACHE_QUERY_QUERY_SQL
ignite::cache::query::SqlQuery::SetType
void SetType(const std::string &type)
Set type name.
Definition: query_sql.h:145
ignite::cache::query::SqlQuery::SetPageSize
void SetPageSize(int32_t pageSize)
Set page size.
Definition: query_sql.h:185
ignite
Ignite API.
Definition: cache.h:47
ignite::cache::query::SqlQuery::~SqlQuery
~SqlQuery()
Destructor.
Definition: query_sql.h:104
ignite::binary::BinaryRawWriter::WriteInt32
void WriteInt32(int32_t val)
Write 32-byte signed integer.
Definition: binary_raw_writer.cpp:71
ignite::cache::query::SqlQuery::GetType
const std::string & GetType() const
Get type name.
Definition: query_sql.h:135
ignite::cache::query::SqlQuery::operator=
SqlQuery & operator=(const SqlQuery &other)
Assignment operator.
Definition: query_sql.h:89
ignite::cache::query::SqlQuery::SqlQuery
SqlQuery(const SqlQuery &other)
Copy constructor.
Definition: query_sql.h:68
ignite::cache::query::SqlQuery::GetPageSize
int32_t GetPageSize() const
Get page size.
Definition: query_sql.h:175
ignite::cache::query::SqlQuery::SetLocal
void SetLocal(bool loc)
Set local flag.
Definition: query_sql.h:207
ignite::cache::query::SqlQuery::AddArgument
void AddArgument(const T &arg)
Add argument.
Definition: query_sql.h:245
ignite::binary::BinaryRawWriter
Binary raw writer.
Definition: binary_raw_writer.h:61
ignite::cache::query::SqlQuery::GetSql
const std::string & GetSql() const
Get SQL string.
Definition: query_sql.h:155
ignite::cache::query::SqlQuery::SetSql
void SetSql(const std::string &sql)
Set SQL string.
Definition: query_sql.h:165
ignite::cache::query::SqlQuery::Write
void Write(binary::BinaryRawWriter &writer) const
Write query info to the stream.
Definition: query_sql.h:267
ignite::cache::query::SqlQuery::Swap
void Swap(SqlQuery &other)
Efficiently swaps contents with another SqlQuery instance.
Definition: query_sql.h:117
ignite::binary::BinaryRawWriter::WriteString
void WriteString(const char *val)
Write string.
Definition: binary_raw_writer.cpp:151
ignite::cache::query::SqlQuery::IsLocal
bool IsLocal() const
Get local flag.
Definition: query_sql.h:195
ignite::binary::BinaryRawWriter::WriteBool
void WriteBool(bool val)
Write bool.
Definition: binary_raw_writer.cpp:41
ignite::cache::query::SqlQuery::ClearArguments
void ClearArguments()
Remove all added arguments.
Definition: query_sql.h:253
ignite::cache::query::SqlQuery::SetDistributedJoins
void SetDistributedJoins(bool enabled)
Specify if distributed joins are enabled for this query.
Definition: query_sql.h:230
ignite::cache::query::SqlQuery::IsDistributedJoins
bool IsDistributedJoins() const
Check if distributed joins are enabled for this query.
Definition: query_sql.h:217
ignite::cache::query::SqlQuery::SqlQuery
SqlQuery(const std::string &type, const std::string &sql)
Constructor.
Definition: query_sql.h:52
binary_raw_writer.h
ignite::cache::query::SqlQuery
Sql query.
Definition: query_sql.h:43