SQLsmith  v1.2.1-5-gfacd7a8
A random SQL query generator
expr.hh
Go to the documentation of this file.
1 
4 #ifndef EXPR_HH
5 #define EXPR_HH
6 
7 #include "prod.hh"
8 #include <string>
9 
10 using std::shared_ptr;
11 using std::vector;
12 using std::string;
13 
14 struct value_expr: prod {
15  sqltype *type;
16  virtual void out(std::ostream &out) = 0;
17  virtual ~value_expr() { }
18  value_expr(prod *p) : prod(p) { }
19  static shared_ptr<value_expr> factory(prod *p, sqltype *type_constraint = 0);
20 };
21 
23  shared_ptr<value_expr> condition;
24  shared_ptr<value_expr> true_expr;
25  shared_ptr<value_expr> false_expr;
26  case_expr(prod *p, sqltype *type_constraint = 0);
27  virtual void out(std::ostream &out);
28  virtual void accept(prod_visitor *v);
29 };
30 
31 struct funcall : value_expr {
32  routine *proc;
33  bool is_aggregate;
34  vector<shared_ptr<value_expr> > parms;
35  virtual void out(std::ostream &out);
36  virtual ~funcall() { }
37  funcall(prod *p, sqltype *type_constraint = 0, bool agg = 0);
38  virtual void accept(prod_visitor *v) {
39  v->visit(this);
40  for (auto p : parms)
41  p->accept(v);
42  }
43 };
44 
46  table *tab;
47  column *col;
48  int offset;
49  routine *agg;
50  atomic_subselect(prod *p, sqltype *type_constraint = 0);
51  virtual void out(std::ostream &out);
52 };
53 
55  std::string expr;
56  const_expr(prod *p, sqltype *type_constraint = 0);
57  virtual void out(std::ostream &out) { out << expr; }
58  virtual ~const_expr() { }
59 };
60 
62  column_reference(prod *p, sqltype *type_constraint = 0);
63  virtual void out(std::ostream &out) { out << reference; }
64  std::string reference;
65  virtual ~column_reference() { }
66 };
67 
68 struct coalesce : value_expr {
69  const char *abbrev_;
70  vector<shared_ptr<value_expr> > value_exprs;
71  virtual ~coalesce() { };
72  coalesce(prod *p, sqltype *type_constraint = 0, const char *abbrev = "coalesce");
73  virtual void out(std::ostream &out);
74  virtual void accept(prod_visitor *v) {
75  v->visit(this);
76  for (auto p : value_exprs)
77  p->accept(v);
78  }
79 };
80 
81 struct nullif : coalesce {
82  virtual ~nullif() { };
83  nullif(prod *p, sqltype *type_constraint = 0)
84  : coalesce(p, type_constraint, "nullif")
85  { };
86 };
87 
89  virtual ~bool_expr() { }
90  bool_expr(prod *p) : value_expr(p) { type = scope->schema->booltype; }
91  static shared_ptr<bool_expr> factory(prod *p);
92 };
93 
95  virtual ~truth_value() { }
96  const char *op;
97  virtual void out(std::ostream &out) { out << op; }
98  truth_value(prod *p) : bool_expr(p) {
99  op = ( (d6() < 4) ? scope->schema->true_literal : scope->schema->false_literal);
100  }
101 };
102 
104  virtual ~null_predicate() { }
105  const char *negate;
106  shared_ptr<value_expr> expr;
107  null_predicate(prod *p) : bool_expr(p) {
108  negate = ((d6()<4) ? "not " : "");
109  expr = value_expr::factory(this);
110  }
111  virtual void out(std::ostream &out) {
112  out << *expr << " is " << negate << "NULL";
113  }
114  virtual void accept(prod_visitor *v) {
115  v->visit(this);
116  expr->accept(v);
117  }
118 };
119 
121  shared_ptr<struct query_spec> subquery;
122  virtual ~exists_predicate() { }
124  virtual void out(std::ostream &out);
125  virtual void accept(prod_visitor *v);
126 };
127 
129  shared_ptr<value_expr> lhs, rhs;
130  bool_binop(prod *p) : bool_expr(p) { }
131  virtual void out(std::ostream &out) = 0;
132  virtual void accept(prod_visitor *v) {
133  v->visit(this);
134  lhs->accept(v);
135  rhs->accept(v);
136  }
137 };
138 
140  virtual ~bool_term() { }
141  const char *op;
142  virtual void out(std::ostream &out) {
143  out << "(" << *lhs << ") ";
144  indent(out);
145  out << op << " (" << *rhs << ")";
146  }
147  bool_term(prod *p) : bool_binop(p)
148  {
149  op = ((d6()<4) ? "or" : "and");
150  lhs = bool_expr::factory(this);
151  rhs = bool_expr::factory(this);
152  }
153 };
154 
156  distinct_pred(prod *p);
157  virtual ~distinct_pred() { };
158  virtual void out(std::ostream &o) {
159  o << *lhs << " is distinct from " << *rhs;
160  }
161 };
162 
164  op *oper;
165  comparison_op(prod *p);
166  virtual ~comparison_op() { };
167  virtual void out(std::ostream &o) {
168  o << *lhs << " " << oper->name << " " << *rhs;
169  }
170 };
171 
173  virtual void out(std::ostream &out);
174  virtual ~window_function() { }
175  window_function(prod *p, sqltype *type_constraint);
176  vector<shared_ptr<column_reference> > partition_by;
177  vector<shared_ptr<column_reference> > order_by;
178  shared_ptr<funcall> aggregate;
179  static bool allowed(prod *pprod);
180  virtual void accept(prod_visitor *v) {
181  v->visit(this);
182  aggregate->accept(v);
183  for (auto p : partition_by)
184  p->accept(v);
185  for (auto p : order_by)
186  p->accept(v);
187  }
188 };
189 
190 #endif
Base class for grammar productions.
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: expr.cc:319
virtual void out(std::ostream &out)=0
Emit SQL for this production.
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: expr.hh:132
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: expr.hh:142
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: expr.cc:60
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: expr.cc:69
Definition: expr.hh:68
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: expr.hh:74
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: expr.cc:183
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: expr.hh:63
virtual void out(std::ostream &o)
Emit SQL for this production.
Definition: expr.hh:167
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: expr.hh:57
virtual void out(std::ostream &o)
Emit SQL for this production.
Definition: expr.hh:158
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: expr.cc:130
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: expr.cc:124
Definition: expr.hh:31
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: expr.cc:263
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: expr.hh:38
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: expr.hh:111
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: expr.hh:114
Definition: expr.hh:81
Definition: relmodel.hh:117
Base class for walking the AST.
Definition: prod.hh:11
Base class for AST nodes.
Definition: prod.hh:17
virtual void indent(std::ostream &out)
Newline and indent according to tree level.
Definition: prod.cc:20
struct prod * pprod
Parent production that instanciated this one.
Definition: prod.hh:20
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: prod.hh:41
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: expr.hh:97
virtual void out(std::ostream &out)=0
Emit SQL for this production.
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: expr.cc:337
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: expr.hh:180