ClickHouse/src/Parsers/formatAST.h

49 lines
1.1 KiB
C++
Raw Normal View History

2011-09-04 05:14:52 +00:00
#pragma once
2010-06-28 13:44:42 +00:00
#include <Parsers/IAST.h>
2010-06-28 13:44:42 +00:00
namespace DB
{
2020-11-09 16:05:40 +00:00
class WriteBuffer;
2023-07-19 18:02:09 +00:00
/// Takes a syntax tree and turns it into text.
/// Intended for pretty-printing (multi-line + hiliting).
/// In case of INSERT query, the data will be missing.
2023-07-19 12:54:25 +00:00
void formatAST(const IAST & ast, WriteBuffer & buf, bool hilite = true, bool one_line = false, bool show_secrets = true);
2010-06-28 13:44:42 +00:00
2023-07-19 18:02:09 +00:00
/// Like formatAST() but intended for serialization w/o pretty-printing (single-line, no hiliting).
String serializeAST(const IAST & ast);
2020-11-10 18:22:26 +00:00
inline WriteBuffer & operator<<(WriteBuffer & buf, const IAST & ast)
{
formatAST(ast, buf, false, true);
return buf;
}
inline WriteBuffer & operator<<(WriteBuffer & buf, const ASTPtr & ast)
{
formatAST(*ast, buf, false, true);
return buf;
}
2014-10-01 16:38:04 +00:00
2010-06-28 13:44:42 +00:00
}
2020-11-22 17:13:40 +00:00
template<>
struct fmt::formatter<DB::ASTPtr>
{
template<typename ParseContext>
constexpr auto parse(ParseContext & context)
{
return context.begin();
}
template<typename FormatContext>
auto format(const DB::ASTPtr & ast, FormatContext & context)
{
return fmt::format_to(context.out(), "{}", DB::serializeAST(*ast));
}
};