2011-10-30 11:30:52 +00:00
|
|
|
|
#include <DB/IO/ConcatReadBuffer.h>
|
|
|
|
|
|
2011-11-06 06:22:52 +00:00
|
|
|
|
#include <DB/DataStreams/MaterializingBlockInputStream.h>
|
2011-10-30 11:30:52 +00:00
|
|
|
|
#include <DB/DataStreams/FormatFactory.h>
|
|
|
|
|
#include <DB/DataStreams/copyData.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/Parsers/ASTInsertQuery.h>
|
|
|
|
|
#include <DB/Parsers/ASTSelectQuery.h>
|
|
|
|
|
#include <DB/Parsers/ASTIdentifier.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/Interpreters/InterpreterSelectQuery.h>
|
|
|
|
|
#include <DB/Interpreters/InterpreterInsertQuery.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
2012-03-05 00:09:41 +00:00
|
|
|
|
InterpreterInsertQuery::InterpreterInsertQuery(ASTPtr query_ptr_, Context & context_)
|
|
|
|
|
: query_ptr(query_ptr_), context(context_)
|
2011-10-30 11:30:52 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StoragePtr InterpreterInsertQuery::getTable()
|
|
|
|
|
{
|
|
|
|
|
ASTInsertQuery & query = dynamic_cast<ASTInsertQuery &>(*query_ptr);
|
|
|
|
|
|
|
|
|
|
/// В какую таблицу писать.
|
|
|
|
|
|
|
|
|
|
String database_name = query.database;
|
|
|
|
|
String table_name = query.table;
|
|
|
|
|
|
|
|
|
|
/** Если база данных не указана - используем текущую базу данных.
|
|
|
|
|
*/
|
|
|
|
|
if (database_name.empty())
|
|
|
|
|
database_name = context.current_database;
|
|
|
|
|
|
|
|
|
|
if (context.databases->end() == context.databases->find(database_name)
|
|
|
|
|
|| (*context.databases)[database_name].end() == (*context.databases)[database_name].find(table_name))
|
|
|
|
|
throw Exception("Unknown table '" + table_name + "' in database '" + database_name + "'", ErrorCodes::UNKNOWN_TABLE);
|
|
|
|
|
|
|
|
|
|
return (*context.databases)[database_name][table_name];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-01-09 19:20:48 +00:00
|
|
|
|
void InterpreterInsertQuery::execute(ReadBuffer * remaining_data_istr)
|
2011-10-30 11:30:52 +00:00
|
|
|
|
{
|
|
|
|
|
ASTInsertQuery & query = dynamic_cast<ASTInsertQuery &>(*query_ptr);
|
|
|
|
|
StoragePtr table = getTable();
|
|
|
|
|
|
|
|
|
|
/// TODO - если указаны не все столбцы, то дополнить поток недостающими столбцами со значениями по-умолчанию.
|
|
|
|
|
|
|
|
|
|
BlockInputStreamPtr in;
|
|
|
|
|
BlockOutputStreamPtr out = table->write(query_ptr);
|
|
|
|
|
|
|
|
|
|
/// Какой тип запроса: INSERT VALUES | INSERT FORMAT | INSERT SELECT?
|
|
|
|
|
if (!query.select)
|
|
|
|
|
{
|
|
|
|
|
FormatFactory format_factory;
|
|
|
|
|
String format = query.format;
|
|
|
|
|
if (format.empty())
|
|
|
|
|
format = "Values";
|
|
|
|
|
|
|
|
|
|
/// Данные могут содержаться в распарсенной и ещё не распарсенной части запроса.
|
|
|
|
|
ConcatReadBuffer::ReadBuffers buffers;
|
|
|
|
|
ReadBuffer buf1(const_cast<char *>(query.data), query.end - query.data, 0);
|
|
|
|
|
|
|
|
|
|
buffers.push_back(&buf1);
|
|
|
|
|
if (remaining_data_istr)
|
2012-01-09 19:20:48 +00:00
|
|
|
|
buffers.push_back(remaining_data_istr);
|
2011-10-30 11:30:52 +00:00
|
|
|
|
|
|
|
|
|
ConcatReadBuffer istr(buffers);
|
|
|
|
|
Block sample = table->getSampleBlock();
|
|
|
|
|
|
2012-03-05 00:09:41 +00:00
|
|
|
|
in = format_factory.getInput(format, istr, sample, context.settings.max_block_size, *context.data_type_factory);
|
2011-10-30 11:30:52 +00:00
|
|
|
|
copyData(*in, *out);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-03-05 00:09:41 +00:00
|
|
|
|
InterpreterSelectQuery interpreter_select(query.select, context);
|
2011-10-30 11:30:52 +00:00
|
|
|
|
in = interpreter_select.execute();
|
2011-11-06 06:22:52 +00:00
|
|
|
|
in = new MaterializingBlockInputStream(in);
|
2011-10-30 11:30:52 +00:00
|
|
|
|
copyData(*in, *out);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|