src/GuileHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 03 Feb 2019 01:44:07 +0100
branchv_0
changeset 6 4062b8436838
parent 5 17bb45570099
child 7 61fc569b77e6
permissions -rw-r--r--
CLI parser: first version (only single relation is supported)
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
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * 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
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * 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
    16
 * 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
    17
 */
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#pragma once
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <memory>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <string>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <vector>
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"
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    42
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
namespace relpipe {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
namespace tr {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
namespace guile {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
using namespace std;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
using namespace relpipe;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
using namespace relpipe::reader;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
using namespace relpipe::reader::handlers;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
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
    52
class GuileHandler : public RelationalReaderValueHadler {
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
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
    54
	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
    55
6
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    56
	Configuration configuration;
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	shared_ptr<writer::RelationalWriter> relationalWriter;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	wregex relationNameRegEx;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
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;
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	vector<string_t> currentRecord;
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;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
	boolean_t filterCurrentRelation = false;
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
    67
	string_t guileCodeWhereCondition;
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
    68
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
    69
	/**
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
    70
	 * @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
    71
	 * @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
    72
	 */
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
    73
	string_t a2v(const string_t& 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
    74
		// TODO: escape spaces and special characters
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
    75
		return L"$" + attributeName;
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
	}
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
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
    78
	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
    79
		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
    80
	}
2
7fb4d2c70e8c record filtering in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    81
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
    82
	SCM evalGuileCode(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
    83
		return scm_eval_string(toGuileValue(&value, typeid (string_t), TypeId::STRING));
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
    84
	}
2
7fb4d2c70e8c record filtering in Guile
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    85
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
    86
	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
    87
		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
    88
			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
    89
			{
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
    90
				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
    91
				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
    92
				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
    93
			}
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
    94
			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
    95
			{
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
    96
				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
    97
				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
    98
				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
    99
			}
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
			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
   101
			{
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
   102
				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
   103
				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
   104
				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
   105
			}
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
   106
			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
   107
				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
   108
		}
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
	}
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
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
   111
	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
   112
		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
   113
	}
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
   114
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
   115
	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
   116
		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
   117
		// 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
   118
	}
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
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
   120
	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
   121
		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
   122
		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
   123
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
   124
		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
   125
			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
   126
			{
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
   127
				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
   128
				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
   129
			}
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
   130
			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
   131
			{
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
   132
				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
   133
				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
   134
			}
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
   135
			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
   136
			{
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
   137
				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
   138
				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
   139
				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
   140
				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
   141
			}
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
			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
   143
				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
   144
		}
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
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   147
public:
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   148
6
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   149
	GuileHandler(ostream& output, Configuration& configuration, const vector<string_t>& arguments) : configuration(configuration) {
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   150
		relationalWriter.reset(writer::Factory::create(output));
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   151
6
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   152
		// FIXME: remove and work directly with configuration in startRelation() and attribute()
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   153
		// i.e. support multiple relationConfigurations
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   154
		if (configuration.relationConfigurations.size() == 1) {
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   155
			relationNameRegEx = wregex(configuration.relationConfigurations[0].relation);
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   156
			guileCodeWhereCondition = configuration.relationConfigurations[0].guileWhere;
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   157
		} else {
6
4062b8436838 CLI parser: first version (only single relation is supported)
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   158
			throw cli::RelpipeCLIException(L"FIXME: only single relationConfiguration is currently supported", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   159
		}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   160
	}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   161
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   162
	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
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
   163
		for (auto attribute : currentReaderMetadata) undefineGuileVariable(attribute.getAttributeName());
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
		currentReaderMetadata = attributes;
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   165
		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
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
   166
		// TODO: allow structural changes during transformation
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
   167
		// TODO: clear Guile variables for attributes from previous relation
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
   168
		currentWriterMetadata.clear();
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   169
		for (AttributeMetadata readerMetadata : attributes) {
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
   170
			currentWriterMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   171
		}
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
   172
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   173
		currentRecord.resize(attributes.size());
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   174
		filterCurrentRelation = regex_match(name, relationNameRegEx);
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   175
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
   176
		relationalWriter->startRelation(name, currentWriterMetadata, true);
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   177
	}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   178
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
   179
	void attribute(const void* value, const std::type_info& type) override {
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   180
		if (filterCurrentRelation) {
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
   181
			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
   182
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   183
			currentAttributeIndex++;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   184
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
   185
			if (currentAttributeIndex > 0 && currentAttributeIndex % currentReaderMetadata.size() == 0) {
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
   186
				includeCurrentRecord = scm_to_bool(evalGuileCode(guileCodeWhereCondition));
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
   187
				if (includeCurrentRecord) for (auto attribute : currentWriterMetadata) writeGuileValueToAttribute(attribute);
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   188
				includeCurrentRecord = false;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   189
			}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   190
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
   191
			currentAttributeIndex = currentAttributeIndex % currentReaderMetadata.size();
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   192
		} 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
   193
			relationalWriter->writeAttribute(value, type);
0
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   194
		}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   195
	}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   196
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   197
	void endOfPipe() {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   198
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   199
	}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   200
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   201
};
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   202
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   203
}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   204
}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   205
}