2017-11-11 01:04:14 +00:00
|
|
|
#include <iostream>
|
2020-09-26 00:38:59 +00:00
|
|
|
#include <string_view>
|
|
|
|
#include <functional>
|
2017-11-11 01:04:14 +00:00
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
|
|
|
#include <IO/ReadBufferFromFileDescriptor.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
2020-09-26 00:38:59 +00:00
|
|
|
#include <IO/WriteBufferFromFileDescriptor.h>
|
2020-11-09 16:05:40 +00:00
|
|
|
#include <IO/WriteBufferFromOStream.h>
|
2017-11-11 01:04:14 +00:00
|
|
|
#include <Parsers/ParserQuery.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
|
|
|
#include <Parsers/formatAST.h>
|
2020-09-26 00:38:59 +00:00
|
|
|
#include <Parsers/obfuscateQueries.h>
|
2019-08-23 15:47:27 +00:00
|
|
|
#include <Common/TerminalSize.h>
|
2017-11-11 01:04:14 +00:00
|
|
|
|
2020-09-26 00:38:59 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/registerFunctions.h>
|
|
|
|
#include <AggregateFunctions/AggregateFunctionFactory.h>
|
|
|
|
#include <AggregateFunctions/registerAggregateFunctions.h>
|
|
|
|
#include <TableFunctions/TableFunctionFactory.h>
|
|
|
|
#include <TableFunctions/registerTableFunctions.h>
|
|
|
|
#include <Storages/StorageFactory.h>
|
|
|
|
#include <Storages/registerStorages.h>
|
|
|
|
#include <DataTypes/DataTypeFactory.h>
|
|
|
|
|
|
|
|
|
2019-12-15 06:34:43 +00:00
|
|
|
#pragma GCC diagnostic ignored "-Wunused-function"
|
|
|
|
#pragma GCC diagnostic ignored "-Wmissing-declarations"
|
|
|
|
|
2017-11-11 01:04:14 +00:00
|
|
|
int mainEntryClickHouseFormat(int argc, char ** argv)
|
|
|
|
{
|
|
|
|
using namespace DB;
|
|
|
|
|
2019-08-23 15:47:27 +00:00
|
|
|
boost::program_options::options_description desc = createOptionsDescription("Allowed options", getTerminalWidth());
|
2017-11-11 01:04:14 +00:00
|
|
|
desc.add_options()
|
|
|
|
("help,h", "produce help message")
|
|
|
|
("hilite", "add syntax highlight with ANSI terminal escape sequences")
|
|
|
|
("oneline", "format in single line")
|
|
|
|
("quiet,q", "just check syntax, no output on success")
|
2020-05-12 14:56:52 +00:00
|
|
|
("multiquery,n", "allow multiple queries in the same file")
|
2020-09-26 00:38:59 +00:00
|
|
|
("obfuscate", "obfuscate instead of formatting")
|
|
|
|
("seed", po::value<std::string>(), "seed (arbitrary string) that determines the result of obfuscation")
|
2017-11-11 01:04:14 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
boost::program_options::variables_map options;
|
|
|
|
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), options);
|
|
|
|
|
|
|
|
if (options.count("help"))
|
|
|
|
{
|
|
|
|
std::cout << "Usage: " << argv[0] << " [options] < query" << std::endl;
|
|
|
|
std::cout << desc << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
bool hilite = options.count("hilite");
|
|
|
|
bool oneline = options.count("oneline");
|
|
|
|
bool quiet = options.count("quiet");
|
2020-05-12 14:56:52 +00:00
|
|
|
bool multiple = options.count("multiquery");
|
2020-09-26 00:38:59 +00:00
|
|
|
bool obfuscate = options.count("obfuscate");
|
|
|
|
|
|
|
|
if (quiet && (hilite || oneline || obfuscate))
|
|
|
|
{
|
|
|
|
std::cerr << "Options 'hilite' or 'oneline' or 'obfuscate' have no sense in 'quiet' mode." << std::endl;
|
|
|
|
return 2;
|
|
|
|
}
|
2017-11-11 01:04:14 +00:00
|
|
|
|
2020-09-26 00:38:59 +00:00
|
|
|
if (obfuscate && (hilite || oneline || quiet))
|
2017-11-11 01:04:14 +00:00
|
|
|
{
|
2020-09-26 00:38:59 +00:00
|
|
|
std::cerr << "Options 'hilite' or 'oneline' or 'quiet' have no sense in 'obfuscate' mode." << std::endl;
|
2017-11-11 01:04:14 +00:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
String query;
|
|
|
|
ReadBufferFromFileDescriptor in(STDIN_FILENO);
|
|
|
|
readStringUntilEOF(query, in);
|
|
|
|
|
2020-09-26 00:38:59 +00:00
|
|
|
if (obfuscate)
|
2020-05-12 16:13:34 +00:00
|
|
|
{
|
2020-09-26 00:38:59 +00:00
|
|
|
WordMap obfuscated_words_map;
|
|
|
|
WordSet used_nouns;
|
|
|
|
SipHash hash_func;
|
|
|
|
|
|
|
|
if (options.count("seed"))
|
2020-05-12 14:56:52 +00:00
|
|
|
{
|
2020-09-26 00:38:59 +00:00
|
|
|
std::string seed;
|
|
|
|
hash_func.update(options["seed"].as<std::string>());
|
2020-05-12 14:56:52 +00:00
|
|
|
}
|
2020-09-26 00:38:59 +00:00
|
|
|
|
|
|
|
SharedContextHolder shared_context = Context::createShared();
|
|
|
|
Context context = Context::createGlobal(shared_context.get());
|
|
|
|
context.makeGlobalContext();
|
|
|
|
|
|
|
|
registerFunctions();
|
|
|
|
registerAggregateFunctions();
|
|
|
|
registerTableFunctions();
|
|
|
|
registerStorages();
|
|
|
|
|
|
|
|
std::unordered_set<std::string> additional_names;
|
|
|
|
|
|
|
|
auto all_known_storage_names = StorageFactory::instance().getAllRegisteredNames();
|
|
|
|
auto all_known_data_type_names = DataTypeFactory::instance().getAllRegisteredNames();
|
|
|
|
|
|
|
|
additional_names.insert(all_known_storage_names.begin(), all_known_storage_names.end());
|
|
|
|
additional_names.insert(all_known_data_type_names.begin(), all_known_data_type_names.end());
|
|
|
|
|
|
|
|
KnownIdentifierFunc is_known_identifier = [&](std::string_view name)
|
|
|
|
{
|
|
|
|
std::string what(name);
|
|
|
|
|
|
|
|
return FunctionFactory::instance().tryGet(what, context) != nullptr
|
|
|
|
|| AggregateFunctionFactory::instance().isAggregateFunctionName(what)
|
|
|
|
|| TableFunctionFactory::instance().isTableFunctionName(what)
|
|
|
|
|| additional_names.count(what);
|
|
|
|
};
|
|
|
|
|
|
|
|
WriteBufferFromFileDescriptor out(STDOUT_FILENO);
|
|
|
|
obfuscateQueries(query, out, obfuscated_words_map, used_nouns, hash_func, is_known_identifier);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const char * pos = query.data();
|
|
|
|
const char * end = pos + query.size();
|
|
|
|
|
|
|
|
ParserQuery parser(end);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ASTPtr res = parseQueryAndMovePosition(parser, pos, end, "query", multiple, 0, DBMS_DEFAULT_MAX_PARSER_DEPTH);
|
|
|
|
if (!quiet)
|
|
|
|
{
|
2020-11-09 16:05:40 +00:00
|
|
|
WriteBufferFromOStream res_buf(std::cout, 4096);
|
|
|
|
formatAST(*res, res_buf, hilite, oneline);
|
2020-11-09 19:07:38 +00:00
|
|
|
res_buf.next();
|
2020-09-26 00:38:59 +00:00
|
|
|
if (multiple)
|
|
|
|
std::cout << "\n;\n";
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
|
|
|
} while (multiple && pos != end);
|
|
|
|
}
|
2017-11-11 01:04:14 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
std::cerr << getCurrentExceptionMessage(true);
|
|
|
|
return getCurrentExceptionCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|