src/StreamRelationalWriter.h
branchv_0
changeset 15 8fd6c4d44071
parent 14 733334eca89b
child 16 3613617d3076
equal deleted inserted replaced
14:733334eca89b 15:8fd6c4d44071
    21 	std::ostream &output;
    21 	std::ostream &output;
    22 	types::BooleanDataTypeWriter booleanWriter;
    22 	types::BooleanDataTypeWriter booleanWriter;
    23 	types::IntegerDataTypeWriter integerWriter;
    23 	types::IntegerDataTypeWriter integerWriter;
    24 	types::StringDataTypeWriter stringWriter;
    24 	types::StringDataTypeWriter stringWriter;
    25 	vector<DataTypeWriterBase*> writers = {&booleanWriter, &integerWriter, &stringWriter};
    25 	vector<DataTypeWriterBase*> writers = {&booleanWriter, &integerWriter, &stringWriter};
    26 	
    26 
    27 	/**
    27 	/**
    28 	 * count of columns in the current table
    28 	 * count of columns in the current table
    29 	 */
    29 	 */
    30 	integer_t columnCount;
    30 	integer_t columnCount;
    31 	
    31 
    32 	/**
    32 	/**
    33 	 * types of columns in the current table
    33 	 * types of columns in the current table
    34 	 */
    34 	 */
    35 	vector<integer_t> columnTypes;
    35 	vector<TypeId> columnTypes;
    36 
    36 
    37 	void writeString(const string_t &stringValue, const integer_t typeId) {
    37 	void writeString(const string_t &stringValue, const TypeId typeId) {
    38 		for (DataTypeWriterBase* writer : writers) if (writer->supports(typeId)) return writer->writeString(output, stringValue);
    38 		for (DataTypeWriterBase* writer : writers) if (writer->supports(typeId)) return writer->writeString(output, stringValue);
    39 		throw RelpipeWriterException(L"Unsupported data type: " + typeId);
    39 		throw RelpipeWriterException(L"Unsupported data type: " + static_cast<integer_t>(typeId));
    40 	}
    40 	}
    41 
    41 
    42 public:
    42 public:
    43 
    43 
    44 	StreamRelationalWriter(std::ostream &output) :
    44 	StreamRelationalWriter(std::ostream &output) :
    45 	output(output) {
    45 	output(output) {
    46 	}
    46 	}
    47 
    47 
    48 	integer_t toTypeId(const string_t typeCode) override {
    48 	TypeId toTypeId(const string_t typeCode) override {
    49 		for (DataTypeWriterBase* writer : writers) if (writer->supports(typeCode)) return writer->getTypeId();
    49 		for (DataTypeWriterBase* writer : writers) if (writer->supports(typeCode)) return writer->getTypeId();
    50 		throw RelpipeWriterException(L"Unsupported data type: " + typeCode);
    50 		throw RelpipeWriterException(L"Unsupported data type: " + typeCode);
    51 	}
    51 	}
    52 
    52 
    53 	string_t toTypeCode(const integer_t typeId) override {
    53 	string_t toTypeCode(const TypeId typeId) override {
    54 		for (DataTypeWriterBase* writer : writers) if (writer->supports(typeId)) return writer->getTypeCode();
    54 		for (DataTypeWriterBase* writer : writers) if (writer->supports(typeId)) return writer->getTypeCode();
    55 		throw RelpipeWriterException(L"Unsupported data type: " + typeId);
    55 		throw RelpipeWriterException(L"Unsupported data type: " + static_cast<integer_t>(typeId));
    56 	}
    56 	}
    57 
    57 	
    58 	void startRelation(string_t name, std::vector<std::pair<string_t, TypeId> > attributes, boolean_t writeHeader) override {
    58 	void startRelation(string_t name, std::vector<std::pair<string_t, TypeId> > attributes, boolean_t writeHeader) override {
    59 		string_t tableName = name;
    59 		string_t tableName = name;
    60 		columnCount = attributes.size();
    60 		columnCount = attributes.size();
    61 
    61 
    62 		// Write table name and column count:
    62 		// Write table name and column count:
    67 		columnTypes.clear();
    67 		columnTypes.clear();
    68 		columnTypes.resize(columnCount);
    68 		columnTypes.resize(columnCount);
    69 
    69 
    70 		// Write column names:
    70 		// Write column names:
    71 		for (size_t c = 0; c < columnCount; c++) {
    71 		for (size_t c = 0; c < columnCount; c++) {
    72 			wstring columnName = attributes[c].first;			
    72 			wstring columnName = attributes[c].first;
    73 			stringWriter.writeValue(output, columnName);
    73 			stringWriter.writeValue(output, columnName);
    74 		}
    74 		}
    75 
    75 
    76 		// Write column types:
    76 		// Write column types:
    77 		for (size_t c = 0; c < columnCount; c++) {
    77 		for (size_t c = 0; c < columnCount; c++) {
    78 			integer_t typeId = static_cast<integer_t>(attributes[c].second);
    78 			TypeId typeId = attributes[c].second;
    79 			integerWriter.writeValue(output, typeId);
    79 			integerWriter.writeValue(output, static_cast<integer_t>(typeId));
    80 			columnTypes[c] = typeId;
    80 			columnTypes[c] = typeId;
    81 		}
    81 		}
    82 
    82 
    83 	}
    83 	}
    84 
    84 
    89 			if (c % columnCount == 0) {
    89 			if (c % columnCount == 0) {
    90 				integerWriter.writeValue(output, DATA_PART_ROW);
    90 				integerWriter.writeValue(output, DATA_PART_ROW);
    91 			}
    91 			}
    92 
    92 
    93 			wstring stringValue = attributes[c];
    93 			wstring stringValue = attributes[c];
    94 			integer_t typeId = columnTypes[c % columnCount];
    94 			TypeId typeId = columnTypes[c % columnCount];
    95 			writeString(stringValue, typeId);
    95 			writeString(stringValue, typeId);
    96 		}
    96 		}
    97 	}
    97 	}
    98 
    98 
    99 };
    99 };