1 | #include <Interpreters/ExternalLoaderDatabaseConfigRepository.h> |
2 | #include <Interpreters/ExternalDictionariesLoader.h> |
3 | #include <Dictionaries/getDictionaryConfigurationFromAST.h> |
4 | |
5 | namespace DB |
6 | { |
7 | |
8 | namespace ErrorCodes |
9 | { |
10 | extern const int UNKNOWN_DICTIONARY; |
11 | } |
12 | |
13 | namespace |
14 | { |
15 | String trimDatabaseName(const std::string & loadable_definition_name, const IDatabase & database) |
16 | { |
17 | const auto & dbname = database.getDatabaseName(); |
18 | if (!startsWith(loadable_definition_name, dbname)) |
19 | throw Exception( |
20 | "Loadable '" + loadable_definition_name + "' is not from database '" + database.getDatabaseName(), ErrorCodes::UNKNOWN_DICTIONARY); |
21 | /// dbname.loadable_name |
22 | ///--> remove <--- |
23 | return loadable_definition_name.substr(dbname.length() + 1); |
24 | } |
25 | } |
26 | |
27 | ExternalLoaderDatabaseConfigRepository::ExternalLoaderDatabaseConfigRepository(IDatabase & database_, const Context & context_) |
28 | : name(database_.getDatabaseName()) |
29 | , database(database_) |
30 | , context(context_) |
31 | { |
32 | } |
33 | |
34 | LoadablesConfigurationPtr ExternalLoaderDatabaseConfigRepository::load(const std::string & loadable_definition_name) |
35 | { |
36 | String dictname = trimDatabaseName(loadable_definition_name, database); |
37 | return getDictionaryConfigurationFromAST(database.getCreateDictionaryQuery(context, dictname)->as<const ASTCreateQuery &>()); |
38 | } |
39 | |
40 | bool ExternalLoaderDatabaseConfigRepository::exists(const std::string & loadable_definition_name) |
41 | { |
42 | return database.isDictionaryExist(context, trimDatabaseName(loadable_definition_name, database)); |
43 | } |
44 | |
45 | Poco::Timestamp ExternalLoaderDatabaseConfigRepository::getUpdateTime(const std::string & loadable_definition_name) |
46 | { |
47 | return database.getObjectMetadataModificationTime(trimDatabaseName(loadable_definition_name, database)); |
48 | } |
49 | |
50 | std::set<std::string> ExternalLoaderDatabaseConfigRepository::getAllLoadablesDefinitionNames() |
51 | { |
52 | std::set<std::string> result; |
53 | const auto & dbname = database.getDatabaseName(); |
54 | auto itr = database.getDictionariesIterator(context); |
55 | while (itr && itr->isValid()) |
56 | { |
57 | result.insert(dbname + "." + itr->name()); |
58 | itr->next(); |
59 | } |
60 | return result; |
61 | } |
62 | |
63 | } |
64 | |