SQLsmith  v1.2.1-5-gfacd7a8
A random SQL query generator
grammar.hh
Go to the documentation of this file.
1 
4 #ifndef GRAMMAR_HH
5 #define GRAMMAR_HH
6 
7 #include <ostream>
8 #include "relmodel.hh"
9 #include <memory>
10 #include "schema.hh"
11 
12 #include "prod.hh"
13 #include "expr.hh"
14 
15 using std::shared_ptr;
16 
17 struct table_ref : prod {
18  vector<shared_ptr<named_relation> > refs;
19  static shared_ptr<table_ref> factory(prod *p);
20  table_ref(prod *p) : prod(p) { }
21  virtual ~table_ref() { }
22 };
23 
25  virtual void out(std::ostream &out);
27  virtual ~table_or_query_name() { }
28  named_relation *t;
29 };
30 
32  virtual void out(std::ostream &out);
33  target_table(prod *p, table *victim = 0);
34  virtual ~target_table() { }
35  table *victim_;
36 };
37 
39  virtual void out(std::ostream &out);
40  table_sample(prod *p);
41  virtual ~table_sample() { }
42  struct table *t;
43 private:
44  string method;
45  double percent;
46 };
47 
49  bool is_lateral;
50  virtual void out(std::ostream &out);
51  shared_ptr<struct query_spec> query;
52  table_subquery(prod *p, bool lateral = false);
53  virtual ~table_subquery();
54  virtual void accept(prod_visitor *v);
55 };
56 
59  : table_subquery(p, true) { }
60 };
61 
62 struct join_cond : prod {
63  static shared_ptr<join_cond> factory(prod *p, table_ref &lhs, table_ref &rhs);
64  join_cond(prod *p, table_ref &lhs, table_ref &rhs)
65  : prod(p) { (void) lhs; (void) rhs;}
66 };
67 
69  std::string condition;
70  simple_join_cond(prod *p, table_ref &lhs, table_ref &rhs);
71  virtual void out(std::ostream &out);
72 };
73 
75  struct scope joinscope;
76  shared_ptr<bool_expr> search;
77  expr_join_cond(prod *p, table_ref &lhs, table_ref &rhs);
78  virtual void out(std::ostream &out);
79  virtual void accept(prod_visitor *v) {
80  search->accept(v);
81  v->visit(this);
82  }
83 };
84 
86  virtual void out(std::ostream &out);
87  joined_table(prod *p);
88  std::string type;
89  std::string alias;
90  virtual std::string ident() { return alias; }
91  shared_ptr<table_ref> lhs;
92  shared_ptr<table_ref> rhs;
93  shared_ptr<join_cond> condition;
94  virtual ~joined_table() {
95  }
96  virtual void accept(prod_visitor *v) {
97  lhs->accept(v);
98  rhs->accept(v);
99  condition->accept(v);
100  v->visit(this);
101  }
102 };
103 
104 struct from_clause : prod {
105  std::vector<shared_ptr<table_ref> > reflist;
106  virtual void out(std::ostream &out);
107  from_clause(prod *p);
108  ~from_clause() { }
109  virtual void accept(prod_visitor *v) {
110  v->visit(this);
111  for (auto p : reflist)
112  p->accept(v);
113  }
114 };
115 
116 struct select_list : prod {
117  std::vector<shared_ptr<value_expr> > value_exprs;
118  relation derived_table;
119  int columns = 0;
120  select_list(prod *p);
121  virtual void out(std::ostream &out);
122  ~select_list() { }
123  virtual void accept(prod_visitor *v) {
124  v->visit(this);
125  for (auto p : value_exprs)
126  p->accept(v);
127  }
128 };
129 
130 struct query_spec : prod {
131  std::string set_quantifier;
132  shared_ptr<struct from_clause> from_clause;
133  shared_ptr<struct select_list> select_list;
134  shared_ptr<bool_expr> search;
135  std::string limit_clause;
136  struct scope myscope;
137  virtual void out(std::ostream &out);
138  query_spec(prod *p, struct scope *s, bool lateral = 0);
139  virtual void accept(prod_visitor *v) {
140  v->visit(this);
141  select_list->accept(v);
142  from_clause->accept(v);
143  search->accept(v);
144  }
145 };
146 
148  const char *lockmode;
149  virtual void out(std::ostream &out);
150  select_for_update(prod *p, struct scope *s, bool lateral = 0);
151 };
152 
153 struct prepare_stmt : prod {
154  query_spec q;
155  static long seq;
156  long id;
157  virtual void out(std::ostream &out) {
158  out << "prepare prep" << id << " as " << q;
159  }
160  prepare_stmt(prod *p) : prod(p), q(p, scope) {
161  id = seq++;
162  }
163  virtual void accept(prod_visitor *v) {
164  v->visit(this);
165  q.accept(v);
166  }
167 };
168 
170  table *victim;
171  struct scope myscope;
172  modifying_stmt(prod *p, struct scope *s, struct table *victim = 0);
173 // shared_ptr<modifying_stmt> modifying_stmt::factory(prod *p, struct scope *s);
174  virtual void pick_victim();
175 };
176 
178  shared_ptr<bool_expr> search;
179  delete_stmt(prod *p, struct scope *s, table *v);
180  virtual ~delete_stmt() { }
181  virtual void out(std::ostream &out) {
182  out << "delete from " << victim->ident();
183  indent(out);
184  out << "where " << std::endl << *search;
185  }
186  virtual void accept(prod_visitor *v) {
187  v->visit(this);
188  search->accept(v);
189  }
190 };
191 
193  shared_ptr<struct select_list> select_list;
194  delete_returning(prod *p, struct scope *s, table *victim = 0);
195  virtual void out(std::ostream &out) {
197  out << std::endl << "returning " << *select_list;
198  }
199  virtual void accept(prod_visitor *v) {
200  v->visit(this);
201  search->accept(v);
202  select_list->accept(v);
203  }
204 };
205 
207  vector<shared_ptr<value_expr> > value_exprs;
208  insert_stmt(prod *p, struct scope *s, table *victim = 0);
209  virtual ~insert_stmt() { }
210  virtual void out(std::ostream &out);
211  virtual void accept(prod_visitor *v) {
212  v->visit(this);
213  for (auto p : value_exprs) p->accept(v);
214  }
215 };
216 
217 struct set_list : prod {
218  vector<shared_ptr<value_expr> > value_exprs;
219  vector<string> names;
220  set_list(prod *p, table *target);
221  virtual ~set_list() { }
222  virtual void out(std::ostream &out);
223  virtual void accept(prod_visitor *v) {
224  v->visit(this);
225  for (auto p : value_exprs) p->accept(v);
226  }
227 };
228 
230  shared_ptr<struct set_list> set_list;
231  string constraint;
232  shared_ptr<bool_expr> search;
233  upsert_stmt(prod *p, struct scope *s, table *v = 0);
234  virtual void out(std::ostream &out) {
236  out << " on conflict on constraint " << constraint << " do update ";
237  out << *set_list << " where " << *search;
238  }
239  virtual void accept(prod_visitor *v) {
241  set_list->accept(v);
242  search->accept(v);
243  }
244  virtual ~upsert_stmt() { }
245 };
246 
248  shared_ptr<bool_expr> search;
249  shared_ptr<struct set_list> set_list;
250  update_stmt(prod *p, struct scope *s, table *victim = 0);
251  virtual ~update_stmt() { }
252  virtual void out(std::ostream &out);
253  virtual void accept(prod_visitor *v) {
254  v->visit(this);
255  search->accept(v);
256  }
257 };
258 
259 struct when_clause : prod {
260  bool matched;
261  shared_ptr<bool_expr> condition;
262 // shared_ptr<prod> merge_action;
263  when_clause(struct merge_stmt *p);
264  virtual ~when_clause() { }
265  static shared_ptr<when_clause> factory(struct merge_stmt *p);
266  virtual void out(std::ostream &out);
267  virtual void accept(prod_visitor *v);
268 };
269 
271  shared_ptr<struct set_list> set_list;
272  struct scope myscope;
273  when_clause_update(struct merge_stmt *p);
274  virtual ~when_clause_update() { }
275  virtual void out(std::ostream &out);
276  virtual void accept(prod_visitor *v);
277 };
278 
280  vector<shared_ptr<value_expr> > exprs;
281  when_clause_insert(struct merge_stmt *p);
282  virtual ~when_clause_insert() { }
283  virtual void out(std::ostream &out);
284  virtual void accept(prod_visitor *v);
285 };
286 
288  merge_stmt(prod *p, struct scope *s, table *victim = 0);
289  shared_ptr<table_ref> target_table_;
290  shared_ptr<table_ref> data_source;
291  shared_ptr<join_cond> join_condition;
292  vector<shared_ptr<when_clause> > clauselist;
293  virtual ~merge_stmt() { }
294  virtual void out(std::ostream &out);
295  virtual void accept(prod_visitor *v);
296 };
297 
299  shared_ptr<struct select_list> select_list;
300  update_returning(prod *p, struct scope *s, table *victim = 0);
301  virtual void out(std::ostream &out) {
303  out << std::endl << "returning " << *select_list;
304  }
305  virtual void accept(prod_visitor *v) {
306  v->visit(this);
307  search->accept(v);
308  set_list->accept(v);
309  select_list->accept(v);
310  }
311 };
312 
313 shared_ptr<prod> statement_factory(struct scope *s);
314 
316  vector<shared_ptr<prod> > with_queries;
317  shared_ptr<prod> query;
318  vector<shared_ptr<named_relation> > refs;
319  struct scope myscope;
320  virtual void out(std::ostream &out);
321  virtual void accept(prod_visitor *v);
322  common_table_expression(prod *parent, struct scope *s);
323 };
324 
325 #endif
grammar: Value expression productions
Base class for grammar productions.
supporting classes for the grammar
Base class providing schema information to grammar.
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:527
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.cc:490
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.hh:195
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.hh:199
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.hh:186
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.hh:181
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:149
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.hh:79
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.hh:109
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:187
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:386
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.hh:211
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:173
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.hh:96
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.cc:574
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:560
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.hh:163
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.hh:157
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
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: grammar.cc:240
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.hh:139
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:306
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.hh:123
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:228
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:421
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.hh:223
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:134
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:38
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:74
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.cc:91
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:181
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:56
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.hh:301
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.hh:305
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:440
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.hh:253
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.hh:234
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.hh:239
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.cc:658
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:642
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.cc:625
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:619
virtual void out(std::ostream &out)
Emit SQL for this production.
Definition: grammar.cc:592
virtual void accept(prod_visitor *v)
Visitor pattern for walking the AST.
Definition: grammar.cc:602