2019-10-02 10:10:45 +00:00
|
|
|
#pragma once
|
2019-10-03 09:31:59 +00:00
|
|
|
|
|
|
|
#include <Common/escapeForFileName.h>
|
2019-10-08 18:42:22 +00:00
|
|
|
#include <Common/quoteString.h>
|
2019-10-02 10:10:45 +00:00
|
|
|
#include <Databases/DatabasesCommon.h>
|
2019-10-03 07:59:48 +00:00
|
|
|
#include <Parsers/ASTCreateQuery.h>
|
|
|
|
#include <Storages/IStorage.h>
|
2019-10-02 10:10:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-05-20 20:16:32 +00:00
|
|
|
class Context;
|
2019-10-10 17:33:01 +00:00
|
|
|
std::pair<String, StoragePtr> createTableFromAST(
|
|
|
|
ASTCreateQuery ast_create_query,
|
|
|
|
const String & database_name,
|
2019-11-01 12:47:55 +00:00
|
|
|
const String & table_data_path_relative,
|
2021-05-31 14:49:02 +00:00
|
|
|
ContextMutablePtr context,
|
2021-09-13 19:11:16 +00:00
|
|
|
bool force_restore);
|
2019-10-10 17:33:01 +00:00
|
|
|
|
2019-12-02 19:11:18 +00:00
|
|
|
/** Get the string with the table definition based on the CREATE query.
|
2019-10-10 17:33:01 +00:00
|
|
|
* It is an ATTACH query that you can execute to create a table from the correspondent database.
|
|
|
|
* See the implementation.
|
|
|
|
*/
|
|
|
|
String getObjectDefinitionFromCreateQuery(const ASTPtr & query);
|
|
|
|
|
|
|
|
|
2019-10-08 00:24:26 +00:00
|
|
|
/* Class to provide basic operations with tables when metadata is stored on disk in .sql files.
|
2019-10-03 07:59:48 +00:00
|
|
|
*/
|
2019-11-06 16:05:04 +00:00
|
|
|
class DatabaseOnDisk : public DatabaseWithOwnTablesBase
|
2019-10-02 10:10:45 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-04-10 23:33:54 +00:00
|
|
|
DatabaseOnDisk(const String & name, const String & metadata_path_, const String & data_path_, const String & logger, ContextPtr context);
|
2019-10-10 17:33:01 +00:00
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
void createTable(
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context,
|
2019-10-03 07:59:48 +00:00
|
|
|
const String & table_name,
|
2019-11-06 16:05:04 +00:00
|
|
|
const StoragePtr & table,
|
|
|
|
const ASTPtr & query) override;
|
2019-10-02 10:10:45 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void detachTablePermanently(ContextPtr context, const String & table_name) override;
|
2020-11-30 17:52:32 +00:00
|
|
|
|
2020-01-22 11:30:11 +00:00
|
|
|
void dropTable(
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context,
|
2020-03-20 00:07:52 +00:00
|
|
|
const String & table_name,
|
2020-03-20 12:45:06 +00:00
|
|
|
bool no_delay) override;
|
2019-10-10 17:33:01 +00:00
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
void renameTable(
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context,
|
2019-10-02 10:10:45 +00:00
|
|
|
const String & table_name,
|
|
|
|
IDatabase & to_database,
|
2020-03-31 20:38:05 +00:00
|
|
|
const String & to_table_name,
|
2020-07-16 21:41:26 +00:00
|
|
|
bool exchange,
|
|
|
|
bool dictionary) override;
|
2019-10-02 10:10:45 +00:00
|
|
|
|
2020-04-23 16:51:48 +00:00
|
|
|
ASTPtr getCreateDatabaseQuery() const override;
|
2019-10-02 10:10:45 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void drop(ContextPtr context) override;
|
2019-10-02 10:10:45 +00:00
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
String getObjectMetadataPath(const String & object_name) const override;
|
2019-10-02 10:10:45 +00:00
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
time_t getObjectMetadataModificationTime(const String & object_name) const override;
|
2019-10-03 08:35:58 +00:00
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
String getDataPath() const override { return data_path; }
|
2019-12-25 16:13:48 +00:00
|
|
|
String getTableDataPath(const String & table_name) const override { return data_path + escapeForFileName(table_name) + "/"; }
|
2021-09-06 22:13:54 +00:00
|
|
|
String getTableDataPath(const ASTCreateQuery & query) const override { return getTableDataPath(query.getTable()); }
|
2019-11-06 16:05:04 +00:00
|
|
|
String getMetadataPath() const override { return metadata_path; }
|
2019-10-03 08:35:58 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
static ASTPtr parseQueryFromMetadata(Poco::Logger * log, ContextPtr context, const String & metadata_file_path, bool throw_on_error = true, bool remove_empty = false);
|
2020-03-19 21:14:52 +00:00
|
|
|
|
2020-12-14 14:21:31 +00:00
|
|
|
/// will throw when the table we want to attach already exists (in active / detached / detached permanently form)
|
2020-12-15 14:46:13 +00:00
|
|
|
void checkMetadataFilenameAvailability(const String & to_table_name) const;
|
|
|
|
void checkMetadataFilenameAvailabilityUnlocked(const String & to_table_name, std::unique_lock<std::mutex> &) const;
|
2020-12-14 14:21:31 +00:00
|
|
|
|
2021-09-08 22:25:08 +00:00
|
|
|
void modifySettingsMetadata(const SettingsChanges & settings_changes, ContextPtr query_context);
|
2021-09-04 22:59:44 +00:00
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
protected:
|
2020-01-22 11:30:11 +00:00
|
|
|
static constexpr const char * create_suffix = ".tmp";
|
|
|
|
static constexpr const char * drop_suffix = ".tmp_drop";
|
2020-12-15 14:46:13 +00:00
|
|
|
static constexpr const char * detached_suffix = ".detached";
|
2020-01-22 11:30:11 +00:00
|
|
|
|
2019-10-03 08:27:43 +00:00
|
|
|
using IteratingFunction = std::function<void(const String &)>;
|
2020-01-23 19:10:09 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void iterateMetadataFiles(ContextPtr context, const IteratingFunction & process_metadata_file) const;
|
2019-10-03 08:27:43 +00:00
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
ASTPtr getCreateTableQueryImpl(
|
2019-10-03 07:59:48 +00:00
|
|
|
const String & table_name,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context,
|
2019-11-06 16:05:04 +00:00
|
|
|
bool throw_on_error) const override;
|
2019-10-10 17:33:01 +00:00
|
|
|
|
2020-04-23 16:51:48 +00:00
|
|
|
ASTPtr getCreateQueryFromMetadata(const String & metadata_path, bool throw_on_error) const;
|
2021-11-19 10:25:55 +00:00
|
|
|
ASTPtr getCreateQueryFromStorage(const String & table_name, const StoragePtr & storage, bool throw_on_error) const;
|
2019-10-03 07:59:48 +00:00
|
|
|
|
2020-03-23 00:12:13 +00:00
|
|
|
virtual void commitCreateTable(const ASTCreateQuery & query, const StoragePtr & table,
|
2021-04-10 23:33:54 +00:00
|
|
|
const String & table_metadata_tmp_path, const String & table_metadata_path, ContextPtr query_context);
|
2020-01-23 19:10:09 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
virtual void removeDetachedPermanentlyFlag(ContextPtr context, const String & table_name, const String & table_metadata_path, bool attach) const;
|
2021-03-09 00:48:37 +00:00
|
|
|
|
2019-11-06 16:05:04 +00:00
|
|
|
const String metadata_path;
|
2020-04-06 23:22:44 +00:00
|
|
|
const String data_path;
|
2021-09-04 22:59:44 +00:00
|
|
|
|
2021-09-08 22:25:08 +00:00
|
|
|
/// For alter settings.
|
|
|
|
std::mutex modify_settings_mutex;
|
2019-11-06 16:05:04 +00:00
|
|
|
};
|
2019-10-03 07:59:48 +00:00
|
|
|
|
2019-10-02 10:10:45 +00:00
|
|
|
}
|