src/GuileHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 19 Jan 2019 16:01:08 +0100
branchv_0
changeset 0 f36bf14d45cb
child 1 9179406ab3b3
permissions -rw-r--r--
project skeleton
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>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/typedefs.h>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/TypeId.h>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include <relpipe/reader/handlers/AttributeMetadata.h>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include <relpipe/writer/Factory.h>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
#include <relpipe/cli/RelpipeCLIException.h>
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
namespace relpipe {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
namespace tr {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
namespace guile {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
using namespace std;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
using namespace relpipe;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
using namespace relpipe::reader;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
using namespace relpipe::reader::handlers;
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
class GuileHandler : public RelationalReaderStringHadler {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
private:
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	shared_ptr<writer::RelationalWriter> relationalWriter;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	wregex relationNameRegEx;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	vector<AttributeMetadata> currentMetadata;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	vector<string_t> currentRecord;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	integer_t currentAttributeIndex = 0;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	boolean_t includeCurrentRecord = false;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	boolean_t filterCurrentRelation = false;
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
public:
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	GuileHandler(ostream& output, const vector<string_t>& arguments) {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		relationalWriter.reset(writer::Factory::create(output));
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		// TODO: options and parser
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
		if (arguments.size() == 1) {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
			relationNameRegEx = wregex(arguments[0]);
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		} else {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
			throw cli::RelpipeCLIException(L"Usage: relpipe-tr-guile <relationNameRegExp>", cli::CLI::EXIT_CODE_UNKNOWN_COMMAND);
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
		}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	void startRelation(string_t name, vector<AttributeMetadata> attributes) override {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		currentMetadata = attributes;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		// TODO: move to a reusable method (or use same metadata on both reader and writer side?)
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		vector<writer::AttributeMetadata> writerMetadata;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
		for (AttributeMetadata readerMetadata : attributes) {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
			writerMetadata.push_back({readerMetadata.getAttributeName(), relationalWriter->toTypeId(readerMetadata.getTypeName())});
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
		}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
		currentRecord.resize(attributes.size());
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
		filterCurrentRelation = regex_match(name, relationNameRegEx);
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
		relationalWriter->startRelation(name, writerMetadata, true);
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
	}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
	void attribute(const string_t& value) override {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
		if (filterCurrentRelation) {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
			// TODO: evaluate condition and updates in Guile
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
			currentRecord[currentAttributeIndex] = value;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
			includeCurrentRecord = false;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
			currentAttributeIndex++;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
			if (currentAttributeIndex > 0 && currentAttributeIndex % currentMetadata.size() == 0) {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
				if (includeCurrentRecord) for (string_t v : currentRecord) relationalWriter->writeAttribute(v);
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
				includeCurrentRecord = false;
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
			}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
			currentAttributeIndex = currentAttributeIndex % currentMetadata.size();
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
		} else {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
			relationalWriter->writeAttribute(value);
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
		}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
	}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
	void endOfPipe() {
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
	}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
};
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
}
f36bf14d45cb project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
}