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  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(false),
73  distributedJoins(false),
74  enforceJoinOrder(false),
75  args()
76  {
77  // No-op.
78  }
79 
86  sql(other.sql),
87  schema(other.schema),
88  pageSize(other.pageSize),
89  loc(other.loc),
90  distributedJoins(other.distributedJoins),
91  enforceJoinOrder(other.enforceJoinOrder),
92  args()
93  {
94  args.reserve(other.args.size());
95 
96  typedef std::vector<impl::cache::query::QueryArgumentBase*>::const_iterator Iter;
97 
98  for (Iter i = other.args.begin(); i != other.args.end(); ++i)
99  args.push_back((*i)->Copy());
100  }
101 
108  {
109  if (this != &other)
110  {
111  SqlFieldsQuery tmp(other);
112 
113  Swap(tmp);
114  }
115 
116  return *this;
117  }
118 
123  {
124  typedef std::vector<impl::cache::query::QueryArgumentBase*>::const_iterator Iter;
125 
126  for (Iter it = args.begin(); it != args.end(); ++it)
127  delete *it;
128  }
129 
135  void Swap(SqlFieldsQuery& other)
136  {
137  if (this != &other)
138  {
139  using std::swap;
140 
141  swap(sql, other.sql);
142  swap(sql, other.schema);
143  swap(pageSize, other.pageSize);
144  swap(loc, other.loc);
145  swap(distributedJoins, other.distributedJoins);
146  swap(enforceJoinOrder, other.enforceJoinOrder);
147  swap(args, other.args);
148  }
149  }
150 
156  const std::string& GetSql() const
157  {
158  return sql;
159  }
160 
166  void SetSql(const std::string& sql)
167  {
168  this->sql = sql;
169  }
170 
176  int32_t GetPageSize() const
177  {
178  return pageSize;
179  }
180 
186  void SetPageSize(int32_t pageSize)
187  {
188  this->pageSize = pageSize;
189  }
190 
196  bool IsLocal() const
197  {
198  return loc;
199  }
200 
206  void SetLocal(bool loc)
207  {
208  this->loc = loc;
209  }
210 
216  bool IsEnforceJoinOrder() const
217  {
218  return enforceJoinOrder;
219  }
220 
233  void SetEnforceJoinOrder(bool enforce)
234  {
235  enforceJoinOrder = enforce;
236  }
237 
243  bool IsDistributedJoins() const
244  {
245  return distributedJoins;
246  }
247 
256  void SetDistributedJoins(bool enabled)
257  {
258  distributedJoins = enabled;
259  }
260 
270  template<typename T>
271  void AddArgument(const T& arg)
272  {
273  args.push_back(new impl::cache::query::QueryArgument<T>(arg));
274  }
275 
280  {
281  args.clear();
282  }
283 
291  void SetSchema(const std::string& schema)
292  {
293  this->schema = schema;
294  }
295 
304  const std::string& GetSchema() const
305  {
306  return schema;
307  }
308 
314  void Write(binary::BinaryRawWriter& writer) const
315  {
316  writer.WriteBool(loc);
317  writer.WriteString(sql);
318  writer.WriteInt32(pageSize);
319 
320  writer.WriteInt32(static_cast<int32_t>(args.size()));
321 
322  std::vector<impl::cache::query::QueryArgumentBase*>::const_iterator it;
323 
324  for (it = args.begin(); it != args.end(); ++it)
325  (*it)->Write(writer);
326 
327  writer.WriteBool(distributedJoins);
328  writer.WriteBool(enforceJoinOrder);
329  writer.WriteInt32(0); // Timeout, ms
330  writer.WriteBool(false); // ReplicatedOnly
331  writer.WriteBool(false); // Colocated
332 
333  if (schema.empty())
334  writer.WriteNull();
335  else
336  writer.WriteString(schema);
337  }
338 
339  private:
341  std::string sql;
342 
344  std::string schema;
345 
347  int32_t pageSize;
348 
350  bool loc;
351 
353  bool distributedJoins;
354 
356  bool enforceJoinOrder;
357 
359  std::vector<impl::cache::query::QueryArgumentBase*> args;
360  };
361  }
362  }
363 }
364 
365 #endif //_IGNITE_CACHE_QUERY_QUERY_SQL_FIELDS
void SetPageSize(int32_t pageSize)
Set page size.
Definition: query_sql_fields.h:186
bool IsDistributedJoins() const
Check if distributed joins are enabled for this query.
Definition: query_sql_fields.h:243
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:135
const std::string & GetSchema() const
Get schema name for the query.
Definition: query_sql_fields.h:304
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:271
int32_t GetPageSize() const
Get page size.
Definition: query_sql_fields.h:176
void SetEnforceJoinOrder(bool enforce)
Sets flag to enforce join order of tables in the query.
Definition: query_sql_fields.h:233
SqlFieldsQuery(const std::string &sql, bool loc)
Constructor.
Definition: query_sql_fields.h:68
void SetSql(const std::string &sql)
Set SQL string.
Definition: query_sql_fields.h:166
void SetLocal(bool loc)
Set local flag.
Definition: query_sql_fields.h:206
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:314
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:216
void ClearArguments()
Remove all added arguments.
Definition: query_sql_fields.h:279
bool IsLocal() const
Get local flag.
Definition: query_sql_fields.h:196
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:291
~SqlFieldsQuery()
Destructor.
Definition: query_sql_fields.h:122
const std::string & GetSql() const
Get SQL string.
Definition: query_sql_fields.h:156
Apache Ignite API.
Definition: cache.h:48
SqlFieldsQuery(const SqlFieldsQuery &other)
Copy constructor.
Definition: query_sql_fields.h:85
void SetDistributedJoins(bool enabled)
Specify if distributed joins are enabled for this query.
Definition: query_sql_fields.h:256
SqlFieldsQuery & operator=(const SqlFieldsQuery &other)
Assignment operator.
Definition: query_sql_fields.h:107