src/SerializeHandler.h
branchv_0
changeset 3 9e16e31fa756
parent 2 d2ba14aa4e20
child 4 0890135ff1f7
equal deleted inserted replaced
2:d2ba14aa4e20 3:9e16e31fa756
    35 
    35 
    36 namespace relpipe {
    36 namespace relpipe {
    37 namespace tr {
    37 namespace tr {
    38 namespace serialize {
    38 namespace serialize {
    39 
    39 
    40 class SerializeHandler : public relpipe::reader::handlers::RelationalReaderStringHandler {
    40 class SerializeHandler : public relpipe::reader::handlers::RelationalReaderValueHandler {
    41 private:
    41 private:
    42 	Configuration configuration;
    42 	Configuration configuration;
    43 	shared_ptr<relpipe::writer::RelationalWriter> writer;
    43 	shared_ptr<relpipe::writer::RelationalWriter> writer;
    44 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor;
    44 	std::wstring_convert<codecvt_utf8<wchar_t>> convertor;
    45 
    45 
    90 		for (relpipe::reader::handlers::AttributeMetadata readerMetadata : attributes) {
    90 		for (relpipe::reader::handlers::AttributeMetadata readerMetadata : attributes) {
    91 			relationContext.writerMetadata.push_back({readerMetadata.getAttributeName(), writer->toTypeId(readerMetadata.getTypeName())});
    91 			relationContext.writerMetadata.push_back({readerMetadata.getAttributeName(), writer->toTypeId(readerMetadata.getTypeName())});
    92 		}
    92 		}
    93 	}
    93 	}
    94 
    94 
    95 	void attribute(const relpipe::common::type::StringX& value) override {
    95 	void attribute(const void* value, const std::type_info& type) override {
    96 		if (recordContext.attributeIndex == 0) {
    96 		if (recordContext.attributeIndex == 0) {
    97 			recordContext = RecordContext();
    97 			recordContext = RecordContext();
    98 			recordContext.writer.reset(relpipe::writer::Factory::create(recordContext.buffer));
    98 			recordContext.writer.reset(relpipe::writer::Factory::create(recordContext.buffer));
    99 			recordContext.writer->startRelation(relationContext.name, relationContext.writerMetadata, true);
    99 			recordContext.writer->startRelation(relationContext.name, relationContext.writerMetadata, true);
   100 		}
   100 		}
   101 
   101 
   102 		recordContext.writer->writeAttribute(value); // TODO: typed values instead of strings
   102 		recordContext.writer->writeAttribute(value, type);
   103 		recordContext.attributeIndex++;
   103 		recordContext.attributeIndex++;
   104 
   104 
   105 		if (recordContext.attributeIndex % relationContext.readerMetadata.size() == 0) {
   105 		if (recordContext.attributeIndex % relationContext.readerMetadata.size() == 0) {
   106 			writer->writeAttribute(toHex(recordContext.buffer.str()));
   106 			writer->writeAttribute(toHex(recordContext.buffer.str()));
   107 			recordContext.attributeIndex = 0;
   107 			recordContext.attributeIndex = 0;
   108 		}
   108 		}
   109 	}
   109 	}
   110 
   110 
   111 	void endOfPipe() {
   111 	void endOfPipe() {
   112 		// TODO: check number of attribute values
   112 		if (recordContext.attributeIndex != 0) throw relpipe::reader::RelpipeReaderException(L"Invalid number of attributes at the end of the pipe – incomplete record.");
   113 	}
   113 	}
   114 
   114 
   115 };
   115 };
   116 
   116 
   117 }
   117 }