2017-11-11 01:04:14 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
|
|
|
#include <IO/ReadBufferFromFileDescriptor.h>
|
|
|
|
#include <IO/ReadHelpers.h>
|
|
|
|
#include <Parsers/ParserQuery.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
|
|
|
#include <Parsers/formatAST.h>
|
2019-08-23 15:47:27 +00:00
|
|
|
#include <Common/TerminalSize.h>
|
2017-11-11 01:04:14 +00:00
|
|
|
|
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")
|
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");
|
2017-11-11 01:04:14 +00:00
|
|
|
|
|
|
|
if (quiet && (hilite || oneline))
|
|
|
|
{
|
|
|
|
std::cerr << "Options 'hilite' or 'oneline' have no sense in 'quiet' mode." << std::endl;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
String query;
|
|
|
|
ReadBufferFromFileDescriptor in(STDIN_FILENO);
|
|
|
|
readStringUntilEOF(query, in);
|
|
|
|
|
|
|
|
const char * pos = query.data();
|
|
|
|
const char * end = pos + query.size();
|
|
|
|
|
|
|
|
ParserQuery parser(end);
|
2020-05-12 16:13:34 +00:00
|
|
|
do
|
|
|
|
{
|
2020-05-12 14:56:52 +00:00
|
|
|
ASTPtr res = parseQueryAndMovePosition(parser, pos, end, "query", multiple, 0, DBMS_DEFAULT_MAX_PARSER_DEPTH);
|
|
|
|
if (!quiet)
|
|
|
|
{
|
|
|
|
formatAST(*res, std::cout, hilite, oneline);
|
2020-05-12 16:13:34 +00:00
|
|
|
if (multiple)
|
2020-05-12 14:56:52 +00:00
|
|
|
std::cout << "\n;\n";
|
|
|
|
std::cout << std::endl;
|
|
|
|
}
|
2020-05-12 16:21:37 +00:00
|
|
|
} while (multiple && pos != end);
|
2017-11-11 01:04:14 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
std::cerr << getCurrentExceptionMessage(true);
|
|
|
|
return getCurrentExceptionCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|