src/SQLHandler.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 10 Dec 2022 20:41:40 +0100
branchv_0
changeset 2 8a30971d285f
parent 1 4c0366e1b4df
child 3 202ce847990c
permissions -rw-r--r--
insert multiple records at once – 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
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    87
	void endRelation() {
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    88
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    89
		if (configuration.insertMode == Configuration::InsertMode::MULTI) {
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    90
			output << std::endl << ";" << std::endl;
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    91
		}
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    92
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    93
		writeRecordCount();
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    94
	}
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
    95
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
public:
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
	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
    99
	}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   101
	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
   102
		// 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
   103
		// TODO: optionally omit CREATE/ALTER table (just INSERT)
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   104
		// 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
   105
		// TODO: custom data type mapping
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   106
		// TODO: custom primary key or other column properties
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   107
		// TODO: custom table properties
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   108
		// 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
   109
		// 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
   110
		// 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
   111
		// TODO: optional wrapping at certain width (like 80 characters)?
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   112
		// TODO: optional syntax highlighting?
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   113
		// 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
   114
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   115
		if (currentTable.size()) {
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   116
			endRelation();
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   117
			output << std::endl;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   118
		}
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   119
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   120
		currentTable = name;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   121
		currentAttributes = attributes;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   122
		recordCount = 0;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   123
		valueCount = 0;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   124
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   125
		output << "CREATE TABLE ";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   126
		writeIdentifier(output, convertor.to_bytes(currentTable));
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   127
		output << " (" << std::endl;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   128
		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
   129
			auto attribute = attributes[i];
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   130
			output << "\t";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   131
			writeIdentifier(output, convertor.to_bytes(attribute.getAttributeName()));
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   132
			// TODO: support all data types + implement RelationalReaderValueHandler
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   133
			output << " TEXT";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   134
			if (i < (limit - 1)) output << ",";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   135
			output << std::endl;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   136
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   137
		}
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   138
		output << ");" << std::endl << std::endl;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   139
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   140
		if (currentAttributes.empty()) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   141
			//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
   142
		} else if (matches(currentAttributes, attributes)) {
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   143
			// do UNION ALL – just append the records
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   144
		} else {
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   145
			// 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
   146
		}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   147
	}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   148
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   149
	void attribute(const string_t& value) override {
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   150
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   151
		if (valueCount % currentAttributes.size() == 0) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   152
			// TODO: optional use of function/procedure instead of INSERT
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   153
			// TODO: optional INSERT of multiple records
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   154
			// TODO: custom line-ends + indentation
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   155
			// TODO: optionally write also the column names
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   156
			recordCount++;
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   157
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   158
			if (configuration.insertMode == Configuration::InsertMode::SINGLE) {
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   159
				output << "INSERT INTO ";
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   160
				writeIdentifier(output, convertor.to_bytes(currentTable));
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   161
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   162
				output << " (";
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   163
				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
   164
					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
   165
					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
   166
				}
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   167
				output << ")";
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   168
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   169
				output << " VALUES (";
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   170
			} else if (configuration.insertMode == Configuration::InsertMode::MULTI) {
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   171
				if (recordCount == 1) {
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   172
					// --------
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   173
					output << "INSERT INTO ";
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   174
					writeIdentifier(output, convertor.to_bytes(currentTable));
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   175
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   176
					output << "\n\t(";
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   177
					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
   178
						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
   179
						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
   180
					}
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   181
					output << ")";
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   182
					// --------
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   183
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   184
					output << std::endl << "VALUES" << std::endl;
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   185
				} else {
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   186
					output << "," << std::endl;
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   187
				}
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   188
				output << "\t(";
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   189
			} else {
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   190
				throw RelpipeSQLWriterException(L"Unsupported InsertMode: " + std::to_wstring((int) configuration.insertMode));
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   191
			}
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   192
		}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   193
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   194
		valueCount++;
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   195
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   196
		if (value.size() > 0) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   197
			// TODO: support all data types + implement RelationalReaderValueHandler
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   198
			writeValue(output, convertor.to_bytes(value));
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   199
		} else {
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   200
			// 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
   201
			output << "NULL";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   202
		}
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   203
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   204
		if (valueCount % currentAttributes.size()) {
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   205
			output << ", ";
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   206
		} else {
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   207
			if (configuration.insertMode == Configuration::InsertMode::SINGLE) {
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   208
				output << ");" << std::endl;
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   209
			} else if (configuration.insertMode == Configuration::InsertMode::MULTI) {
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   210
				output << ")";
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   211
			} else {
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   212
				throw RelpipeSQLWriterException(L"Unsupported InsertMode: " + std::to_wstring((int) configuration.insertMode));
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   213
			}
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   214
			valueCount = 0;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   215
		}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   216
	}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   217
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   218
	void endOfPipe() {
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   219
		if (currentTable.size()) {
2
8a30971d285f insert multiple records at once – first version
František Kučera <franta-hg@frantovo.cz>
parents: 1
diff changeset
   220
			endRelation();
1
4c0366e1b4df first version
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   221
		}
0
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   222
		output.flush();
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   223
	}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   224
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   225
};
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   226
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   227
}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   228
}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   229
}