| 1 | #include <iomanip> |
| 2 | #include <Parsers/ASTInsertQuery.h> |
| 3 | #include <Parsers/ASTFunction.h> |
| 4 | #include <Common/quoteString.h> |
| 5 | |
| 6 | |
| 7 | namespace DB |
| 8 | { |
| 9 | |
| 10 | namespace ErrorCodes |
| 11 | { |
| 12 | extern const int INVALID_USAGE_OF_INPUT; |
| 13 | } |
| 14 | |
| 15 | |
| 16 | void ASTInsertQuery::formatImpl(const FormatSettings & settings, FormatState & state, FormatStateStacked frame) const |
| 17 | { |
| 18 | frame.need_parens = false; |
| 19 | |
| 20 | settings.ostr << (settings.hilite ? hilite_keyword : "" ) << "INSERT INTO " ; |
| 21 | if (table_function) |
| 22 | { |
| 23 | settings.ostr << (settings.hilite ? hilite_keyword : "" ) << "FUNCTION " ; |
| 24 | table_function->formatImpl(settings, state, frame); |
| 25 | } |
| 26 | else |
| 27 | settings.ostr << (settings.hilite ? hilite_none : "" ) |
| 28 | << (!database.empty() ? backQuoteIfNeed(database) + "." : "" ) << backQuoteIfNeed(table); |
| 29 | |
| 30 | if (columns) |
| 31 | { |
| 32 | settings.ostr << " (" ; |
| 33 | columns->formatImpl(settings, state, frame); |
| 34 | settings.ostr << ")" ; |
| 35 | } |
| 36 | |
| 37 | if (select) |
| 38 | { |
| 39 | settings.ostr << " " ; |
| 40 | select->formatImpl(settings, state, frame); |
| 41 | } |
| 42 | else |
| 43 | { |
| 44 | if (!format.empty()) |
| 45 | { |
| 46 | settings.ostr << (settings.hilite ? hilite_keyword : "" ) << " FORMAT " << (settings.hilite ? hilite_none : "" ) << format; |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | settings.ostr << (settings.hilite ? hilite_keyword : "" ) << " VALUES" << (settings.hilite ? hilite_none : "" ); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | if (settings_ast) |
| 55 | { |
| 56 | settings.ostr << (settings.hilite ? hilite_keyword : "" ) << "SETTINGS " << (settings.hilite ? hilite_none : "" ); |
| 57 | settings_ast->formatImpl(settings, state, frame); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | static void tryFindInputFunctionImpl(const ASTPtr & ast, ASTPtr & input_function) |
| 63 | { |
| 64 | if (!ast) |
| 65 | return; |
| 66 | for (const auto & child : ast->children) |
| 67 | tryFindInputFunctionImpl(child, input_function); |
| 68 | |
| 69 | if (const auto * table_function_ast = ast->as<ASTFunction>()) |
| 70 | { |
| 71 | if (table_function_ast->name == "input" ) |
| 72 | { |
| 73 | if (input_function) |
| 74 | throw Exception("You can use 'input()' function only once per request." , ErrorCodes::INVALID_USAGE_OF_INPUT); |
| 75 | input_function = ast; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | |
| 81 | void ASTInsertQuery::tryFindInputFunction(ASTPtr & input_function) const |
| 82 | { |
| 83 | tryFindInputFunctionImpl(select, input_function); |
| 84 | } |
| 85 | |
| 86 | } |
| 87 | |