src/GrepHandler.h
branchv_0
changeset 24 c69670b7b4ef
parent 18 5a3602fb1649
child 25 98be80d2e65b
--- a/src/GrepHandler.h	Sat Oct 24 00:08:19 2020 +0200
+++ b/src/GrepHandler.h	Tue May 11 18:30:50 2021 +0200
@@ -34,6 +34,8 @@
 
 #include <relpipe/cli/RelpipeCLIException.h>
 
+#include "Configuration.h"
+
 namespace relpipe {
 namespace tr {
 namespace grep {
@@ -46,29 +48,17 @@
 class GrepHandler : public RelationalReaderStringHandler {
 private:
 	shared_ptr<writer::RelationalWriter> relationalWriter;
-
-	wregex relationNameRegEx;
-	wregex attributeNameRegEx;
-	wregex searchRegEx;
+	Configuration configuration;
+	RelationConfiguration* currentFilter = nullptr;
 
 	vector<boolean_t> currentSearchableAttributes;
 	vector<string_t> currentRecord;
 	integer_t currentAttributeIndex = 0;
 	boolean_t includeCurrentRecord = false;
-	boolean_t filterCurrentRelation = false;
 
 public:
 
-	GrepHandler(ostream& output, const vector<string_t>& arguments) {
-		relationalWriter.reset(writer::Factory::create(output));
-
-		if (arguments.size() == 3) {
-			relationNameRegEx = wregex(arguments[0]);
-			attributeNameRegEx = wregex(arguments[1]);
-			searchRegEx = wregex(arguments[2]);
-		} else {
-			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-grep <relationNameRegExp> <attributeNameRegExp> <searchRegExp>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
-		}
+	GrepHandler(shared_ptr<writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
 	}
 
 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
@@ -81,10 +71,17 @@
 
 		currentRecord.resize(attributes.size());
 		currentSearchableAttributes.resize(attributes.size(), false);
-		filterCurrentRelation = regex_match(name, relationNameRegEx);
-		if (filterCurrentRelation) {
+		currentFilter = nullptr;
+		for (int i = 0; i < configuration.relationConfigurations.size(); i++) {
+			if (regex_match(name, configuration.relationConfigurations[i].relationPattern)) {
+				currentFilter = &configuration.relationConfigurations[i];
+				break;
+			}
+		}
+
+		if (currentFilter) {
 			for (int i = 0; i < currentSearchableAttributes.size(); i++) {
-				currentSearchableAttributes[i] = regex_match(attributes[i].getAttributeName(), attributeNameRegEx);
+				currentSearchableAttributes[i] = regex_match(attributes[i].getAttributeName(), currentFilter->attributePattern);
 			}
 		}
 
@@ -92,11 +89,11 @@
 	}
 
 	void attribute(const string_t& value) override {
-		if (filterCurrentRelation) {
+		if (currentFilter) {
 			currentRecord[currentAttributeIndex] = value;
 
 			if (currentSearchableAttributes[currentAttributeIndex]) {
-				includeCurrentRecord |= regex_search(value, searchRegEx);
+				includeCurrentRecord |= regex_search(value, currentFilter->valuePattern);
 			}
 
 			currentAttributeIndex++;