src/TabularPrefetchingHandler.h
branchv_0
changeset 38 2cc2d3f658f4
parent 37 5dcff3c35462
child 39 f33464965693
equal deleted inserted replaced
37:5dcff3c35462 38:2cc2d3f658f4
    26 
    26 
    27 #include <relpipe/reader/typedefs.h>
    27 #include <relpipe/reader/typedefs.h>
    28 #include <relpipe/reader/TypeId.h>
    28 #include <relpipe/reader/TypeId.h>
    29 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
    29 #include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
    30 #include <relpipe/reader/handlers/AttributeMetadata.h>
    30 #include <relpipe/reader/handlers/AttributeMetadata.h>
       
    31 #include <relpipe/reader/RelpipeReaderException.h>
    31 
    32 
    32 #include "Configuration.h"
    33 #include "Configuration.h"
    33 
    34 
    34 namespace relpipe {
    35 namespace relpipe {
    35 namespace out {
    36 namespace out {
   136 			}
   137 			}
   137 		}
   138 		}
   138 		return width;
   139 		return width;
   139 	}
   140 	}
   140 
   141 
   141 	void printHorizontalLine(const string_t &left, const string_t &middle, const string_t &right) {
   142 	void printHorizontalLine(const string_t& left, const string_t& middle, const string_t& right, const string_t& bar) {
   142 		const string_t bar = L"─";
       
   143 		// TODO: support also ASCII nostalgia:
       
   144 		// border = border.replaceAll("─", "-");
       
   145 		// border = border.replaceAll("│", "|");
       
   146 		// border = border.replaceAll("[╭┬╮├┼┤╰┴╯]", "+");
       
   147 
       
   148 		output << INDENT << ESC_BORDER;
   143 		output << INDENT << ESC_BORDER;
   149 		output << convertor.to_bytes(left);
   144 		output << convertor.to_bytes(left);
   150 		for (size_t c = 0; c < columnCount; c++) {
   145 		for (size_t c = 0; c < columnCount; c++) {
   151 			integer_t width = columnWidths[c];
   146 			integer_t width = columnWidths[c];
   152 			for (integer_t w = 0; w < (width + 2); w++) output << convertor.to_bytes(bar); // 2 = left and right padding of the value
   147 			for (integer_t w = 0; w < (width + 2); w++) output << convertor.to_bytes(bar); // 2 = left and right padding of the value
   166 			integer_t minWidth = columnName.size() + (getConfiguration(writeTypes) ? typeCode.size() + 3 : 0); // 3 = " ()" in "columnName (typeCode)"
   161 			integer_t minWidth = columnName.size() + (getConfiguration(writeTypes) ? typeCode.size() + 3 : 0); // 3 = " ()" in "columnName (typeCode)"
   167 			columnWidths[i] = max(columnWidths[i], minWidth);
   162 			columnWidths[i] = max(columnWidths[i], minWidth);
   168 			paddings[i] = columnWidths[i] - minWidth;
   163 			paddings[i] = columnWidths[i] - minWidth;
   169 		}
   164 		}
   170 
   165 
   171 		printHorizontalLine(L"╭", L"┬", L"╮");
   166 		if (configuration.tableStyle == Configuration::TableStyle::Rounded) printHorizontalLine(L"╭", L"┬", L"╮", L"─");
       
   167 		else if (configuration.tableStyle == Configuration::TableStyle::Sharp) printHorizontalLine(L"┌", L"┬", L"┐", L"─");
       
   168 		else if (configuration.tableStyle == Configuration::TableStyle::SharpDouble) printHorizontalLine(L"╔", L"╦", L"╗", L"═");
       
   169 		else if (configuration.tableStyle == Configuration::TableStyle::HorizontalOnly) printHorizontalLine(L"─", L"─", L"─", L"─");
       
   170 		else if (configuration.tableStyle == Configuration::TableStyle::Ascii) printHorizontalLine(L"+", L"+", L"+", L"-");
       
   171 		else throw RelpipeReaderException(L"Unsupported TableStyle: " + std::to_wstring((int) configuration.tableStyle));
       
   172 
       
   173 		std::string verticalSeparator;
       
   174 		if (configuration.tableStyle == Configuration::TableStyle::Rounded) verticalSeparator = "│";
       
   175 		else if (configuration.tableStyle == Configuration::TableStyle::Sharp) verticalSeparator = "│";
       
   176 		else if (configuration.tableStyle == Configuration::TableStyle::SharpDouble) verticalSeparator = "║";
       
   177 		else if (configuration.tableStyle == Configuration::TableStyle::HorizontalOnly) verticalSeparator = " ";
       
   178 		else if (configuration.tableStyle == Configuration::TableStyle::Ascii) verticalSeparator = "|";
       
   179 		else throw RelpipeReaderException(L"Unsupported TableStyle: " + std::to_wstring((int) configuration.tableStyle));
   172 
   180 
   173 		// Print column headers:
   181 		// Print column headers:
   174 		output << INDENT << ESC_BORDER << "│" << ESC_RESET;
   182 		output << INDENT << ESC_BORDER << verticalSeparator << ESC_RESET;
   175 		for (size_t i = 0; i < columnCount; i++) {
   183 		for (size_t i = 0; i < columnCount; i++) {
   176 			output << " " << convertor.to_bytes(formatValue(columnNames[i], ESC_HEADER));
   184 			output << " " << convertor.to_bytes(formatValue(columnNames[i], ESC_HEADER));
   177 			for (integer_t p = 0; p < paddings[i]; p++) {
   185 			for (integer_t p = 0; p < paddings[i]; p++) {
   178 				output << " ";
   186 				output << " ";
   179 			}
   187 			}
   180 			if (getConfiguration(writeTypes)) output << " (" << convertor.to_bytes(columnTypeCodes[i]) << ")";
   188 			if (getConfiguration(writeTypes)) output << " (" << convertor.to_bytes(columnTypeCodes[i]) << ")";
   181 			output << ESC_BORDER << " │" << ESC_RESET;
   189 			output << ESC_BORDER << " " << verticalSeparator << ESC_RESET;
   182 		}
   190 		}
   183 		output << std::endl;
   191 		output << std::endl;
   184 		printHorizontalLine(L"├", L"┼", L"┤");
   192 		if (configuration.tableStyle == Configuration::TableStyle::Rounded) printHorizontalLine(L"├", L"┼", L"┤", L"─");
       
   193 		else if (configuration.tableStyle == Configuration::TableStyle::Sharp) printHorizontalLine(L"├", L"┼", L"┤", L"─");
       
   194 		else if (configuration.tableStyle == Configuration::TableStyle::SharpDouble) printHorizontalLine(L"╠", L"╬", L"╣", L"═");
       
   195 		else if (configuration.tableStyle == Configuration::TableStyle::HorizontalOnly) printHorizontalLine(L"─", L"─", L"─", L"─");
       
   196 		else if (configuration.tableStyle == Configuration::TableStyle::Ascii) printHorizontalLine(L"+", L"+", L"+", L"-");
       
   197 		else throw RelpipeReaderException(L"Unsupported TableStyle: " + std::to_wstring((int) configuration.tableStyle));
   185 
   198 
   186 		// Print particular rows:
   199 		// Print particular rows:
   187 		for (size_t i = 0; i < values.size(); i++) {
   200 		for (size_t i = 0; i < values.size(); i++) {
   188 			integer_t columnIndex = i % columnCount;
   201 			integer_t columnIndex = i % columnCount;
   189 			if (columnIndex == 0) output << INDENT << ESC_BORDER << "│" << ESC_RESET;
   202 			if (columnIndex == 0) output << INDENT << ESC_BORDER << verticalSeparator << ESC_RESET;
   190 			string_t stringValue = values[i];
   203 			string_t stringValue = values[i];
   191 			integer_t padding = columnWidths[columnIndex] - computeWidth(stringValue);
   204 			integer_t padding = columnWidths[columnIndex] - computeWidth(stringValue);
   192 			boolean_t alignRight = columnTypes[columnIndex] == TypeId::BOOLEAN || columnTypes[columnIndex] == TypeId::INTEGER;
   205 			boolean_t alignRight = columnTypes[columnIndex] == TypeId::BOOLEAN || columnTypes[columnIndex] == TypeId::INTEGER;
   193 
   206 
   194 			if (alignRight) for (integer_t p = 0; p < padding; p++) output << " ";
   207 			if (alignRight) for (integer_t p = 0; p < padding; p++) output << " ";
   195 			output << " " << convertor.to_bytes(formatValue(stringValue, ESC_VALUE));
   208 			output << " " << convertor.to_bytes(formatValue(stringValue, ESC_VALUE));
   196 			if (!alignRight) for (integer_t p = 0; p < padding; p++) output << " ";
   209 			if (!alignRight) for (integer_t p = 0; p < padding; p++) output << " ";
   197 
   210 
   198 			output << ESC_BORDER << " │" << ESC_RESET;
   211 			output << ESC_BORDER << " " << verticalSeparator << ESC_RESET;
   199 			if (columnIndex == (columnCount - 1)) output << std::endl;
   212 			if (columnIndex == (columnCount - 1)) output << std::endl;
   200 		}
   213 		}
   201 		printHorizontalLine(L"╰", L"┴", L"╯");
   214 		if (configuration.tableStyle == Configuration::TableStyle::Rounded) printHorizontalLine(L"╰", L"┴", L"╯", L"─");
       
   215 		else if (configuration.tableStyle == Configuration::TableStyle::Sharp) printHorizontalLine(L"└", L"┴", L"┘", L"─");
       
   216 		else if (configuration.tableStyle == Configuration::TableStyle::SharpDouble) printHorizontalLine(L"╚", L"╩", L"╝", L"═");
       
   217 		else if (configuration.tableStyle == Configuration::TableStyle::HorizontalOnly) printHorizontalLine(L"─", L"─", L"─", L"─");
       
   218 		else if (configuration.tableStyle == Configuration::TableStyle::Ascii) printHorizontalLine(L"+", L"+", L"+", L"-");
       
   219 		else throw RelpipeReaderException(L"Unsupported TableStyle: " + std::to_wstring((int) configuration.tableStyle));
   202 
   220 
   203 		if (getConfiguration(writeRecordCount)) {
   221 		if (getConfiguration(writeRecordCount)) {
   204 			integer_t recordCount = values.size() / columnCount;
   222 			integer_t recordCount = values.size() / columnCount;
   205 			output << ESC_YELLOW << "Record count: " << ESC_RESET << recordCount << std::endl;
   223 			output << ESC_YELLOW << "Record count: " << ESC_RESET << recordCount << std::endl;
   206 		}
   224 		}