# HG changeset patch # User František Kučera # Date 1532206907 -7200 # Node ID 640e88aedf8f2a5321109c63ac469b20a9401bf1 # Parent 27bc67e7c80f03263bd276999949136bf61c97d4 code for table generation ported from the prototype: RelationalGenerator.h diff -r 27bc67e7c80f -r 640e88aedf8f src/StreamRelationalWriter.h --- a/src/StreamRelationalWriter.h Sat Jul 21 22:37:25 2018 +0200 +++ b/src/StreamRelationalWriter.h Sat Jul 21 23:01:47 2018 +0200 @@ -6,6 +6,7 @@ #include "../include/typedefs.h" #include "../include/RelationalWriter.h" +#include "format.h" #include "DataTypeWriterBase.h" #include "types/BooleanDataTypeWriter.h" #include "types/IntegerDataTypeWriter.h" @@ -21,6 +22,16 @@ types::IntegerDataTypeWriter integerWriter; types::StringDataTypeWriter stringWriter; vector writers = {&booleanWriter, &integerWriter, &stringWriter}; + + /** + * count of columns in the current table + */ + integer_t columnCount; + + /** + * types of columns in the current table + */ + vector columnTypes; void writeString(const string_t &stringValue, const integer_t typeId) { for (DataTypeWriterBase* writer : writers) if (writer->supports(typeId)) return writer->writeString(output, stringValue); @@ -44,11 +55,45 @@ } void startRelation(string_t name, std::vector > attributes, boolean_t writeHeader) override { - output << "startRelation(…)" << std::endl; + string_t tableName = name; + columnCount = attributes.size(); + + // Write table name and column count: + integerWriter.writeValue(output, DATA_PART_START); + stringWriter.writeValue(output, tableName); + integerWriter.writeValue(output, columnCount); + + columnTypes.clear(); + columnTypes.resize(columnCount); + + // Write column names: + for (size_t c = 0; c < columnCount; c++) { + wstring columnName = attributes[c].first; + stringWriter.writeValue(output, columnName); + } + + // Write column types: + for (size_t c = 0; c < columnCount; c++) { + wstring typeCode = attributes[c].second; + integer_t typeId = toTypeId(typeCode); + integerWriter.writeValue(output, typeId); + columnTypes[c] = typeId; + } + } void writeRecord(std::vector attributes) override { - output << "writeRecord(…)" << std::endl; + // FIXME: check vector size + for (size_t c = 0; c < columnCount; c++) { + // TODO: support multiple rows in a single method call? + if (c % columnCount == 0) { + integerWriter.writeValue(output, DATA_PART_ROW); + } + + wstring stringValue = attributes[c]; + integer_t typeId = columnTypes[c % columnCount]; + writeString(stringValue, typeId); + } } };