src/TabularPrefetchingHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sun, 16 Sep 2018 14:33:59 +0200
branchv_0
changeset 8 3103333af024
parent 7 2057919937fa
child 9 86d69cfbacc9
permissions -rw-r--r--
remove unused constant
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
#pragma once
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
#include <string>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
#include <vector>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
#include <iostream>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     6
#include <sstream>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
#include <locale>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
#include <codecvt>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
#include <regex>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
#include <relpipe/reader/typedefs.h>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
#include <relpipe/reader/TypeId.h>
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
#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
    14
#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
    15
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
namespace relpipe {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
namespace out {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
namespace tabular {
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
using namespace relpipe::reader;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
class TabularPrefetchingHandler : public handlers::RelationalReaderStringHadler {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
private:
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
	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
    25
	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
    26
	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
    27
	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
    28
	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
    29
	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
    30
	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
    31
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
	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
    33
	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
    34
	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
    35
	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
    36
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
	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
    38
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	std::ostream &output;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	std::vector<TypeId> columnTypes;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
	std::vector<string_t> columnTypeCodes;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
	std::vector<string_t> columnNames;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
	std::vector<integer_t> columnWidths;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	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
    46
	integer_t columnCount = 0;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
	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
    49
		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
    50
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
	/**
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	 * 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
    54
	 * 
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	 * 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
    56
	 * 
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	 * @param value original value
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	 * @param color value foreground color
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
	 * @return value with replaced whitespaces
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
	 */
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	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
    62
		std::wstringstream result;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
		result << convertor.from_bytes(color);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
		for (auto & ch : value) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
			switch (ch) {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    68
				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
    69
					break;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    70
				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
    71
					break;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    72
				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
    73
					break;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    74
				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
    75
					break;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    76
				default: result << ch;
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
		}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    79
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    80
		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
    81
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    82
		return result.str();
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    83
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    84
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    85
	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
    86
		const string_t bar = L"─";
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
		// TODO: support also ASCII nostalgia:
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
		// border = border.replaceAll("─", "-");
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
		// border = border.replaceAll("│", "|");
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
		// border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
		output << INDENT << ESC_BORDER;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
		output << convertor.to_bytes(left);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
		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
    95
			integer_t width = columnWidths[c];
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
			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
    97
			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
    98
		}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
		output << convertor.to_bytes(right);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
		output << ESC_RESET << std::endl;
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
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   103
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   104
	void printCachedData() {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
		// Compute column widths and paddings:
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   106
		vector<integer_t> paddings(columnCount);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   107
		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
   108
			string_t typeCode = columnTypeCodes[i];
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   109
			string_t columnName = columnNames[i];
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   110
			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
   111
			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
   112
			paddings[i] = columnWidths[i] - minWidth;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
		}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
		printHorizontalLine(L"╭", L"┬", L"╮");
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   116
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   117
		// Print column headers:
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   118
		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
   119
		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
   120
			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
   121
			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
   122
				output << " ";
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   123
			}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   124
			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
   125
			output << ESC_BORDER << " │" << ESC_RESET;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   126
		}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   127
		output << std::endl;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   128
		printHorizontalLine(L"├", L"┼", L"┤");
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   129
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   130
		// Print particular rows:
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   131
		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
   132
			integer_t columnIndex = i % columnCount;
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   133
			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
   134
			string_t stringValue = values[i];
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   135
			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
   136
			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
   137
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   138
			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
   139
			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
   140
			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
   141
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
			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
   144
		}
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
		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
   147
		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
   148
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   149
		values.clear();
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   150
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   151
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   152
public:
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   153
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   154
	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
   155
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   156
6
fc1e746e26a5 use AttributeMetadata instead of std::pair for relation header
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
   157
	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
   158
		if (columnCount) printCachedData();
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   159
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   160
		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
   161
		columnCount = attributes.size();
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   162
		columnTypes.resize(columnCount);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   163
		columnTypeCodes.resize(columnCount);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   164
		columnNames.resize(columnCount);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   165
		columnWidths.resize(columnCount, 0);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   166
		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
   167
			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
   168
			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
   169
			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
   170
		}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   171
	}
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
	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
   174
		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
   175
		values.push_back(value);
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
		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
   177
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   178
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   179
	void endOfPipe() {
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   180
		if (columnCount) printCachedData();
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   181
	}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   182
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   183
};
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   184
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   185
}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   186
}
911ec74cce33 tabular handler, based on the prototype code
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   187
}