27 namespace tr { |
27 namespace tr { |
28 namespace infertypes { |
28 namespace infertypes { |
29 |
29 |
30 class DataMode : public Mode { |
30 class DataMode : public Mode { |
31 private: |
31 private: |
|
32 relpipe::common::type::StringX name; |
|
33 std::vector<relpipe::reader::handlers::AttributeMetadata> attributes; |
|
34 std::vector<relpipe::common::type::StringX> values; |
|
35 |
|
36 bool matches(int attributeIndex, const std::wregex& pattern) { |
|
37 for (int record = 0, attributeCount = attributes.size(), limit = values.size() / attributeCount; record < limit; record++) if (!std::regex_match(values[record * attributeIndex], pattern)) return false; |
|
38 return true; |
|
39 } |
|
40 |
32 public: |
41 public: |
33 |
42 |
34 DataMode(shared_ptr<writer::RelationalWriter> relationalWriter) : Mode(relationalWriter) { |
43 DataMode(shared_ptr<writer::RelationalWriter> relationalWriter) : Mode(relationalWriter) { |
35 } |
44 } |
36 |
45 |
37 void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
46 void startRelation(relpipe::common::type::StringX name, std::vector<relpipe::reader::handlers::AttributeMetadata> attributes) override { |
|
47 this->name = name; |
|
48 this->attributes = attributes; |
38 } |
49 } |
39 |
50 |
40 void attribute(const relpipe::common::type::StringX& value) override { |
51 void attribute(const relpipe::common::type::StringX& value) override { |
|
52 values.push_back(value); |
|
53 } |
|
54 |
|
55 virtual ~DataMode() { |
|
56 std::vector<bool> booleans(attributes.size(), true); |
|
57 std::vector<bool> integers(attributes.size(), true); |
|
58 |
|
59 for (int i = 0, limit = attributes.size(); i < limit; i++) { |
|
60 booleans[i] = matches(i, std::wregex(L"true|false")); |
|
61 integers[i] = matches(i, std::wregex(L"[0-9]+")); |
|
62 } |
|
63 |
|
64 vector<writer::AttributeMetadata> writerMetadata; |
|
65 for (int i = 0, limit = attributes.size(); i < limit; i++) { |
|
66 relpipe::reader::handlers::AttributeMetadata& am = attributes[i]; |
|
67 |
|
68 relpipe::writer::TypeId type; |
|
69 if (booleans[i]) type = relpipe::writer::TypeId::BOOLEAN; |
|
70 else if (integers[i]) type = relpipe::writer::TypeId::INTEGER; |
|
71 else type = relpipe::writer::TypeId::STRING; |
|
72 |
|
73 writerMetadata.push_back({am.getAttributeName(), type}); |
|
74 } |
|
75 |
|
76 relationalWriter->startRelation(name, writerMetadata, true); |
|
77 for (relpipe::common::type::StringX& value : values) relationalWriter->writeAttribute(value); |
41 } |
78 } |
42 }; |
79 }; |
43 |
80 |
44 |
81 |
45 } |
82 } |