src/CSVHandler.h
branchv_0
changeset 14 a7596589a5b0
parent 10 4bcf3fb7cc48
child 17 ea36eed9683f
equal deleted inserted replaced
13:e4118c36bef6 14:a7596589a5b0
    27 #include <relpipe/reader/typedefs.h>
    27 #include <relpipe/reader/typedefs.h>
    28 #include <relpipe/reader/TypeId.h>
    28 #include <relpipe/reader/TypeId.h>
    29 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
    29 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
    30 #include <relpipe/reader/handlers/AttributeMetadata.h>
    30 #include <relpipe/reader/handlers/AttributeMetadata.h>
    31 
    31 
       
    32 #include "Configuration.h"
    32 #include "RelpipeCSVWriterException.h"
    33 #include "RelpipeCSVWriterException.h"
    33 
    34 
    34 namespace relpipe {
    35 namespace relpipe {
    35 namespace out {
    36 namespace out {
    36 namespace csv {
    37 namespace csv {
    40 using namespace relpipe::reader::handlers;
    41 using namespace relpipe::reader::handlers;
    41 
    42 
    42 class CSVHandler : public RelationalReaderStringHandler {
    43 class CSVHandler : public RelationalReaderStringHandler {
    43 private:
    44 private:
    44 	std::ostream& output;
    45 	std::ostream& output;
       
    46 	Configuration& configuration;
    45 	const char QUOTE = '"';
    47 	const char QUOTE = '"';
    46 	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding or generate CSV always in UTF-8 like XML?
    48 	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: local system encoding or generate CSV always in UTF-8 like XML?
    47 	std::vector<AttributeMetadata> firstAttributes;
    49 	std::vector<AttributeMetadata> firstAttributes;
    48 	integer_t valueCount = 0;
    50 	integer_t valueCount = 0;
    49 public:
    51 public:
    50 
    52 
    51 	CSVHandler(std::ostream& output) : output(output) {
    53 	CSVHandler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) {
    52 	}
    54 	}
    53 
    55 
    54 	void startRelation(string_t name, std::vector<AttributeMetadata> attributes) override {
    56 	void startRelation(string_t name, std::vector<AttributeMetadata> attributes) override {
    55 		if (firstAttributes.empty()) {
    57 		if (firstAttributes.empty()) {
    56 			firstAttributes = attributes;
    58 			firstAttributes = attributes;
    57 			for (auto attr : attributes) attribute(attr.getAttributeName());
    59 			if (configuration.writeHeader) for (auto attr : attributes) attribute(attr.getAttributeName());
    58 		} else {
    60 		} else {
    59 			// TODO: UNION ALL if data types and attribute count matches
    61 			// TODO: UNION ALL if data types and attribute count matches
    60 			throw RelpipeCSVWriterException(L"Only a single relation can be converted to the CSV format.");
    62 			throw RelpipeCSVWriterException(L"Only a single relation can be converted to the CSV format.");
    61 		}
    63 		}
    62 	}
    64 	}