author | František Kučera <franta-hg@frantovo.cz> |
Tue, 22 Jan 2019 16:12:32 +0100 | |
branch | v_0 |
changeset 4 | 9b4fe4bc5f0f |
parent 3 | d4d0e50f54da |
child 5 | 17bb45570099 |
permissions | -rw-r--r-- |
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 |
||
1
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
29 |
#include <libguile.h> |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
30 |
|
0 | 31 |
#include <relpipe/reader/typedefs.h> |
32 |
#include <relpipe/reader/TypeId.h> |
|
33 |
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h> |
|
34 |
#include <relpipe/reader/handlers/AttributeMetadata.h> |
|
35 |
||
36 |
#include <relpipe/writer/Factory.h> |
|
37 |
||
38 |
#include <relpipe/cli/RelpipeCLIException.h> |
|
39 |
||
40 |
namespace relpipe { |
|
41 |
namespace tr { |
|
42 |
namespace guile { |
|
43 |
||
44 |
using namespace std; |
|
45 |
using namespace relpipe; |
|
46 |
using namespace relpipe::reader; |
|
47 |
using namespace relpipe::reader::handlers; |
|
48 |
||
49 |
class GuileHandler : public RelationalReaderStringHadler { |
|
50 |
private: |
|
1
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
51 |
std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
52 |
|
0 | 53 |
shared_ptr<writer::RelationalWriter> relationalWriter; |
54 |
||
55 |
wregex relationNameRegEx; |
|
56 |
||
57 |
vector<AttributeMetadata> currentMetadata; |
|
58 |
vector<string_t> currentRecord; |
|
59 |
integer_t currentAttributeIndex = 0; |
|
60 |
boolean_t includeCurrentRecord = false; |
|
61 |
boolean_t filterCurrentRelation = false; |
|
1
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
62 |
string_t guileCode; |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
63 |
|
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
64 |
SCM toGuileSymbol(const string_t& name) { |
3
d4d0e50f54da
add $ prefix to attribute variables
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
65 |
return scm_string_to_symbol(scm_from_locale_string(convertor.to_bytes(L"$" + name).c_str())); |
1
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
66 |
} |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
67 |
|
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
68 |
SCM toGuileString(const string_t& value) { |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
69 |
return scm_from_locale_string(convertor.to_bytes(value).c_str()); |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
70 |
} |
2
7fb4d2c70e8c
record filtering in Guile
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
71 |
|
1
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
72 |
SCM toGuileInteger(const string_t& value) { |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
73 |
return scm_from_uint64(stoul(value)); |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
74 |
} |
2
7fb4d2c70e8c
record filtering in Guile
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
75 |
|
1
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
76 |
SCM toGuileBoolean(const string_t& value) { |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
77 |
return value == L"true" ? SCM_BOOL_T : SCM_BOOL_F; |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
78 |
} |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
79 |
|
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
80 |
void defineGuileVariable(const string_t& name, TypeId type, const string_t& value) { |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
81 |
// TODO: RelationalReaderValueHadler? |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
82 |
switch (type) { |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
83 |
case TypeId::BOOLEAN: |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
84 |
scm_define(toGuileSymbol(name), toGuileBoolean(value)); |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
85 |
break; |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
86 |
case TypeId::INTEGER: |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
87 |
scm_define(toGuileSymbol(name), toGuileInteger(value)); |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
88 |
break; |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
89 |
case TypeId::STRING: |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
90 |
scm_define(toGuileSymbol(name), toGuileString(value)); |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
91 |
break; |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
92 |
default: |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
93 |
throw cli::RelpipeCLIException(L"Unsupported type in defineGuileVariable()", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
94 |
} |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
95 |
} |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
96 |
|
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
97 |
void undefineGuileVariable(const string_t& name, TypeId type, const string_t& value) { |
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
98 |
scm_define(toGuileSymbol(name), scm_make_undefined_variable()); // undefined != (define n) |
3
d4d0e50f54da
add $ prefix to attribute variables
František Kučera <franta-hg@frantovo.cz>
parents:
2
diff
changeset
|
99 |
// TODO: or use: scm_variable_unset_x() ? |
1
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
100 |
} |
0 | 101 |
|
102 |
public: |
|
103 |
||
104 |
GuileHandler(ostream& output, const vector<string_t>& arguments) { |
|
105 |
relationalWriter.reset(writer::Factory::create(output)); |
|
106 |
||
107 |
// TODO: options and parser |
|
1
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
108 |
if (arguments.size() == 2) { |
0 | 109 |
relationNameRegEx = wregex(arguments[0]); |
1
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
110 |
guileCode = arguments[1]; |
0 | 111 |
} else { |
4 | 112 |
throw cli::RelpipeCLIException(L"Usage: relpipe-tr-guile <relationNameRegExp> <whereConditionGuileCode>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND); |
0 | 113 |
} |
114 |
} |
|
115 |
||
116 |
void startRelation(string_t name, vector<AttributeMetadata> attributes) override { |
|
117 |
currentMetadata = attributes; |
|
118 |
// TODO: move to a reusable method (or use same metadata on both reader and writer side?) |
|
119 |
vector<writer::AttributeMetadata> writerMetadata; |
|
120 |
for (AttributeMetadata readerMetadata : attributes) { |
|
1
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
121 |
|
0 | 122 |
writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())}); |
123 |
} |
|
1
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
124 |
|
0 | 125 |
currentRecord.resize(attributes.size()); |
126 |
filterCurrentRelation = regex_match(name, relationNameRegEx); |
|
127 |
||
128 |
relationalWriter->startRelation(name, writerMetadata, true); |
|
129 |
} |
|
130 |
||
131 |
void attribute(const string_t& value) override { |
|
132 |
if (filterCurrentRelation) { |
|
133 |
currentRecord[currentAttributeIndex] = value; |
|
2
7fb4d2c70e8c
record filtering in Guile
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
134 |
defineGuileVariable(currentMetadata[currentAttributeIndex].getAttributeName(), currentMetadata[currentAttributeIndex].getTypeId(), value); |
0 | 135 |
currentAttributeIndex++; |
136 |
||
137 |
if (currentAttributeIndex > 0 && currentAttributeIndex % currentMetadata.size() == 0) { |
|
2
7fb4d2c70e8c
record filtering in Guile
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
138 |
// TODO: support also updates in Guile |
7fb4d2c70e8c
record filtering in Guile
František Kučera <franta-hg@frantovo.cz>
parents:
1
diff
changeset
|
139 |
includeCurrentRecord = scm_to_bool(scm_eval_string(toGuileString(guileCode))); |
0 | 140 |
if (includeCurrentRecord) for (string_t v : currentRecord) relationalWriter->writeAttribute(v); |
141 |
includeCurrentRecord = false; |
|
142 |
} |
|
143 |
||
144 |
currentAttributeIndex = currentAttributeIndex % currentMetadata.size(); |
|
145 |
} else { |
|
1
9179406ab3b3
link to the Guile library and add some Guile demo code – evaluate code from CLI argument
František Kučera <franta-hg@frantovo.cz>
parents:
0
diff
changeset
|
146 |
|
0 | 147 |
relationalWriter->writeAttribute(value); |
148 |
} |
|
149 |
} |
|
150 |
||
151 |
void endOfPipe() { |
|
152 |
||
153 |
} |
|
154 |
||
155 |
}; |
|
156 |
||
157 |
} |
|
158 |
} |
|
159 |
} |