2017-12-26 21:34:06 +00:00
|
|
|
#include <Common/PocoSessionPoolHelpers.h>
|
|
|
|
#include <Storages/transformQueryForExternalDatabase.h>
|
2017-12-05 13:32:02 +00:00
|
|
|
#include <Storages/StorageODBC.h>
|
2017-12-30 00:36:06 +00:00
|
|
|
#include <Storages/StorageFactory.h>
|
|
|
|
#include <Interpreters/evaluateConstantExpression.h>
|
2017-12-05 13:32:02 +00:00
|
|
|
#include <Dictionaries/ODBCBlockInputStream.h>
|
2017-12-30 00:36:06 +00:00
|
|
|
#include <Parsers/ASTLiteral.h>
|
2017-12-05 13:32:02 +00:00
|
|
|
|
2017-12-13 16:46:57 +00:00
|
|
|
|
2017-12-05 13:32:02 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-12-30 00:36:06 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NUMBER_OF_ARGUMENTS_DOESNT_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-26 21:34:06 +00:00
|
|
|
StorageODBC::StorageODBC(
|
|
|
|
const std::string & name,
|
|
|
|
const std::string & connection_string,
|
|
|
|
const std::string & remote_database_name,
|
|
|
|
const std::string & remote_table_name,
|
2018-01-25 14:42:39 +00:00
|
|
|
const NamesAndTypesList & columns_)
|
|
|
|
: IStorage{columns_, {}, {}, {}}
|
|
|
|
, name(name)
|
2017-12-26 21:34:06 +00:00
|
|
|
, remote_database_name(remote_database_name)
|
|
|
|
, remote_table_name(remote_table_name)
|
2017-12-05 13:32:02 +00:00
|
|
|
{
|
2017-12-26 21:34:06 +00:00
|
|
|
pool = createAndCheckResizePocoSessionPool([&]
|
2017-12-05 13:32:02 +00:00
|
|
|
{
|
2017-12-26 21:34:06 +00:00
|
|
|
return std::make_shared<Poco::Data::SessionPool>("ODBC", connection_string);
|
|
|
|
});
|
2017-12-05 13:32:02 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 21:34:06 +00:00
|
|
|
BlockInputStreams StorageODBC::read(
|
|
|
|
const Names & column_names,
|
2017-12-05 13:32:02 +00:00
|
|
|
const SelectQueryInfo & query_info,
|
|
|
|
const Context & context,
|
|
|
|
QueryProcessingStage::Enum & processed_stage,
|
|
|
|
size_t max_block_size,
|
2017-12-26 21:34:06 +00:00
|
|
|
unsigned /*num_streams*/)
|
2017-12-05 13:32:02 +00:00
|
|
|
{
|
2017-12-13 16:46:57 +00:00
|
|
|
check(column_names);
|
|
|
|
processed_stage = QueryProcessingStage::FetchColumns;
|
2017-12-26 21:34:06 +00:00
|
|
|
String query = transformQueryForExternalDatabase(*query_info.query, columns, remote_database_name, remote_table_name, context);
|
2017-12-28 05:26:45 +00:00
|
|
|
|
|
|
|
Block sample_block;
|
|
|
|
for (const String & name : column_names)
|
|
|
|
{
|
|
|
|
auto column_data = getColumn(name);
|
|
|
|
sample_block.insert({ column_data.type, column_data.name });
|
|
|
|
}
|
|
|
|
|
|
|
|
return { std::make_shared<ODBCBlockInputStream>(pool->get(), query, sample_block, max_block_size) };
|
2017-12-05 13:32:02 +00:00
|
|
|
}
|
2017-12-26 21:34:06 +00:00
|
|
|
|
2017-12-30 00:36:06 +00:00
|
|
|
|
|
|
|
void registerStorageODBC(StorageFactory & factory)
|
|
|
|
{
|
|
|
|
factory.registerStorage("ODBC", [](const StorageFactory::Arguments & args)
|
|
|
|
{
|
|
|
|
ASTs & engine_args = args.engine_args;
|
|
|
|
|
|
|
|
if (engine_args.size() != 3)
|
|
|
|
throw Exception(
|
|
|
|
"Storage ODBC requires exactly 3 parameters: ODBC('DSN', database, table).",
|
|
|
|
ErrorCodes::NUMBER_OF_ARGUMENTS_DOESNT_MATCH);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 2; ++i)
|
|
|
|
engine_args[i] = evaluateConstantExpressionOrIdentifierAsLiteral(engine_args[i], args.local_context);
|
|
|
|
|
|
|
|
return StorageODBC::create(args.table_name,
|
|
|
|
static_cast<const ASTLiteral &>(*engine_args[0]).value.safeGet<String>(),
|
|
|
|
static_cast<const ASTLiteral &>(*engine_args[1]).value.safeGet<String>(),
|
|
|
|
static_cast<const ASTLiteral &>(*engine_args[2]).value.safeGet<String>(),
|
|
|
|
args.columns);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-26 18:32:17 +00:00
|
|
|
}
|