Merge pull request #21494 from ucasFL/format

add --backslash option for clickhouse-format
This commit is contained in:
Kruglov Pavel 2021-03-12 14:31:58 +03:00 committed by GitHub
commit 2e7f756d7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 7 deletions

View File

@ -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
{ {

View File

@ -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 \
)

View 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;