2015-01-26 15:27:51 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Core/Block.h>
|
|
|
|
#include <DB/Dictionaries/DictionaryStructure.h>
|
|
|
|
#include <DB/Dictionaries/FileDictionarySource.h>
|
2015-02-10 14:50:43 +00:00
|
|
|
#include <DB/Dictionaries/MySQLDictionarySource.h>
|
|
|
|
#include <DB/Dictionaries/ClickHouseDictionarySource.h>
|
2015-01-26 15:27:51 +00:00
|
|
|
#include <DB/DataTypes/DataTypesNumberFixed.h>
|
|
|
|
#include <Yandex/singleton.h>
|
2015-02-10 21:10:58 +00:00
|
|
|
#include <memory>
|
2015-01-26 15:27:51 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2015-03-20 15:21:29 +00:00
|
|
|
Block createSampleBlock(const DictionaryStructure & dict_struct)
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
|
|
|
Block block{
|
|
|
|
ColumnWithNameAndType{
|
|
|
|
new ColumnUInt64,
|
|
|
|
new DataTypeUInt64,
|
|
|
|
dict_struct.id_name
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto & attribute : dict_struct.attributes)
|
|
|
|
block.insert(ColumnWithNameAndType{
|
2015-03-20 15:21:29 +00:00
|
|
|
attribute.type->createColumn(), attribute.type, attribute.name
|
2015-01-26 15:27:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
/// creates IDictionarySource instance from config and DictionaryStructure
|
2015-01-26 15:27:51 +00:00
|
|
|
class DictionarySourceFactory : public Singleton<DictionarySourceFactory>
|
|
|
|
{
|
|
|
|
public:
|
2015-01-26 16:53:44 +00:00
|
|
|
DictionarySourcePtr create(Poco::Util::AbstractConfiguration & config,
|
2015-01-26 15:27:51 +00:00
|
|
|
const std::string & config_prefix,
|
|
|
|
const DictionaryStructure & dict_struct,
|
2015-02-03 17:03:35 +00:00
|
|
|
Context & context) const
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
2015-02-10 14:50:43 +00:00
|
|
|
Poco::Util::AbstractConfiguration::Keys keys;
|
|
|
|
config.keys(config_prefix, keys);
|
|
|
|
if (keys.size() != 1)
|
|
|
|
throw Exception{
|
|
|
|
"Element dictionary.source should have exactly one child element",
|
|
|
|
ErrorCodes::EXCESSIVE_ELEMENT_IN_CONFIG
|
|
|
|
};
|
|
|
|
|
2015-03-20 15:21:29 +00:00
|
|
|
auto sample_block = createSampleBlock(dict_struct);
|
2015-01-26 15:27:51 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
const auto & source_type = keys.front();
|
|
|
|
|
|
|
|
if ("file" == source_type)
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
2015-02-10 14:50:43 +00:00
|
|
|
const auto filename = config.getString(config_prefix + ".file.path");
|
|
|
|
const auto format = config.getString(config_prefix + ".file.format");
|
2015-02-10 21:10:58 +00:00
|
|
|
return std::make_unique<FileDictionarySource>(filename, format, sample_block, context);
|
2015-01-26 15:27:51 +00:00
|
|
|
}
|
2015-02-10 14:50:43 +00:00
|
|
|
else if ("mysql" == source_type)
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
2015-02-10 21:10:58 +00:00
|
|
|
return std::make_unique<MySQLDictionarySource>(config, config_prefix + ".mysql", sample_block);
|
2015-01-26 15:27:51 +00:00
|
|
|
}
|
2015-02-10 14:50:43 +00:00
|
|
|
else if ("clickhouse" == source_type)
|
2015-01-29 11:51:52 +00:00
|
|
|
{
|
2015-02-10 21:10:58 +00:00
|
|
|
return std::make_unique<ClickHouseDictionarySource>(config, config_prefix + ".clickhouse",
|
2015-02-04 13:06:56 +00:00
|
|
|
sample_block, context);
|
2015-01-29 11:51:52 +00:00
|
|
|
}
|
2015-01-26 15:27:51 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
throw Exception{
|
|
|
|
"Unknown dictionary source type: " + source_type,
|
|
|
|
ErrorCodes::UNKNOWN_ELEMENT_IN_CONFIG
|
|
|
|
};
|
2015-01-26 15:27:51 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|