2013-05-15 11:18:58 +00:00
|
|
|
#include <DB/DataStreams/JSONRowOutputStream.h>
|
|
|
|
|
|
|
|
#include <DB/IO/WriteHelpers.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
using Poco::SharedPtr;
|
|
|
|
|
|
|
|
|
2013-05-22 14:57:43 +00:00
|
|
|
JSONRowOutputStream::JSONRowOutputStream(WriteBuffer & ostr_, const Block & sample_)
|
|
|
|
: ostr(ostr_), field_number(0), row_count(0), applied_limit(false), rows_before_limit(0)
|
2013-05-15 11:18:58 +00:00
|
|
|
{
|
|
|
|
NamesAndTypesList columns(sample_.getColumnsList());
|
|
|
|
fields.assign(columns.begin(), columns.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONRowOutputStream::writePrefix()
|
|
|
|
{
|
2013-05-15 22:58:57 +00:00
|
|
|
writeCString("{\n", ostr);
|
|
|
|
writeCString("\t\"meta\":\n", ostr);
|
|
|
|
writeCString("\t[\n", ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
|
|
|
|
for (size_t i = 0; i < fields.size(); ++i)
|
|
|
|
{
|
2013-05-15 22:58:57 +00:00
|
|
|
writeCString("\t\t{\n", ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
|
2013-05-15 22:58:57 +00:00
|
|
|
writeCString("\t\t\t\"name\": ", ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
writeDoubleQuotedString(fields[i].first, ostr);
|
2013-05-15 22:58:57 +00:00
|
|
|
writeCString(",\n", ostr);
|
|
|
|
writeCString("\t\t\t\"type\": ", ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
writeDoubleQuotedString(fields[i].second->getName(), ostr);
|
|
|
|
writeChar('\n', ostr);
|
|
|
|
|
2013-05-15 22:58:57 +00:00
|
|
|
writeCString("\t\t}", ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
if (i + 1 < fields.size())
|
|
|
|
writeChar(',', ostr);
|
|
|
|
writeChar('\n', ostr);
|
|
|
|
}
|
|
|
|
|
2013-05-15 22:58:57 +00:00
|
|
|
writeCString("\t],\n", ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
writeChar('\n', ostr);
|
2013-05-15 22:58:57 +00:00
|
|
|
writeCString("\t\"data\":\n", ostr);
|
|
|
|
writeCString("\t[\n", ostr);
|
2013-06-30 17:04:50 +00:00
|
|
|
|
|
|
|
ostr.next();
|
2013-05-15 11:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONRowOutputStream::writeField(const Field & field)
|
|
|
|
{
|
2013-05-15 22:58:57 +00:00
|
|
|
writeCString("\t\t\t", ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
writeDoubleQuotedString(fields[field_number].first, ostr);
|
2013-05-15 22:58:57 +00:00
|
|
|
writeCString(": ", ostr);
|
2013-05-16 12:52:09 +00:00
|
|
|
fields[field_number].second->serializeTextJSON(field, ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
++field_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONRowOutputStream::writeFieldDelimiter()
|
|
|
|
{
|
2013-05-15 22:58:57 +00:00
|
|
|
writeCString(",\n", ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONRowOutputStream::writeRowStartDelimiter()
|
|
|
|
{
|
|
|
|
if (row_count > 0)
|
2013-05-15 22:58:57 +00:00
|
|
|
writeCString(",\n", ostr);
|
|
|
|
writeCString("\t\t{\n", ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONRowOutputStream::writeRowEndDelimiter()
|
|
|
|
{
|
|
|
|
writeChar('\n', ostr);
|
2013-05-15 22:58:57 +00:00
|
|
|
writeCString("\t\t}", ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
field_number = 0;
|
|
|
|
++row_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void JSONRowOutputStream::writeSuffix()
|
|
|
|
{
|
|
|
|
writeChar('\n', ostr);
|
2013-05-15 22:58:57 +00:00
|
|
|
writeCString("\t],\n", ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
writeChar('\n', ostr);
|
2013-05-15 22:58:57 +00:00
|
|
|
writeCString("\t\"rows\": ", ostr);
|
2013-05-15 11:18:58 +00:00
|
|
|
writeIntText(row_count, ostr);
|
2013-05-20 12:21:51 +00:00
|
|
|
|
|
|
|
writeRowsBeforeLimitAtLeast();
|
|
|
|
|
2013-05-15 11:18:58 +00:00
|
|
|
writeChar('\n', ostr);
|
2013-05-15 22:58:57 +00:00
|
|
|
writeCString("}\n", ostr);
|
2013-05-16 13:36:27 +00:00
|
|
|
ostr.next();
|
2013-05-15 11:18:58 +00:00
|
|
|
}
|
|
|
|
|
2013-05-20 12:21:51 +00:00
|
|
|
void JSONRowOutputStream::writeRowsBeforeLimitAtLeast()
|
|
|
|
{
|
2013-05-22 14:57:43 +00:00
|
|
|
if (applied_limit)
|
2013-05-20 12:21:51 +00:00
|
|
|
{
|
2013-05-22 14:57:43 +00:00
|
|
|
writeCString(",\n", ostr);
|
|
|
|
writeChar('\n', ostr);
|
|
|
|
writeCString("\t\"rows_before_limit_at_least\": ", ostr);
|
|
|
|
writeIntText(rows_before_limit, ostr);
|
2013-05-20 12:21:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-15 11:18:58 +00:00
|
|
|
}
|