aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Core/MySQL/PacketsProtocolText.cpp
blob: 728e8061e874533d64056634352e3e8ba07b822d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <Core/MySQL/PacketsProtocolText.h>
#include <IO/WriteBufferFromString.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h>

namespace DB
{

namespace MySQLProtocol
{

namespace ProtocolText
{

ResultSetRow::ResultSetRow(const Serializations & serializations, const Columns & columns_, int row_num_)
    : columns(columns_), row_num(row_num_)
{
    for (size_t i = 0; i < columns.size(); ++i)
    {
        if (columns[i]->isNullAt(row_num))
        {
            payload_size += 1;
            serialized.emplace_back("\xfb");
        }
        else
        {
            WriteBufferFromOwnString ostr;
            serializations[i]->serializeText(*columns[i], row_num, ostr, FormatSettings());
            payload_size += getLengthEncodedStringSize(ostr.str());
            serialized.push_back(std::move(ostr.str()));
        }
    }
}

size_t ResultSetRow::getPayloadSize() const
{
    return payload_size;
}

void ResultSetRow::writePayloadImpl(WriteBuffer & buffer) const
{
    for (size_t i = 0; i < columns.size(); ++i)
    {
        if (columns[i]->isNullAt(row_num))
            buffer.write(serialized[i].data(), 1);
        else
            writeLengthEncodedString(serialized[i], buffer);
    }
}

void ComFieldList::readPayloadImpl(ReadBuffer & payload)
{
    // Command byte has been already read from payload.
    readNullTerminated(table, payload);
    readStringUntilEOF(field_wildcard, payload);
}

ColumnDefinition::ColumnDefinition()
    : character_set(0x00), column_length(0), column_type(MYSQL_TYPE_DECIMAL), flags(0x00)
{
}

ColumnDefinition::ColumnDefinition(
    String schema_, String table_, String org_table_, String name_, String org_name_, uint16_t character_set_, uint32_t column_length_,
    ColumnType column_type_, uint16_t flags_, uint8_t decimals_, bool with_defaults_)
    : schema(std::move(schema_)), table(std::move(table_)), org_table(std::move(org_table_)), name(std::move(name_)),
      org_name(std::move(org_name_)), character_set(character_set_), column_length(column_length_), column_type(column_type_),
      flags(flags_), decimals(decimals_), is_comm_field_list_response(with_defaults_)
{
}

ColumnDefinition::ColumnDefinition(
    String name_, uint16_t character_set_, uint32_t column_length_, ColumnType column_type_, uint16_t flags_, uint8_t decimals_)
    : ColumnDefinition("", "", "", std::move(name_), "", character_set_, column_length_, column_type_, flags_, decimals_)
{
}

size_t ColumnDefinition::getPayloadSize() const
{
    return 12 +
           getLengthEncodedStringSize("def") +
           getLengthEncodedStringSize(schema) +
           getLengthEncodedStringSize(table) +
           getLengthEncodedStringSize(org_table) +
           getLengthEncodedStringSize(name) +
           getLengthEncodedStringSize(org_name) +
           getLengthEncodedNumberSize(next_length) +
           is_comm_field_list_response;
}

void ColumnDefinition::readPayloadImpl(ReadBuffer & payload)
{
    String def;
    readLengthEncodedString(def, payload);
    assert(def == "def");
    readLengthEncodedString(schema, payload);
    readLengthEncodedString(table, payload);
    readLengthEncodedString(org_table, payload);
    readLengthEncodedString(name, payload);
    readLengthEncodedString(org_name, payload);
    next_length = readLengthEncodedNumber(payload);
    payload.readStrict(reinterpret_cast<char *>(&character_set), 2);
    payload.readStrict(reinterpret_cast<char *>(&column_length), 4);
    payload.readStrict(reinterpret_cast<char *>(&column_type), 1);
    payload.readStrict(reinterpret_cast<char *>(&flags), 2);
    payload.readStrict(reinterpret_cast<char *>(&decimals), 1);
    payload.ignore(2);
}

void ColumnDefinition::writePayloadImpl(WriteBuffer & buffer) const
{
    writeLengthEncodedString(std::string("def"), buffer); /// always "def"
    writeLengthEncodedString(schema, buffer);
    writeLengthEncodedString(table, buffer);
    writeLengthEncodedString(org_table, buffer);
    writeLengthEncodedString(name, buffer);
    writeLengthEncodedString(org_name, buffer);
    writeLengthEncodedNumber(next_length, buffer);
    buffer.write(reinterpret_cast<const char *>(&character_set), 2);
    buffer.write(reinterpret_cast<const char *>(&column_length), 4);
    buffer.write(reinterpret_cast<const char *>(&column_type), 1);
    buffer.write(reinterpret_cast<const char *>(&flags), 2);
    buffer.write(reinterpret_cast<const char *>(&decimals), 1);
    writeChar(0x0, 2, buffer);
    if (is_comm_field_list_response)
    {
        /// We should write length encoded int with string size
        /// followed by string with some "default values" (possibly it's column defaults).
        /// But we just send NULL for simplicity.
        writeChar(0xfb, buffer);
    }
}

ColumnDefinition getColumnDefinition(const String & column_name, const TypeIndex type_index)
{
    ColumnType column_type;
    CharacterSet charset = CharacterSet::binary;
    int flags = 0;
    switch (type_index)
    {
        case TypeIndex::UInt8:
            column_type = ColumnType::MYSQL_TYPE_TINY;
            flags = ColumnDefinitionFlags::BINARY_FLAG | ColumnDefinitionFlags::UNSIGNED_FLAG;
            break;
        case TypeIndex::UInt16:
            column_type = ColumnType::MYSQL_TYPE_SHORT;
            flags = ColumnDefinitionFlags::BINARY_FLAG | ColumnDefinitionFlags::UNSIGNED_FLAG;
            break;
        case TypeIndex::UInt32:
            column_type = ColumnType::MYSQL_TYPE_LONG;
            flags = ColumnDefinitionFlags::BINARY_FLAG | ColumnDefinitionFlags::UNSIGNED_FLAG;
            break;
        case TypeIndex::UInt64:
            column_type = ColumnType::MYSQL_TYPE_LONGLONG;
            flags = ColumnDefinitionFlags::BINARY_FLAG | ColumnDefinitionFlags::UNSIGNED_FLAG;
            break;
        case TypeIndex::Int8:
            column_type = ColumnType::MYSQL_TYPE_TINY;
            flags = ColumnDefinitionFlags::BINARY_FLAG;
            break;
        case TypeIndex::Int16:
            column_type = ColumnType::MYSQL_TYPE_SHORT;
            flags = ColumnDefinitionFlags::BINARY_FLAG;
            break;
        case TypeIndex::Int32:
            column_type = ColumnType::MYSQL_TYPE_LONG;
            flags = ColumnDefinitionFlags::BINARY_FLAG;
            break;
        case TypeIndex::Int64:
            column_type = ColumnType::MYSQL_TYPE_LONGLONG;
            flags = ColumnDefinitionFlags::BINARY_FLAG;
            break;
        case TypeIndex::Float32:
            column_type = ColumnType::MYSQL_TYPE_FLOAT;
            flags = ColumnDefinitionFlags::BINARY_FLAG;
            break;
        case TypeIndex::Float64:
            column_type = ColumnType::MYSQL_TYPE_DOUBLE;
            flags = ColumnDefinitionFlags::BINARY_FLAG;
            break;
        case TypeIndex::Date:
            column_type = ColumnType::MYSQL_TYPE_DATE;
            flags = ColumnDefinitionFlags::BINARY_FLAG;
            break;
        case TypeIndex::DateTime:
            column_type = ColumnType::MYSQL_TYPE_DATETIME;
            flags = ColumnDefinitionFlags::BINARY_FLAG;
            break;
        case TypeIndex::String:
        case TypeIndex::FixedString:
            column_type = ColumnType::MYSQL_TYPE_STRING;
            charset = CharacterSet::utf8_general_ci;
            break;
        default:
            column_type = ColumnType::MYSQL_TYPE_STRING;
            charset = CharacterSet::utf8_general_ci;
            break;
    }
    return ColumnDefinition(column_name, charset, 0, column_type, flags, 0);
}

}

}

}