aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Parsers/formatAST.h
blob: dd72a59b4a2426d0dc99ba113968930c92c448b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once

#include <Parsers/IAST.h>


namespace DB
{

class WriteBuffer;

/// 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.
void formatAST(const IAST & ast, WriteBuffer & buf, bool hilite = true, bool one_line = false, bool show_secrets = true);

/// Like formatAST() but intended for serialization w/o pretty-printing (single-line, no hiliting).
String serializeAST(const IAST & ast);

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;
}

}

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));
    }
};