| 1 | #include <IO/WriteHelpers.h> |
| 2 | #include <IO/WriteBufferFromString.h> |
| 3 | #include <Processors/Formats/Impl/TSKVRowOutputFormat.h> |
| 4 | #include <Formats/FormatFactory.h> |
| 5 | |
| 6 | |
| 7 | |
| 8 | namespace DB |
| 9 | { |
| 10 | |
| 11 | TSKVRowOutputFormat::TSKVRowOutputFormat(WriteBuffer & out_, const Block & , FormatFactory::WriteCallback callback, const FormatSettings & format_settings_) |
| 12 | : TabSeparatedRowOutputFormat(out_, header, false, false, callback, format_settings_) |
| 13 | { |
| 14 | auto & sample = getPort(PortKind::Main).getHeader(); |
| 15 | NamesAndTypesList columns(sample.getNamesAndTypesList()); |
| 16 | fields.assign(columns.begin(), columns.end()); |
| 17 | |
| 18 | for (auto & field : fields) |
| 19 | { |
| 20 | WriteBufferFromOwnString wb; |
| 21 | writeAnyEscapedString<'='>(field.name.data(), field.name.data() + field.name.size(), wb); |
| 22 | writeCString("=" , wb); |
| 23 | field.name = wb.str(); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | |
| 28 | void TSKVRowOutputFormat::writeField(const IColumn & column, const IDataType & type, size_t row_num) |
| 29 | { |
| 30 | writeString(fields[field_number].name, out); |
| 31 | type.serializeAsTextEscaped(column, row_num, out, format_settings); |
| 32 | ++field_number; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | void TSKVRowOutputFormat::writeRowEndDelimiter() |
| 37 | { |
| 38 | writeChar('\n', out); |
| 39 | field_number = 0; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | void registerOutputFormatProcessorTSKV(FormatFactory & factory) |
| 44 | { |
| 45 | factory.registerOutputFormatProcessor("TSKV" , []( |
| 46 | WriteBuffer & buf, |
| 47 | const Block & sample, |
| 48 | FormatFactory::WriteCallback callback, |
| 49 | const FormatSettings & settings) |
| 50 | { |
| 51 | return std::make_shared<TSKVRowOutputFormat>(buf, sample, callback, settings); |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | } |
| 56 | |