src/InferTypesHandler.h
branchv_0
changeset 1 da114916734b
parent 0 5d1c556ff7db
child 4 66c8a1783884
--- a/src/InferTypesHandler.h	Fri May 21 20:16:03 2021 +0200
+++ b/src/InferTypesHandler.h	Sun May 23 13:59:09 2021 +0200
@@ -24,6 +24,7 @@
 #include <locale>
 #include <codecvt>
 #include <regex>
+#include <stdexcept>
 
 #include <relpipe/reader/typedefs.h>
 #include <relpipe/reader/TypeId.h>
@@ -35,6 +36,9 @@
 #include <relpipe/cli/RelpipeCLIException.h>
 
 #include "Configuration.h"
+#include "Mode.h"
+#include "DataMode.h"
+#include "MetadataMode.h"
 
 namespace relpipe {
 namespace tr {
@@ -49,8 +53,16 @@
 private:
 	shared_ptr<writer::RelationalWriter> relationalWriter;
 	Configuration configuration;
+	std::shared_ptr<Mode> currentMode;
 
-	integer_t currentAttributeIndex = 0;
+	vector<writer::AttributeMetadata> toWriterMetadata(vector<AttributeMetadata> attributes) {
+		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
+		vector<writer::AttributeMetadata> writerMetadata;
+		for (AttributeMetadata readerMetadata : attributes) {
+			writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
+		}
+		return writerMetadata;
+	}
 
 public:
 
@@ -58,24 +70,27 @@
 	}
 
 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
-		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
-		vector<writer::AttributeMetadata> writerMetadata;
-		for (AttributeMetadata readerMetadata : attributes) {
-			writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
+		// TODO: move this logic to relpipe-lib-infertypes and share with certain modules like relpipe-in-csv
+		for (RelationConfiguration rc : configuration.relationConfigurations) {
+			if (std::regex_match(name, rc.relationPattern)) {
+				if (rc.mode == MODE::METADATA || rc.mode == MODE::AUTO && MetadataMode::willInfer(attributes)) currentMode.reset(new MetadataMode(relationalWriter));
+				else if (rc.mode == MODE::DATA) currentMode.reset(new DataMode(relationalWriter));
+				else throw std::logic_error("Unsupported mode: " + std::to_string((int) rc.mode));
+				break;
+			}
 		}
 
-		relationalWriter->startRelation(name, writerMetadata, true);
+		if (currentMode) currentMode->startRelation(name, attributes);
+		else relationalWriter->startRelation(name, toWriterMetadata(attributes), true);
 	}
 
 	void attribute(const string_t& value) override {
-		relationalWriter->writeAttribute(value);
-
-		currentAttributeIndex++;
-		//currentAttributeIndex = currentAttributeIndex % currentRules.size();
+		if (currentMode) currentMode->attribute(value);
+		else relationalWriter->writeAttribute(value);
 	}
 
 	void endOfPipe() {
-
+		currentMode.reset();
 	}
 
 };