DataTypeFactory.h 3.6 KB
Newer Older
1 2
#pragma once

3
#include <DataTypes/IDataType.h>
4 5
#include <Parsers/IAST_fwd.h>
#include <Common/IFactoryWithAliases.h>
A
Anton Popov 已提交
6
#include <DataTypes/DataTypeCustom.h>
7

8

9 10 11 12
#include <functional>
#include <memory>
#include <unordered_map>

13 14 15 16

namespace DB
{

17
class IDataType;
18
using DataTypePtr = std::shared_ptr<const IDataType>;
19

20

21
/** Creates a data type by name of data type family and parameters.
22
  */
23
class DataTypeFactory final : private boost::noncopyable, public IFactoryWithAliases<std::function<DataTypePtr(const ASTPtr & parameters)>>
24
{
25
private:
26
    using SimpleCreator = std::function<DataTypePtr()>;
A
Alexey Milovidov 已提交
27
    using DataTypesDictionary = std::unordered_map<String, Value>;
28 29
    using CreatorWithCustom = std::function<std::pair<DataTypePtr,DataTypeCustomDescPtr>(const ASTPtr & parameters)>;
    using SimpleCreatorWithCustom = std::function<std::pair<DataTypePtr,DataTypeCustomDescPtr>()>;
30

31
public:
32 33
    static DataTypeFactory & instance();

34 35 36
    DataTypePtr get(const String & full_name) const;
    DataTypePtr get(const String & family_name, const ASTPtr & parameters) const;
    DataTypePtr get(const ASTPtr & ast) const;
37
    DataTypePtr getCustom(DataTypeCustomDescPtr customization) const;
38

39
    /// Register a type family by its name.
A
Alexey Milovidov 已提交
40
    void registerDataType(const String & family_name, Value creator, CaseSensitiveness case_sensitiveness = CaseSensitive);
41

42 43
    /// Register a simple data type, that have no parameters.
    void registerSimpleDataType(const String & name, SimpleCreator creator, CaseSensitiveness case_sensitiveness = CaseSensitive);
44

45 46
    /// Register a customized type family
    void registerDataTypeCustom(const String & family_name, CreatorWithCustom creator, CaseSensitiveness case_sensitiveness = CaseSensitive);
47

48 49
    /// Register a simple customized data type
    void registerSimpleDataTypeCustom(const String & name, SimpleCreatorWithCustom creator, CaseSensitiveness case_sensitiveness = CaseSensitive);
50 51

private:
A
Alexey Milovidov 已提交
52
    const Value & findCreatorByName(const String & family_name) const;
53

54 55 56 57 58
private:
    DataTypesDictionary data_types;

    /// Case insensitive data types will be additionally added here with lowercased name.
    DataTypesDictionary case_insensitive_data_types;
A
Alexey Milovidov 已提交
59

60
    DataTypeFactory();
61

A
Alexey Milovidov 已提交
62
    const DataTypesDictionary & getMap() const override { return data_types; }
63

A
Alexey Milovidov 已提交
64
    const DataTypesDictionary & getCaseInsensitiveMap() const override { return case_insensitive_data_types; }
65 66

    String getFactoryName() const override { return "DataTypeFactory"; }
67 68
};

K
kreuzerkrieg 已提交
69 70 71 72 73 74 75 76 77
void registerDataTypeNumbers(DataTypeFactory & factory);
void registerDataTypeDecimal(DataTypeFactory & factory);
void registerDataTypeDate(DataTypeFactory & factory);
void registerDataTypeDateTime(DataTypeFactory & factory);
void registerDataTypeString(DataTypeFactory & factory);
void registerDataTypeFixedString(DataTypeFactory & factory);
void registerDataTypeEnum(DataTypeFactory & factory);
void registerDataTypeArray(DataTypeFactory & factory);
void registerDataTypeTuple(DataTypeFactory & factory);
H
hexiaoting 已提交
78
void registerDataTypeMap(DataTypeFactory & factory);
K
kreuzerkrieg 已提交
79 80 81 82 83 84 85 86 87
void registerDataTypeNullable(DataTypeFactory & factory);
void registerDataTypeNothing(DataTypeFactory & factory);
void registerDataTypeUUID(DataTypeFactory & factory);
void registerDataTypeAggregateFunction(DataTypeFactory & factory);
void registerDataTypeNested(DataTypeFactory & factory);
void registerDataTypeInterval(DataTypeFactory & factory);
void registerDataTypeLowCardinality(DataTypeFactory & factory);
void registerDataTypeDomainIPv4AndIPv6(DataTypeFactory & factory);
void registerDataTypeDomainSimpleAggregateFunction(DataTypeFactory & factory);
88
void registerDataTypeDomainGeo(DataTypeFactory & factory);
K
kreuzerkrieg 已提交
89

90
}