2016-03-19 01:18:49 +00:00
# pragma once
2017-04-01 09:19:00 +00:00
# include <Core/Types.h>
# include <Core/NamesAndTypes.h>
# include <Storages/ColumnDefault.h>
2016-09-02 13:04:11 +00:00
# include <ctime>
2016-12-12 07:24:56 +00:00
# include <memory>
# include <functional>
class ThreadPool ;
2016-03-19 01:18:49 +00:00
namespace DB
{
2016-12-12 07:24:56 +00:00
class Context ;
class IStorage ;
using StoragePtr = std : : shared_ptr < IStorage > ;
class IAST ;
using ASTPtr = std : : shared_ptr < IAST > ;
2017-01-23 18:05:07 +00:00
struct Settings ;
2016-12-12 07:24:56 +00:00
2017-01-23 18:05:07 +00:00
/** Allows to iterate over tables.
2016-03-19 01:18:49 +00:00
*/
class IDatabaseIterator
{
public :
2017-04-01 07:20:54 +00:00
virtual void next ( ) = 0 ;
virtual bool isValid ( ) const = 0 ;
2016-03-19 01:18:49 +00:00
2017-04-01 07:20:54 +00:00
virtual const String & name ( ) const = 0 ;
virtual StoragePtr & table ( ) const = 0 ;
2016-03-19 01:18:49 +00:00
2017-04-01 07:20:54 +00:00
virtual ~ IDatabaseIterator ( ) { }
2016-03-19 01:18:49 +00:00
} ;
using DatabaseIteratorPtr = std : : unique_ptr < IDatabaseIterator > ;
2017-01-23 18:05:07 +00:00
/** Database engine.
* It is responsible for :
* - initialization of set of known tables ;
* - checking existence of a table and getting a table object ;
* - retrieving a list of all tables ;
* - creating and dropping tables ;
* - renaming tables and moving between databases with same engine .
2016-03-19 01:18:49 +00:00
*/
class IDatabase : public std : : enable_shared_from_this < IDatabase >
{
public :
2017-04-01 07:20:54 +00:00
/// Get name of database engine.
virtual String getEngineName ( ) const = 0 ;
2016-03-19 01:18:49 +00:00
2017-04-17 11:56:55 +00:00
/// Load a set of existing tables. If thread_pool is specified, use it.
/// You can call only once, right after the object is created.
2017-09-11 12:39:01 +00:00
virtual void loadTables (
Context & context ,
ThreadPool * thread_pool ,
bool has_force_restore_data_flag ) = 0 ;
2016-06-10 20:46:58 +00:00
2017-04-17 11:56:55 +00:00
/// Check the existence of the table.
2017-09-11 12:39:01 +00:00
virtual bool isTableExist (
const Context & context ,
const String & name ) const = 0 ;
2016-03-19 01:18:49 +00:00
2017-04-17 11:56:55 +00:00
/// Get the table for work. Return nullptr if there is no table.
2017-09-11 12:39:01 +00:00
virtual StoragePtr tryGetTable (
const Context & context ,
2018-01-30 17:47:04 +00:00
const String & name ) const = 0 ;
2016-03-19 01:18:49 +00:00
2017-04-17 11:56:55 +00:00
/// Get an iterator that allows you to pass through all the tables.
/// It is possible to have "hidden" tables that are not visible when passing through, but are visible if you get them by name using the functions above.
2017-09-11 12:39:01 +00:00
virtual DatabaseIteratorPtr getIterator ( const Context & context ) = 0 ;
2016-03-19 01:18:49 +00:00
2017-04-17 11:56:55 +00:00
/// Is the database empty.
2017-09-11 12:39:01 +00:00
virtual bool empty ( const Context & context ) const = 0 ;
2016-03-19 01:18:49 +00:00
2017-04-17 11:56:55 +00:00
/// Add the table to the database. Record its presence in the metadata.
2017-04-01 07:20:54 +00:00
virtual void createTable (
2017-09-11 12:39:01 +00:00
const Context & context ,
const String & name ,
const StoragePtr & table ,
2017-10-25 19:52:32 +00:00
const ASTPtr & query ) = 0 ;
2016-03-19 01:18:49 +00:00
2017-04-17 11:56:55 +00:00
/// Delete the table from the database and return it. Delete the metadata.
2017-09-11 12:39:01 +00:00
virtual void removeTable (
const Context & context ,
const String & name ) = 0 ;
2016-03-19 01:18:49 +00:00
2017-04-17 11:56:55 +00:00
/// Add a table to the database, but do not add it to the metadata. The database may not support this method.
2017-04-01 07:20:54 +00:00
virtual void attachTable ( const String & name , const StoragePtr & table ) = 0 ;
2016-03-19 01:18:49 +00:00
2017-04-17 11:56:55 +00:00
/// Forget about the table without deleting it, and return it. The database may not support this method.
2017-04-01 07:20:54 +00:00
virtual StoragePtr detachTable ( const String & name ) = 0 ;
2016-03-19 01:18:49 +00:00
2017-04-17 11:56:55 +00:00
/// Rename the table and possibly move the table to another database.
2017-04-01 07:20:54 +00:00
virtual void renameTable (
2017-09-11 12:39:01 +00:00
const Context & context ,
const String & name ,
IDatabase & to_database ,
const String & to_name ) = 0 ;
2016-09-02 13:04:11 +00:00
2017-09-17 18:49:43 +00:00
using ASTModifier = std : : function < void ( IAST & ) > ;
2016-05-13 21:08:19 +00:00
2017-04-17 11:56:55 +00:00
/// Change the table structure in metadata.
/// You must call under the TableStructureLock of the corresponding table . If engine_modifier is empty, then engine does not change.
2017-04-01 07:20:54 +00:00
virtual void alterTable (
const Context & context ,
const String & name ,
const NamesAndTypesList & columns ,
const NamesAndTypesList & materialized_columns ,
const NamesAndTypesList & alias_columns ,
const ColumnDefaults & column_defaults ,
const ASTModifier & engine_modifier ) = 0 ;
2016-05-13 21:08:19 +00:00
2017-09-11 12:39:01 +00:00
/// Returns time of table's metadata change, 0 if there is no corresponding metadata file.
virtual time_t getTableMetadataModificationTime (
const Context & context ,
const String & name ) = 0 ;
2017-04-17 11:56:55 +00:00
/// Get the CREATE TABLE query for the table. It can also provide information for detached tables for which there is metadata.
2017-09-11 12:39:01 +00:00
virtual ASTPtr getCreateQuery (
const Context & context ,
const String & name ) const = 0 ;
2016-03-19 01:18:49 +00:00
2017-11-03 19:53:10 +00:00
/// Returns path for persistent data storage if the database supports it, empty string otherwise
virtual String getDataPath ( const Context & context ) const = 0 ;
2017-04-17 11:56:55 +00:00
/// Ask all tables to complete the background threads they are using and delete all table objects.
2017-04-01 07:20:54 +00:00
virtual void shutdown ( ) = 0 ;
2016-03-19 01:18:49 +00:00
2017-04-17 11:56:55 +00:00
/// Delete metadata, the deletion of which differs from the recursive deletion of the directory, if any.
2017-04-01 07:20:54 +00:00
virtual void drop ( ) = 0 ;
2016-03-28 11:19:14 +00:00
2017-04-01 07:20:54 +00:00
virtual ~ IDatabase ( ) { }
2016-03-19 01:18:49 +00:00
} ;
using DatabasePtr = std : : shared_ptr < IDatabase > ;
using Databases = std : : map < String , DatabasePtr > ;
}
2016-09-02 13:04:11 +00:00