mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-12-18 04:12:19 +00:00
Merge pull request #21494 from ucasFL/format
add --backslash option for clickhouse-format
This commit is contained in:
commit
2e7f756d7c
@ -1,6 +1,6 @@
|
|||||||
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <functional>
|
|
||||||
#include <boost/program_options.hpp>
|
#include <boost/program_options.hpp>
|
||||||
|
|
||||||
#include <IO/ReadBufferFromFileDescriptor.h>
|
#include <IO/ReadBufferFromFileDescriptor.h>
|
||||||
@ -50,6 +50,7 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
|
|||||||
("quiet,q", "just check syntax, no output on success")
|
("quiet,q", "just check syntax, no output on success")
|
||||||
("multiquery,n", "allow multiple queries in the same file")
|
("multiquery,n", "allow multiple queries in the same file")
|
||||||
("obfuscate", "obfuscate instead of formatting")
|
("obfuscate", "obfuscate instead of formatting")
|
||||||
|
("backslash", "add a backslash at the end of each line of the formatted query")
|
||||||
("seed", po::value<std::string>(), "seed (arbitrary string) that determines the result of obfuscation")
|
("seed", po::value<std::string>(), "seed (arbitrary string) that determines the result of obfuscation")
|
||||||
;
|
;
|
||||||
|
|
||||||
@ -70,6 +71,7 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
|
|||||||
bool quiet = options.count("quiet");
|
bool quiet = options.count("quiet");
|
||||||
bool multiple = options.count("multiquery");
|
bool multiple = options.count("multiquery");
|
||||||
bool obfuscate = options.count("obfuscate");
|
bool obfuscate = options.count("obfuscate");
|
||||||
|
bool backslash = options.count("backslash");
|
||||||
|
|
||||||
if (quiet && (hilite || oneline || obfuscate))
|
if (quiet && (hilite || oneline || obfuscate))
|
||||||
{
|
{
|
||||||
@ -147,6 +149,8 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
|
|||||||
DB::ErrorCodes::INVALID_FORMAT_INSERT_QUERY_WITH_DATA);
|
DB::ErrorCodes::INVALID_FORMAT_INSERT_QUERY_WITH_DATA);
|
||||||
}
|
}
|
||||||
if (!quiet)
|
if (!quiet)
|
||||||
|
{
|
||||||
|
if (!backslash)
|
||||||
{
|
{
|
||||||
WriteBufferFromOStream res_buf(std::cout, 4096);
|
WriteBufferFromOStream res_buf(std::cout, 4096);
|
||||||
formatAST(*res, res_buf, hilite, oneline);
|
formatAST(*res, res_buf, hilite, oneline);
|
||||||
@ -155,6 +159,31 @@ int mainEntryClickHouseFormat(int argc, char ** argv)
|
|||||||
std::cout << "\n;\n";
|
std::cout << "\n;\n";
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
/// add additional '\' at the end of each line;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
WriteBufferFromOwnString str_buf;
|
||||||
|
formatAST(*res, str_buf, hilite, oneline);
|
||||||
|
|
||||||
|
auto res_string = str_buf.str();
|
||||||
|
WriteBufferFromOStream res_cout(std::cout, 4096);
|
||||||
|
|
||||||
|
const char * s_pos= res_string.data();
|
||||||
|
const char * s_end = s_pos + res_string.size();
|
||||||
|
|
||||||
|
while (s_pos != s_end)
|
||||||
|
{
|
||||||
|
if (*s_pos == '\n')
|
||||||
|
res_cout.write(" \\", 2);
|
||||||
|
res_cout.write(*s_pos++);
|
||||||
|
}
|
||||||
|
|
||||||
|
res_cout.next();
|
||||||
|
if (multiple)
|
||||||
|
std::cout << " \\\n;\n";
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
@ -0,0 +1,16 @@
|
|||||||
|
SELECT * \
|
||||||
|
FROM \
|
||||||
|
( \
|
||||||
|
SELECT 1 AS x \
|
||||||
|
UNION ALL \
|
||||||
|
SELECT 1 \
|
||||||
|
UNION DISTINCT \
|
||||||
|
SELECT 3 \
|
||||||
|
)
|
||||||
|
SELECT 1 \
|
||||||
|
UNION ALL \
|
||||||
|
( \
|
||||||
|
SELECT 1 \
|
||||||
|
UNION DISTINCT \
|
||||||
|
SELECT 1 \
|
||||||
|
)
|
9
tests/queries/0_stateless/01754_clickhouse_format_backslash.sh
Executable file
9
tests/queries/0_stateless/01754_clickhouse_format_backslash.sh
Executable file
@ -0,0 +1,9 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
CURDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
|
||||||
|
# shellcheck source=../shell_config.sh
|
||||||
|
. "$CURDIR"/../shell_config.sh
|
||||||
|
|
||||||
|
echo "select * from (select 1 as x union all select 1 union distinct select 3)" | $CLICKHOUSE_FORMAT --backslash;
|
||||||
|
|
||||||
|
echo "select 1 union all (select 1 union distinct select 1)" | $CLICKHOUSE_FORMAT --backslash;
|
Loading…
Reference in New Issue
Block a user