2017-04-01 09:19:00 +00:00
|
|
|
#include <Functions/FunctionFactory.h>
|
|
|
|
#include <Functions/IFunction.h>
|
|
|
|
#include <Columns/ColumnConst.h>
|
|
|
|
#include <DataTypes/DataTypeString.h>
|
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
#include <DataStreams/VerticalRowOutputStream.h>
|
2011-11-28 04:05:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-12-30 00:15:50 +00:00
|
|
|
VerticalRowOutputStream::VerticalRowOutputStream(WriteBuffer & ostr_, const Block & sample_, const Context & context)
|
2017-04-01 07:20:54 +00:00
|
|
|
: ostr(ostr_), sample(sample_)
|
2011-11-28 04:05:53 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
size_t columns = sample.columns();
|
|
|
|
|
|
|
|
using Widths_t = std::vector<size_t>;
|
|
|
|
Widths_t name_widths(columns);
|
|
|
|
size_t max_name_width = 0;
|
|
|
|
|
|
|
|
FunctionPtr visible_width_func = FunctionFactory::instance().get("visibleWidth", context);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Block block_with_name
|
|
|
|
{
|
|
|
|
{ std::make_shared<ColumnConstString>(1, sample.getByPosition(i).name), std::make_shared<DataTypeString>(), "name" },
|
|
|
|
{ nullptr, std::make_shared<DataTypeUInt64>(), "width" }
|
|
|
|
};
|
|
|
|
|
|
|
|
visible_width_func->execute(block_with_name, {0}, 1);
|
|
|
|
|
|
|
|
name_widths[i] = (*block_with_name.getByPosition(1).column)[0].get<UInt64>();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name_widths[i] > max_name_width)
|
|
|
|
max_name_width = name_widths[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
names_and_paddings.resize(columns);
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
{
|
|
|
|
WriteBufferFromString out(names_and_paddings[i]);
|
|
|
|
writeEscapedString(sample.getByPosition(i).name, out);
|
|
|
|
writeCString(": ", out);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < columns; ++i)
|
|
|
|
names_and_paddings[i].resize(max_name_width + strlen(": "), ' ');
|
2011-11-28 04:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-13 01:57:35 +00:00
|
|
|
void VerticalRowOutputStream::flush()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ostr.next();
|
2016-08-13 01:57:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-16 16:39:39 +00:00
|
|
|
void VerticalRowOutputStream::writeField(const IColumn & column, const IDataType & type, size_t row_num)
|
2011-11-28 04:05:53 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeString(names_and_paddings[field_number], ostr);
|
|
|
|
writeValue(column, type, row_num);
|
|
|
|
writeChar('\n', ostr);
|
2016-12-30 00:15:50 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
++field_number;
|
2011-11-28 04:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-16 16:39:39 +00:00
|
|
|
void VerticalRowOutputStream::writeValue(const IColumn & column, const IDataType & type, size_t row_num) const
|
2015-09-20 04:04:52 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
type.serializeTextEscaped(column, row_num, ostr);
|
2015-09-20 04:04:52 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 16:39:39 +00:00
|
|
|
void VerticalRawRowOutputStream::writeValue(const IColumn & column, const IDataType & type, size_t row_num) const
|
2015-09-20 04:04:52 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
type.serializeText(column, row_num, ostr);
|
2015-09-20 04:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-28 04:05:53 +00:00
|
|
|
void VerticalRowOutputStream::writeRowStartDelimiter()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
++row_number;
|
|
|
|
writeCString("Row ", ostr);
|
|
|
|
writeIntText(row_number, ostr);
|
|
|
|
writeCString(":\n", ostr);
|
|
|
|
|
|
|
|
size_t width = log10(row_number + 1) + 1 + strlen("Row :");
|
|
|
|
for (size_t i = 0; i < width; ++i)
|
|
|
|
writeCString("─", ostr);
|
|
|
|
writeChar('\n', ostr);
|
2011-11-28 04:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void VerticalRowOutputStream::writeRowBetweenDelimiter()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
writeCString("\n", ostr);
|
|
|
|
field_number = 0;
|
2011-11-28 04:05:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|