2011-10-30 05:19:41 +00:00
|
|
|
#include <DB/Parsers/ASTIdentifier.h>
|
2014-03-25 14:06:40 +00:00
|
|
|
#include <DB/Parsers/ASTLiteral.h>
|
2011-10-30 05:19:41 +00:00
|
|
|
#include <DB/Parsers/ASTSelectQuery.h>
|
|
|
|
#include <DB/Parsers/ASTInsertQuery.h>
|
|
|
|
|
|
|
|
#include <DB/Parsers/CommonParsers.h>
|
|
|
|
#include <DB/Parsers/ExpressionElementParsers.h>
|
|
|
|
#include <DB/Parsers/ExpressionListParsers.h>
|
|
|
|
#include <DB/Parsers/ParserSelectQuery.h>
|
|
|
|
#include <DB/Parsers/ParserInsertQuery.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-01-11 21:46:36 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int SYNTAX_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-10-30 05:19:41 +00:00
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
bool ParserInsertQuery::parseImpl(Pos & pos, Pos end, ASTPtr & node, Pos & max_parsed_pos, Expected & expected)
|
2011-10-30 05:19:41 +00:00
|
|
|
{
|
|
|
|
Pos begin = pos;
|
|
|
|
|
|
|
|
ParserWhiteSpaceOrComments ws;
|
|
|
|
ParserString s_insert("INSERT", true, true);
|
|
|
|
ParserString s_into("INTO", true, true);
|
|
|
|
ParserString s_dot(".");
|
2014-03-25 14:06:40 +00:00
|
|
|
ParserString s_id("ID");
|
|
|
|
ParserString s_eq("=");
|
|
|
|
ParserStringLiteral id_p;
|
2011-10-30 05:19:41 +00:00
|
|
|
ParserString s_values("VALUES", true, true);
|
|
|
|
ParserString s_format("FORMAT", true, true);
|
|
|
|
ParserString s_select("SELECT", true, true);
|
|
|
|
ParserString s_lparen("(");
|
|
|
|
ParserString s_rparen(")");
|
|
|
|
ParserIdentifier name_p;
|
2016-11-20 04:47:51 +00:00
|
|
|
ParserList columns_p(std::make_unique<ParserCompoundIdentifier>(), std::make_unique<ParserString>(","), false);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2011-10-30 05:19:41 +00:00
|
|
|
ASTPtr database;
|
|
|
|
ASTPtr table;
|
|
|
|
ASTPtr columns;
|
|
|
|
ASTPtr format;
|
|
|
|
ASTPtr select;
|
2014-03-25 14:06:40 +00:00
|
|
|
ASTPtr id;
|
2011-10-30 05:19:41 +00:00
|
|
|
/// Данные для вставки
|
2014-04-08 07:31:51 +00:00
|
|
|
const char * data = nullptr;
|
2011-10-30 05:19:41 +00:00
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
/// INSERT INTO
|
2015-04-11 03:10:23 +00:00
|
|
|
if (!s_insert.ignore(pos, end, max_parsed_pos, expected)
|
2011-10-30 05:19:41 +00:00
|
|
|
|| !ws.ignore(pos, end)
|
2015-04-11 03:10:23 +00:00
|
|
|
|| !s_into.ignore(pos, end, max_parsed_pos, expected))
|
2011-10-30 05:19:41 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
if (!name_p.parse(pos, end, table, max_parsed_pos, expected))
|
2011-10-30 05:19:41 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
if (s_dot.ignore(pos, end, max_parsed_pos, expected))
|
2011-10-30 05:19:41 +00:00
|
|
|
{
|
|
|
|
database = table;
|
2015-04-11 03:10:23 +00:00
|
|
|
if (!name_p.parse(pos, end, table, max_parsed_pos, expected))
|
2011-10-30 05:19:41 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
if (s_id.ignore(pos, end, max_parsed_pos, expected))
|
2014-03-25 14:06:40 +00:00
|
|
|
{
|
2015-04-11 03:10:23 +00:00
|
|
|
if (!s_eq.ignore(pos, end, max_parsed_pos, expected))
|
2014-03-25 14:06:40 +00:00
|
|
|
return false;
|
|
|
|
|
2015-05-18 20:22:23 +00:00
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
if (!id_p.parse(pos, end, id, max_parsed_pos, expected))
|
2014-03-25 14:06:40 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
2011-10-30 05:19:41 +00:00
|
|
|
/// Есть ли список столбцов
|
2015-04-11 03:10:23 +00:00
|
|
|
if (s_lparen.ignore(pos, end, max_parsed_pos, expected))
|
2013-10-25 14:56:47 +00:00
|
|
|
{
|
2015-05-18 20:22:23 +00:00
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
if (!columns_p.parse(pos, end, columns, max_parsed_pos, expected))
|
2013-10-28 14:43:36 +00:00
|
|
|
return false;
|
2015-05-18 20:22:23 +00:00
|
|
|
|
2013-10-28 14:43:36 +00:00
|
|
|
ws.ignore(pos, end);
|
2015-05-18 20:22:23 +00:00
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
if (!s_rparen.ignore(pos, end, max_parsed_pos, expected))
|
2013-10-28 14:43:36 +00:00
|
|
|
return false;
|
2013-10-25 14:56:47 +00:00
|
|
|
}
|
2011-10-30 05:19:41 +00:00
|
|
|
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
|
|
|
|
Pos before_select = pos;
|
|
|
|
|
|
|
|
/// VALUES или FORMAT или SELECT
|
2015-04-11 03:10:23 +00:00
|
|
|
if (s_values.ignore(pos, end, max_parsed_pos, expected))
|
2011-10-30 05:19:41 +00:00
|
|
|
{
|
|
|
|
ws.ignore(pos, end);
|
|
|
|
data = pos;
|
2011-10-31 06:37:12 +00:00
|
|
|
pos = end;
|
2011-10-30 05:19:41 +00:00
|
|
|
}
|
2015-04-11 03:10:23 +00:00
|
|
|
else if (s_format.ignore(pos, end, max_parsed_pos, expected))
|
2011-10-30 05:19:41 +00:00
|
|
|
{
|
2011-10-31 06:37:12 +00:00
|
|
|
ws.ignore(pos, end);
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2015-04-11 03:10:23 +00:00
|
|
|
if (!name_p.parse(pos, end, format, max_parsed_pos, expected))
|
2011-10-30 05:19:41 +00:00
|
|
|
return false;
|
|
|
|
|
2011-10-31 06:37:12 +00:00
|
|
|
/// Данные начинаются после первого перевода строки, если такой есть, или после всех пробельных символов, иначе.
|
|
|
|
ParserWhiteSpaceOrComments ws_without_nl(false);
|
|
|
|
|
|
|
|
ws_without_nl.ignore(pos, end);
|
2015-03-29 01:51:01 +00:00
|
|
|
if (pos != end && *pos == ';')
|
|
|
|
throw Exception("You have excessive ';' symbol before data for INSERT.\n"
|
|
|
|
"Example:\n\n"
|
|
|
|
"INSERT INTO t (x, y) FORMAT TabSeparated\n"
|
|
|
|
"1\tHello\n"
|
|
|
|
"2\tWorld\n"
|
|
|
|
"\n"
|
|
|
|
"Note that there is no ';' in first line.", ErrorCodes::SYNTAX_ERROR);
|
|
|
|
|
2011-10-31 06:37:12 +00:00
|
|
|
if (pos != end && *pos == '\n')
|
|
|
|
++pos;
|
2014-06-26 00:58:14 +00:00
|
|
|
|
2011-10-30 05:19:41 +00:00
|
|
|
data = pos;
|
2011-10-31 06:37:12 +00:00
|
|
|
pos = end;
|
2011-10-30 05:19:41 +00:00
|
|
|
}
|
2015-04-11 03:10:23 +00:00
|
|
|
else if (s_select.ignore(pos, end, max_parsed_pos, expected))
|
2011-10-30 05:19:41 +00:00
|
|
|
{
|
|
|
|
pos = before_select;
|
|
|
|
ParserSelectQuery select_p;
|
2015-04-11 03:10:23 +00:00
|
|
|
select_p.parse(pos, end, select, max_parsed_pos, expected);
|
2011-10-30 05:19:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
expected = "VALUES or FORMAT or SELECT";
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-31 06:37:12 +00:00
|
|
|
|
2016-05-28 15:42:22 +00:00
|
|
|
auto query = std::make_shared<ASTInsertQuery>(StringRange(begin, data ? data : pos));
|
2011-10-30 05:19:41 +00:00
|
|
|
node = query;
|
|
|
|
|
2011-10-31 06:37:12 +00:00
|
|
|
if (database)
|
2014-06-26 00:58:14 +00:00
|
|
|
query->database = typeid_cast<ASTIdentifier &>(*database).name;
|
|
|
|
|
|
|
|
query->table = typeid_cast<ASTIdentifier &>(*table).name;
|
2011-10-31 06:37:12 +00:00
|
|
|
|
2014-03-25 14:06:40 +00:00
|
|
|
if (id)
|
2014-06-26 00:58:14 +00:00
|
|
|
query->insert_id = safeGet<const String &>(typeid_cast<ASTLiteral &>(*id).value);
|
2014-03-25 14:06:40 +00:00
|
|
|
|
2011-10-31 06:37:12 +00:00
|
|
|
if (format)
|
2014-06-26 00:58:14 +00:00
|
|
|
query->format = typeid_cast<ASTIdentifier &>(*format).name;
|
|
|
|
|
2011-10-30 05:19:41 +00:00
|
|
|
query->columns = columns;
|
|
|
|
query->select = select;
|
2012-05-28 19:34:55 +00:00
|
|
|
query->data = data != end ? data : NULL;
|
2011-10-30 05:19:41 +00:00
|
|
|
query->end = end;
|
|
|
|
|
|
|
|
if (columns)
|
|
|
|
query->children.push_back(columns);
|
|
|
|
if (select)
|
|
|
|
query->children.push_back(select);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|