// Copyright 2023 PingCAP, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #pragma once #include namespace DB { /** Query with output options (supporting [INTO OUTFILE 'file_name'] [FORMAT format_name] suffix). */ class ASTQueryWithOutput : public IAST { public: ASTPtr out_file; ASTPtr format; void formatImpl(const FormatSettings & s, FormatState & state, FormatStateStacked frame) const final; /// Remove 'FORMAT and INTO OUTFILE ' if exists static bool resetOutputASTIfExist(IAST & ast); protected: /// NOTE: call this helper at the end of the clone() method of descendant class. void cloneOutputOptions(ASTQueryWithOutput & cloned) const; /// Format only the query part of the AST (without output options). virtual void formatQueryImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const = 0; }; /** Helper template for simple queries like SHOW PROCESSLIST. */ template class ASTQueryWithOutputImpl : public ASTQueryWithOutput { public: String getID() const override { return ASTIDAndQueryNames::ID; }; ASTPtr clone() const override { auto res = std::make_shared>(*this); res->children.clear(); cloneOutputOptions(*res); return res; } protected: void formatQueryImpl(const FormatSettings & settings, FormatState &, FormatStateStacked) const override { settings.ostr << (settings.hilite ? hilite_keyword : "") << ASTIDAndQueryNames::Query << (settings.hilite ? hilite_none : ""); } }; } // namespace DB