| 1 | #pragma once |
|---|---|
| 2 | |
| 3 | #include <Parsers/IAST.h> |
| 4 | |
| 5 | |
| 6 | namespace DB |
| 7 | { |
| 8 | |
| 9 | /** Query with output options |
| 10 | * (supporting [INTO OUTFILE 'file_name'] [FORMAT format_name] [SETTINGS key1 = value1, key2 = value2, ...] suffix). |
| 11 | */ |
| 12 | class ASTQueryWithOutput : public IAST |
| 13 | { |
| 14 | public: |
| 15 | ASTPtr out_file; |
| 16 | ASTPtr format; |
| 17 | ASTPtr settings_ast; |
| 18 | |
| 19 | void formatImpl(const FormatSettings & s, FormatState & state, FormatStateStacked frame) const final; |
| 20 | |
| 21 | /// Remove 'FORMAT <fmt> and INTO OUTFILE <file>' if exists |
| 22 | static bool resetOutputASTIfExist(IAST & ast); |
| 23 | |
| 24 | protected: |
| 25 | /// NOTE: call this helper at the end of the clone() method of descendant class. |
| 26 | void cloneOutputOptions(ASTQueryWithOutput & cloned) const; |
| 27 | |
| 28 | /// Format only the query part of the AST (without output options). |
| 29 | virtual void formatQueryImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const = 0; |
| 30 | }; |
| 31 | |
| 32 | |
| 33 | /** Helper template for simple queries like SHOW PROCESSLIST. |
| 34 | */ |
| 35 | template <typename ASTIDAndQueryNames> |
| 36 | class ASTQueryWithOutputImpl : public ASTQueryWithOutput |
| 37 | { |
| 38 | public: |
| 39 | String getID(char) const override { return ASTIDAndQueryNames::ID; } |
| 40 | |
| 41 | ASTPtr clone() const override |
| 42 | { |
| 43 | auto res = std::make_shared<ASTQueryWithOutputImpl<ASTIDAndQueryNames>>(*this); |
| 44 | res->children.clear(); |
| 45 | cloneOutputOptions(*res); |
| 46 | return res; |
| 47 | } |
| 48 | |
| 49 | protected: |
| 50 | void formatQueryImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const override |
| 51 | { |
| 52 | settings.ostr << (settings.hilite ? hilite_keyword : "") |
| 53 | << ASTIDAndQueryNames::Query << (settings.hilite ? hilite_none : ""); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | } |
| 58 |