src/InferTypesHandler.h
branchv_0
changeset 0 5d1c556ff7db
child 1 da114916734b
equal deleted inserted replaced
-1:000000000000 0:5d1c556ff7db
       
     1 /**
       
     2  * Relational pipes
       
     3  * Copyright © 2021 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, version 3 of the License.
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
       
    16  */
       
    17 #pragma once
       
    18 
       
    19 #include <memory>
       
    20 #include <string>
       
    21 #include <vector>
       
    22 #include <iostream>
       
    23 #include <sstream>
       
    24 #include <locale>
       
    25 #include <codecvt>
       
    26 #include <regex>
       
    27 
       
    28 #include <relpipe/reader/typedefs.h>
       
    29 #include <relpipe/reader/TypeId.h>
       
    30 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
       
    31 #include <relpipe/reader/handlers/AttributeMetadata.h>
       
    32 
       
    33 #include <relpipe/writer/Factory.h>
       
    34 
       
    35 #include <relpipe/cli/RelpipeCLIException.h>
       
    36 
       
    37 #include "Configuration.h"
       
    38 
       
    39 namespace relpipe {
       
    40 namespace tr {
       
    41 namespace infertypes {
       
    42 
       
    43 using namespace std;
       
    44 using namespace relpipe;
       
    45 using namespace relpipe::reader;
       
    46 using namespace relpipe::reader::handlers;
       
    47 
       
    48 class InferTypesHandler : public RelationalReaderStringHandler {
       
    49 private:
       
    50 	shared_ptr<writer::RelationalWriter> relationalWriter;
       
    51 	Configuration configuration;
       
    52 
       
    53 	integer_t currentAttributeIndex = 0;
       
    54 
       
    55 public:
       
    56 
       
    57 	InferTypesHandler(shared_ptr<writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) {
       
    58 	}
       
    59 
       
    60 	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
       
    61 		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
       
    62 		vector<writer::AttributeMetadata> writerMetadata;
       
    63 		for (AttributeMetadata readerMetadata : attributes) {
       
    64 			writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
       
    65 		}
       
    66 
       
    67 		relationalWriter->startRelation(name, writerMetadata, true);
       
    68 	}
       
    69 
       
    70 	void attribute(const string_t& value) override {
       
    71 		relationalWriter->writeAttribute(value);
       
    72 
       
    73 		currentAttributeIndex++;
       
    74 		//currentAttributeIndex = currentAttributeIndex % currentRules.size();
       
    75 	}
       
    76 
       
    77 	void endOfPipe() {
       
    78 
       
    79 	}
       
    80 
       
    81 };
       
    82 
       
    83 }
       
    84 }
       
    85 }