src/GuileHandler.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 22 Oct 2019 22:03:24 +0200
branchv_0
changeset 26 421608ecc12a
parent 23 6ee7a9e311e9
child 29 caee22a6370a
permissions -rw-r--r--
fix license version: GNU GPLv3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
26
421608ecc12a fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 23
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
23
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    22
#include <map>
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <iostream>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <sstream>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <locale>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <codecvt>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <regex>
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    28
#include <assert.h>
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
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
    30
#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
    31
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include <relpipe/reader/typedefs.h>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include <relpipe/reader/TypeId.h>
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    34
#include <relpipe/reader/handlers/RelationalReaderValueHandler.h>
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
#include <relpipe/reader/handlers/AttributeMetadata.h>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
#include <relpipe/writer/Factory.h>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
#include <relpipe/cli/RelpipeCLIException.h>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
6
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    41
#include "Configuration.h"
23
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    42
#include "GuileException.h"
6
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    43
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
namespace relpipe {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
namespace tr {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
namespace guile {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
using namespace std;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
using namespace relpipe;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
using namespace relpipe::reader;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
using namespace relpipe::reader::handlers;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
18
3f46b784b617 fix typo: Hadler → Handler
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    53
class GuileHandler : public RelationalReaderValueHandler {
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
private:
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    55
	std::wstring_convert<codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings or use always UTF-8 between C++ and Guile
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
    56
6
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    57
	Configuration configuration;
7
61fc569b77e6 RelationalWriter dependency injection
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
    58
	writer::RelationalWriter* relationalWriter;
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
8
1e6206284c6c Guile can now process multiple relations in the stream
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
    60
	RelationConfiguration* currentRelationConfiguration = nullptr;
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    61
	vector<AttributeMetadata> currentReaderMetadata;
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    62
	vector<writer::AttributeMetadata> currentWriterMetadata;
23
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    63
	std::map<string_t, string_t> currenVariablesMapping;
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	integer_t currentAttributeIndex = 0;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	boolean_t includeCurrentRecord = 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
    66
15
051e58022783 add --input-attributes-append and --input-attributes-prepend for adding attributes using --output-attribute instead of replacing whole attribute list
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    67
	void add(vector<AttributeMetadata>& readerAttributes, vector<writer::AttributeMetadata>& writerAttributes) {
051e58022783 add --input-attributes-append and --input-attributes-prepend for adding attributes using --output-attribute instead of replacing whole attribute list
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    68
		for (AttributeMetadata readerAttributes : readerAttributes)
051e58022783 add --input-attributes-append and --input-attributes-prepend for adding attributes using --output-attribute instead of replacing whole attribute list
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    69
			writerAttributes.push_back({
051e58022783 add --input-attributes-append and --input-attributes-prepend for adding attributes using --output-attribute instead of replacing whole attribute list
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    70
				readerAttributes.getAttributeName(),
051e58022783 add --input-attributes-append and --input-attributes-prepend for adding attributes using --output-attribute instead of replacing whole attribute list
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    71
				relationalWriter->toTypeId(readerAttributes.getTypeName())
051e58022783 add --input-attributes-append and --input-attributes-prepend for adding attributes using --output-attribute instead of replacing whole attribute list
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    72
			});
051e58022783 add --input-attributes-append and --input-attributes-prepend for adding attributes using --output-attribute instead of replacing whole attribute list
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    73
	}
051e58022783 add --input-attributes-append and --input-attributes-prepend for adding attributes using --output-attribute instead of replacing whole attribute list
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    74
23
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    75
	void generateVariableMappings() {
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    76
		currenVariablesMapping.clear();
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    77
		for (AttributeMetadata m : currentReaderMetadata) currenVariablesMapping[m.getAttributeName()] = L"";
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    78
		for (writer::AttributeMetadata m : currentWriterMetadata) currenVariablesMapping[m.attributeName] = L"";
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    79
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    80
		for (std::pair<string_t, string_t> m : currenVariablesMapping) {
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    81
			currenVariablesMapping[m.first] = escapeAwkVariableName(m.first);
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    82
		}
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    83
	}
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    84
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    85
	/**
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    86
	 * @param attributeName name from relational pipe
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    87
	 * @return variable name in Guile
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    88
	 */
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
    89
	string_t a2v(const string_t& attributeName) {
23
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    90
		if (currenVariablesMapping.find(attributeName) != currenVariablesMapping.end()) return currenVariablesMapping[attributeName];
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    91
		else throw GuileException(L"Unable to find value in currenVariablesMapping");
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    92
	}
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    93
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    94
	template <typename K, typename V> bool containsValue(std::map<K, V> map, V value) { // TODO: common function (Guile, AWK)
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    95
		for (std::pair<K, V> p : map) if (p.second == value) return true;
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    96
		return false;
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    97
	}
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    98
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
    99
	string_t escapeAwkVariableName(const string_t& attributeName, bool addPrefix = true) {
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   100
		std::wregex badCharacters(L"\\s");
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   101
		string_t name = std::regex_replace(attributeName, badCharacters, L"-");
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   102
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   103
		if (addPrefix) name = L"$" + name; // $ = standard attribute-variable prefix
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   104
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   105
		if (containsValue(currenVariablesMapping, name)) return escapeAwkVariableName(L"$" + name, false); // $ = different prefix added to distinguish two attributes with ambiguous names
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   106
		else return name;
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   107
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   108
	}
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   109
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   110
	void debugVariableMapping(const string_t& relationName) {
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   111
		relationalWriter->startRelation(relationName + L".variableMapping",{
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   112
			{L"attribute", writer::TypeId::STRING},
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   113
			{L"variable", writer::TypeId::STRING},
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   114
		}, true);
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   115
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   116
		for (std::pair<string_t, string_t> m : currenVariablesMapping) {
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   117
			relationalWriter->writeAttribute(m.first);
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   118
			relationalWriter->writeAttribute(m.second);
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   119
		}
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
	}
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
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   122
	SCM toGuileSymbol(const string_t& name) {
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   123
		return scm_string_to_symbol(scm_from_locale_string(convertor.to_bytes(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
   124
	}
2
7fb4d2c70e8c record filtering in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   125
9
65f9d6b94f5c Guile --for-each record
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   126
	/**
65f9d6b94f5c Guile --for-each record
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   127
	 * @param code guile source code e.g. (+ 1 2 3) or #t
65f9d6b94f5c Guile --for-each record
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   128
	 * @param defaultReturnValue is returned if code is empty
65f9d6b94f5c Guile --for-each record
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   129
	 * @return result of code execution or defaultReturnValue
65f9d6b94f5c Guile --for-each record
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   130
	 */
65f9d6b94f5c Guile --for-each record
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   131
	SCM evalGuileCode(const string_t& code, SCM defaultReturnValue = SCM_BOOL_F) {
65f9d6b94f5c Guile --for-each record
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   132
		if (code.size()) return scm_eval_string(toGuileValue(&code, typeid (string_t), TypeId::STRING));
65f9d6b94f5c Guile --for-each record
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   133
		else return defaultReturnValue;
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
   134
	}
2
7fb4d2c70e8c record filtering in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   135
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   136
	SCM toGuileValue(const void* value, const std::type_info& typeInfo, TypeId type) {
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
   137
		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
   138
			case TypeId::BOOLEAN:
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   139
			{
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   140
				assert(typeInfo == typeid (boolean_t));
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   141
				auto* typedValue = static_cast<const boolean_t*> (value);
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   142
				return *typedValue ? SCM_BOOL_T : SCM_BOOL_F;
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   143
			}
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
   144
			case TypeId::INTEGER:
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   145
			{
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   146
				assert(typeInfo == typeid (integer_t));
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   147
				auto* typedValue = static_cast<const integer_t*> (value);
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   148
				return scm_from_uint64(*typedValue);
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   149
			}
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
   150
			case TypeId::STRING:
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   151
			{
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   152
				assert(typeInfo == typeid (string_t));
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   153
				auto* typedValue = static_cast<const string_t*> (value);
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   154
				return scm_from_locale_string(convertor.to_bytes(*typedValue).c_str());
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   155
			}
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
   156
			default:
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   157
				throw cli::RelpipeCLIException(L"Unsupported type in toGuileValue()", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR);
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
   158
		}
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
   159
	}
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
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   161
	void defineGuileVariable(const string_t& name, const void* value, const std::type_info& typeInfo, TypeId type) {
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   162
		scm_define(toGuileSymbol(name), toGuileValue(value, typeInfo, type));
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   163
	}
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   164
11
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   165
	/**
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   166
	 * TODO: use a common method
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   167
	 */
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   168
	bool parseBoolean(const string_t& value) {
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   169
		if (value == L"true") return true;
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   170
		else if (value == L"false") return false;
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   171
		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   172
	}
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   173
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   174
	void defineGuileVariable(const DefinitionRecipe& definition) {
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   175
		switch (relationalWriter->toTypeId(definition.type)) {
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   176
			case writer::TypeId::BOOLEAN:
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   177
			{
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   178
				boolean_t value = parseBoolean(definition.value);
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   179
				defineGuileVariable(definition.name, &value, typeid (value), TypeId::BOOLEAN);
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   180
				break;
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   181
			}
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   182
			case writer::TypeId::INTEGER:
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   183
			{
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   184
				integer_t value = stoul(definition.value);
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   185
				defineGuileVariable(definition.name, &value, typeid (value), TypeId::INTEGER);
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   186
				break;
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   187
			}
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   188
			case writer::TypeId::STRING:
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   189
			{
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   190
				defineGuileVariable(definition.name, &definition.value, typeid (definition.value), TypeId::STRING);
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   191
				break;
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   192
			}
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   193
			default:
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   194
				throw cli::RelpipeCLIException(L"Unsupported type in defineGuileVariable(): " + definition.type, cli::CLI::EXIT_CODE_UNEXPECTED_ERROR);
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   195
		}
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   196
	}
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   197
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   198
	void undefineGuileVariable(const string_t& name) {
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
   199
		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
   200
		// 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
   201
	}
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   202
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   203
	void writeGuileValueToAttribute(const writer::AttributeMetadata& attribute) {
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   204
		string_t variableName = a2v(attribute.attributeName);
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   205
		SCM guileValue = scm_eval_string(toGuileValue(&variableName, typeid (variableName), TypeId::STRING));
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   206
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   207
		switch (attribute.typeId) {
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   208
			case writer::TypeId::BOOLEAN:
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   209
			{
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   210
				boolean_t value = scm_to_bool(guileValue);
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   211
				return relationalWriter->writeAttribute(&value, typeid (value));
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   212
			}
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   213
			case writer::TypeId::INTEGER:
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   214
			{
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   215
				integer_t value = scm_to_uint64(guileValue);
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   216
				return relationalWriter->writeAttribute(&value, typeid (value));
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   217
			}
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   218
			case writer::TypeId::STRING:
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   219
			{
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   220
				char* ch = scm_to_locale_string(guileValue);
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   221
				string_t value = convertor.from_bytes(ch);
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   222
				free(ch);
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   223
				return relationalWriter->writeAttribute(&value, typeid (value));
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   224
			}
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   225
			default:
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   226
				throw cli::RelpipeCLIException(L"Unsupported type in writeGuileValueToAttribute()", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR);
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   227
		}
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   228
	}
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   229
16
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   230
	/**
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   231
	 * Read from the Guile variables and write to relational output stream.
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   232
	 */
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   233
	void writeCurrentRecord() {
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   234
		for (auto attribute : currentWriterMetadata) writeGuileValueToAttribute(attribute);
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   235
	}
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   236
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   237
	void writeMoreRecords() {
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   238
		while (scm_to_bool(evalGuileCode(currentRelationConfiguration->guileHasMoreRecords, SCM_BOOL_F))) writeCurrentRecord();
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   239
	}
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   240
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   241
public:
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   242
20
dccbfd273a5a remove unused argument
František Kučera <franta-hg@frantovo.cz>
parents: 18
diff changeset
   243
	GuileHandler(writer::RelationalWriter* relationalWriter, Configuration& configuration) : relationalWriter(relationalWriter), configuration(configuration) {
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   244
	}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   245
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   246
	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
11
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   247
		if (currentRelationConfiguration) {
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   248
			evalGuileCode(currentRelationConfiguration->guileAfterRecords);
16
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   249
			writeMoreRecords();
11
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   250
			for (DefinitionRecipe definition : currentRelationConfiguration->definitions) undefineGuileVariable(definition.name);
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   251
		}
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   252
		for (auto attribute : currentReaderMetadata) undefineGuileVariable(attribute.getAttributeName());
14
82bd0f57a889 add --drop option: all Guile code will be executed but not relational output for given relation will be generated
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   253
13
c9fece435aa2 add --define for global variables (can be overridden by relation's ones)
František Kučera <franta-hg@frantovo.cz>
parents: 12
diff changeset
   254
		for (DefinitionRecipe definition : configuration.definitions) defineGuileVariable(definition);
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
   255
10
f7f9a2553128 Guile --before-records, --after-records and relpipe-relation-name variable
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   256
		currentRelationConfiguration = nullptr;
8
1e6206284c6c Guile can now process multiple relations in the stream
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   257
		for (int i = 0; i < configuration.relationConfigurations.size(); i++) {
1e6206284c6c Guile can now process multiple relations in the stream
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   258
			if (regex_match(name, wregex(configuration.relationConfigurations[i].relation))) {
1e6206284c6c Guile can now process multiple relations in the stream
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   259
				currentRelationConfiguration = &configuration.relationConfigurations[i];
11
9603e64324bc add --define for passing parameters from the outside environment
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
   260
				for (DefinitionRecipe definition : currentRelationConfiguration->definitions) defineGuileVariable(definition);
8
1e6206284c6c Guile can now process multiple relations in the stream
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   261
				break; // it there are multiple matches, only the first configuration is used
1e6206284c6c Guile can now process multiple relations in the stream
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   262
			}
1e6206284c6c Guile can now process multiple relations in the stream
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   263
		}
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   264
12
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   265
		currentReaderMetadata = attributes;
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   266
		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   267
		currentWriterMetadata.clear();
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   268
		if (currentRelationConfiguration && currentRelationConfiguration->writerMetadata.size()) {
15
051e58022783 add --input-attributes-append and --input-attributes-prepend for adding attributes using --output-attribute instead of replacing whole attribute list
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   269
			if (currentRelationConfiguration->inputAttributesPrepend) add(currentReaderMetadata, currentWriterMetadata);
051e58022783 add --input-attributes-append and --input-attributes-prepend for adding attributes using --output-attribute instead of replacing whole attribute list
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   270
			currentWriterMetadata.insert(currentWriterMetadata.end(), currentRelationConfiguration->writerMetadata.begin(), currentRelationConfiguration->writerMetadata.end());
051e58022783 add --input-attributes-append and --input-attributes-prepend for adding attributes using --output-attribute instead of replacing whole attribute list
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   271
			if (currentRelationConfiguration->inputAttributesAppend) add(currentReaderMetadata, currentWriterMetadata);
12
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   272
		} else {
15
051e58022783 add --input-attributes-append and --input-attributes-prepend for adding attributes using --output-attribute instead of replacing whole attribute list
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   273
			add(currentReaderMetadata, currentWriterMetadata);
12
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   274
		}
7977c1bdba1f add --output-attribute so output relation can have different attributes than the input relation
František Kučera <franta-hg@frantovo.cz>
parents: 11
diff changeset
   275
23
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   276
		generateVariableMappings();
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   277
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   278
		if (currentRelationConfiguration->debugVariableMapping) debugVariableMapping(name);
6ee7a9e311e9 add option: --debug-variable-mapping + different variables for attributes with ambiguous names
František Kučera <franta-hg@frantovo.cz>
parents: 20
diff changeset
   279
14
82bd0f57a889 add --drop option: all Guile code will be executed but not relational output for given relation will be generated
František Kučera <franta-hg@frantovo.cz>
parents: 13
diff changeset
   280
		if (!currentRelationConfiguration || !currentRelationConfiguration->drop) relationalWriter->startRelation(name, currentWriterMetadata, true);
10
f7f9a2553128 Guile --before-records, --after-records and relpipe-relation-name variable
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   281
f7f9a2553128 Guile --before-records, --after-records and relpipe-relation-name variable
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   282
		if (currentRelationConfiguration) {
f7f9a2553128 Guile --before-records, --after-records and relpipe-relation-name variable
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   283
			// TODO: better variable name, object, function?
f7f9a2553128 Guile --before-records, --after-records and relpipe-relation-name variable
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   284
			defineGuileVariable(L"relpipe-relation-name", &name, typeid (name), TypeId::STRING);
f7f9a2553128 Guile --before-records, --after-records and relpipe-relation-name variable
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   285
			evalGuileCode(currentRelationConfiguration->guileBeforeRecords);
f7f9a2553128 Guile --before-records, --after-records and relpipe-relation-name variable
František Kučera <franta-hg@frantovo.cz>
parents: 9
diff changeset
   286
		}
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   287
	}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   288
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   289
	void attribute(const void* value, const std::type_info& type) override {
8
1e6206284c6c Guile can now process multiple relations in the stream
František Kučera <franta-hg@frantovo.cz>
parents: 7
diff changeset
   290
		if (currentRelationConfiguration) {
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   291
			defineGuileVariable(a2v(currentReaderMetadata[currentAttributeIndex].getAttributeName()), value, type, currentReaderMetadata[currentAttributeIndex].getTypeId());
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   292
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   293
			currentAttributeIndex++;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   294
7
61fc569b77e6 RelationalWriter dependency injection
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   295
			// TODO: > 0 ?:
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   296
			if (currentAttributeIndex > 0 && currentAttributeIndex % currentReaderMetadata.size() == 0) {
9
65f9d6b94f5c Guile --for-each record
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   297
				evalGuileCode(currentRelationConfiguration->guileForEach);
65f9d6b94f5c Guile --for-each record
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
   298
				includeCurrentRecord = scm_to_bool(evalGuileCode(currentRelationConfiguration->guileWhere, SCM_BOOL_T));
16
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   299
				if (includeCurrentRecord && !currentRelationConfiguration->drop) writeCurrentRecord();
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   300
				includeCurrentRecord = false;
16
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   301
				writeMoreRecords();
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   302
			}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   303
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   304
			currentAttributeIndex = currentAttributeIndex % currentReaderMetadata.size();
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   305
		} else {
5
17bb45570099 ValueHandler instead of StringHandler interface; Guile now allows filtering and record modifications
František Kučera <franta-hg@frantovo.cz>
parents: 4
diff changeset
   306
			relationalWriter->writeAttribute(value, type);
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   307
		}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   308
	}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   309
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   310
	void endOfPipe() {
16
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   311
		if (currentRelationConfiguration) {
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   312
			evalGuileCode(currentRelationConfiguration->guileAfterRecords);
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   313
			writeMoreRecords();
2dcc22b7a424 add --has-more-records and allow adding more records dynamically in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 15
diff changeset
   314
		}
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   315
	}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   316
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   317
};
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   318
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   319
}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   320
}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   321
}