src/TabularPrefetchingHandler.h
author František Kučera <franta-hg@frantovo.cz>
Thu, 29 Nov 2018 22:30:12 +0100
branchv_0
changeset 9 86d69cfbacc9
parent 8 3103333af024
child 13 3435efcaa8ca
permissions -rw-r--r--
license: GNU GPLv3+
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     1
/**
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     2
 * Relational pipes
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     3
 * Copyright © 2018 František Kučera (Frantovo.cz, GlobalCode.info)
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     4
 *
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     7
 * the Free Software Foundation, either version 3 of the License, or
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     8
 * (at your option) any later version.
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
     9
 *
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    13
 * GNU General Public License for more details.
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    14
 *
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    15
 * You should have received a copy of the GNU General Public License
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
86d69cfbacc9 license: GNU GPLv3+
František Kučera <franta-hg@frantovo.cz>
parents: 8
diff changeset
    17
 */
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#pragma once
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <codecvt>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <regex>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/typedefs.h>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/TypeId.h>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
6
fc1e746e26a5 use AttributeMetadata instead of std::pair for relation header
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
    31
#include <relpipe/reader/handlers/AttributeMetadata.h>
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
namespace relpipe {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
namespace out {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
namespace tabular {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
using namespace relpipe::reader;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
class TabularPrefetchingHandler : public handlers::RelationalReaderStringHadler {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
private:
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings.
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	const char* ESC_BRIGHT = "\u001b[1m";
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	const char* ESC_RED = "\u001b[31m";
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	const char* ESC_GREEN = "\u001b[32m";
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	const char* ESC_YELLOW = "\u001b[33m";
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	const char* ESC_CYAN = "\u001b[36m";
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	const char* ESC_RESET = "\u001b[0m";
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	const char* ESC_HEADER = ESC_BRIGHT;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	const char* ESC_BORDER = ESC_GREEN;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
	const char* ESC_VALUE = ESC_CYAN;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	const char* ESC_REPLACEMENT = ESC_RED;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	const char* INDENT = " "; // table indent from the left
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	std::ostream &output;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	std::vector<TypeId> columnTypes;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	std::vector<string_t> columnTypeCodes;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	std::vector<string_t> columnNames;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	std::vector<integer_t> columnWidths;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	std::vector<string_t> values; // all values are saved here and processed at the end of the relation
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
	integer_t columnCount = 0;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
	const string_t colorizeReplacement(const string_t &replacement, const char* valueColor) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		return convertor.from_bytes(ESC_RESET) + convertor.from_bytes(ESC_REPLACEMENT) + replacement + convertor.from_bytes(ESC_RESET) + convertor.from_bytes(valueColor);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    69
	/**
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
	 * Sanitizes whitespace that could broke table layout.
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    71
	 * 
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
	 * TODO: sanitize also escape sequences and emoji (resp. properly measure their width)
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    73
	 * 
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
	 * @param value original value
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    75
	 * @param color value foreground color
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
	 * @return value with replaced whitespaces
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    77
	 */
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    78
	const string_t formatValue(const string_t &value, const char* color) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
		std::wstringstream result;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    81
		result << convertor.from_bytes(color);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
		for (auto & ch : value) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
			switch (ch) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
				case L'\n': result << colorizeReplacement(L"↲", color);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    86
					break;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
				case L'\r': result << colorizeReplacement(L"⏎", color);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
					break;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
				case L'\t': result << colorizeReplacement(L"↹", color);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
					break;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
				case L' ': result << colorizeReplacement(L"⎵", color);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
					break;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
				default: result << ch;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
			}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
		}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
		result << convertor.from_bytes(ESC_RESET);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
		return result.str();
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   102
	void printHorizontalLine(const string_t &left, const string_t &middle, const string_t &right) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
		const string_t bar = L"─";
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
		// TODO: support also ASCII nostalgia:
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
		// border = border.replaceAll("─", "-");
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
		// border = border.replaceAll("│", "|");
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
		// border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
		output << INDENT << ESC_BORDER;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
		output << convertor.to_bytes(left);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   111
		for (size_t c = 0; c < columnCount; c++) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
			integer_t width = columnWidths[c];
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
			for (integer_t w = 0; w < (width + 2); w++) output << convertor.to_bytes(bar); // 2 = left and right padding of the value
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
			if (c < (columnCount - 1)) output << convertor.to_bytes(middle);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
		}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
		output << convertor.to_bytes(right);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
		output << ESC_RESET << std::endl;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   119
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   120
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   121
	void printCachedData() {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   122
		// Compute column widths and paddings:
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
		vector<integer_t> paddings(columnCount);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
		for (size_t i = 0; i < columnCount; i++) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   125
			string_t typeCode = columnTypeCodes[i];
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
			string_t columnName = columnNames[i];
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
			integer_t minWidth = columnName.size() + typeCode.size() + 3; // 3 = " ()" in "columnName (typeCode)"
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
			columnWidths[i] = max(columnWidths[i], minWidth);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
			paddings[i] = columnWidths[i] - minWidth;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
		}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   131
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   132
		printHorizontalLine(L"╭", L"┬", L"╮");
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   133
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   134
		// Print column headers:
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   135
		output << INDENT << ESC_BORDER << "│" << ESC_RESET;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   136
		for (size_t i = 0; i < columnCount; i++) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   137
			output << " " << convertor.to_bytes(formatValue(columnNames[i], ESC_HEADER));
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   138
			for (integer_t p = 0; p < paddings[i]; p++) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   139
				output << " ";
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   140
			}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   141
			output << " (" << convertor.to_bytes(columnTypeCodes[i]) << ")";
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   142
			output << ESC_BORDER << " │" << ESC_RESET;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   143
		}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   144
		output << std::endl;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   145
		printHorizontalLine(L"├", L"┼", L"┤");
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   146
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   147
		// Print particular rows:
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   148
		for (size_t i = 0; i < values.size(); i++) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   149
			integer_t columnIndex = i % columnCount;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   150
			if (columnIndex == 0) output << INDENT << ESC_BORDER << "│" << ESC_RESET;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   151
			string_t stringValue = values[i];
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   152
			integer_t padding = columnWidths[columnIndex] - stringValue.size();
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   153
			boolean_t alignRight = columnTypes[columnIndex] == TypeId::BOOLEAN || columnTypes[columnIndex] == TypeId::INTEGER;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   154
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   155
			if (alignRight) for (integer_t p = 0; p < padding; p++) output << " ";
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   156
			output << " " << convertor.to_bytes(formatValue(stringValue, ESC_VALUE));
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   157
			if (!alignRight) for (integer_t p = 0; p < padding; p++) output << " ";
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   158
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   159
			output << ESC_BORDER << " │" << ESC_RESET;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   160
			if (columnIndex == (columnCount - 1)) output << std::endl;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   161
		}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   162
		printHorizontalLine(L"╰", L"┴", L"╯");
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   163
		integer_t recordCount = values.size() / columnCount;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   164
		output << ESC_YELLOW << "Record count: " << ESC_RESET << recordCount << std::endl;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   165
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   166
		values.clear();
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   167
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   168
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   169
public:
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   170
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   171
	TabularPrefetchingHandler(std::ostream& output) : output(output) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   172
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   173
6
fc1e746e26a5 use AttributeMetadata instead of std::pair for relation header
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   174
	void startRelation(string_t name, std::vector<handlers::AttributeMetadata> attributes) override {
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   175
		if (columnCount) printCachedData();
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   177
		output << ESC_RED << convertor.to_bytes(name) << ":" << ESC_RESET << endl;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   178
		columnCount = attributes.size();
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   179
		columnTypes.resize(columnCount);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   180
		columnTypeCodes.resize(columnCount);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   181
		columnNames.resize(columnCount);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   182
		columnWidths.resize(columnCount, 0);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   183
		for (int i = 0; i < attributes.size(); i++) {
7
2057919937fa AttributeMetadata: pImpl / d-pointer version but with memory-leak and unwanted empty constructor
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   184
			columnNames[i] = attributes[i].getAttributeName();
2057919937fa AttributeMetadata: pImpl / d-pointer version but with memory-leak and unwanted empty constructor
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   185
			columnTypes[i] = attributes[i].getTypeId();
2057919937fa AttributeMetadata: pImpl / d-pointer version but with memory-leak and unwanted empty constructor
František Kučera <franta-hg@frantovo.cz>
parents: 6
diff changeset
   186
			columnTypeCodes[i] = attributes[i].getTypeName();
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   187
		}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   188
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   189
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   190
	void attribute(const string_t& value) override {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   191
		integer_t i = values.size() % columnCount;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   192
		values.push_back(value);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   193
		columnWidths[i] = max(columnWidths[i], value.length());
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   194
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   195
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   196
	void endOfPipe() {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   197
		if (columnCount) printCachedData();
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   198
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   199
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   200
};
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   201
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   202
}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   203
}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   204
}