0
|
1 |
/**
|
|
2 |
* Relational pipes
|
|
3 |
* Copyright © 2019 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, either version 3 of the License, or
|
|
8 |
* (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17 |
*/
|
|
18 |
#pragma once
|
|
19 |
|
|
20 |
#include <memory>
|
|
21 |
#include <string>
|
|
22 |
#include <vector>
|
|
23 |
#include <iostream>
|
|
24 |
#include <sstream>
|
|
25 |
#include <locale>
|
|
26 |
#include <codecvt>
|
|
27 |
#include <regex>
|
|
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 |
namespace relpipe {
|
|
39 |
namespace tr {
|
|
40 |
namespace guile {
|
|
41 |
|
|
42 |
using namespace std;
|
|
43 |
using namespace relpipe;
|
|
44 |
using namespace relpipe::reader;
|
|
45 |
using namespace relpipe::reader::handlers;
|
|
46 |
|
|
47 |
class GuileHandler : public RelationalReaderStringHadler {
|
|
48 |
private:
|
|
49 |
shared_ptr<writer::RelationalWriter> relationalWriter;
|
|
50 |
|
|
51 |
wregex relationNameRegEx;
|
|
52 |
|
|
53 |
vector<AttributeMetadata> currentMetadata;
|
|
54 |
vector<string_t> currentRecord;
|
|
55 |
integer_t currentAttributeIndex = 0;
|
|
56 |
boolean_t includeCurrentRecord = false;
|
|
57 |
boolean_t filterCurrentRelation = false;
|
|
58 |
|
|
59 |
public:
|
|
60 |
|
|
61 |
GuileHandler(ostream& output, const vector<string_t>& arguments) {
|
|
62 |
relationalWriter.reset(writer::Factory::create(output));
|
|
63 |
|
|
64 |
// TODO: options and parser
|
|
65 |
if (arguments.size() == 1) {
|
|
66 |
relationNameRegEx = wregex(arguments[0]);
|
|
67 |
} else {
|
|
68 |
throw cli::RelpipeCLIException(L"Usage: relpipe-tr-guile <relationNameRegExp>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
|
|
69 |
}
|
|
70 |
}
|
|
71 |
|
|
72 |
void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
|
|
73 |
currentMetadata = attributes;
|
|
74 |
// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
|
|
75 |
vector<writer::AttributeMetadata> writerMetadata;
|
|
76 |
for (AttributeMetadata readerMetadata : attributes) {
|
|
77 |
writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
|
|
78 |
}
|
|
79 |
|
|
80 |
currentRecord.resize(attributes.size());
|
|
81 |
filterCurrentRelation = regex_match(name, relationNameRegEx);
|
|
82 |
|
|
83 |
relationalWriter->startRelation(name, writerMetadata, true);
|
|
84 |
}
|
|
85 |
|
|
86 |
void attribute(const string_t& value) override {
|
|
87 |
if (filterCurrentRelation) {
|
|
88 |
// TODO: evaluate condition and updates in Guile
|
|
89 |
currentRecord[currentAttributeIndex] = value;
|
|
90 |
includeCurrentRecord = false;
|
|
91 |
|
|
92 |
currentAttributeIndex++;
|
|
93 |
|
|
94 |
if (currentAttributeIndex > 0 && currentAttributeIndex % currentMetadata.size() == 0) {
|
|
95 |
if (includeCurrentRecord) for (string_t v : currentRecord) relationalWriter->writeAttribute(v);
|
|
96 |
includeCurrentRecord = false;
|
|
97 |
}
|
|
98 |
|
|
99 |
currentAttributeIndex = currentAttributeIndex % currentMetadata.size();
|
|
100 |
} else {
|
|
101 |
relationalWriter->writeAttribute(value);
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
void endOfPipe() {
|
|
106 |
|
|
107 |
}
|
|
108 |
|
|
109 |
};
|
|
110 |
|
|
111 |
}
|
|
112 |
}
|
|
113 |
}
|