author | František Kučera <franta-hg@frantovo.cz> |
Sun, 23 May 2021 17:17:33 +0200 | |
branch | v_0 |
changeset 4 | 66c8a1783884 |
parent 1 | da114916734b |
child 9 | 215f62cc655f |
permissions | -rw-r--r-- |
0 | 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> |
|
1
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
27 |
#include <stdexcept> |
0 | 28 |
|
29 |
#include <relpipe/reader/typedefs.h> |
|
30 |
#include <relpipe/reader/TypeId.h> |
|
31 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
32 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
33 |
||
34 |
#include <relpipe/writer/Factory.h> |
|
35 |
||
36 |
#include <relpipe/cli/RelpipeCLIException.h> |
|
37 |
||
38 |
#include "Configuration.h" |
|
1
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
39 |
#include "Mode.h" |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
40 |
#include "DataMode.h" |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
41 |
#include "MetadataMode.h" |
0 | 42 |
|
43 |
namespace relpipe { |
|
44 |
namespace tr { |
|
45 |
namespace infertypes { |
|
46 |
||
47 |
using namespace std; |
|
48 |
using namespace relpipe; |
|
49 |
using namespace relpipe::reader; |
|
50 |
using namespace relpipe::reader::handlers; |
|
51 |
||
52 |
class InferTypesHandler : public RelationalReaderStringHandler { |
|
53 |
private: |
|
54 |
shared_ptr<writer::RelationalWriter> relationalWriter; |
|
55 |
Configuration configuration; |
|
1
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
56 |
std::shared_ptr<Mode> currentMode; |
0 | 57 |
|
1
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
58 |
vector<writer::AttributeMetadata> toWriterMetadata(vector<AttributeMetadata> attributes) { |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
59 |
// TODO: move to a reusable method (or use same metadata on both reader and writer side?) |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
60 |
vector<writer::AttributeMetadata> writerMetadata; |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
61 |
for (AttributeMetadata readerMetadata : attributes) { |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
62 |
writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())}); |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
63 |
} |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
64 |
return writerMetadata; |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
65 |
} |
0 | 66 |
|
67 |
public: |
|
68 |
||
69 |
InferTypesHandler(shared_ptr<writer::RelationalWriter> relationalWriter, Configuration configuration) : relationalWriter(relationalWriter), configuration(configuration) { |
|
70 |
} |
|
71 |
||
72 |
void startRelation(string_t name, vector<AttributeMetadata> attributes) override { |
|
1
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
73 |
// TODO: move this logic to relpipe-lib-infertypes and share with certain modules like relpipe-in-csv |
4
66c8a1783884
do not keep Mode from previous relation
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
74 |
currentMode.reset(); |
1
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
75 |
for (RelationConfiguration rc : configuration.relationConfigurations) { |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
76 |
if (std::regex_match(name, rc.relationPattern)) { |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
if (rc.mode == MODE::METADATA || rc.mode == MODE::AUTO && MetadataMode::willInfer(attributes)) currentMode.reset(new MetadataMode(relationalWriter)); |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
else if (rc.mode == MODE::DATA) currentMode.reset(new DataMode(relationalWriter)); |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
79 |
else throw std::logic_error("Unsupported mode: " + std::to_string((int) rc.mode)); |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
80 |
break; |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
81 |
} |
0 | 82 |
} |
83 |
||
1
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
84 |
if (currentMode) currentMode->startRelation(name, attributes); |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
85 |
else relationalWriter->startRelation(name, toWriterMetadata(attributes), true); |
0 | 86 |
} |
87 |
||
88 |
void attribute(const string_t& value) override { |
|
1
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
89 |
if (currentMode) currentMode->attribute(value); |
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
90 |
else relationalWriter->writeAttribute(value); |
0 | 91 |
} |
92 |
||
93 |
void endOfPipe() { |
|
1
da114916734b
multiple modes infrastructure
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
94 |
currentMode.reset(); |
0 | 95 |
} |
96 |
||
97 |
}; |
|
98 |
||
99 |
} |
|
100 |
} |
|
101 |
} |