InterpreterCreateQuery.h 2.8 KB
Newer Older
1 2
#pragma once

3
#include <Interpreters/IInterpreter.h>
4
#include <Storages/ColumnsDescription.h>
5
#include <Storages/IStorage_fwd.h>
N
Nikita Vasilev 已提交
6
#include <Storages/IndicesDescription.h>
7
#include <Storages/ConstraintsDescription.h>
8
#include <Common/ThreadPool.h>
9
#include <Access/AccessRightsElement.h>
10 11 12 13


namespace DB
{
14

15
class Context;
16
class ASTCreateQuery;
17
class ASTExpressionList;
A
Alexey Milovidov 已提交
18
class ASTConstraintDeclaration;
19

20

21 22
/** Allows to create new table or database,
  *  or create an object for existing table or database.
23
  */
24
class InterpreterCreateQuery : public IInterpreter
25 26
{
public:
27
    InterpreterCreateQuery(const ASTPtr & query_ptr_, Context & context_);
28 29 30 31

    BlockIO execute() override;

    /// List of columns and their types in AST.
32
    static ASTPtr formatColumns(const NamesAndTypesList & columns);
33
    static ASTPtr formatColumns(const ColumnsDescription & columns);
34

N
Nikita Vasilev 已提交
35
    static ASTPtr formatIndices(const IndicesDescription & indices);
36
    static ASTPtr formatConstraints(const ConstraintsDescription & constraints);
N
Nikita Vasilev 已提交
37

38 39 40 41 42
    void setForceRestoreData(bool has_force_restore_data_flag_)
    {
        has_force_restore_data_flag = has_force_restore_data_flag_;
    }

43 44 45 46 47
    void setInternal(bool internal_)
    {
        internal = internal_;
    }

48
    /// Obtain information about columns, their types, default values and column comments, for case when columns in CREATE query is specified explicitly.
49
    static ColumnsDescription getColumnsDescription(const ASTExpressionList & columns, const Context & context);
A
Alexey Milovidov 已提交
50
    static ConstraintsDescription getConstraintsDescription(const ASTExpressionList * constraints);
51 52

private:
53 54 55 56 57 58 59
    struct TableProperties
    {
        ColumnsDescription columns;
        IndicesDescription indices;
        ConstraintsDescription constraints;
    };

60
    BlockIO createDatabase(ASTCreateQuery & create);
61
    BlockIO createTable(ASTCreateQuery & create);
62
    BlockIO createDictionary(ASTCreateQuery & create);
63

64 65 66
    /// Calculate list of columns, constraints, indices, etc... of table. Rewrite query in canonical way.
    TableProperties setProperties(ASTCreateQuery & create) const;
    void validateTableStructure(const ASTCreateQuery & create, const TableProperties & properties) const;
67
    void setEngine(ASTCreateQuery & create) const;
68
    AccessRightsElements getRequiredAccess() const;
69

70
    /// Create IStorage and add it to database. If table already exists and IF NOT EXISTS specified, do nothing and return false.
71
    bool doCreateTable(const ASTCreateQuery & create, const TableProperties & properties);
72
    /// Inserts data in created table if it's CREATE ... SELECT
73
    BlockIO fillTableIfNeeded(const ASTCreateQuery & create);
74

75
    ASTPtr query_ptr;
76
    Context & context;
77

78 79
    /// Skip safety threshold when loading tables.
    bool has_force_restore_data_flag = false;
80 81
    /// Is this an internal query - not from the user.
    bool internal = false;
82 83
};
}