2019-10-02 10:10:45 +00:00
|
|
|
#pragma once
|
|
|
|
#include <Databases/DatabasesCommon.h>
|
2019-10-03 07:59:48 +00:00
|
|
|
#include <Parsers/ASTCreateQuery.h>
|
|
|
|
#include <Storages/IStorage.h>
|
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Common/escapeForFileName.h>
|
2019-10-02 10:10:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
String getTableMetadataPath(const String & base_path, const String & table_name);
|
|
|
|
String getDatabaseMetadataPath(const String & base_path);
|
2019-10-03 07:59:48 +00:00
|
|
|
ASTPtr getQueryFromMetadata(const String & metadata_path, bool throw_on_error = true);
|
|
|
|
ASTPtr getCreateQueryFromMetadata(const String & metadata_path, const String & database, bool throw_on_error);
|
2019-10-02 10:10:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 07:59:48 +00:00
|
|
|
/* Class to provide basic operations with tables stored on disk.
|
|
|
|
*/
|
|
|
|
class DatabaseOnDisk
|
2019-10-02 10:10:45 +00:00
|
|
|
{
|
|
|
|
public:
|
2019-10-02 12:58:13 +00:00
|
|
|
static void createTable(
|
2019-10-03 07:59:48 +00:00
|
|
|
IDatabase & database,
|
2019-10-02 10:10:45 +00:00
|
|
|
const Context & context,
|
|
|
|
const String & table_name,
|
|
|
|
const StoragePtr & table,
|
2019-10-02 12:58:13 +00:00
|
|
|
const ASTPtr & query);
|
2019-10-02 10:10:45 +00:00
|
|
|
|
2019-10-02 12:58:13 +00:00
|
|
|
static void removeTable(
|
2019-10-03 07:59:48 +00:00
|
|
|
IDatabase & database,
|
2019-10-02 10:10:45 +00:00
|
|
|
const Context & context,
|
2019-10-03 07:59:48 +00:00
|
|
|
const String & table_name,
|
|
|
|
Poco::Logger * log);
|
2019-10-02 10:10:45 +00:00
|
|
|
|
2019-10-03 07:59:48 +00:00
|
|
|
template <typename Database>
|
2019-10-02 12:58:13 +00:00
|
|
|
static void renameTable(
|
2019-10-03 07:59:48 +00:00
|
|
|
IDatabase & database,
|
2019-10-02 10:10:45 +00:00
|
|
|
const Context & context,
|
|
|
|
const String & table_name,
|
|
|
|
IDatabase & to_database,
|
|
|
|
const String & to_table_name,
|
2019-10-03 07:59:48 +00:00
|
|
|
TableStructureWriteLockHolder & lock);
|
2019-10-02 10:10:45 +00:00
|
|
|
|
2019-10-02 12:58:13 +00:00
|
|
|
static ASTPtr getCreateTableQuery(
|
2019-10-03 07:59:48 +00:00
|
|
|
const IDatabase & database,
|
2019-10-02 10:10:45 +00:00
|
|
|
const Context & context,
|
2019-10-03 07:59:48 +00:00
|
|
|
const String & table_name);
|
2019-10-02 10:10:45 +00:00
|
|
|
|
2019-10-02 12:58:13 +00:00
|
|
|
static ASTPtr tryGetCreateTableQuery(
|
2019-10-03 07:59:48 +00:00
|
|
|
const IDatabase & database,
|
2019-10-02 10:10:45 +00:00
|
|
|
const Context & context,
|
2019-10-03 07:59:48 +00:00
|
|
|
const String & table_name);
|
2019-10-02 10:10:45 +00:00
|
|
|
|
2019-10-03 07:59:48 +00:00
|
|
|
static ASTPtr getCreateDatabaseQuery(
|
|
|
|
const IDatabase & database,
|
|
|
|
const Context & context);
|
2019-10-02 10:10:45 +00:00
|
|
|
|
2019-10-03 07:59:48 +00:00
|
|
|
static void drop(const IDatabase & database);
|
2019-10-02 10:10:45 +00:00
|
|
|
|
2019-10-03 07:59:48 +00:00
|
|
|
static String getTableMetadataPath(
|
|
|
|
const IDatabase & database,
|
|
|
|
const String & table_name);
|
2019-10-02 10:10:45 +00:00
|
|
|
|
|
|
|
private:
|
2019-10-03 07:59:48 +00:00
|
|
|
static ASTPtr getCreateTableQueryImpl(
|
|
|
|
const IDatabase & database,
|
|
|
|
const Context & context,
|
|
|
|
const String & table_name,
|
|
|
|
bool throw_on_error);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int NOT_IMPLEMENTED;
|
|
|
|
extern const int UNKNOWN_TABLE;
|
|
|
|
extern const int FILE_DOESNT_EXIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Database>
|
|
|
|
void DatabaseOnDisk::renameTable(
|
|
|
|
IDatabase & database,
|
|
|
|
const Context & context,
|
|
|
|
const String & table_name,
|
|
|
|
IDatabase & to_database,
|
|
|
|
const String & to_table_name,
|
|
|
|
TableStructureWriteLockHolder & lock)
|
|
|
|
{
|
|
|
|
Database * to_database_concrete = typeid_cast<Database *>(&to_database);
|
|
|
|
|
|
|
|
if (!to_database_concrete)
|
|
|
|
throw Exception("Moving tables between databases of different engines is not supported", ErrorCodes::NOT_IMPLEMENTED);
|
|
|
|
|
|
|
|
StoragePtr table = database.tryGetTable(context, table_name);
|
|
|
|
|
|
|
|
if (!table)
|
|
|
|
throw Exception("Table " + database.getDatabaseName() + "." + table_name + " doesn't exist.", ErrorCodes::UNKNOWN_TABLE);
|
|
|
|
|
|
|
|
/// Notify the table that it is renamed. If the table does not support renaming, exception is thrown.
|
|
|
|
try
|
|
|
|
{
|
|
|
|
table->rename(context.getPath() + "/data/" + escapeForFileName(to_database_concrete->getDatabaseName()) + "/",
|
|
|
|
to_database_concrete->getDatabaseName(),
|
|
|
|
to_table_name, lock);
|
|
|
|
}
|
|
|
|
catch (const Exception &)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception & e)
|
|
|
|
{
|
|
|
|
/// Better diagnostics.
|
|
|
|
throw Exception{Exception::CreateFromPoco, e};
|
|
|
|
}
|
|
|
|
|
|
|
|
ASTPtr ast = detail::getQueryFromMetadata(detail::getTableMetadataPath(database.getMetadataPath(), table_name));
|
|
|
|
if (!ast)
|
|
|
|
throw Exception("There is no metadata file for table " + table_name, ErrorCodes::FILE_DOESNT_EXIST);
|
|
|
|
ast->as<ASTCreateQuery &>().table = to_table_name;
|
|
|
|
|
|
|
|
/// NOTE Non-atomic.
|
|
|
|
to_database_concrete->createTable(context, to_table_name, table, ast);
|
|
|
|
database.removeTable(context, table_name);
|
|
|
|
}
|
|
|
|
|
2019-10-02 10:10:45 +00:00
|
|
|
}
|