src/CutHandler.h
branchv_0
changeset 28 bc15f5471b6a
parent 22 d07ac873cc89
--- a/src/CutHandler.h	Sat Oct 24 00:08:19 2020 +0200
+++ b/src/CutHandler.h	Tue May 11 22:03:45 2021 +0200
@@ -34,9 +34,11 @@
 
 #include <relpipe/cli/RelpipeCLIException.h>
 
+#include "Configuration.h"
+
 namespace relpipe {
 namespace tr {
-namespace grep {
+namespace cut {
 
 using namespace std;
 using namespace relpipe;
@@ -46,26 +48,16 @@
 class CutHandler : public RelationalReaderStringHandler {
 private:
 	shared_ptr<writer::RelationalWriter> relationalWriter;
-
-	wregex relationNameRegEx;
-	vector<wregex> attributeNameRegExes;
+	Configuration configuration;
+	RelationConfiguration* currentFilter = nullptr;
 
 	vector<integer_t> currentAttributeMapping;
 	vector<string_t> currentRecord;
 	integer_t currentAttributeIndex = 0;
-	boolean_t filterCurrentRelation = false;
 
 public:
 
-	CutHandler(ostream& output, const vector<string_t>& arguments) {
-		relationalWriter.reset(writer::Factory::create(output));
-
-		if (arguments.size() >= 2) {
-			relationNameRegEx = wregex(arguments[0]);
-			for (int i = 1; i < arguments.size(); i++) attributeNameRegExes.push_back(wregex(arguments[i]));
-		} else {
-			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-cut <relationNameRegExp> <attributeNameRegExp> [<otherAttributeNameRegExp> ...]", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
-		}
+	CutHandler(shared_ptr<writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
 	}
 
 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
@@ -78,12 +70,21 @@
 
 		vector<writer::AttributeMetadata> writerMetadata;
 
-		filterCurrentRelation = regex_match(name, relationNameRegEx);
-		if (filterCurrentRelation) {
+		using E = RelationConfiguration::ENTITY;
+
+		currentFilter = nullptr;
+		for (int i = 0; i < configuration.relationConfigurations.size(); i++) {
+			if (regex_match(name, configuration.relationConfigurations[i].relationPattern) ^ configuration.relationConfigurations[i].invertMatch[E::RELATION]) {
+				currentFilter = &configuration.relationConfigurations[i];
+				break;
+			}
+		}
+
+		if (currentFilter) {
 			currentAttributeMapping.clear();
-			for (wregex attributeNameRegEx : attributeNameRegExes) {
+			for (std::wregex attributePattern : currentFilter->attributePatterns) {
 				for (int i = 0; i < allWriterMetadata.size(); i++) {
-					if (regex_match(allWriterMetadata[i].attributeName, attributeNameRegEx)) currentAttributeMapping.push_back(i);
+					if (regex_match(allWriterMetadata[i].attributeName, attributePattern) ^ currentFilter->invertMatch[E::ATTRIBUTE]) currentAttributeMapping.push_back(i);
 				}
 			}
 
@@ -97,7 +98,7 @@
 	}
 
 	void attribute(const string_t& value) override {
-		if (filterCurrentRelation) {
+		if (currentFilter) {
 			currentRecord[currentAttributeIndex] = value;
 			currentAttributeIndex++;