mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-21 15:12:02 +00:00
Merge pull request #10852 from dgrr/format/multiple-queries
Added multiple query formatting on clickhouse-format
This commit is contained in:
commit
b3d8c09bfb
@ -21,6 +21,7 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
|
||||
("hilite", "add syntax highlight with ANSI terminal escape sequences")
|
||||
("oneline", "format in single line")
|
||||
("quiet,q", "just check syntax, no output on success")
|
||||
("multiquery,n", "allow multiple queries in the same file")
|
||||
;
|
||||
|
||||
boost::program_options::variables_map options;
|
||||
@ -38,6 +39,7 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
|
||||
bool hilite = options.count("hilite");
|
||||
bool oneline = options.count("oneline");
|
||||
bool quiet = options.count("quiet");
|
||||
bool multiple = options.count("multiquery");
|
||||
|
||||
if (quiet && (hilite || oneline))
|
||||
{
|
||||
@ -53,13 +55,17 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
|
||||
const char * end = pos + query.size();
|
||||
|
||||
ParserQuery parser(end);
|
||||
ASTPtr res = parseQuery(parser, pos, end, "query", 0, DBMS_DEFAULT_MAX_PARSER_DEPTH);
|
||||
|
||||
if (!quiet)
|
||||
do
|
||||
{
|
||||
formatAST(*res, std::cout, hilite, oneline);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
ASTPtr res = parseQueryAndMovePosition(parser, pos, end, "query", multiple, 0, DBMS_DEFAULT_MAX_PARSER_DEPTH);
|
||||
if (!quiet)
|
||||
{
|
||||
formatAST(*res, std::cout, hilite, oneline);
|
||||
if (multiple)
|
||||
std::cout << "\n;\n";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
} while (multiple && pos != end);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
|
@ -0,0 +1,16 @@
|
||||
SELECT
|
||||
a,
|
||||
b AS x
|
||||
FROM table AS t
|
||||
INNER JOIN table2 AS t2 ON t.id = t2.t_id
|
||||
WHERE 1 = 1
|
||||
;
|
||||
|
||||
SELECT
|
||||
a,
|
||||
b AS x,
|
||||
if(x = 0, a, b)
|
||||
FROM table2 AS t
|
||||
WHERE t.id != 0
|
||||
;
|
||||
|
18
tests/queries/0_stateless/01278_format_multiple_queries.sh
Executable file
18
tests/queries/0_stateless/01278_format_multiple_queries.sh
Executable file
@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
||||
. $CURDIR/../shell_config.sh
|
||||
|
||||
set -e
|
||||
|
||||
format="$CLICKHOUSE_FORMAT -n"
|
||||
|
||||
$format <<EOF
|
||||
SELECT a, b AS x FROM table AS t
|
||||
JOIN table2 AS t2 ON (t.id = t2.t_id)
|
||||
WHERE 1 = 1
|
||||
;
|
||||
SELECT a, b AS x,
|
||||
x == 0 ? a : b
|
||||
FROM table2 AS t WHERE t.id != 0
|
||||
EOF
|
Loading…
Reference in New Issue
Block a user