Apache Ignite C++
query_sql_fields.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
23 #ifndef _IGNITE_CACHE_QUERY_QUERY_SQL_FIELDS
24 #define _IGNITE_CACHE_QUERY_QUERY_SQL_FIELDS
25 
26 #include <stdint.h>
27 #include <string>
28 #include <vector>
29 
30 #include <ignite/impl/cache/query/query_argument.h>
32 
33 namespace ignite
34 {
35  namespace cache
36  {
37  namespace query
38  {
43  {
44  public:
50  SqlFieldsQuery(const std::string& sql) :
51  sql(sql),
52  schema(),
53  pageSize(1024),
54  loc(false),
55  distributedJoins(false),
56  enforceJoinOrder(false),
57  lazy(false),
58  args()
59  {
60  // No-op.
61  }
62 
69  SqlFieldsQuery(const std::string& sql, bool loc) :
70  sql(sql),
71  schema(),
72  pageSize(1024),
73  loc(false),
74  distributedJoins(false),
75  enforceJoinOrder(false),
76  lazy(false),
77  args()
78  {
79  // No-op.
80  }
81 
88  sql(other.sql),
89  schema(other.schema),
90  pageSize(other.pageSize),
91  loc(other.loc),
92  distributedJoins(other.distributedJoins),
93  enforceJoinOrder(other.enforceJoinOrder),
94  lazy(other.lazy),
95  args()
96  {
97  args.reserve(other.args.size());
98 
99  typedef std::vector<impl::cache::query::QueryArgumentBase*>::const_iterator Iter;
100 
101  for (Iter i = other.args.begin(); i != other.args.end(); ++i)
102  args.push_back((*i)->Copy());
103  }
104 
111  {
112  if (this != &other)
113  {
114  SqlFieldsQuery tmp(other);
115 
116  Swap(tmp);
117  }
118 
119  return *this;
120  }
121 
126  {
127  typedef std::vector<impl::cache::query::QueryArgumentBase*>::const_iterator Iter;
128 
129  for (Iter it = args.begin(); it != args.end(); ++it)
130  delete *it;
131  }
132 
138  void Swap(SqlFieldsQuery& other)
139  {
140  if (this != &other)
141  {
142  using std::swap;
143 
144  swap(sql, other.sql);
145  swap(schema, other.schema);
146  swap(pageSize, other.pageSize);
147  swap(loc, other.loc);
148  swap(distributedJoins, other.distributedJoins);
149  swap(enforceJoinOrder, other.enforceJoinOrder);
150  swap(lazy, other.lazy);
151  swap(args, other.args);
152  }
153  }
154 
160  const std::string& GetSql() const
161  {
162  return sql;
163  }
164 
170  void SetSql(const std::string& sql)
171  {
172  this->sql = sql;
173  }
174 
180  int32_t GetPageSize() const
181  {
182  return pageSize;
183  }
184 
190  void SetPageSize(int32_t pageSize)
191  {
192  this->pageSize = pageSize;
193  }
194 
200  bool IsLocal() const
201  {
202  return loc;
203  }
204 
210  void SetLocal(bool loc)
211  {
212  this->loc = loc;
213  }
214 
222  bool IsLazy() const
223  {
224  return lazy;
225  }
226 
242  void SetLazy(bool lazy)
243  {
244  this->lazy = lazy;
245  }
246 
252  bool IsEnforceJoinOrder() const
253  {
254  return enforceJoinOrder;
255  }
256 
267  void SetEnforceJoinOrder(bool enforce)
268  {
269  enforceJoinOrder = enforce;
270  }
271 
277  bool IsDistributedJoins() const
278  {
279  return distributedJoins;
280  }
281 
290  void SetDistributedJoins(bool enabled)
291  {
292  distributedJoins = enabled;
293  }
294 
303  template<typename T>
304  void AddArgument(const T& arg)
305  {
306  args.push_back(new impl::cache::query::QueryArgument<T>(arg));
307  }
308 
315  void AddInt8ArrayArgument(const int8_t* src, int32_t len)
316  {
317  args.push_back(new impl::cache::query::QueryInt8ArrayArgument(src, len));
318  }
319 
324  {
325  std::vector<impl::cache::query::QueryArgumentBase*>::iterator iter;
326  for (iter = args.begin(); iter != args.end(); ++iter)
327  delete *iter;
328 
329  args.clear();
330  }
331 
339  void SetSchema(const std::string& schema)
340  {
341  this->schema = schema;
342  }
343 
352  const std::string& GetSchema() const
353  {
354  return schema;
355  }
356 
362  void Write(binary::BinaryRawWriter& writer) const
363  {
364  writer.WriteBool(loc);
365  writer.WriteString(sql);
366  writer.WriteInt32(pageSize);
367 
368  writer.WriteInt32(static_cast<int32_t>(args.size()));
369 
370  std::vector<impl::cache::query::QueryArgumentBase*>::const_iterator it;
371 
372  for (it = args.begin(); it != args.end(); ++it)
373  (*it)->Write(writer);
374 
375  writer.WriteBool(distributedJoins);
376  writer.WriteBool(enforceJoinOrder);
377  writer.WriteBool(lazy);
378  writer.WriteInt32(0); // Timeout, ms
379  writer.WriteBool(false); // ReplicatedOnly
380  writer.WriteBool(false); // Colocated
381 
382  if (schema.empty())
383  writer.WriteNull();
384  else
385  writer.WriteString(schema);
386  }
387 
388  private:
390  std::string sql;
391 
393  std::string schema;
394 
396  int32_t pageSize;
397 
399  bool loc;
400 
402  bool distributedJoins;
403 
405  bool enforceJoinOrder;
406 
408  bool lazy;
409 
411  std::vector<impl::cache::query::QueryArgumentBase*> args;
412  };
413  }
414  }
415 }
416 
417 #endif //_IGNITE_CACHE_QUERY_QUERY_SQL_FIELDS
void SetPageSize(int32_t pageSize)
Set page size.
Definition: query_sql_fields.h:190
void SetLazy(bool lazy)
Sets lazy query execution flag.
Definition: query_sql_fields.h:242
bool IsDistributedJoins() const
Check if distributed joins are enabled for this query.
Definition: query_sql_fields.h:277
void WriteNull()
Write NULL value.
Definition: binary_raw_writer.cpp:172
void Swap(SqlFieldsQuery &other)
Efficiently swaps contents with another SqlQuery instance.
Definition: query_sql_fields.h:138
const std::string & GetSchema() const
Get schema name for the query.
Definition: query_sql_fields.h:352
Declares ignite::binary::BinaryRawWriter class.
void WriteInt32(int32_t val)
Write 32-byte signed integer.
Definition: binary_raw_writer.cpp:72
void AddArgument(const T &arg)
Add argument.
Definition: query_sql_fields.h:304
int32_t GetPageSize() const
Get page size.
Definition: query_sql_fields.h:180
void SetEnforceJoinOrder(bool enforce)
Sets flag to enforce join order of tables in the query.
Definition: query_sql_fields.h:267
bool IsLazy() const
Gets lazy query execution flag.
Definition: query_sql_fields.h:222
SqlFieldsQuery(const std::string &sql, bool loc)
Constructor.
Definition: query_sql_fields.h:69
void SetSql(const std::string &sql)
Set SQL string.
Definition: query_sql_fields.h:170
void AddInt8ArrayArgument(const int8_t *src, int32_t len)
Add array of bytes as an argument.
Definition: query_sql_fields.h:315
void SetLocal(bool loc)
Set local flag.
Definition: query_sql_fields.h:210
Sql fields query.
Definition: query_sql_fields.h:42
void Write(binary::BinaryRawWriter &writer) const
Write query info to the stream.
Definition: query_sql_fields.h:362
void WriteString(const char *val)
Write string.
Definition: binary_raw_writer.cpp:152
bool IsEnforceJoinOrder() const
Checks if join order of tables if enforced.
Definition: query_sql_fields.h:252
void ClearArguments()
Remove all added arguments.
Definition: query_sql_fields.h:323
bool IsLocal() const
Get local flag.
Definition: query_sql_fields.h:200
Binary raw writer.
Definition: binary_raw_writer.h:55
void WriteBool(bool val)
Write bool.
Definition: binary_raw_writer.cpp:42
SqlFieldsQuery(const std::string &sql)
Constructor.
Definition: query_sql_fields.h:50
void SetSchema(const std::string &schema)
Set schema name for the query.
Definition: query_sql_fields.h:339
~SqlFieldsQuery()
Destructor.
Definition: query_sql_fields.h:125
const std::string & GetSql() const
Get SQL string.
Definition: query_sql_fields.h:160
Apache Ignite API.
Definition: cache.h:48
SqlFieldsQuery(const SqlFieldsQuery &other)
Copy constructor.
Definition: query_sql_fields.h:87
void SetDistributedJoins(bool enabled)
Specify if distributed joins are enabled for this query.
Definition: query_sql_fields.h:290
SqlFieldsQuery & operator=(const SqlFieldsQuery &other)
Assignment operator.
Definition: query_sql_fields.h:110