1#pragma once
2
3#include <Formats/FormatFactory.h>
4#include <Processors/Formats/IOutputFormat.h>
5
6#include <string>
7
8
9namespace DB
10{
11
12class WriteBuffer;
13
14/** Output format that writes data row by row.
15 */
16class IRowOutputFormat : public IOutputFormat
17{
18protected:
19 DataTypes types;
20
21 void consume(Chunk chunk) override;
22 void consumeTotals(Chunk chunk) override;
23 void consumeExtremes(Chunk chunk) override;
24 void finalize() override;
25
26public:
27 IRowOutputFormat(const Block & header, WriteBuffer & out_, FormatFactory::WriteCallback callback)
28 : IOutputFormat(header, out_), types(header.getDataTypes()), write_single_row_callback(callback)
29 {
30 }
31
32 /** Write a row.
33 * Default implementation calls methods to write single values and delimiters
34 * (except delimiter between rows (writeRowBetweenDelimiter())).
35 */
36 virtual void write(const Columns & columns, size_t row_num);
37 virtual void writeMinExtreme(const Columns & columns, size_t row_num);
38 virtual void writeMaxExtreme(const Columns & columns, size_t row_num);
39 virtual void writeTotals(const Columns & columns, size_t row_num);
40
41 /** Write single value. */
42 virtual void writeField(const IColumn & column, const IDataType & type, size_t row_num) = 0;
43
44 /** Write delimiter. */
45 virtual void writeFieldDelimiter() {} /// delimiter between values
46 virtual void writeRowStartDelimiter() {} /// delimiter before each row
47 virtual void writeRowEndDelimiter() {} /// delimiter after each row
48 virtual void writeRowBetweenDelimiter() {} /// delimiter between rows
49 virtual void writePrefix() {} /// delimiter before resultset
50 virtual void writeSuffix() {} /// delimiter after resultset
51 virtual void writeBeforeTotals() {}
52 virtual void writeAfterTotals() {}
53 virtual void writeBeforeExtremes() {}
54 virtual void writeAfterExtremes() {}
55 virtual void writeLastSuffix() {} /// Write something after resultset, totals end extremes.
56
57private:
58 bool first_row = true;
59 bool prefix_written = false;
60 bool suffix_written = false;
61
62 // Callback used to indicate that another row is written.
63 FormatFactory::WriteCallback write_single_row_callback;
64
65 void writePrefixIfNot()
66 {
67 if (!prefix_written)
68 writePrefix();
69
70 prefix_written = true;
71 }
72
73 void writeSuffixIfNot()
74 {
75 if (!suffix_written)
76 writeSuffix();
77
78 suffix_written = true;
79 }
80
81};
82
83}
84