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
|
#include <Interpreters/ProcessList.h>
#include "PostgreSQLOutputFormat.h"
namespace DB
{
PostgreSQLOutputFormat::PostgreSQLOutputFormat(WriteBuffer & out_, const Block & header_, const FormatSettings & settings_)
: IOutputFormat(header_, out_)
, format_settings(settings_)
, message_transport(&out)
{
}
void PostgreSQLOutputFormat::writePrefix()
{
const auto & header = getPort(PortKind::Main).getHeader();
auto data_types = header.getDataTypes();
if (header.columns())
{
std::vector<PostgreSQLProtocol::Messaging::FieldDescription> columns;
columns.reserve(header.columns());
for (size_t i = 0; i < header.columns(); ++i)
{
const auto & column_name = header.getColumnsWithTypeAndName()[i].name;
columns.emplace_back(column_name, data_types[i]->getTypeId());
serializations.emplace_back(data_types[i]->getDefaultSerialization());
}
message_transport.send(PostgreSQLProtocol::Messaging::RowDescription(columns));
}
}
void PostgreSQLOutputFormat::consume(Chunk chunk)
{
for (size_t i = 0; i != chunk.getNumRows(); ++i)
{
const Columns & columns = chunk.getColumns();
std::vector<std::shared_ptr<PostgreSQLProtocol::Messaging::ISerializable>> row;
row.reserve(chunk.getNumColumns());
for (size_t j = 0; j != chunk.getNumColumns(); ++j)
{
if (columns[j]->isNullAt(i))
row.push_back(std::make_shared<PostgreSQLProtocol::Messaging::NullField>());
else
{
WriteBufferFromOwnString ostr;
serializations[j]->serializeText(*columns[j], i, ostr, format_settings);
row.push_back(std::make_shared<PostgreSQLProtocol::Messaging::StringField>(std::move(ostr.str())));
}
}
message_transport.send(PostgreSQLProtocol::Messaging::DataRow(row));
}
}
void PostgreSQLOutputFormat::flush()
{
message_transport.flush();
}
void registerOutputFormatPostgreSQLWire(FormatFactory & factory)
{
factory.registerOutputFormat(
"PostgreSQLWire",
[](WriteBuffer & buf,
const Block & sample,
const FormatSettings & settings) { return std::make_shared<PostgreSQLOutputFormat>(buf, sample, settings); });
}
}
|