47 |
51 |
48 class InferTypesHandler : public RelationalReaderStringHandler { |
52 class InferTypesHandler : public RelationalReaderStringHandler { |
49 private: |
53 private: |
50 shared_ptr<writer::RelationalWriter> relationalWriter; |
54 shared_ptr<writer::RelationalWriter> relationalWriter; |
51 Configuration configuration; |
55 Configuration configuration; |
|
56 std::shared_ptr<Mode> currentMode; |
52 |
57 |
53 integer_t currentAttributeIndex = 0; |
58 vector<writer::AttributeMetadata> toWriterMetadata(vector<AttributeMetadata> attributes) { |
|
59 // TODO: move to a reusable method (or use same metadata on both reader and writer side?) |
|
60 vector<writer::AttributeMetadata> writerMetadata; |
|
61 for (AttributeMetadata readerMetadata : attributes) { |
|
62 writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())}); |
|
63 } |
|
64 return writerMetadata; |
|
65 } |
54 |
66 |
55 public: |
67 public: |
56 |
68 |
57 InferTypesHandler(shared_ptr<writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) { |
69 InferTypesHandler(shared_ptr<writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) { |
58 } |
70 } |
59 |
71 |
60 void startRelation(string_t name, vector<AttributeMetadata> attributes) override { |
72 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?) |
73 // TODO: move this logic to relpipe-lib-infertypes and share with certain modules like relpipe-in-csv |
62 vector<writer::AttributeMetadata> writerMetadata; |
74 for (RelationConfiguration rc : configuration.relationConfigurations) { |
63 for (AttributeMetadata readerMetadata : attributes) { |
75 if (std::regex_match(name, rc.relationPattern)) { |
64 writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())}); |
76 if (rc.mode == MODE::METADATA || rc.mode == MODE::AUTO && MetadataMode::willInfer(attributes)) currentMode.reset(new MetadataMode(relationalWriter)); |
|
77 else if (rc.mode == MODE::DATA) currentMode.reset(new DataMode(relationalWriter)); |
|
78 else throw std::logic_error("Unsupported mode: " + std::to_string((int) rc.mode)); |
|
79 break; |
|
80 } |
65 } |
81 } |
66 |
82 |
67 relationalWriter->startRelation(name, writerMetadata, true); |
83 if (currentMode) currentMode->startRelation(name, attributes); |
|
84 else relationalWriter->startRelation(name, toWriterMetadata(attributes), true); |
68 } |
85 } |
69 |
86 |
70 void attribute(const string_t& value) override { |
87 void attribute(const string_t& value) override { |
71 relationalWriter->writeAttribute(value); |
88 if (currentMode) currentMode->attribute(value); |
72 |
89 else relationalWriter->writeAttribute(value); |
73 currentAttributeIndex++; |
|
74 //currentAttributeIndex = currentAttributeIndex % currentRules.size(); |
|
75 } |
90 } |
76 |
91 |
77 void endOfPipe() { |
92 void endOfPipe() { |
78 |
93 currentMode.reset(); |
79 } |
94 } |
80 |
95 |
81 }; |
96 }; |
82 |
97 |
83 } |
98 } |