| 1 | #pragma once |
| 2 | |
| 3 | #include <DataTypes/IDataType.h> |
| 4 | #include <Parsers/IAST_fwd.h> |
| 5 | #include <Common/IFactoryWithAliases.h> |
| 6 | |
| 7 | |
| 8 | #include <functional> |
| 9 | #include <memory> |
| 10 | #include <unordered_map> |
| 11 | |
| 12 | |
| 13 | namespace DB |
| 14 | { |
| 15 | |
| 16 | class IDataType; |
| 17 | using DataTypePtr = std::shared_ptr<const IDataType>; |
| 18 | |
| 19 | /** Creates a data type by name of data type family and parameters. |
| 20 | */ |
| 21 | class DataTypeFactory final : private boost::noncopyable, public IFactoryWithAliases<std::function<DataTypePtr(const String & type_name, const ASTPtr & parameters)>> |
| 22 | { |
| 23 | private: |
| 24 | using SimpleCreator = std::function<DataTypePtr(const String & type_name)>; |
| 25 | using DataTypesDictionary = std::unordered_map<String, Creator>; |
| 26 | using CreatorWithCustom = std::function<std::pair<DataTypePtr, DataTypeCustomDescPtr>(const String & type_name, const ASTPtr & parameters)>; |
| 27 | using SimpleCreatorWithCustom = std::function<std::pair<DataTypePtr, DataTypeCustomDescPtr>(const String & type_name)>; |
| 28 | |
| 29 | public: |
| 30 | static DataTypeFactory & instance(); |
| 31 | |
| 32 | DataTypePtr get(const String & full_name) const; |
| 33 | DataTypePtr get(const String & family_name, const ASTPtr & parameters) const; |
| 34 | DataTypePtr get(const ASTPtr & ast) const; |
| 35 | |
| 36 | /// Register a type family by its name. |
| 37 | void registerDataType(const String & family_name, Creator creator, CaseSensitiveness case_sensitiveness = CaseSensitive); |
| 38 | |
| 39 | /// Register a simple data type, that have no parameters. |
| 40 | void registerSimpleDataType(const String & name, SimpleCreator creator, CaseSensitiveness case_sensitiveness = CaseSensitive); |
| 41 | |
| 42 | /// Register a customized type family |
| 43 | void registerDataTypeCustom(const String & family_name, CreatorWithCustom creator, CaseSensitiveness case_sensitiveness = CaseSensitive); |
| 44 | |
| 45 | /// Register a simple customized data type |
| 46 | void registerSimpleDataTypeCustom(const String & name, SimpleCreatorWithCustom creator, CaseSensitiveness case_sensitiveness = CaseSensitive); |
| 47 | |
| 48 | private: |
| 49 | const Creator& findCreatorByName(const String & family_name) const; |
| 50 | |
| 51 | private: |
| 52 | DataTypesDictionary data_types; |
| 53 | |
| 54 | /// Case insensitive data types will be additionally added here with lowercased name. |
| 55 | DataTypesDictionary case_insensitive_data_types; |
| 56 | |
| 57 | DataTypeFactory(); |
| 58 | ~DataTypeFactory() override; |
| 59 | |
| 60 | const DataTypesDictionary & getCreatorMap() const override { return data_types; } |
| 61 | |
| 62 | const DataTypesDictionary & getCaseInsensitiveCreatorMap() const override { return case_insensitive_data_types; } |
| 63 | |
| 64 | String getFactoryName() const override { return "DataTypeFactory" ; } |
| 65 | }; |
| 66 | |
| 67 | void registerDataTypeNumbers(DataTypeFactory & factory); |
| 68 | void registerDataTypeDecimal(DataTypeFactory & factory); |
| 69 | void registerDataTypeDate(DataTypeFactory & factory); |
| 70 | void registerDataTypeDateTime(DataTypeFactory & factory); |
| 71 | void registerDataTypeString(DataTypeFactory & factory); |
| 72 | void registerDataTypeFixedString(DataTypeFactory & factory); |
| 73 | void registerDataTypeEnum(DataTypeFactory & factory); |
| 74 | void registerDataTypeArray(DataTypeFactory & factory); |
| 75 | void registerDataTypeTuple(DataTypeFactory & factory); |
| 76 | void registerDataTypeNullable(DataTypeFactory & factory); |
| 77 | void registerDataTypeNothing(DataTypeFactory & factory); |
| 78 | void registerDataTypeUUID(DataTypeFactory & factory); |
| 79 | void registerDataTypeAggregateFunction(DataTypeFactory & factory); |
| 80 | void registerDataTypeNested(DataTypeFactory & factory); |
| 81 | void registerDataTypeInterval(DataTypeFactory & factory); |
| 82 | void registerDataTypeLowCardinality(DataTypeFactory & factory); |
| 83 | void registerDataTypeDomainIPv4AndIPv6(DataTypeFactory & factory); |
| 84 | void registerDataTypeDomainSimpleAggregateFunction(DataTypeFactory & factory); |
| 85 | void registerDataTypeDateTime64(DataTypeFactory & factory); |
| 86 | |
| 87 | } |
| 88 | |