src/CutHandler.h
branchv_0
changeset 11 9d528c98912d
parent 10 9ec1290b4a9d
child 15 63e804b63321
equal deleted inserted replaced
10:9ec1290b4a9d 11:9d528c98912d
    68 			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-cut <relationNameRegExp> <attributeNameRegExp> [<otherAttributeNameRegExp> ...]", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
    68 			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-cut <relationNameRegExp> <attributeNameRegExp> [<otherAttributeNameRegExp> ...]", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
    69 		}
    69 		}
    70 	}
    70 	}
    71 
    71 
    72 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
    72 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
       
    73 		currentRecord.resize(attributes.size());
    73 		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
    74 		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
    74 		vector<writer::AttributeMetadata> allWriterMetadata;
    75 		vector<writer::AttributeMetadata> allWriterMetadata;
    75 		for (AttributeMetadata readerMetadata : attributes) {
    76 		for (AttributeMetadata readerMetadata : attributes) {
    76 			allWriterMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
    77 			allWriterMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
    77 		}
    78 		}
    85 				for (int i = 0; i < allWriterMetadata.size(); i++) {
    86 				for (int i = 0; i < allWriterMetadata.size(); i++) {
    86 					if (regex_match(allWriterMetadata[i].attributeName, attributeNameRegEx)) currentAttributeMapping.push_back(i);
    87 					if (regex_match(allWriterMetadata[i].attributeName, attributeNameRegEx)) currentAttributeMapping.push_back(i);
    87 				}
    88 				}
    88 			}
    89 			}
    89 
    90 
    90 			if (currentAttributeMapping.empty()) throw cli::RelpipeCLIException(L"No attribute matches. Relation must have at least one attribute.", cli::CLI::EXIT_CODE_BAD_SYNTAX); // TODO: review exit code
    91 			if (currentAttributeMapping.empty()) return; // No attribute matches → DROP whole relation
    91 			for (integer_t i : currentAttributeMapping) writerMetadata.push_back(allWriterMetadata[i]);
    92 			for (integer_t i : currentAttributeMapping) writerMetadata.push_back(allWriterMetadata[i]);
    92 		} else {
    93 		} else {
    93 			writerMetadata = allWriterMetadata;
    94 			writerMetadata = allWriterMetadata;
    94 		}
    95 		}
    95 
    96 
    96 		currentRecord.resize(allWriterMetadata.size());
       
    97 		relationalWriter->startRelation(name, writerMetadata, true);
    97 		relationalWriter->startRelation(name, writerMetadata, true);
    98 	}
    98 	}
    99 
    99 
   100 	void attribute(const string_t& value) override {
   100 	void attribute(const string_t& value) override {
   101 		if (filterCurrentRelation) {
   101 		if (filterCurrentRelation) {