2010-06-28 13:44:42 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include <boost/variant/static_visitor.hpp>
|
|
|
|
|
|
|
|
#include <Poco/NumberFormatter.h>
|
|
|
|
|
2011-03-29 17:56:51 +00:00
|
|
|
#include <mysqlxx/Manip.h>
|
2010-06-28 13:44:42 +00:00
|
|
|
|
|
|
|
#include <DB/Core/Exception.h>
|
|
|
|
#include <DB/Core/ErrorCodes.h>
|
|
|
|
|
|
|
|
#include <DB/Parsers/formatAST.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
void formatAST(const IAST & ast, std::ostream & s)
|
|
|
|
{
|
|
|
|
const ASTSelectQuery * select = dynamic_cast<const ASTSelectQuery *>(&ast);
|
|
|
|
if (select)
|
|
|
|
{
|
|
|
|
formatAST(*select, s);
|
|
|
|
return;
|
|
|
|
}
|
2011-08-18 18:48:00 +00:00
|
|
|
|
|
|
|
const ASTCreateQuery * create = dynamic_cast<const ASTCreateQuery *>(&ast);
|
|
|
|
if (create)
|
|
|
|
{
|
|
|
|
formatAST(*create, s);
|
|
|
|
return;
|
|
|
|
}
|
2010-06-28 13:44:42 +00:00
|
|
|
|
|
|
|
const ASTExpressionList * exp_list = dynamic_cast<const ASTExpressionList *>(&ast);
|
|
|
|
if (exp_list)
|
|
|
|
{
|
|
|
|
formatAST(*exp_list, s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ASTFunction * func = dynamic_cast<const ASTFunction *>(&ast);
|
|
|
|
if (func)
|
|
|
|
{
|
|
|
|
formatAST(*func, s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ASTIdentifier * id = dynamic_cast<const ASTIdentifier *>(&ast);
|
|
|
|
if (id)
|
|
|
|
{
|
|
|
|
formatAST(*id, s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ASTLiteral * lit = dynamic_cast<const ASTLiteral *>(&ast);
|
|
|
|
if (lit)
|
|
|
|
{
|
|
|
|
formatAST(*lit, s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-08-18 18:48:00 +00:00
|
|
|
const ASTNameTypePair * ntp = dynamic_cast<const ASTNameTypePair *>(&ast);
|
|
|
|
if (ntp)
|
|
|
|
{
|
|
|
|
formatAST(*ntp, s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-06-28 13:44:42 +00:00
|
|
|
throw DB::Exception("Unknown element in AST", ErrorCodes::UNKNOWN_ELEMENT_IN_AST);
|
|
|
|
}
|
|
|
|
|
|
|
|
void formatAST(const ASTSelectQuery & ast, std::ostream & s)
|
|
|
|
{
|
|
|
|
s << "SELECT ";
|
2011-08-28 00:31:30 +00:00
|
|
|
formatAST(*ast.select_expression_list, s);
|
|
|
|
|
|
|
|
if (ast.table)
|
|
|
|
{
|
|
|
|
s << " FROM ";
|
|
|
|
if (ast.database)
|
|
|
|
{
|
|
|
|
formatAST(*ast.database, s);
|
|
|
|
s << ".";
|
|
|
|
}
|
|
|
|
formatAST(*ast.table, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ast.where_expression)
|
|
|
|
{
|
|
|
|
s << " WHERE ";
|
|
|
|
formatAST(*ast.where_expression, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ast.group_expression_list)
|
|
|
|
{
|
|
|
|
s << " GROUP BY ";
|
|
|
|
formatAST(*ast.group_expression_list, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ast.having_expression)
|
|
|
|
{
|
|
|
|
s << " HAVING ";
|
|
|
|
formatAST(*ast.having_expression, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ast.order_expression_list)
|
|
|
|
{
|
|
|
|
s << " ORDER BY ";
|
|
|
|
formatAST(*ast.order_expression_list, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ast.limit_length)
|
|
|
|
{
|
|
|
|
s << " LIMIT ";
|
|
|
|
if (ast.limit_offset)
|
|
|
|
{
|
|
|
|
formatAST(*ast.limit_offset, s);
|
|
|
|
s << ", ";
|
|
|
|
}
|
|
|
|
formatAST(*ast.limit_length, s);
|
|
|
|
}
|
2010-06-28 13:44:42 +00:00
|
|
|
}
|
|
|
|
|
2011-08-18 18:48:00 +00:00
|
|
|
void formatAST(const ASTCreateQuery & ast, std::ostream & s)
|
|
|
|
{
|
|
|
|
s << (ast.attach ? "ATTACH TABLE " : "CREATE TABLE ") << ast.name << " (";
|
|
|
|
formatAST(*ast.columns, s);
|
|
|
|
s << ") ENGINE = ";
|
|
|
|
formatAST(*ast.storage, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void formatAST(const ASTExpressionList & ast, std::ostream & s)
|
2010-06-28 13:44:42 +00:00
|
|
|
{
|
|
|
|
for (ASTs::const_iterator it = ast.children.begin(); it != ast.children.end(); ++it)
|
|
|
|
{
|
|
|
|
if (it != ast.children.begin())
|
|
|
|
s << ", ";
|
|
|
|
formatAST(**it, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void formatAST(const ASTFunction & ast, std::ostream & s)
|
|
|
|
{
|
2011-08-18 18:48:00 +00:00
|
|
|
s << ast.name;
|
|
|
|
if (ast.arguments)
|
|
|
|
{
|
|
|
|
s << '(';
|
|
|
|
formatAST(*ast.arguments, s);
|
|
|
|
s << ')';
|
|
|
|
}
|
2010-06-28 13:44:42 +00:00
|
|
|
}
|
|
|
|
|
2011-08-18 18:48:00 +00:00
|
|
|
void formatAST(const ASTIdentifier & ast, std::ostream & s)
|
2010-06-28 13:44:42 +00:00
|
|
|
{
|
|
|
|
s << ast.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void formatAST(const ASTLiteral & ast, std::ostream & s)
|
|
|
|
{
|
|
|
|
s << boost::apply_visitor(FieldVisitorToString(), ast.value);
|
|
|
|
}
|
|
|
|
|
2011-08-18 18:48:00 +00:00
|
|
|
void formatAST(const ASTNameTypePair & ast, std::ostream & s)
|
|
|
|
{
|
|
|
|
s << ast.name << " ";
|
|
|
|
formatAST(*ast.type, s);
|
|
|
|
}
|
|
|
|
|
2010-06-28 13:44:42 +00:00
|
|
|
}
|
|
|
|
|