src/MetadataMode.h
branchv_0
changeset 2 eddead33f633
parent 1 da114916734b
child 5 9ddc9462e25b
equal deleted inserted replaced
1:da114916734b 2:eddead33f633
    16  */
    16  */
    17 #pragma once
    17 #pragma once
    18 
    18 
    19 #include <regex>
    19 #include <regex>
    20 #include <locale>
    20 #include <locale>
       
    21 #include <stdexcept>
    21 
    22 
    22 #include <relpipe/common/type/typedefs.h>
    23 #include <relpipe/common/type/typedefs.h>
       
    24 #include <relpipe/writer/RelpipeWriterException.h>
    23 
    25 
    24 #include "Mode.h"
    26 #include "Mode.h"
    25 
    27 
    26 namespace relpipe {
    28 namespace relpipe {
    27 namespace tr {
    29 namespace tr {
    39 		for (relpipe::reader::handlers::AttributeMetadata a : attributes) if (!std::regex_match(a.getAttributeName(), pattern)) return false;
    41 		for (relpipe::reader::handlers::AttributeMetadata a : attributes) if (!std::regex_match(a.getAttributeName(), pattern)) return false;
    40 		return true;
    42 		return true;
    41 	}
    43 	}
    42 
    44 
    43 	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
    45 	void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override {
    44 		// TODO: share this code through relpipe-lib-infertypes (when available)
    46 		// TODO: this logic should be same as in relpipe-in-csv and should be shared through relpipe-lib-infertypes later
       
    47 		vector<writer::AttributeMetadata> writerMetadata;
       
    48 		std::wsmatch match;
       
    49 		for (relpipe::reader::handlers::AttributeMetadata& am : attributes) {
       
    50 			relpipe::common::type::StringX nameAndType = am.getAttributeName();
       
    51 			if (std::regex_match(nameAndType, match, pattern)) writerMetadata.push_back({match[1], relationalWriter->toTypeId(match[2])});
       
    52 			else throw relpipe::writer::RelpipeWriterException(L"Unable to find type in the attribute name: " + am.getAttributeName());
       
    53 		}
       
    54 
       
    55 		relationalWriter->startRelation(name, writerMetadata, true);
    45 	}
    56 	}
    46 
    57 
    47 	void attribute(const relpipe::common::type::StringX& value) override {
    58 	void attribute(const relpipe::common::type::StringX& value) override {
    48 		relationalWriter->writeAttribute(value);
    59 		relationalWriter->writeAttribute(value);
    49 	}
    60 	}
    50 };
    61 };
    51 
    62 
    52 std::wregex MetadataMode::pattern = std::wregex(L"(.*)::(.*)");
    63 std::wregex MetadataMode::pattern = std::wregex(L"(.*)::(.*)"); // name::type
    53 
    64 
    54 }
    65 }
    55 }
    66 }
    56 }
    67 }