2015-01-26 16:53:44 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <DB/Dictionaries/IDictionarySource.h>
|
2015-02-10 14:50:43 +00:00
|
|
|
#include <DB/Dictionaries/MySQLBlockInputStream.h>
|
2015-01-26 16:53:44 +00:00
|
|
|
#include <statdaemons/ext/range.hpp>
|
|
|
|
#include <mysqlxx/Pool.h>
|
2015-01-30 13:43:16 +00:00
|
|
|
#include <Poco/Util/AbstractConfiguration.h>
|
2015-02-10 14:50:43 +00:00
|
|
|
#include <strconvert/escape.h>
|
2015-01-26 16:53:44 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
/// Allows loading dictionaries from a MySQL database
|
|
|
|
class MySQLDictionarySource final : public IDictionarySource
|
2015-01-26 16:53:44 +00:00
|
|
|
{
|
|
|
|
static const auto max_block_size = 8192;
|
|
|
|
|
|
|
|
public:
|
2015-05-26 15:09:53 +00:00
|
|
|
MySQLDictionarySource(const DictionaryStructure & dict_struct,
|
|
|
|
const Poco::Util::AbstractConfiguration & config, const std::string & config_prefix,
|
2015-02-10 17:40:40 +00:00
|
|
|
Block & sample_block)
|
2015-05-26 15:09:53 +00:00
|
|
|
: dict_struct{dict_struct},
|
|
|
|
db{config.getString(config_prefix + ".db", "")},
|
2015-03-25 10:09:33 +00:00
|
|
|
table{config.getString(config_prefix + ".table")},
|
2015-05-22 13:25:45 +00:00
|
|
|
where{config.getString(config_prefix + ".where", "")},
|
2015-02-10 17:40:40 +00:00
|
|
|
sample_block{sample_block},
|
2015-02-05 18:48:29 +00:00
|
|
|
pool{config, config_prefix},
|
2015-05-22 13:25:45 +00:00
|
|
|
load_all_query{composeLoadAllQuery()},
|
2015-01-30 11:51:59 +00:00
|
|
|
last_modification{getLastModification()}
|
2015-01-27 13:00:20 +00:00
|
|
|
{}
|
2015-01-26 16:53:44 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
/// copy-constructor is provided in order to support cloneability
|
|
|
|
MySQLDictionarySource(const MySQLDictionarySource & other)
|
2015-05-26 15:09:53 +00:00
|
|
|
: dict_struct{other.dict_struct},
|
|
|
|
db{other.db},
|
2015-03-25 10:09:33 +00:00
|
|
|
table{other.table},
|
2015-05-22 13:25:45 +00:00
|
|
|
where{other.where},
|
2015-02-10 17:40:40 +00:00
|
|
|
sample_block{other.sample_block},
|
2015-02-05 18:48:29 +00:00
|
|
|
pool{other.pool},
|
2015-01-30 13:43:16 +00:00
|
|
|
load_all_query{other.load_all_query}, last_modification{other.last_modification}
|
|
|
|
{}
|
|
|
|
|
2015-01-26 16:53:44 +00:00
|
|
|
BlockInputStreamPtr loadAll() override
|
|
|
|
{
|
2015-02-10 14:50:43 +00:00
|
|
|
last_modification = getLastModification();
|
2015-03-18 12:09:49 +00:00
|
|
|
return new MySQLBlockInputStream{pool.Get(), load_all_query, sample_block, max_block_size};
|
2015-01-26 16:53:44 +00:00
|
|
|
}
|
|
|
|
|
2015-05-22 13:25:45 +00:00
|
|
|
BlockInputStreamPtr loadIds(const std::vector<std::uint64_t> & ids) override
|
2015-01-26 16:53:44 +00:00
|
|
|
{
|
2015-03-04 09:36:30 +00:00
|
|
|
last_modification = getLastModification();
|
|
|
|
const auto query = composeLoadIdsQuery(ids);
|
|
|
|
|
2015-03-18 12:09:49 +00:00
|
|
|
return new MySQLBlockInputStream{pool.Get(), query, sample_block, max_block_size};
|
2015-01-26 16:53:44 +00:00
|
|
|
}
|
|
|
|
|
2015-01-30 11:51:59 +00:00
|
|
|
bool isModified() const override { return getLastModification() > last_modification; }
|
2015-02-10 14:50:43 +00:00
|
|
|
bool supportsSelectiveLoad() const override { return true; }
|
2015-01-30 11:51:59 +00:00
|
|
|
|
2015-02-10 21:14:11 +00:00
|
|
|
DictionarySourcePtr clone() const override { return std::make_unique<MySQLDictionarySource>(*this); }
|
2015-01-30 13:43:16 +00:00
|
|
|
|
2015-05-22 13:25:45 +00:00
|
|
|
std::string toString() const override
|
|
|
|
{
|
|
|
|
return "MySQL: " + db + '.' + table + (where.empty() ? "" : ", where: " + where);
|
|
|
|
}
|
2015-03-25 10:09:33 +00:00
|
|
|
|
2015-01-30 13:43:16 +00:00
|
|
|
private:
|
2015-01-30 11:51:59 +00:00
|
|
|
mysqlxx::DateTime getLastModification() const
|
|
|
|
{
|
|
|
|
const auto Update_time_idx = 12;
|
2015-03-18 09:58:58 +00:00
|
|
|
mysqlxx::DateTime update_time{std::time(nullptr)};
|
2015-01-30 11:51:59 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto connection = pool.Get();
|
2015-02-10 14:50:43 +00:00
|
|
|
auto query = connection->query("SHOW TABLE STATUS LIKE '%" + strconvert::escaped_for_like(table) + "%';");
|
2015-01-30 11:51:59 +00:00
|
|
|
auto result = query.use();
|
2015-03-18 09:58:58 +00:00
|
|
|
|
2015-03-20 15:15:58 +00:00
|
|
|
if (auto row = result.fetch())
|
|
|
|
{
|
|
|
|
const auto & update_time_value = row[Update_time_idx];
|
|
|
|
|
|
|
|
if (!update_time_value.isNull())
|
|
|
|
update_time = update_time_value.getDateTime();
|
2015-03-18 09:58:58 +00:00
|
|
|
|
2015-03-20 15:15:58 +00:00
|
|
|
/// fetch remaining rows to avoid "commands out of sync" error
|
|
|
|
while (auto row = result.fetch());
|
|
|
|
}
|
2015-01-30 11:51:59 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2015-02-10 14:50:43 +00:00
|
|
|
tryLogCurrentException("MySQLDictionarySource");
|
2015-01-30 11:51:59 +00:00
|
|
|
}
|
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
/// we suppose failure to get modification time is not an error, therefore return current time
|
2015-03-18 09:58:58 +00:00
|
|
|
return update_time;
|
2015-01-30 11:51:59 +00:00
|
|
|
}
|
2015-01-29 15:47:21 +00:00
|
|
|
|
2015-05-22 13:25:45 +00:00
|
|
|
std::string composeLoadAllQuery() const
|
2015-01-27 13:00:20 +00:00
|
|
|
{
|
2015-03-04 09:36:30 +00:00
|
|
|
std::string query;
|
2015-01-27 13:00:20 +00:00
|
|
|
|
|
|
|
{
|
2015-03-04 09:36:30 +00:00
|
|
|
WriteBufferFromString out{query};
|
|
|
|
writeString("SELECT ", out);
|
|
|
|
|
2015-05-26 15:09:53 +00:00
|
|
|
writeProbablyBackQuotedString(dict_struct.id_name, out);
|
|
|
|
|
|
|
|
for (const auto & attr : dict_struct.attributes)
|
2015-03-04 09:36:30 +00:00
|
|
|
{
|
2015-05-26 15:09:53 +00:00
|
|
|
writeString(", ", out);
|
2015-03-04 09:36:30 +00:00
|
|
|
|
2015-05-26 15:09:53 +00:00
|
|
|
if (!attr.expression.empty())
|
|
|
|
{
|
|
|
|
writeString(attr.expression, out);
|
|
|
|
writeString(" AS ", out);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeProbablyBackQuotedString(attr.name, out);
|
2015-03-04 09:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeString(" FROM ", out);
|
2015-03-25 10:09:33 +00:00
|
|
|
if (!db.empty())
|
|
|
|
{
|
|
|
|
writeProbablyBackQuotedString(db, out);
|
|
|
|
writeChar('.', out);
|
|
|
|
}
|
2015-03-04 09:36:30 +00:00
|
|
|
writeProbablyBackQuotedString(table, out);
|
2015-05-22 13:25:45 +00:00
|
|
|
|
|
|
|
if (!where.empty())
|
|
|
|
{
|
|
|
|
writeString(" WHERE ", out);
|
|
|
|
writeString(where, out);
|
|
|
|
}
|
|
|
|
|
2015-03-04 09:36:30 +00:00
|
|
|
writeChar(';', out);
|
2015-01-27 13:00:20 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 09:36:30 +00:00
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2015-05-22 13:25:45 +00:00
|
|
|
std::string composeLoadIdsQuery(const std::vector<std::uint64_t> & ids)
|
2015-03-04 09:36:30 +00:00
|
|
|
{
|
|
|
|
std::string query;
|
|
|
|
|
|
|
|
{
|
|
|
|
WriteBufferFromString out{query};
|
|
|
|
writeString("SELECT ", out);
|
|
|
|
|
2015-05-26 15:09:53 +00:00
|
|
|
writeProbablyBackQuotedString(dict_struct.id_name, out);
|
|
|
|
|
|
|
|
for (const auto & attr : dict_struct.attributes)
|
2015-03-04 09:36:30 +00:00
|
|
|
{
|
2015-05-26 15:09:53 +00:00
|
|
|
writeString(", ", out);
|
2015-03-04 09:36:30 +00:00
|
|
|
|
2015-05-26 15:09:53 +00:00
|
|
|
if (!attr.expression.empty())
|
|
|
|
{
|
|
|
|
writeString(attr.expression, out);
|
|
|
|
writeString(" AS ", out);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeProbablyBackQuotedString(attr.name, out);
|
2015-03-04 09:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeString(" FROM ", out);
|
2015-03-25 10:09:33 +00:00
|
|
|
if (!db.empty())
|
|
|
|
{
|
|
|
|
writeProbablyBackQuotedString(db, out);
|
|
|
|
writeChar('.', out);
|
|
|
|
}
|
2015-03-04 09:36:30 +00:00
|
|
|
writeProbablyBackQuotedString(table, out);
|
2015-05-22 13:25:45 +00:00
|
|
|
|
2015-03-04 09:36:30 +00:00
|
|
|
writeString(" WHERE ", out);
|
2015-05-22 13:25:45 +00:00
|
|
|
|
|
|
|
if (!where.empty())
|
|
|
|
{
|
|
|
|
writeString(where, out);
|
|
|
|
writeString(" AND ", out);
|
|
|
|
}
|
|
|
|
|
2015-05-26 15:09:53 +00:00
|
|
|
writeProbablyBackQuotedString(dict_struct.id_name, out);
|
2015-03-04 09:36:30 +00:00
|
|
|
writeString(" IN (", out);
|
|
|
|
|
2015-05-26 15:09:53 +00:00
|
|
|
auto first = true;
|
2015-03-04 09:36:30 +00:00
|
|
|
for (const auto id : ids)
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
writeString(", ", out);
|
|
|
|
|
|
|
|
first = false;
|
2015-03-25 10:09:33 +00:00
|
|
|
writeString(DB::toString(id), out);
|
2015-03-04 09:36:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
writeString(");", out);
|
|
|
|
}
|
2015-01-27 13:00:20 +00:00
|
|
|
|
|
|
|
return query;
|
|
|
|
}
|
|
|
|
|
2015-05-26 15:09:53 +00:00
|
|
|
const DictionaryStructure dict_struct;
|
2015-03-25 10:09:33 +00:00
|
|
|
const std::string db;
|
2015-01-30 13:43:16 +00:00
|
|
|
const std::string table;
|
2015-05-22 13:25:45 +00:00
|
|
|
const std::string where;
|
2015-01-26 16:53:44 +00:00
|
|
|
Block sample_block;
|
2015-02-05 18:48:29 +00:00
|
|
|
mutable mysqlxx::PoolWithFailover pool;
|
2015-01-27 13:00:20 +00:00
|
|
|
const std::string load_all_query;
|
2015-01-30 11:51:59 +00:00
|
|
|
mysqlxx::DateTime last_modification;
|
2015-01-26 16:53:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|