2016-02-07 08:42:21 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <DB/IO/WriteBuffer.h>
|
|
|
|
|
#include <DB/DataStreams/IRowOutputStream.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Поток для вывода данных в формате csv.
|
2016-02-07 10:43:02 +00:00
|
|
|
|
* Не соответствует https://tools.ietf.org/html/rfc4180 потому что использует LF, а не CR LF.
|
2016-02-07 08:42:21 +00:00
|
|
|
|
*/
|
|
|
|
|
class CSVRowOutputStream : public IRowOutputStream
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/** with_names - выводить в первой строке заголовок с именами столбцов
|
|
|
|
|
* with_types - выводить на следующей строке заголовок с именами типов
|
|
|
|
|
*/
|
|
|
|
|
CSVRowOutputStream(WriteBuffer & ostr_, const Block & sample_, bool with_names_ = false, bool with_types_ = false);
|
|
|
|
|
|
2016-02-16 16:39:39 +00:00
|
|
|
|
void writeField(const IColumn & column, const IDataType & type, size_t row_num) override;
|
2016-02-07 08:42:21 +00:00
|
|
|
|
void writeFieldDelimiter() override;
|
|
|
|
|
void writeRowEndDelimiter() override;
|
|
|
|
|
void writePrefix() override;
|
|
|
|
|
void writeSuffix() override;
|
|
|
|
|
|
|
|
|
|
void flush() override { ostr.next(); }
|
|
|
|
|
|
|
|
|
|
void setTotals(const Block & totals_) override { totals = totals_; }
|
|
|
|
|
void setExtremes(const Block & extremes_) override { extremes = extremes_; }
|
|
|
|
|
|
|
|
|
|
/// https://www.iana.org/assignments/media-types/text/csv
|
|
|
|
|
String getContentType() const override
|
|
|
|
|
{
|
|
|
|
|
return String("text/csv; charset=UTF-8; header=") + ((with_names || with_types) ? "present" : "absent");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void writeTotals();
|
|
|
|
|
void writeExtremes();
|
|
|
|
|
|
|
|
|
|
WriteBuffer & ostr;
|
|
|
|
|
const Block sample;
|
|
|
|
|
bool with_names;
|
|
|
|
|
bool with_types;
|
|
|
|
|
DataTypes data_types;
|
|
|
|
|
Block totals;
|
|
|
|
|
Block extremes;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|