GridGain C++
core/include/ignite/cache/query/query_sql_fields.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_FIELDS
23 #define _IGNITE_CACHE_QUERY_QUERY_SQL_FIELDS
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  {
42  {
43  public:
49  SqlFieldsQuery(const std::string& sql) :
50  sql(sql),
51  schema(),
52  pageSize(1024),
53  loc(false),
54  distributedJoins(false),
55  enforceJoinOrder(false),
56  lazy(false),
57  args()
58  {
59  // No-op.
60  }
61 
68  SqlFieldsQuery(const std::string& sql, bool loc) :
69  sql(sql),
70  schema(),
71  pageSize(1024),
72  loc(loc),
73  distributedJoins(false),
74  enforceJoinOrder(false),
75  lazy(false),
76  args()
77  {
78  // No-op.
79  }
80 
87  sql(other.sql),
88  schema(other.schema),
89  pageSize(other.pageSize),
90  loc(other.loc),
91  distributedJoins(other.distributedJoins),
92  enforceJoinOrder(other.enforceJoinOrder),
93  lazy(other.lazy),
94  args()
95  {
96  args.reserve(other.args.size());
97 
98  typedef std::vector<impl::WritableObjectBase*>::const_iterator Iter;
99 
100  for (Iter i = other.args.begin(); i != other.args.end(); ++i)
101  args.push_back((*i)->Copy());
102  }
103 
110  {
111  if (this != &other)
112  {
113  SqlFieldsQuery tmp(other);
114 
115  Swap(tmp);
116  }
117 
118  return *this;
119  }
120 
125  {
126  typedef std::vector<impl::WritableObjectBase*>::const_iterator Iter;
127 
128  for (Iter it = args.begin(); it != args.end(); ++it)
129  delete *it;
130  }
131 
137  void Swap(SqlFieldsQuery& other)
138  {
139  if (this != &other)
140  {
141  using std::swap;
142 
143  swap(sql, other.sql);
144  swap(schema, other.schema);
145  swap(pageSize, other.pageSize);
146  swap(loc, other.loc);
147  swap(distributedJoins, other.distributedJoins);
148  swap(enforceJoinOrder, other.enforceJoinOrder);
149  swap(lazy, other.lazy);
150  swap(args, other.args);
151  }
152  }
153 
159  const std::string& GetSql() const
160  {
161  return sql;
162  }
163 
169  void SetSql(const std::string& sql)
170  {
171  this->sql = sql;
172  }
173 
179  int32_t GetPageSize() const
180  {
181  return pageSize;
182  }
183 
189  void SetPageSize(int32_t pageSize)
190  {
191  this->pageSize = pageSize;
192  }
193 
199  bool IsLocal() const
200  {
201  return loc;
202  }
203 
211  void SetLocal(bool loc)
212  {
213  this->loc = loc;
214  }
215 
223  bool IsLazy() const
224  {
225  return lazy;
226  }
227 
243  void SetLazy(bool lazy)
244  {
245  this->lazy = lazy;
246  }
247 
253  bool IsEnforceJoinOrder() const
254  {
255  return enforceJoinOrder;
256  }
257 
268  void SetEnforceJoinOrder(bool enforce)
269  {
270  enforceJoinOrder = enforce;
271  }
272 
278  bool IsDistributedJoins() const
279  {
280  return distributedJoins;
281  }
282 
291  void SetDistributedJoins(bool enabled)
292  {
293  distributedJoins = enabled;
294  }
295 
304  template<typename T>
305  void AddArgument(const T& arg)
306  {
307  args.push_back(new impl::WritableObject<T>(arg));
308  }
309 
316  void AddInt8ArrayArgument(const int8_t* src, int32_t len)
317  {
318  args.push_back(new impl::WritableObjectInt8Array(src, len));
319  }
320 
325  {
326  std::vector<impl::WritableObjectBase*>::iterator iter;
327  for (iter = args.begin(); iter != args.end(); ++iter)
328  delete *iter;
329 
330  args.clear();
331  }
332 
340  void SetSchema(const std::string& schema)
341  {
342  this->schema = schema;
343  }
344 
353  const std::string& GetSchema() const
354  {
355  return schema;
356  }
357 
363  void Write(binary::BinaryRawWriter& writer) const
364  {
365  writer.WriteBool(loc);
366  writer.WriteString(sql);
367  writer.WriteInt32(pageSize);
368 
369  writer.WriteInt32(static_cast<int32_t>(args.size()));
370 
371  std::vector<impl::WritableObjectBase*>::const_iterator it;
372 
373  for (it = args.begin(); it != args.end(); ++it)
374  (*it)->Write(writer);
375 
376  writer.WriteBool(distributedJoins);
377  writer.WriteBool(enforceJoinOrder);
378  writer.WriteBool(lazy);
379  writer.WriteInt32(0); // Timeout, ms
380  writer.WriteBool(false); // ReplicatedOnly
381  writer.WriteBool(false); // Colocated
382 
383  if (schema.empty())
384  writer.WriteNull();
385  else
386  writer.WriteString(schema);
387 
388  writer.WriteInt32Array(NULL, 0); // Partitions
389  writer.WriteInt32(1); // UpdateBatchSize
390  }
391 
392  private:
394  std::string sql;
395 
397  std::string schema;
398 
400  int32_t pageSize;
401 
403  bool loc;
404 
406  bool distributedJoins;
407 
409  bool enforceJoinOrder;
410 
412  bool lazy;
413 
415  std::vector<impl::WritableObjectBase*> args;
416  };
417  }
418  }
419 }
420 
421 #endif //_IGNITE_CACHE_QUERY_QUERY_SQL_FIELDS
ignite::cache::query::SqlFieldsQuery::SetLazy
void SetLazy(bool lazy)
Sets lazy query execution flag.
Definition: core/include/ignite/cache/query/query_sql_fields.h:243
ignite
Ignite API.
Definition: cache.h:47
ignite::cache::query::SqlFieldsQuery::SqlFieldsQuery
SqlFieldsQuery(const SqlFieldsQuery &other)
Copy constructor.
Definition: core/include/ignite/cache/query/query_sql_fields.h:86
ignite::binary::BinaryRawWriter::WriteInt32
void WriteInt32(int32_t val)
Write 32-byte signed integer.
Definition: binary_raw_writer.cpp:71
ignite::cache::query::SqlFieldsQuery::IsDistributedJoins
bool IsDistributedJoins() const
Check if distributed joins are enabled for this query.
Definition: core/include/ignite/cache/query/query_sql_fields.h:278
ignite::cache::query::SqlFieldsQuery::AddArgument
void AddArgument(const T &arg)
Add argument.
Definition: core/include/ignite/cache/query/query_sql_fields.h:305
ignite::cache::query::SqlFieldsQuery::GetSql
const std::string & GetSql() const
Get SQL string.
Definition: core/include/ignite/cache/query/query_sql_fields.h:159
ignite::cache::query::SqlFieldsQuery::SqlFieldsQuery
SqlFieldsQuery(const std::string &sql, bool loc)
Constructor.
Definition: core/include/ignite/cache/query/query_sql_fields.h:68
ignite::cache::query::SqlFieldsQuery::Swap
void Swap(SqlFieldsQuery &other)
Efficiently swaps contents with another SqlQuery instance.
Definition: core/include/ignite/cache/query/query_sql_fields.h:137
ignite::cache::query::SqlFieldsQuery::SetSchema
void SetSchema(const std::string &schema)
Set schema name for the query.
Definition: core/include/ignite/cache/query/query_sql_fields.h:340
ignite::cache::query::SqlFieldsQuery::SetPageSize
void SetPageSize(int32_t pageSize)
Set page size.
Definition: core/include/ignite/cache/query/query_sql_fields.h:189
ignite::binary::BinaryRawWriter::WriteInt32Array
void WriteInt32Array(const int32_t *val, int32_t len)
Write array of 32-byte signed integers.
Definition: binary_raw_writer.cpp:76
ignite::cache::query::SqlFieldsQuery::AddInt8ArrayArgument
void AddInt8ArrayArgument(const int8_t *src, int32_t len)
Add array of bytes as an argument.
Definition: core/include/ignite/cache/query/query_sql_fields.h:316
ignite::cache::query::SqlFieldsQuery::Write
void Write(binary::BinaryRawWriter &writer) const
Write query info to the stream.
Definition: core/include/ignite/cache/query/query_sql_fields.h:363
ignite::cache::query::SqlFieldsQuery::SetLocal
void SetLocal(bool loc)
Set local flag.
Definition: core/include/ignite/cache/query/query_sql_fields.h:211
ignite::binary::BinaryRawWriter
Binary raw writer.
Definition: binary_raw_writer.h:61
ignite::cache::query::SqlFieldsQuery::operator=
SqlFieldsQuery & operator=(const SqlFieldsQuery &other)
Assignment operator.
Definition: core/include/ignite/cache/query/query_sql_fields.h:109
ignite::cache::query::SqlFieldsQuery::SetEnforceJoinOrder
void SetEnforceJoinOrder(bool enforce)
Sets flag to enforce join order of tables in the query.
Definition: core/include/ignite/cache/query/query_sql_fields.h:268
ignite::binary::BinaryRawWriter::WriteString
void WriteString(const char *val)
Write string.
Definition: binary_raw_writer.cpp:151
ignite::cache::query::SqlFieldsQuery::~SqlFieldsQuery
~SqlFieldsQuery()
Destructor.
Definition: core/include/ignite/cache/query/query_sql_fields.h:124
ignite::cache::query::SqlFieldsQuery::GetSchema
const std::string & GetSchema() const
Get schema name for the query.
Definition: core/include/ignite/cache/query/query_sql_fields.h:353
ignite::binary::BinaryRawWriter::WriteBool
void WriteBool(bool val)
Write bool.
Definition: binary_raw_writer.cpp:41
ignite::cache::query::SqlFieldsQuery::SetSql
void SetSql(const std::string &sql)
Set SQL string.
Definition: core/include/ignite/cache/query/query_sql_fields.h:169
ignite::cache::query::SqlFieldsQuery::SqlFieldsQuery
SqlFieldsQuery(const std::string &sql)
Constructor.
Definition: core/include/ignite/cache/query/query_sql_fields.h:49
ignite::cache::query::SqlFieldsQuery::ClearArguments
void ClearArguments()
Remove all added arguments.
Definition: core/include/ignite/cache/query/query_sql_fields.h:324
ignite::cache::query::SqlFieldsQuery
Sql fields query.
Definition: core/include/ignite/cache/query/query_sql_fields.h:41
ignite::cache::query::SqlFieldsQuery::GetPageSize
int32_t GetPageSize() const
Get page size.
Definition: core/include/ignite/cache/query/query_sql_fields.h:179
binary_raw_writer.h
ignite::cache::query::SqlFieldsQuery::IsEnforceJoinOrder
bool IsEnforceJoinOrder() const
Checks if join order of tables if enforced.
Definition: core/include/ignite/cache/query/query_sql_fields.h:253
ignite::cache::query::SqlFieldsQuery::IsLazy
bool IsLazy() const
Gets lazy query execution flag.
Definition: core/include/ignite/cache/query/query_sql_fields.h:223
ignite::cache::query::SqlFieldsQuery::SetDistributedJoins
void SetDistributedJoins(bool enabled)
Specify if distributed joins are enabled for this query.
Definition: core/include/ignite/cache/query/query_sql_fields.h:291
ignite::binary::BinaryRawWriter::WriteNull
void WriteNull()
Write NULL value.
Definition: binary_raw_writer.cpp:176
ignite::cache::query::SqlFieldsQuery::IsLocal
bool IsLocal() const
Get local flag.
Definition: core/include/ignite/cache/query/query_sql_fields.h:199