1#pragma once
2
3#include <Common/escapeForFileName.h>
4#include <Common/quoteString.h>
5#include <Databases/DatabasesCommon.h>
6#include <Interpreters/Context.h>
7#include <Parsers/ASTCreateQuery.h>
8#include <Storages/IStorage.h>
9
10
11namespace DB
12{
13
14std::pair<String, StoragePtr> createTableFromAST(
15 ASTCreateQuery ast_create_query,
16 const String & database_name,
17 const String & table_data_path_relative,
18 Context & context,
19 bool has_force_restore_data_flag);
20
21/** Get the string with the table definition based on the CREATE query.
22 * It is an ATTACH query that you can execute to create a table from the correspondent database.
23 * See the implementation.
24 */
25String getObjectDefinitionFromCreateQuery(const ASTPtr & query);
26
27
28/* Class to provide basic operations with tables when metadata is stored on disk in .sql files.
29 */
30class DatabaseOnDisk : public DatabaseWithOwnTablesBase
31{
32public:
33 DatabaseOnDisk(const String & name, const String & metadata_path_, const String & logger)
34 : DatabaseWithOwnTablesBase(name, logger)
35 , metadata_path(metadata_path_)
36 , data_path("data/" + escapeForFileName(database_name) + "/") {}
37
38 void createTable(
39 const Context & context,
40 const String & table_name,
41 const StoragePtr & table,
42 const ASTPtr & query) override;
43
44 void removeTable(
45 const Context & context,
46 const String & table_name) override;
47
48 void renameTable(
49 const Context & context,
50 const String & table_name,
51 IDatabase & to_database,
52 const String & to_table_name,
53 TableStructureWriteLockHolder & lock) override;
54
55 ASTPtr getCreateDatabaseQuery() const override;
56
57 void drop(const Context & context) override;
58
59 String getObjectMetadataPath(const String & object_name) const override;
60
61 time_t getObjectMetadataModificationTime(const String & object_name) const override;
62
63 String getDataPath() const override { return data_path; }
64 String getTableDataPath(const String & table_name) const override { return data_path + escapeForFileName(table_name) + "/"; }
65 String getTableDataPath(const ASTCreateQuery & query) const override { return getTableDataPath(query.table); }
66 String getMetadataPath() const override { return metadata_path; }
67
68protected:
69 using IteratingFunction = std::function<void(const String &)>;
70 void iterateMetadataFiles(const Context & context, const IteratingFunction & iterating_function) const;
71
72 ASTPtr getCreateTableQueryImpl(
73 const Context & context,
74 const String & table_name,
75 bool throw_on_error) const override;
76
77 ASTPtr parseQueryFromMetadata(const String & metadata_file_path, bool throw_on_error = true, bool remove_empty = false) const;
78 ASTPtr getCreateQueryFromMetadata(const String & metadata_path, bool throw_on_error) const;
79
80
81 const String metadata_path;
82 const String data_path;
83};
84
85}
86