提交 831c48c5 编写于 作者: A Avogar

Add MarkdownRowOutput format

上级 23ae3f4b
......@@ -377,6 +377,7 @@ FormatFactory::FormatFactory()
registerOutputFormatProcessorODBCDriver2(*this);
registerOutputFormatProcessorNull(*this);
registerOutputFormatProcessorMySQLWrite(*this);
registerOutputFormatProcessorMarkdown(*this);
}
FormatFactory & FormatFactory::instance()
......
......@@ -198,6 +198,7 @@ void registerOutputFormatProcessorODBCDriver(FormatFactory & factory);
void registerOutputFormatProcessorODBCDriver2(FormatFactory & factory);
void registerOutputFormatProcessorNull(FormatFactory & factory);
void registerOutputFormatProcessorMySQLWrite(FormatFactory & factory);
void registerOutputFormatProcessorMarkdown(FormatFactory & factory);
/// Input only formats.
void registerInputFormatProcessorCapnProto(FormatFactory & factory);
......
#include <Processors/Formats/Impl/MarkdownRowOutputFormat.h>
#include <IO/WriteHelpers.h>
namespace DB
{
MarkdownRowOutputFormat::MarkdownRowOutputFormat(WriteBuffer & out_, const Block & header_, FormatFactory::WriteCallback callback, const FormatSettings & format_settings_)
: IRowOutputFormat(header_, out_, callback), format_settings(format_settings_) {}
void MarkdownRowOutputFormat::writePrefix()
{
auto & header = getPort(PortKind::Main).getHeader();
size_t columns = header.columns();
writeChar('|', out);
for (size_t i = 0; i < columns; ++i)
{
writeEscapedString(header.safeGetByPosition(i).name, out);
writeChar('|', out);
}
writeCString("\n|", out);
String format = ":-:|";
for (size_t i = 0; i < columns; ++i)
{
writeString(format, out);
}
writeChar('\n', out);
}
void MarkdownRowOutputFormat::writeRowStartDelimiter()
{
writeChar('|', out);
}
void MarkdownRowOutputFormat::writeFieldDelimiter()
{
writeChar('|', out);
}
void MarkdownRowOutputFormat::writeRowEndDelimiter()
{
writeCString("|\n", out);
}
void MarkdownRowOutputFormat::writeField(const IColumn & column, const IDataType & type, size_t row_num)
{
type.serializeAsTextEscaped(column, row_num, out, format_settings);
}
void registerOutputFormatProcessorMarkdown(FormatFactory & factory)
{
factory.registerOutputFormatProcessor("Markdown", [](
WriteBuffer & buf,
const Block & sample,
FormatFactory::WriteCallback callback,
const FormatSettings & settings)
{
return std::make_shared<MarkdownRowOutputFormat>(buf, sample, callback, settings);
});
}
}
\ No newline at end of file
#pragma once
#include <Processors/Formats/IRowOutputFormat.h>
#include <Formats/FormatFactory.h>
#include <Formats/FormatSettings.h>
namespace DB
{
class ReadBuffer;
class MarkdownRowOutputFormat : public IRowOutputFormat
{
public:
MarkdownRowOutputFormat(WriteBuffer & out_, const Block & header_, FormatFactory::WriteCallback callback, const FormatSettings & format_settings_);
/// Write higher part of markdown table like this:
/// |columnName1|columnName2|...|columnNameN|
/// |:-:|:-:|...|:-:|
void writePrefix() override;
/// Write '|' before each row
void writeRowStartDelimiter() override;
/// Write '|' between values
void writeFieldDelimiter() override;
/// Write '|\n' after each row
void writeRowEndDelimiter() override ;
void writeField(const IColumn & column, const IDataType & type, size_t row_num) override;
String getName() const override { return "MarkdownRowOutputFormat"; }
protected:
const FormatSettings format_settings;
};
}
|id|name|array|
|:-:|:-:|:-:|
|1|name1|[1,2,3]|
|2|name2|[4,5,6]|
|3|name3|[7,8,9]|
DROP TABLE IF EXISTS makrdown;
CREATE TABLE markdown (id UInt32, name String, array Array(Int8)) ENGINE = Memory;
INSERT INTO markdown VALUES (1, 'name1', [1,2,3]), (2, 'name2', [4,5,6]), (3, 'name3', [7,8,9]);
SELECT * FROM markdown FORMAT Markdown;
DROP TABLE IF EXISTS markdown
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册