Apache Ignite C++
query_sql.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
24 #define _IGNITE_CACHE_QUERY_QUERY_SQL
25 
26 #include <stdint.h>
27 #include <string>
28 #include <vector>
29 
32 
33 namespace ignite
34 {
35  namespace cache
36  {
37  namespace query
38  {
42  class SqlQuery
43  {
44  public:
51  SqlQuery(const std::string& type, const std::string& sql) :
52  type(type),
53  sql(sql),
54  pageSize(1024),
55  loc(false),
56  distributedJoins(false),
57  args()
58  {
59  // No-op.
60  }
61 
67  SqlQuery(const SqlQuery& other) :
68  type(other.type),
69  sql(other.sql),
70  pageSize(other.pageSize),
71  loc(other.loc),
72  distributedJoins(other.distributedJoins),
73  args()
74  {
75  args.reserve(other.args.size());
76 
77  typedef std::vector<QueryArgumentBase*>::const_iterator Iter;
78 
79  for (Iter i = other.args.begin(); i != other.args.end(); ++i)
80  args.push_back((*i)->Copy());
81  }
82 
88  SqlQuery& operator=(const SqlQuery& other)
89  {
90  if (this != &other)
91  {
92  SqlQuery tmp(other);
93 
94  Swap(tmp);
95  }
96 
97  return *this;
98  }
99 
104  {
105  typedef std::vector<QueryArgumentBase*>::const_iterator Iter;
106 
107  for (Iter it = args.begin(); it != args.end(); ++it)
108  delete *it;
109  }
110 
116  void Swap(SqlQuery& other)
117  {
118  if (this != &other)
119  {
120  std::swap(type, other.type);
121  std::swap(sql, other.sql);
122  std::swap(pageSize, other.pageSize);
123  std::swap(loc, other.loc);
124  std::swap(distributedJoins, other.distributedJoins);
125  std::swap(args, other.args);
126  }
127  }
128 
134  const std::string& GetType() const
135  {
136  return type;
137  }
138 
144  void SetType(const std::string& type)
145  {
146  this->type = type;
147  }
148 
154  const std::string& GetSql() const
155  {
156  return sql;
157  }
158 
164  void SetSql(const std::string& sql)
165  {
166  this->sql = sql;
167  }
168 
174  int32_t GetPageSize() const
175  {
176  return pageSize;
177  }
178 
184  void SetPageSize(int32_t pageSize)
185  {
186  this->pageSize = pageSize;
187  }
188 
194  bool IsLocal() const
195  {
196  return loc;
197  }
198 
204  void SetLocal(bool loc)
205  {
206  this->loc = loc;
207  }
208 
214  bool IsDistributedJoins() const
215  {
216  return distributedJoins;
217  }
218 
227  void SetDistributedJoins(bool enabled)
228  {
229  distributedJoins = enabled;
230  }
231 
241  template<typename T>
242  void AddArgument(const T& arg)
243  {
244  args.push_back(new QueryArgument<T>(arg));
245  }
246 
252  void Write(binary::BinaryRawWriter& writer) const
253  {
254  writer.WriteBool(loc);
255  writer.WriteString(sql);
256  writer.WriteString(type);
257  writer.WriteInt32(pageSize);
258 
259  writer.WriteInt32(static_cast<int32_t>(args.size()));
260 
261  for (std::vector<QueryArgumentBase*>::const_iterator it = args.begin(); it != args.end(); ++it)
262  (*it)->Write(writer);
263 
264  writer.WriteBool(distributedJoins);
265  }
266 
267  private:
269  std::string type;
270 
272  std::string sql;
273 
275  int32_t pageSize;
276 
278  bool loc;
279 
281  bool distributedJoins;
282 
284  std::vector<QueryArgumentBase*> args;
285  };
286  }
287  }
288 }
289 
290 #endif //_IGNITE_CACHE_QUERY_QUERY_SQL
int32_t GetPageSize() const
Get page size.
Definition: query_sql.h:174
Declares ignite::binary::BinaryRawWriter class.
void SetDistributedJoins(bool enabled)
Specify if distributed joins are enabled for this query.
Definition: query_sql.h:227
void WriteInt32(int32_t val)
Write 32-byte signed integer.
Definition: binary_raw_writer.cpp:72
SqlQuery & operator=(const SqlQuery &other)
Assignment operator.
Definition: query_sql.h:88
Sql query.
Definition: query_sql.h:42
void WriteString(const char *val)
Write string.
Definition: binary_raw_writer.cpp:142
void SetPageSize(int32_t pageSize)
Set page size.
Definition: query_sql.h:184
void SetLocal(bool loc)
Set local flag.
Definition: query_sql.h:204
void Write(binary::BinaryRawWriter &writer) const
Write query info to the stream.
Definition: query_sql.h:252
const std::string & GetType() const
Get type name.
Definition: query_sql.h:134
Declares ignite::cache::query::QueryArgument class template and ignite::cache::query::QueryArgumentBa...
bool IsDistributedJoins() const
Check if distributed joins are enabled for this query.
Definition: query_sql.h:214
Binary raw writer.
Definition: binary_raw_writer.h:55
void SetSql(const std::string &sql)
Set SQL string.
Definition: query_sql.h:164
void SetType(const std::string &type)
Set type name.
Definition: query_sql.h:144
void AddArgument(const T &arg)
Add argument.
Definition: query_sql.h:242
void WriteBool(bool val)
Write bool.
Definition: binary_raw_writer.cpp:42
bool IsLocal() const
Get local flag.
Definition: query_sql.h:194
void Swap(SqlQuery &other)
Efficiently swaps contents with another SqlQuery instance.
Definition: query_sql.h:116
SqlQuery(const SqlQuery &other)
Copy constructor.
Definition: query_sql.h:67
~SqlQuery()
Destructor.
Definition: query_sql.h:103
Apache Ignite API.
Definition: cache.h:43
const std::string & GetSql() const
Get SQL string.
Definition: query_sql.h:154
Query argument class template.
Definition: query_argument.h:72
SqlQuery(const std::string &type, const std::string &sql)
Constructor.
Definition: query_sql.h:51