src/CutHandler.h
branchv_0
changeset 9 f06781a5071b
parent 8 f66c759d1111
child 10 9ec1290b4a9d
equal deleted inserted replaced
8:f66c759d1111 9:f06781a5071b
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
       
     4  *
       
     5  * This program is free software: you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation, either version 3 of the License, or
       
     8  * (at your option) any later version.
       
     9  *
       
    10  * This program is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    13  * GNU General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License
       
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    17  */
       
    18 #pragma once
       
    19 
       
    20 #include <memory>
       
    21 #include <string>
       
    22 #include <vector>
       
    23 #include <iostream>
       
    24 #include <sstream>
       
    25 #include <locale>
       
    26 #include <codecvt>
       
    27 #include <regex>
       
    28 
       
    29 #include <relpipe/reader/typedefs.h>
       
    30 #include <relpipe/reader/TypeId.h>
       
    31 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
       
    32 #include <relpipe/reader/handlers/AttributeMetadata.h>
       
    33 
       
    34 #include <relpipe/writer/Factory.h>
       
    35 
       
    36 #include <relpipe/cli/RelpipeCLIException.h>
       
    37 
       
    38 namespace relpipe {
       
    39 namespace tr {
       
    40 namespace grep {
       
    41 
       
    42 using namespace std;
       
    43 using namespace relpipe;
       
    44 using namespace relpipe::reader;
       
    45 using namespace relpipe::reader::handlers;
       
    46 
       
    47 class GrepHandler : public RelationalReaderStringHadler {
       
    48 private:
       
    49 	shared_ptr<writer::RelationalWriter> relationalWriter;
       
    50 
       
    51 	wregex relationNameRegEx;
       
    52 	wregex attributeNameRegEx;
       
    53 	wregex searchRegEx;
       
    54 
       
    55 	vector<boolean_t> currentSearchableAttributes;
       
    56 	vector<string_t> currentRecord;
       
    57 	integer_t currentAttributeIndex = 0;
       
    58 	boolean_t includeCurrentRecord = false;
       
    59 	boolean_t filterCurrentRelation = false;
       
    60 
       
    61 public:
       
    62 
       
    63 	GrepHandler(ostream& output, const vector<string_t>& arguments) {
       
    64 		relationalWriter.reset(writer::Factory::create(output));
       
    65 
       
    66 		if (arguments.size() == 3) {
       
    67 			relationNameRegEx = wregex(arguments[0]);
       
    68 			attributeNameRegEx = wregex(arguments[1]);
       
    69 			searchRegEx = wregex(arguments[2]);
       
    70 		} else {
       
    71 			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-cut <relationNameRegExp> <attributeNameRegExp> <searchRegExp>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
       
    72 		}
       
    73 	}
       
    74 
       
    75 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
       
    76 		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
       
    77 		vector<writer::AttributeMetadata> writerMetadata;
       
    78 		for (AttributeMetadata readerMetadata : attributes) {
       
    79 			writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
       
    80 		}
       
    81 
       
    82 
       
    83 		currentRecord.resize(attributes.size());
       
    84 		currentSearchableAttributes.resize(attributes.size(), false);
       
    85 		filterCurrentRelation = regex_match(name, relationNameRegEx);
       
    86 		if (filterCurrentRelation) {
       
    87 			for (int i = 0; i < currentSearchableAttributes.size(); i++) {
       
    88 				currentSearchableAttributes[i] = regex_match(attributes[i].getAttributeName(), attributeNameRegEx);
       
    89 			}
       
    90 		}
       
    91 
       
    92 		relationalWriter->startRelation(name, writerMetadata, true);
       
    93 	}
       
    94 
       
    95 	void attribute(const string_t& value) override {
       
    96 		if (filterCurrentRelation) {
       
    97 			currentRecord[currentAttributeIndex] = value;
       
    98 
       
    99 			if (currentSearchableAttributes[currentAttributeIndex]) {
       
   100 				includeCurrentRecord |= regex_search(value, searchRegEx);
       
   101 			}
       
   102 
       
   103 			currentAttributeIndex++;
       
   104 
       
   105 			if (currentAttributeIndex > 0 && currentAttributeIndex % currentSearchableAttributes.size() == 0) {
       
   106 				if (includeCurrentRecord) for (string_t v : currentRecord) relationalWriter->writeAttribute(v);
       
   107 				includeCurrentRecord = false;
       
   108 			}
       
   109 
       
   110 			currentAttributeIndex = currentAttributeIndex % currentSearchableAttributes.size();
       
   111 		} else {
       
   112 			relationalWriter->writeAttribute(value);
       
   113 		}
       
   114 	}
       
   115 
       
   116 	void endOfPipe() {
       
   117 
       
   118 	}
       
   119 
       
   120 };
       
   121 
       
   122 }
       
   123 }
       
   124 }