src/InferTypesHandler.h
branchv_0
changeset 1 da114916734b
parent 0 5d1c556ff7db
child 4 66c8a1783884
equal deleted inserted replaced
0:5d1c556ff7db 1:da114916734b
    22 #include <iostream>
    22 #include <iostream>
    23 #include <sstream>
    23 #include <sstream>
    24 #include <locale>
    24 #include <locale>
    25 #include <codecvt>
    25 #include <codecvt>
    26 #include <regex>
    26 #include <regex>
       
    27 #include <stdexcept>
    27 
    28 
    28 #include <relpipe/reader/typedefs.h>
    29 #include <relpipe/reader/typedefs.h>
    29 #include <relpipe/reader/TypeId.h>
    30 #include <relpipe/reader/TypeId.h>
    30 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
    31 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
    31 #include <relpipe/reader/handlers/AttributeMetadata.h>
    32 #include <relpipe/reader/handlers/AttributeMetadata.h>
    33 #include <relpipe/writer/Factory.h>
    34 #include <relpipe/writer/Factory.h>
    34 
    35 
    35 #include <relpipe/cli/RelpipeCLIException.h>
    36 #include <relpipe/cli/RelpipeCLIException.h>
    36 
    37 
    37 #include "Configuration.h"
    38 #include "Configuration.h"
       
    39 #include "Mode.h"
       
    40 #include "DataMode.h"
       
    41 #include "MetadataMode.h"
    38 
    42 
    39 namespace relpipe {
    43 namespace relpipe {
    40 namespace tr {
    44 namespace tr {
    41 namespace infertypes {
    45 namespace infertypes {
    42 
    46 
    47 
    51 
    48 class InferTypesHandler : public RelationalReaderStringHandler {
    52 class InferTypesHandler : public RelationalReaderStringHandler {
    49 private:
    53 private:
    50 	shared_ptr<writer::RelationalWriter> relationalWriter;
    54 	shared_ptr<writer::RelationalWriter> relationalWriter;
    51 	Configuration configuration;
    55 	Configuration configuration;
       
    56 	std::shared_ptr<Mode> currentMode;
    52 
    57 
    53 	integer_t currentAttributeIndex = 0;
    58 	vector<writer::AttributeMetadata> toWriterMetadata(vector<AttributeMetadata> attributes) {
       
    59 		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
       
    60 		vector<writer::AttributeMetadata> writerMetadata;
       
    61 		for (AttributeMetadata readerMetadata : attributes) {
       
    62 			writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
       
    63 		}
       
    64 		return writerMetadata;
       
    65 	}
    54 
    66 
    55 public:
    67 public:
    56 
    68 
    57 	InferTypesHandler(shared_ptr<writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
    69 	InferTypesHandler(shared_ptr<writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
    58 	}
    70 	}
    59 
    71 
    60 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
    72 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
    61 		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
    73 		// TODO: move this logic to relpipe-lib-infertypes and share with certain modules like relpipe-in-csv
    62 		vector<writer::AttributeMetadata> writerMetadata;
    74 		for (RelationConfiguration rc : configuration.relationConfigurations) {
    63 		for (AttributeMetadata readerMetadata : attributes) {
    75 			if (std::regex_match(name, rc.relationPattern)) {
    64 			writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
    76 				if (rc.mode == MODE::METADATA || rc.mode == MODE::AUTO && MetadataMode::willInfer(attributes)) currentMode.reset(new MetadataMode(relationalWriter));
       
    77 				else if (rc.mode == MODE::DATA) currentMode.reset(new DataMode(relationalWriter));
       
    78 				else throw std::logic_error("Unsupported mode: " + std::to_string((int) rc.mode));
       
    79 				break;
       
    80 			}
    65 		}
    81 		}
    66 
    82 
    67 		relationalWriter->startRelation(name, writerMetadata, true);
    83 		if (currentMode) currentMode->startRelation(name, attributes);
       
    84 		else relationalWriter->startRelation(name, toWriterMetadata(attributes), true);
    68 	}
    85 	}
    69 
    86 
    70 	void attribute(const string_t& value) override {
    87 	void attribute(const string_t& value) override {
    71 		relationalWriter->writeAttribute(value);
    88 		if (currentMode) currentMode->attribute(value);
    72 
    89 		else relationalWriter->writeAttribute(value);
    73 		currentAttributeIndex++;
       
    74 		//currentAttributeIndex = currentAttributeIndex % currentRules.size();
       
    75 	}
    90 	}
    76 
    91 
    77 	void endOfPipe() {
    92 	void endOfPipe() {
    78 
    93 		currentMode.reset();
    79 	}
    94 	}
    80 
    95 
    81 };
    96 };
    82 
    97 
    83 }
    98 }