ClickHouse/dbms/src/DataStreams/TabSeparatedRowInputStream.cpp

75 lines
1.3 KiB
C++
Raw Normal View History

2010-06-04 18:25:25 +00:00
#include <DB/IO/ReadHelpers.h>
2010-05-21 19:52:50 +00:00
#include <DB/DataStreams/TabSeparatedRowInputStream.h>
namespace DB
{
using Poco::SharedPtr;
2011-11-06 06:22:52 +00:00
TabSeparatedRowInputStream::TabSeparatedRowInputStream(ReadBuffer & istr_, const Block & sample_, bool with_names_, bool with_types_)
: istr(istr_), sample(sample_), with_names(with_names_), with_types(with_types_)
2010-05-21 19:52:50 +00:00
{
2011-11-06 05:01:42 +00:00
size_t columns = sample.columns();
data_types.resize(columns);
for (size_t i = 0; i < columns; ++i)
data_types[i] = sample.getByPosition(i).type;
2010-05-21 19:52:50 +00:00
}
2011-11-06 06:22:52 +00:00
void TabSeparatedRowInputStream::readPrefix()
{
size_t columns = sample.columns();
String tmp;
if (with_names)
{
for (size_t i = 0; i < columns; ++i)
{
readEscapedString(tmp, istr);
assertString(i == columns - 1 ? "\n" : "\t", istr);
}
}
if (with_types)
{
for (size_t i = 0; i < columns; ++i)
{
readEscapedString(tmp, istr);
assertString(i == columns - 1 ? "\n" : "\t", istr);
}
}
}
bool TabSeparatedRowInputStream::read(Row & row)
2010-05-21 19:52:50 +00:00
{
2011-11-06 05:01:42 +00:00
size_t size = data_types.size();
row.resize(size);
2010-05-21 19:52:50 +00:00
for (size_t i = 0; i < size; ++i)
{
if (i == 0 && istr.eof())
{
row.clear();
return false;
2010-05-21 19:52:50 +00:00
}
2010-06-04 18:38:56 +00:00
data_types[i]->deserializeTextEscaped(row[i], istr);
2010-05-21 19:52:50 +00:00
/// пропускаем разделители
if (i + 1 == size)
{
2010-06-04 18:25:25 +00:00
if (!istr.eof())
assertString("\n", istr);
2010-05-21 19:52:50 +00:00
}
else
2010-06-04 18:25:25 +00:00
assertString("\t", istr);
2010-05-21 19:52:50 +00:00
}
return true;
2010-05-21 19:52:50 +00:00
}
}