1 | #pragma once |
2 | |
3 | #include <memory> |
4 | #include <cstddef> |
5 | #include <Core/Types.h> |
6 | |
7 | namespace DB |
8 | { |
9 | |
10 | class ReadBuffer; |
11 | class WriteBuffer; |
12 | struct FormatSettings; |
13 | class IColumn; |
14 | |
15 | /** Allow to customize an existing data type and set a different name and/or text serialization/deserialization methods. |
16 | * See use in IPv4 and IPv6 data types, and also in SimpleAggregateFunction. |
17 | */ |
18 | class IDataTypeCustomName |
19 | { |
20 | public: |
21 | virtual ~IDataTypeCustomName() {} |
22 | |
23 | virtual String getName() const = 0; |
24 | }; |
25 | |
26 | class IDataTypeCustomTextSerialization |
27 | { |
28 | public: |
29 | virtual ~IDataTypeCustomTextSerialization() {} |
30 | |
31 | /** Text serialization for displaying on a terminal or saving into a text file, and the like. |
32 | * Without escaping or quoting. |
33 | */ |
34 | virtual void serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const = 0; |
35 | |
36 | /** Text deserialization without quoting or escaping. |
37 | */ |
38 | virtual void deserializeWholeText(IColumn & column, ReadBuffer & istr, const FormatSettings &) const = 0; |
39 | |
40 | /** Text serialization with escaping but without quoting. |
41 | */ |
42 | virtual void serializeTextEscaped(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const = 0; |
43 | virtual void deserializeTextEscaped(IColumn & column, ReadBuffer & istr, const FormatSettings &) const = 0; |
44 | |
45 | /** Text serialization as a literal that may be inserted into a query. |
46 | */ |
47 | virtual void serializeTextQuoted(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const = 0; |
48 | virtual void deserializeTextQuoted(IColumn & column, ReadBuffer & istr, const FormatSettings &) const = 0; |
49 | |
50 | /** Text serialization for the CSV format. |
51 | */ |
52 | virtual void serializeTextCSV(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const = 0; |
53 | virtual void deserializeTextCSV(IColumn & column, ReadBuffer & istr, const FormatSettings &) const = 0; |
54 | |
55 | /** Text serialization intended for using in JSON format. |
56 | */ |
57 | virtual void serializeTextJSON(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const = 0; |
58 | virtual void deserializeTextJSON(IColumn & column, ReadBuffer & istr, const FormatSettings &) const = 0; |
59 | |
60 | /** Text serialization for putting into the XML format. |
61 | */ |
62 | virtual void serializeTextXML(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const = 0; |
63 | }; |
64 | |
65 | using DataTypeCustomNamePtr = std::unique_ptr<const IDataTypeCustomName>; |
66 | using DataTypeCustomTextSerializationPtr = std::unique_ptr<const IDataTypeCustomTextSerialization>; |
67 | |
68 | /** Describe a data type customization |
69 | */ |
70 | struct DataTypeCustomDesc |
71 | { |
72 | DataTypeCustomNamePtr name; |
73 | DataTypeCustomTextSerializationPtr text_serialization; |
74 | |
75 | DataTypeCustomDesc(DataTypeCustomNamePtr name_, DataTypeCustomTextSerializationPtr text_serialization_) |
76 | : name(std::move(name_)), text_serialization(std::move(text_serialization_)) {} |
77 | }; |
78 | |
79 | using DataTypeCustomDescPtr = std::unique_ptr<DataTypeCustomDesc>; |
80 | |
81 | /** A simple implementation of IDataTypeCustomName |
82 | */ |
83 | class DataTypeCustomFixedName : public IDataTypeCustomName |
84 | { |
85 | private: |
86 | String name; |
87 | public: |
88 | DataTypeCustomFixedName(String name_) : name(name_) {} |
89 | String getName() const override { return name; } |
90 | }; |
91 | |
92 | } |
93 | |