SQLsmith  v1.2.1-5-gfacd7a8
A random SQL query generator
relmodel.hh
Go to the documentation of this file.
1 
4 #ifndef RELMODEL_HH
5 #define RELMODEL_HH
6 #include <string>
7 #include <vector>
8 #include <map>
9 #include <utility>
10 #include <memory>
11 #include <cassert>
12 
13 using std::string;
14 using std::vector;
15 using std::map;
16 using std::pair;
17 using std::make_pair;
18 using std::shared_ptr;
19 
20 struct sqltype {
21  string name;
22  static map<string, struct sqltype*> typemap;
23  static struct sqltype *get(string s);
24  sqltype(string n) : name(n) { }
25 
34  virtual bool consistent(struct sqltype *rvalue);
35 };
36 
37 struct column {
38  string name;
39  sqltype *type;
40  column(string name) : name(name) { }
41  column(string name, sqltype *t) : name(name), type(t) {
42  assert(t);
43  }
44 };
45 
46 struct relation {
47  vector<column> cols;
48  virtual vector<column> &columns() { return cols; }
49 };
50 
52  string name;
53  virtual string ident() { return name; }
54  virtual ~named_relation() { }
55  named_relation(string n) : name(n) { }
56 };
57 
59  relation *rel;
60  virtual ~aliased_relation() { }
61  aliased_relation(string n, relation* r) : named_relation(n), rel(r) { }
62  virtual vector<column>& columns() { return rel->columns(); }
63 };
64 
66  string schema;
67  bool is_insertable;
68  bool is_base_table;
69  vector<string> constraints;
70  table(string name, string schema, bool insertable, bool base_table)
71  : named_relation(name),
72  schema(schema),
73  is_insertable(insertable),
74  is_base_table(base_table) { }
75  virtual string ident() { return schema + "." + name; }
76  virtual ~table() { };
77 };
78 
79 struct scope {
80  struct scope *parent;
82  vector<named_relation*> tables;
84  vector<named_relation*> refs;
85  struct schema *schema;
87  shared_ptr<map<string,unsigned int> > stmt_seq;
88  scope(struct scope *parent = 0) : parent(parent) {
89  if (parent) {
90  schema = parent->schema;
91  tables = parent->tables;
92  refs = parent->refs;
93  stmt_seq = parent->stmt_seq;
94  }
95  }
96  vector<pair<named_relation*, column> > refs_of_type(sqltype *t) {
97  vector<pair<named_relation*, column> > result;
98  for (auto r : refs)
99  for (auto c : r->columns())
100  if (t->consistent(c.type))
101  result.push_back(make_pair(r,c));
102  return result;
103  }
105  string stmt_uid(const char* prefix) {
106  string result(prefix);
107  result += "_";
108  result += std::to_string((*stmt_seq)[result]++);
109  return result;
110  }
112  void new_stmt() {
113  stmt_seq = std::make_shared<map<string,unsigned int> >();
114  }
115 };
116 
117 struct op {
118  string name;
119  sqltype *left;
120  sqltype *right;
121  sqltype *result;
122  op(string n,sqltype *l,sqltype *r, sqltype *res)
123  : name(n), left(l), right(r), result(res) { }
124  op() { }
125 };
126 
127 struct routine {
128  string specific_name;
129  string schema;
130  vector<sqltype *> argtypes;
131  sqltype *restype;
132  string name;
133  routine(string schema, string specific_name, sqltype* data_type, string name)
134  : specific_name(specific_name), schema(schema), restype(data_type), name(name) {
135  assert(data_type);
136  }
137  virtual string ident() {
138  if (schema.size())
139  return schema + "." + name;
140  else
141  return name;
142  }
143 };
144 
145 #endif
Definition: relmodel.hh:117
Definition: schema.hh:16
void new_stmt()
Reset unique identifier counters.
Definition: relmodel.hh:112
vector< named_relation * > tables
available to table_ref productions
Definition: relmodel.hh:82
vector< named_relation * > refs
available to column_ref productions
Definition: relmodel.hh:84
shared_ptr< map< string, unsigned int > > stmt_seq
Counters for prefixed stmt-unique identifiers.
Definition: relmodel.hh:87
string stmt_uid(const char *prefix)
Generate unique identifier with prefix.
Definition: relmodel.hh:105
virtual bool consistent(struct sqltype *rvalue)
This function is used to model postgres-style pseudotypes.
Definition: relmodel.cc:13