author | František Kučera <franta-hg@frantovo.cz> |
Sun, 20 Jan 2019 01:02:40 +0100 | |
branch | v_0 |
changeset 1 | 9179406ab3b3 |
parent 0 | f36bf14d45cb |
child 2 | 7fb4d2c70e8c |
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) { |
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
|
65 |
return scm_string_to_symbol(scm_from_locale_string(convertor.to_bytes(name).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
|
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 |
} |
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
|
71 |
|
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 |
} |
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
|
75 |
|
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) |
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
|
99 |
} |
0 | 100 |
|
101 |
public: |
|
102 |
||
103 |
GuileHandler(ostream& output, const vector<string_t>& arguments) { |
|
104 |
relationalWriter.reset(writer::Factory::create(output)); |
|
105 |
||
106 |
// 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
|
107 |
if (arguments.size() == 2) { |
0 | 108 |
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
|
109 |
guileCode = arguments[1]; |
0 | 110 |
} else { |
111 |
throw cli::RelpipeCLIException(L"Usage: relpipe-tr-guile <relationNameRegExp>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND); |
|
112 |
} |
|
113 |
} |
|
114 |
||
115 |
void startRelation(string_t name, vector<AttributeMetadata> attributes) override { |
|
116 |
currentMetadata = attributes; |
|
117 |
// TODO: move to a reusable method (or use same metadata on both reader and writer side?) |
|
118 |
vector<writer::AttributeMetadata> writerMetadata; |
|
119 |
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
|
120 |
|
0 | 121 |
writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())}); |
122 |
} |
|
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
|
123 |
|
0 | 124 |
currentRecord.resize(attributes.size()); |
125 |
filterCurrentRelation = regex_match(name, relationNameRegEx); |
|
126 |
||
127 |
relationalWriter->startRelation(name, writerMetadata, true); |
|
128 |
} |
|
129 |
||
130 |
void attribute(const string_t& value) override { |
|
131 |
if (filterCurrentRelation) { |
|
132 |
// TODO: evaluate condition and updates in Guile |
|
133 |
currentRecord[currentAttributeIndex] = value; |
|
134 |
includeCurrentRecord = false; |
|
135 |
||
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
|
136 |
// TODO: remove, just a demo code |
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
|
137 |
defineGuileVariable(L"hello", TypeId::STRING, L"world"); |
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
|
138 |
defineGuileVariable(L"a", TypeId::INTEGER, L"123"); |
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
|
139 |
defineGuileVariable(L"b", TypeId::INTEGER, L"456"); |
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
|
140 |
defineGuileVariable(L"tr", TypeId::BOOLEAN, L"true"); |
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
|
141 |
defineGuileVariable(L"fa", TypeId::BOOLEAN, L"false"); |
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
|
142 |
|
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
|
143 |
//integer_t guileResult = scm_to_uint64(scm_eval_string(toGuileString(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
|
144 |
//std::wcerr << "result from Guile: " << guileResult << std::endl; |
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
|
145 |
|
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 |
includeCurrentRecord = scm_to_bool(scm_eval_string(toGuileString(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
|
147 |
//scm_shell(0, nullptr); |
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
|
148 |
// -------- |
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
|
149 |
|
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
|
150 |
|
0 | 151 |
currentAttributeIndex++; |
152 |
||
153 |
if (currentAttributeIndex > 0 && currentAttributeIndex % currentMetadata.size() == 0) { |
|
154 |
if (includeCurrentRecord) for (string_t v : currentRecord) relationalWriter->writeAttribute(v); |
|
155 |
includeCurrentRecord = false; |
|
156 |
} |
|
157 |
||
158 |
currentAttributeIndex = currentAttributeIndex % currentMetadata.size(); |
|
159 |
} 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
|
160 |
|
0 | 161 |
relationalWriter->writeAttribute(value); |
162 |
} |
|
163 |
} |
|
164 |
||
165 |
void endOfPipe() { |
|
166 |
||
167 |
} |
|
168 |
||
169 |
}; |
|
170 |
||
171 |
} |
|
172 |
} |
|
173 |
} |