src/SQLHandler.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 07 Dec 2022 01:38:50 +0100
branchv_0
changeset 1 4c0366e1b4df
parent 0 7727b4d5560d
child 2 8a30971d285f
permissions -rw-r--r--
first version
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info)
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
7727b4d5560d new module: relpipe-out-sql
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
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     7
 * the Free Software Foundation, version 3 of the License.
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <memory>
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
#include <string>
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <vector>
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <iostream>
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <sstream>
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <locale>
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <codecvt>
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
#include <relpipe/reader/typedefs.h>
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include <relpipe/reader/TypeId.h>
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include <relpipe/reader/handlers/RelationalReaderStringHandler.h>
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include <relpipe/reader/handlers/AttributeMetadata.h>
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
#include "Configuration.h"
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
#include "RelpipeSQLWriterException.h"
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
namespace relpipe {
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
namespace out {
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
namespace sql {
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
using namespace relpipe;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
using namespace relpipe::reader;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
using namespace relpipe::reader::handlers;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    42
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    43
class SQLHandler : public RelationalReaderStringHandler {
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
private:
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
	std::ostream& output;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	Configuration& configuration;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
	std::wstring_convert<std::codecvt_utf8<wchar_t>> convertor; // generate SQL always in UTF-8
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    48
	std::vector<AttributeMetadata> currentAttributes;
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
	integer_t valueCount = 0;
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    50
	integer_t recordCount = 0;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    51
	string_t currentTable;
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    52
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    53
	/**
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
	 * @param a
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
	 * @param b
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
	 * @return true if relations have same number and types of attributes (names may differ)
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	 */
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	bool matches(const std::vector<AttributeMetadata>& a, const std::vector<AttributeMetadata>& b) {
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		if (a.size() != b.size()) return false;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		for (int i = 0, limit = a.size(); i < limit; i++) if (a[i].getTypeId() != b[i].getTypeId()) return false;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
		return true;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
	}
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    63
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    64
	static void writeIdentifier(std::ostream& output, std::string identifier) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    65
		output << '"';
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
		for (auto & ch : identifier) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    67
			if (ch == '"') output << "\"\"";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    68
			else output << ch;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    69
		}
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    70
		output << '"';
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    71
	}
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    72
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    73
	static void writeValue(std::ostream& output, std::string value) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    74
		output << '\'';
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    75
		for (auto & ch : value) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    76
			if (ch == '\'') output << "''";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    77
			else output << ch;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    78
		}
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    79
		output << '\'';
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    80
	}
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    81
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    82
	void writeRecordCount() {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    83
		// currently disabled due to relpipe-in-sql parser issues with last comment without any following expression
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    84
		// output << "-- Record count: " << recordCount << std::endl;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    85
	}
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    86
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    87
public:
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    88
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    89
	SQLHandler(std::ostream& output, Configuration& configuration) : output(output), configuration(configuration) {
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
	}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    92
	void startRelation(string_t name, std::vector<AttributeMetadata> attributes) override {
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
		// TODO: ALTER TABLE / add columns on duplicate relation name
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    94
		// TODO: optionally omit CREATE/ALTER table (just INSERT)
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    95
		// TODO: optional transformation to upper/lower case
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
		// TODO: custom data type mapping
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    97
		// TODO: custom primary key or other column properties
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    98
		// TODO: custom table properties
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
		// TODO: custom SQL script before/after stream/relation/record
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   100
		// TODO: comments and/or custom comments + record count of each table as a comment
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
		// TODO: optional transactions: BEGIN/COMMIT/ROLLBACK for stream/relation/record
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   102
		// TODO: optional wrapping at certain width (like 80 characters)?
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   103
		// TODO: optional syntax highlighting?
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   104
		// TODO: share code/behavior with relpipe-tr-sql (but it uses parametrized statements)
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   105
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   106
		if (currentTable.size()) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   107
			writeRecordCount();
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   108
			output << std::endl;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   109
		}
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   110
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   111
		currentTable = name;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   112
		currentAttributes = attributes;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   113
		recordCount = 0;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   114
		valueCount = 0;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   115
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   116
		output << "CREATE TABLE ";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   117
		writeIdentifier(output, convertor.to_bytes(currentTable));
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   118
		output << " (" << std::endl;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   119
		for (size_t i = 0, limit = attributes.size(); i < limit; i++) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   120
			auto attribute = attributes[i];
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   121
			output << "\t";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   122
			writeIdentifier(output, convertor.to_bytes(attribute.getAttributeName()));
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   123
			// TODO: support all data types + implement RelationalReaderValueHandler
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   124
			output << " TEXT";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   125
			if (i < (limit - 1)) output << ",";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   126
			output << std::endl;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   127
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   128
		}
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   129
		output << ");" << std::endl << std::endl;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   130
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   131
		if (currentAttributes.empty()) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   132
			//if (configuration.writeHeader) for (auto attr : attributes) attribute(configuration.writeTypes ? attr.getAttributeName() + L"::" + attr.getTypeName() : attr.getAttributeName());
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   133
		} else if (matches(currentAttributes, attributes)) {
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   134
			// do UNION ALL – just append the records
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   135
		} else {
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   136
			// throw RelpipeSQLWriterException(L"To the SQL format we can convert only one relation or multiple relations that have same number of attributes of same types (relation and attribute names may differ – result is named after the first one).");
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   137
		}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   138
	}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   139
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   140
	void attribute(const string_t& value) override {
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   141
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   142
		if (valueCount % currentAttributes.size() == 0) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   143
			// TODO: optional use of function/procedure instead of INSERT
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   144
			// TODO: optional INSERT of multiple records
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   145
			// TODO: custom line-ends + indentation
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   146
			// TODO: optionally write also the column names
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   147
			recordCount++;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   148
			output << "INSERT INTO ";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   149
			writeIdentifier(output, convertor.to_bytes(currentTable));
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   150
			output << " VALUES (";
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   151
		}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   152
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   153
		valueCount++;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   154
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   155
		if (value.size() > 0) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   156
			// TODO: support all data types + implement RelationalReaderValueHandler
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   157
			writeValue(output, convertor.to_bytes(value));
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   158
		} else {
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   159
			// TODO: support actual nulls when supported in the relpipe data format + just optional conversion from empty strings to NULLs
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   160
			output << "NULL";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   161
		}
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   162
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   163
		if (valueCount % currentAttributes.size()) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   164
			output << ", ";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   165
		} else {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   166
			output << ");" << std::endl;
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   167
			valueCount = 0;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   168
		}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   169
	}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   170
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   171
	void endOfPipe() {
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   172
		if (currentTable.size()) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   173
			writeRecordCount();
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   174
		}
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   175
		output.flush();
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   176
	}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   177
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   178
};
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   179
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   180
}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   181
}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   182
}