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