1#include <IO/ReadHelpers.h>
2#include <IO/WriteHelpers.h>
3
4#include <Columns/ColumnsNumber.h>
5#include <DataTypes/DataTypeDate.h>
6#include <DataTypes/DataTypeFactory.h>
7#include <Formats/ProtobufReader.h>
8#include <Formats/ProtobufWriter.h>
9
10#include <Common/assert_cast.h>
11
12
13namespace DB
14{
15
16void DataTypeDate::serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const
17{
18 writeDateText(DayNum(assert_cast<const ColumnUInt16 &>(column).getData()[row_num]), ostr);
19}
20
21void DataTypeDate::deserializeWholeText(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const
22{
23 deserializeTextEscaped(column, istr, settings);
24}
25
26void DataTypeDate::deserializeTextEscaped(IColumn & column, ReadBuffer & istr, const FormatSettings &) const
27{
28 DayNum x;
29 readDateText(x, istr);
30 assert_cast<ColumnUInt16 &>(column).getData().push_back(x);
31}
32
33void DataTypeDate::serializeTextEscaped(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const
34{
35 serializeText(column, row_num, ostr, settings);
36}
37
38void DataTypeDate::serializeTextQuoted(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const
39{
40 writeChar('\'', ostr);
41 serializeText(column, row_num, ostr, settings);
42 writeChar('\'', ostr);
43}
44
45void DataTypeDate::deserializeTextQuoted(IColumn & column, ReadBuffer & istr, const FormatSettings &) const
46{
47 DayNum x;
48 assertChar('\'', istr);
49 readDateText(x, istr);
50 assertChar('\'', istr);
51 assert_cast<ColumnUInt16 &>(column).getData().push_back(x); /// It's important to do this at the end - for exception safety.
52}
53
54void DataTypeDate::serializeTextJSON(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const
55{
56 writeChar('"', ostr);
57 serializeText(column, row_num, ostr, settings);
58 writeChar('"', ostr);
59}
60
61void DataTypeDate::deserializeTextJSON(IColumn & column, ReadBuffer & istr, const FormatSettings &) const
62{
63 DayNum x;
64 assertChar('"', istr);
65 readDateText(x, istr);
66 assertChar('"', istr);
67 assert_cast<ColumnUInt16 &>(column).getData().push_back(x);
68}
69
70void DataTypeDate::serializeTextCSV(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings & settings) const
71{
72 writeChar('"', ostr);
73 serializeText(column, row_num, ostr, settings);
74 writeChar('"', ostr);
75}
76
77void DataTypeDate::deserializeTextCSV(IColumn & column, ReadBuffer & istr, const FormatSettings &) const
78{
79 LocalDate value;
80 readCSV(value, istr);
81 assert_cast<ColumnUInt16 &>(column).getData().push_back(value.getDayNum());
82}
83
84void DataTypeDate::serializeProtobuf(const IColumn & column, size_t row_num, ProtobufWriter & protobuf, size_t & value_index) const
85{
86 if (value_index)
87 return;
88 value_index = static_cast<bool>(protobuf.writeDate(DayNum(assert_cast<const ColumnUInt16 &>(column).getData()[row_num])));
89}
90
91void DataTypeDate::deserializeProtobuf(IColumn & column, ProtobufReader & protobuf, bool allow_add_row, bool & row_added) const
92{
93 row_added = false;
94 DayNum d;
95 if (!protobuf.readDate(d))
96 return;
97
98 auto & container = assert_cast<ColumnUInt16 &>(column).getData();
99 if (allow_add_row)
100 {
101 container.emplace_back(d);
102 row_added = true;
103 }
104 else
105 container.back() = d;
106}
107
108bool DataTypeDate::equals(const IDataType & rhs) const
109{
110 return typeid(rhs) == typeid(*this);
111}
112
113
114void registerDataTypeDate(DataTypeFactory & factory)
115{
116 const auto & creator = [&](const String & /*type_name*/)
117 {
118 return DataTypePtr(std::make_shared<DataTypeDate>());
119 };
120
121 factory.registerSimpleDataType("Date", creator, DataTypeFactory::CaseInsensitive);
122}
123
124}
125