29 namespace tr { |
29 namespace tr { |
30 namespace infertypes { |
30 namespace infertypes { |
31 |
31 |
32 class MetadataMode : public Mode { |
32 class MetadataMode : public Mode { |
33 private: |
33 private: |
34 static std::wregex pattern; |
34 static const std::wregex PATTERN; |
35 public: |
35 public: |
36 |
36 |
37 MetadataMode(shared_ptr<writer::RelationalWriter> relationalWriter) : Mode(relationalWriter) { |
37 MetadataMode(shared_ptr<writer::RelationalWriter> relationalWriter) : Mode(relationalWriter) { |
38 } |
38 } |
39 |
39 |
40 static bool willInfer(std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) { |
40 static bool willInfer(std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) { |
41 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; |
42 return true; |
42 return true; |
43 } |
43 } |
44 |
44 |
45 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 { |
46 // TODO: this logic should be same as in relpipe-in-csv and should be shared through relpipe-lib-infertypes later |
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; |
47 vector<writer::AttributeMetadata> writerMetadata; |
48 std::wsmatch match; |
48 std::wsmatch match; |
49 for (relpipe::reader::handlers::AttributeMetadata& am : attributes) { |
49 for (relpipe::reader::handlers::AttributeMetadata& am : attributes) { |
50 relpipe::common::type::StringX nameAndType = am.getAttributeName(); |
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])}); |
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()); |
52 else throw relpipe::writer::RelpipeWriterException(L"Unable to find type in the attribute name: " + am.getAttributeName()); |
53 } |
53 } |
54 |
54 |
55 relationalWriter->startRelation(name, writerMetadata, true); |
55 relationalWriter->startRelation(name, writerMetadata, true); |
56 } |
56 } |