src/SQLHandler.h
author František Kučera <franta-hg@frantovo.cz>
Tue, 13 Dec 2022 02:07:00 +0100
branchv_0
changeset 3 202ce847990c
parent 2 8a30971d285f
permissions -rw-r--r--
configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
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;
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    51
	string_t currentRelation;
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
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    73
	static void writeType(std::ostream& output, std::string type) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    74
		if (std::regex_match(type, std::regex("[a-z0-9]+(\\([0-9]+(,\\s*[0-9]+)*\\))?"))) output << type;
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    75
		else writeIdentifier(output, type);
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    76
	}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    77
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    78
	static void writeValue(std::ostream& output, std::string value) {
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
		for (auto & ch : value) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    81
			if (ch == '\'') output << "''";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    82
			else output << ch;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    83
		}
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    84
		output << '\'';
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
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    87
	void writeRecordCount() {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    88
		// 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
    89
		// output << "-- Record count: " << recordCount << std::endl;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    90
	}
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    91
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    92
	void endRelation() {
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    93
		if (getWriteDML()) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    94
			if (getInsertMode() == Configuration::InsertMode::MULTI) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    95
				output << std::endl << ";" << std::endl;
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    96
			}
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    97
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    98
			writeRecordCount();
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    99
		}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   100
	}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   101
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   102
	// TODO: code deduplication
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   103
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   104
	Configuration::InsertMode getInsertMode() {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   105
		for (const Configuration::RelationConfiguration& rc : configuration.relationConfigurations) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   106
			if (std::regex_match(currentRelation, rc.relationPattern)) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   107
				if (rc.insertMode != Configuration::InsertMode::DEFAULT) return rc.insertMode;
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   108
			}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   109
		}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   110
		return Configuration::InsertMode::MULTI;
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   111
	}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   112
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   113
	// TODO: code deduplication
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   114
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   115
	bool getWriteDDL() {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   116
		for (const Configuration::RelationConfiguration& rc : configuration.relationConfigurations) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   117
			if (std::regex_match(currentRelation, rc.relationPattern)) return rc.writeDDL;
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   118
		}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   119
		return true;
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   120
	}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   121
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   122
	// TODO: code deduplication
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   123
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   124
	bool getWriteDML() {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   125
		for (const Configuration::RelationConfiguration& rc : configuration.relationConfigurations) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   126
			if (std::regex_match(currentRelation, rc.relationPattern)) return rc.writeDML;
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   127
		}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   128
		return true;
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   129
	}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   130
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   131
	// TODO: code deduplication
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   132
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   133
	bool getWriteColumnNames() {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   134
		for (const Configuration::RelationConfiguration& rc : configuration.relationConfigurations) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   135
			if (std::regex_match(currentRelation, rc.relationPattern)) return rc.writeColumnNames;
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   136
		}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   137
		return true;
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   138
	}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   139
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   140
	string_t findSQLType(const AttributeMetadata attribute) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   141
		// Type casts provided by the user:
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   142
		for (const Configuration::RelationConfiguration& rc : configuration.relationConfigurations) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   143
			if (std::regex_match(currentRelation, rc.relationPattern)) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   144
				for (const Configuration::TypeCastRule tcr : rc.typeCastRules) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   145
					if (std::regex_match(attribute.getAttributeName(), tcr.attribute) && std::regex_match(attribute.getTypeName(), tcr.type)) return tcr.sqlType;
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   146
				}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   147
			}
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   148
		}
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   149
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   150
		// Default mapping:
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   151
		if (attribute.getTypeId() == reader::TypeId::BOOLEAN) return L"integer";
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   152
		else if (attribute.getTypeId() == reader::TypeId::INTEGER) return L"bigint";
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   153
		else if (attribute.getTypeId() == reader::TypeId::STRING) return L"text";
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   154
		else throw RelpipeSQLWriterException(L"Type not yet supported in the SQL output: " + attribute.getTypeName());
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   155
	}
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   156
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   157
public:
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   158
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   159
	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
   160
	}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   161
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   162
	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
   163
		// 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
   164
		// TODO: optional transformation to upper/lower case
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   165
		// TODO: custom primary key or other column properties
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   166
		// TODO: custom table properties
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   167
		// 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
   168
		// 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
   169
		// 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
   170
		// TODO: optional wrapping at certain width (like 80 characters)?
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   171
		// TODO: optional syntax highlighting?
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   172
		// 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
   173
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   174
		if (currentRelation.size()) {
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   175
			endRelation();
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   176
			if (getWriteDDL() || getWriteDML()) output << std::endl;
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   177
		}
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   178
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   179
		currentRelation = name;
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   180
		currentAttributes = attributes;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   181
		recordCount = 0;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   182
		valueCount = 0;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   183
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   184
		if (getWriteDDL()) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   185
			output << "CREATE TABLE ";
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   186
			writeIdentifier(output, convertor.to_bytes(currentRelation));
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   187
			output << " (" << std::endl;
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   188
			for (size_t i = 0, limit = attributes.size(); i < limit; i++) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   189
				auto attribute = attributes[i];
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   190
				output << "\t";
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   191
				writeIdentifier(output, convertor.to_bytes(attribute.getAttributeName()));
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   192
				// TODO: implement RelationalReaderValueHandler
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   193
				output << " ";
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   194
				writeType(output, convertor.to_bytes(findSQLType(attribute)));
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   195
				if (i < (limit - 1)) output << ",";
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   196
				output << std::endl;
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   197
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   198
			}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   199
			output << ");" << std::endl;
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   200
			if (getWriteDML()) output << std::endl;
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   201
		}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   202
	}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   203
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   204
	void attribute(const string_t& value) override {
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   205
		if (getWriteDML() == false) return;
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   206
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   207
		if (valueCount % currentAttributes.size() == 0) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   208
			// TODO: optional use of function/procedure instead of INSERT
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   209
			// TODO: custom line-ends + indentation
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   210
			recordCount++;
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   211
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   212
			auto insertMode = getInsertMode();
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   213
			if (insertMode == Configuration::InsertMode::SINGLE) {
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   214
				output << "INSERT INTO ";
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   215
				writeIdentifier(output, convertor.to_bytes(currentRelation));
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   216
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   217
				if (getWriteColumnNames()) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   218
					output << " (";
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   219
					for (size_t i = 0, limit = currentAttributes.size(); i < limit; i++) {
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   220
						writeIdentifier(output, convertor.to_bytes(currentAttributes[i].getAttributeName()));
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   221
						if (i < (limit - 1)) output << ", ";
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   222
					}
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   223
					output << ")";
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   224
				}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   225
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   226
				output << " VALUES (";
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   227
			} else if (insertMode == Configuration::InsertMode::MULTI) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   228
				if (recordCount == 1) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   229
					output << "INSERT INTO ";
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   230
					writeIdentifier(output, convertor.to_bytes(currentRelation));
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   231
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   232
					if (getWriteColumnNames()) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   233
						output << "\n\t(";
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   234
						for (size_t i = 0, limit = currentAttributes.size(); i < limit; i++) {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   235
							writeIdentifier(output, convertor.to_bytes(currentAttributes[i].getAttributeName()));
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   236
							if (i < (limit - 1)) output << ", ";
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   237
						}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   238
						output << ")" << std::endl;
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   239
					} else {
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   240
						output << " ";
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   241
					}
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   242
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   243
					output << "VALUES" << std::endl;
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   244
				} else {
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   245
					output << "," << std::endl;
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   246
				}
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   247
				output << "\t(";
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   248
			} else {
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   249
				throw RelpipeSQLWriterException(L"Unsupported InsertMode: " + std::to_wstring((int) insertMode));
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   250
			}
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   251
		}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   252
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   253
		valueCount++;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   254
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   255
		if (value.size() > 0) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   256
			// TODO: support all data types + implement RelationalReaderValueHandler
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   257
			writeValue(output, convertor.to_bytes(value));
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   258
		} else {
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   259
			// 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
   260
			output << "NULL";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   261
		}
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   262
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   263
		if (valueCount % currentAttributes.size()) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   264
			output << ", ";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   265
		} else {
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   266
			auto insertMode = getInsertMode();
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   267
			if (insertMode == Configuration::InsertMode::SINGLE) {
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   268
				output << ");" << std::endl;
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   269
			} else if (insertMode == Configuration::InsertMode::MULTI) {
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   270
				output << ")";
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   271
			} else {
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   272
				throw RelpipeSQLWriterException(L"Unsupported InsertMode: " + std::to_wstring((int) insertMode));
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   273
			}
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   274
			valueCount = 0;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   275
		}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   276
	}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   277
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   278
	void endOfPipe() {
3
202ce847990c configuration: --write-ddl, --write-dml, --write-column-names, --insert-mode, --type-cast
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   279
		if (currentRelation.size()) {
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   280
			endRelation();
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   281
		}
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   282
		output.flush();
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   283
	}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   284
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   285
};
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   286
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   287
}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   288
}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   289
}