src/RecfileHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 30 Mar 2019 01:12:42 +0100
branchv_0
changeset 0 9005fdd81bca
child 1 1b4ca23e5d04
permissions -rw-r--r--
project and code skeleton: text output
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
9005fdd81bca project and code skeleton: text output
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
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 * (at your option) any later version.
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 *
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 * GNU General Public License for more details.
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 *
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
 */
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#pragma once
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <codecvt>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <regex>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <cassert>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/typedefs.h>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/TypeId.h>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include <relpipe/reader/handlers/RelationalReaderValueHandler.h>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include <relpipe/reader/handlers/AttributeMetadata.h>
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
#include "RecfileWriter.h"
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
namespace relpipe {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace out {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
namespace recfile {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
using namespace relpipe::reader;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
class RecfileHandler : public handlers::RelationalReaderValueHadler {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
private:
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	shared_ptr<RecfileWriter> recfileWriter;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	std::vector<TypeId> columnTypes;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	std::vector<string_t> columnTypeCodes;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	std::vector<string_t> columnNames;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	integer_t valueCount = 0;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	integer_t columnCount = 0;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	integer_t relationCount = 0;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
public:
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	RecfileHandler(std::ostream& output) : recfileWriter(new RecfileWriter(output)) {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		valueCount = 0;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		columnCount = 0;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		if (relationCount == 0) {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
			recfileWriter->writeStartSequence(); // root
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		} else {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
			recfileWriter->writeEndSequence();
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
			recfileWriter->writeEndSequence();
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
		relationCount++;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
		recfileWriter->writeStartSequence(); // relation
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
		recfileWriter->writeString(name);
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
		recfileWriter->writeStartSequence(); // relation metadata
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
		columnCount = attributes.size();
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
		columnTypes.resize(columnCount);
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
		columnTypeCodes.resize(columnCount);
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
		columnNames.resize(columnCount);
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
		for (int i = 0; i < attributes.size(); i++) {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
			columnNames[i] = attributes[i].getAttributeName();
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
			columnTypes[i] = attributes[i].getTypeId();
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
			columnTypeCodes[i] = attributes[i].getTypeName();
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
			
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
			recfileWriter->writeStartSequence(); // attribute-metadata
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
			recfileWriter->writeString(columnNames[i]);
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
			recfileWriter->writeString(columnTypeCodes[i]);
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
			recfileWriter->writeEndSequence();
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
		}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
		recfileWriter->writeEndSequence();
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
	void attribute(const void* value, const std::type_info& type) override {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
		integer_t i = valueCount % columnCount;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
		if (i == 0 && valueCount) recfileWriter->writeEndSequence();
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
		if (i == 0) recfileWriter->writeStartSequence();
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
		valueCount++;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
		
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
		switch (columnTypes[i]) {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
			case TypeId::BOOLEAN:
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
			{
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
				assert(type == typeid (boolean_t));
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
				auto* typedValue = static_cast<const boolean_t*> (value);
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
				recfileWriter->writeBoolean(*typedValue);
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
				break;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
			}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
			case TypeId::INTEGER:
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
			{
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
				assert(type == typeid (integer_t));
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
				auto* typedValue = static_cast<const integer_t*> (value);
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
				recfileWriter->writeInteger(*typedValue);
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
				break;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
			}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
			case TypeId::STRING:
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
			{
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
				assert(type == typeid (string_t));
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
				auto* typedValue = static_cast<const string_t*> (value);
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
				recfileWriter->writeString(*typedValue);
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
				break;
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
			}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
			default:
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
				throw cli::RelpipeCLIException(L"Unsupported type in RecfileHandler.attribute()", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR);
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
		}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
		
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
	}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
	void endOfPipe() {
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
		if (valueCount) recfileWriter->writeEndSequence();
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
		if (relationCount) recfileWriter->writeEndSequence();
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   131
		recfileWriter->writeEndSequence();
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   132
	}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   133
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   134
};
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   135
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   136
}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   137
}
9005fdd81bca project and code skeleton: text output
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   138
}