12void ASTPrinter::operator()(Const<ErrorExpr>&)
14 out <<
"[error-expression]";
17void ASTPrinter::operator()(Const<Designator> &e)
19 if (e.has_explicit_table_name())
20 out << e.get_table_name() <<
'.';
21 out << e.attr_name.text;
24void ASTPrinter::operator()(Const<Constant> &e)
29void ASTPrinter::operator()(Const<FnApplicationExpr> &e)
33 for (
auto it = e.args.cbegin(), end = e.args.cend(); it != end; ++it) {
34 if (it != e.args.cbegin())
out <<
", ";
40void ASTPrinter::operator()(Const<UnaryExpr> &e)
42 out <<
'(' << e.op().text;
43 if (e.op() == TK_Not)
out <<
' ';
48void ASTPrinter::operator()(Const<BinaryExpr> &e)
52 out <<
' ' << e.op().text <<
' ';
57void ASTPrinter::operator()(Const<QueryExpr> &e)
64 out << e.alias() <<
'.' << e.alias();
70void ASTPrinter::operator()(Const<ErrorClause>&)
72 out <<
"[error-clause]";
75void ASTPrinter::operator()(Const<SelectClause> &c)
78 if (c.select_all)
out <<
'*';
79 for (
auto it = c.select.cbegin(), end = c.select.cend(); it != end; ++it) {
80 if (c.select_all or it != c.select.cbegin())
out <<
", ";
82 if (it->second)
out <<
" AS " << it->second.text;
86void ASTPrinter::operator()(Const<FromClause> &c)
89 for (
auto it = c.from.cbegin(), end = c.from.cend(); it != end; ++it) {
90 if (it != c.from.cbegin())
out <<
", ";
91 if (
auto tok = std::get_if<Token>(&it->source)) {
93 }
else if (
auto stmt = std::get_if<Stmt*>(&it->source)) {
100 if (it->alias)
out <<
" AS " << it->alias.text;
104void ASTPrinter::operator()(Const<WhereClause> &c)
106 out <<
"WHERE " << *c.where;
109void ASTPrinter::operator()(Const<GroupByClause> &c)
112 for (
auto it = c.group_by.cbegin(), end = c.group_by.cend(); it != end; ++it) {
113 if (it != c.group_by.cbegin())
out <<
", ";
114 auto &[grp, alias] = *it;
117 out <<
" AS " << alias.text;
121void ASTPrinter::operator()(Const<HavingClause> &c)
123 out <<
"HAVING " << *c.having;
126void ASTPrinter::operator()(Const<OrderByClause> &c)
129 for (
auto it = c.order_by.cbegin(), end = c.order_by.cend(); it != end; ++it) {
130 if (it != c.order_by.cbegin())
out <<
", ";
132 if (it->second)
out <<
" ASC";
137void ASTPrinter::operator()(Const<LimitClause> &c)
139 out <<
"LIMIT " << c.limit.text;
141 out <<
" OFFSET " << c.offset.text;
146void ASTPrinter::operator()(Const<PrimaryKeyConstraint>&)
148 out <<
"PRIMARY KEY";
151void ASTPrinter::operator()(Const<UniqueConstraint>&)
156void ASTPrinter::operator()(Const<NotNullConstraint>&)
161void ASTPrinter::operator()(Const<CheckConditionConstraint> &c)
163 out <<
"CHECK (" << *c.cond <<
')';
166void ASTPrinter::operator()(Const<ReferenceConstraint> &c)
168 out <<
"REFERENCES " << c.table_name.text <<
'(' << c.attr_name.text <<
')';
174void ASTPrinter::operator()(Const<Instruction> &inst)
176 out << inst.tok.text;
182void ASTPrinter::operator()(Const<ErrorStmt>&)
184 out <<
"[error-statement];";
187void ASTPrinter::operator()(Const<EmptyStmt>&)
192void ASTPrinter::operator()(Const<CreateDatabaseStmt> &s)
194 out <<
"CREATE DATABASE " << s.database_name.text <<
';';
197void ASTPrinter::operator()(Const<DropDatabaseStmt> &s)
199 out <<
"DROP DATABASE ";
202 out << s.database_name.text <<
';';
205void ASTPrinter::operator()(Const<UseDatabaseStmt> &s)
207 out <<
"USE " << s.database_name.text <<
';';
210void ASTPrinter::operator()(Const<CreateTableStmt> &s)
212 out <<
"CREATE TABLE " << s.table_name.text <<
"\n(";
213 for (
auto it = s.attributes.cbegin(), end = s.attributes.cend(); it != end; ++it) {
215 if (it != s.attributes.cbegin())
out <<
',';
216 out <<
"\n " << attr->name.text <<
' ' << *attr->type;
217 for (
auto &c : attr->constraints) {
225void ASTPrinter::operator()(Const<DropTableStmt> &s)
227 out <<
"DROP TABLE ";
230 for (
auto it = s.table_names.cbegin(), end = s.table_names.cend(); it != end; ++it) {
231 auto &table_name = *it;
232 if (it != s.table_names.cbegin())
out <<
", ";
233 out << table_name->text;
238void ASTPrinter::operator()(Const<CreateIndexStmt> &s)
244 if (s.has_if_not_exists)
245 out <<
"IF NOT EXISTS ";
247 out << s.index_name.text <<
' ';
248 out <<
"ON " << s.table_name.text;
250 out <<
" USING " << s.method.text;
252 for (
auto it = s.key_fields.cbegin(), end = s.key_fields.cend(); it != end; ++it) {
254 if (it != s.key_fields.cbegin())
out <<
',';
261void ASTPrinter::operator()(Const<DropIndexStmt> &s)
263 out <<
"DROP INDEX ";
266 for (
auto it = s.index_names.cbegin(), end = s.index_names.cend(); it != end; ++it) {
268 if (it != s.index_names.cbegin())
out <<
", ";
274void ASTPrinter::operator()(Const<SelectStmt> &s)
292 (*this)(*s.group_by);
300 (*this)(*s.order_by);
312void ASTPrinter::operator()(Const<InsertStmt> &s)
314 out <<
"INSERT INTO " << s.table_name.text <<
"\nVALUES\n ";
315 for (
auto value_it = s.tuples.cbegin(), end = s.tuples.cend(); value_it != end; ++value_it) {
316 if (value_it != s.tuples.cbegin())
out <<
",\n ";
318 for (
auto elem_it = value_it->cbegin(), elem_end = value_it->cend(); elem_it != elem_end; ++elem_it) {
319 if (elem_it != value_it->cbegin())
out <<
", ";
320 switch (elem_it->first) {
331void ASTPrinter::operator()(Const<UpdateStmt> &s)
333 out <<
"UPDATE " << s.table_name.text <<
"\nSET\n";
334 for (
auto it = s.set.cbegin(), end = s.set.cend(); it != end; ++it) {
335 if (it != s.set.cbegin())
out <<
",\n";
336 out <<
" " << it->first.text <<
" = " << *it->second;
338 if (s.where)
out <<
'\n' << *s.where;
342void ASTPrinter::operator()(Const<DeleteStmt> &s)
344 out <<
"DELETE FROM " << s.table_name.text;
345 if (s.where)
out <<
'\n' << *s.where;
349void ASTPrinter::operator()(Const<DSVImportStmt> &s)
351 out <<
"IMPORT INTO " << s.table_name.text <<
" DSV " << s.path.text;
353 out <<
" ROWS " << s.rows.text;
355 out <<
" DELIMITER " << s.delimiter.text;
357 out <<
" HAS HEADER";
359 out <<
" SKIP HEADER";
#define M_unreachable(MSG)
bool is_nested_
whether the statement is nested; determines whether a final ';' must be printed
std::ostream & out
the output stream to write to
bool expand_nested_queries_
determines whether nested queries are printed