LiteSQL
expr.hpp
Go to the documentation of this file.
1 /* LiteSQL
2  *
3  * By Tero Laitinen
4  *
5  * See LICENSE for copyright information. */
6 
7 #ifndef litesql_expr_hpp
8 #define litesql_expr_hpp
9 
10 #include "litesql/utils.hpp"
11 #include "litesql/field.hpp"
14 namespace litesql {
15 
18 class Expr {
19 public:
21  static const char* True;
22  // default expression is true
23  virtual std::string asString() const { return True; }
24 
25  const std::vector<std::string>& getExtraTables() const {
26  return extraTables;
27  }
28  virtual ~Expr() {}
29 protected:
30  // extra tables to be joined
31  std::vector<std::string> extraTables;
32 };
33 
35 class RawExpr : public Expr {
36 
37 public:
38  RawExpr(const std::string& e) : expr(e) {}
39  virtual std::string asString() const { return expr; }
40 private:
41  std::string expr;
42 };
44 class Connective : public Expr {
45 private:
46  std::string op;
47 protected:
48  const Expr &e1, &e2;
49 
50  Connective(const std::string& o, const Expr & e1_, const Expr & e2_)
51  : op(o), e1(e1_), e2(e2_) { }
52 
53 public:
54  virtual ~Connective() {}
55 
56  virtual std::string asString() const {
57  std::string res = "(" + e1.asString() + ") " + op
58  + " (" + e2.asString() + ")";
59  return res;
60  }
61 };
63 class And : public Connective {
64 public:
65  And(const Expr & e1_, const Expr & e2_) : Connective("and", e1_, e2_) {}
66  virtual std::string asString() const {
67  if (e1.asString() == True)
68  return e2.asString();
69  else if (e2.asString() == True)
70  return e1.asString();
71  else
72  return Connective::asString();
73  }
74 };
76 class Or : public Connective {
77 public:
78  Or(const Expr & e1_, const Expr & e2_)
79  : Connective("or", e1_, e2_) {}
80  virtual std::string asString() const {
81  if ((e1.asString() == True)||(e2.asString() == True))
82  return True;
83  else
84  return Connective::asString();
85  }
86 };
88 class Not : public Expr {
89 private:
90  const Expr & exp;
91 public:
92  Not(const Expr & _exp) : exp(_exp) {}
93  virtual std::string asString() const {
94  return "not ("+exp.asString()+")";
95  }
96 
97 };
99 class Oper : public Expr {
100 protected:
101  const FieldType & field;
102  std::string op;
103  std::string data;
104  bool escape;
105 
106  Oper(const FieldType & fld, const std::string& o, const std::string& d)
107  : field(fld), op(o), data(d), escape(true) {
108  extraTables.push_back(fld.table());
109  }
110  Oper(const FieldType & fld, const std::string& o, const FieldType &f2)
111  : field(fld), op(o), data(f2.fullName()), escape(false) {
112  extraTables.push_back(fld.table());
113  }
114 
115 public:
116  virtual std::string asString() const {
117  std::string res;
118  res += field.fullName() + " " + op + " " + (escape ? escapeSQL(data) : data);
119  return res;
120  }
121 };
123 class Eq : public Oper {
124 public:
125  Eq(const FieldType & fld, const std::string& d)
126  : Oper(fld, "=", d) {}
127  Eq(const FieldType & fld, const FieldType & f2)
128  : Oper(fld, "=", f2) {}
129 
130 };
132 class NotEq : public Oper {
133 public:
134  NotEq(const FieldType & fld, const std::string& d)
135  : Oper(fld, "<>", d) {}
136  NotEq(const FieldType & fld, const FieldType & f2)
137  : Oper(fld, "<>", f2) {
138  }
139 
140 };
142 class Gt : public Oper {
143 public:
144  Gt(const FieldType & fld, const std::string& d)
145  : Oper(fld, ">", d) {}
146  Gt(const FieldType & fld, const FieldType& d)
147  : Oper(fld, ">", d) {}
148 
149 };
151 class GtEq : public Oper {
152 public:
153  GtEq(const FieldType & fld, const std::string& d)
154  : Oper(fld, ">=", d) {}
155  GtEq(const FieldType & fld, const FieldType& d)
156  : Oper(fld, ">=", d) {}
157 
158 };
160 class Lt : public Oper {
161 public:
162  Lt(const FieldType & fld, const std::string& d)
163  : Oper(fld, "<", d) {}
164  Lt(const FieldType & fld, const FieldType& d)
165  : Oper(fld, "<", d) {}
166 
167 };
169 class LtEq : public Oper {
170 public:
171  LtEq(const FieldType & fld, const std::string& d)
172  : Oper(fld, "<=", d) {}
173  LtEq(const FieldType & fld, const FieldType& d)
174  : Oper(fld, "<=", d) {}
175 
176 };
178 class Like : public Oper {
179 public:
180  Like(const FieldType & fld, const std::string& d)
181  : Oper(fld, "like", d) {}
182 };
183 
185 class In : public Oper {
186 public:
187  In(const FieldType & fld, const std::string& set)
188  : Oper(fld, "in", "("+set+")") {};
189  In(const FieldType & fld, const SelectQuery& s);
190  virtual std::string asString() const {
191  return field.fullName() + " " + op + " " + data;
192  }
193 
194 };
195 And operator&&(const Expr& o1, const Expr& o2);
196 Or operator||(const Expr& o1, const Expr& o2);
197 template <class T>
198 litesql::Eq operator==(const litesql::FieldType& fld, const T& o2) {
199  return litesql::Eq(fld, litesql::toString(o2));
200 }
201 Eq operator==(const FieldType& fld, const FieldType& f2);
202 Gt operator>(const FieldType& fld, const FieldType& o2);
203 GtEq operator>=(const FieldType& fld, const FieldType& o2);
204 Lt operator<(const FieldType& fld, const FieldType& o2);
205 LtEq operator<=(const FieldType& fld, const FieldType& o2);
206 NotEq operator!=(const FieldType& fld, const FieldType& f2);
207 
208 template <class T>
209 litesql::Gt operator>(const litesql::FieldType& fld, const T& o2) {
210  return litesql::Gt(fld, litesql::toString(o2));
211 }
212 
213 template <class T>
214 litesql::GtEq operator>=(const litesql::FieldType& fld, const T& o2) {
215  return litesql::GtEq(fld, litesql::toString(o2));
216 }
217 
218 template <class T>
219 litesql::Lt operator<(const litesql::FieldType& fld, const T& o2) {
220  return litesql::Lt(fld, litesql::toString(o2));
221 }
222 
223 
224 template <class T>
225 litesql::LtEq operator<=(const litesql::FieldType& fld, const T& o2) {
226  return litesql::LtEq(fld, litesql::toString(o2));
227 }
228 template <class T>
229 litesql::NotEq operator!=(const litesql::FieldType& fld, const T& o2) {
230  return litesql::NotEq(fld, litesql::toString(o2));
231 }
232 
233 Not operator!(const Expr &exp);
234 }
235 
236 
237 #endif
litesql::Connective
used to connect two expressions
Definition: expr.hpp:44
litesql::Expr::True
static const char * True
constant for True expression
Definition: expr.hpp:21
litesql::Lt
less than operator
Definition: expr.hpp:160
utils.hpp
litesql::SelectQuery
a class that helps creating SELECT-SQL statements.
Definition: selectquery.hpp:20
litesql::Oper
base class of operators in expressions
Definition: expr.hpp:99
litesql::RawExpr
used to inject custom expression into WHERE-clause
Definition: expr.hpp:35
litesql::In
in operator
Definition: expr.hpp:185
litesql::Gt
greater than operator
Definition: expr.hpp:142
litesql::NotEq
inequality operator
Definition: expr.hpp:132
selectquery.hpp
litesql::Like
like operator
Definition: expr.hpp:178
litesql::Eq
equality operator
Definition: expr.hpp:123
expr.hpp
litesql::GtEq
greater or equal operator
Definition: expr.hpp:151
field.hpp
litesql::Or
connects two expression with or-operator.
Definition: expr.hpp:76
litesql::And
connects two expressions with and-operator.
Definition: expr.hpp:63
litesql::FieldType
Definition: field.hpp:25
litesql::Expr
A base class for expression in WHERE - clause.
Definition: expr.hpp:18
litesql::escapeSQL
std::string escapeSQL(const std::string &str)
escapes ' characters so that they do not break SQL statements.
litesql::LtEq
less than or equal operator
Definition: expr.hpp:169
litesql::Not
negates expression
Definition: expr.hpp:88

SourceForge.net Logo