src/relpipe-out-sql.cpp
author František Kučera <franta-hg@frantovo.cz>
Tue, 13 Dec 2022 02:07:00 +0100
branchv_0
changeset 3 202ce847990c
parent 0 7727b4d5560d
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
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
#include <cstdlib>
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
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <relpipe/cli/CLI.h>
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/cli/RelpipeCLIException.h>
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
#include <relpipe/reader/Factory.h>
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include <relpipe/reader/RelationalReader.h>
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
#include <relpipe/reader/RelpipeReaderException.h>
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
#include "Configuration.h"
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
#include "CLIParser.h"
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
#include "SQLHandler.h"
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
#include "RelpipeSQLWriterException.h"
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
using namespace relpipe::cli;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
using namespace relpipe::reader;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
using namespace relpipe::out::sql;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
int main(int argc, char**argv) {
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
	CLI cli(argc, argv);
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	CLI::untieStdIO();
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
	
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    41
	int resultCode = CLI::EXIT_CODE_UNEXPECTED_ERROR;
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
	try {
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    44
		CLIParser cliParser;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		Configuration configuration = cliParser.parse(cli.arguments());
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
		std::shared_ptr<RelationalReader> reader(Factory::create(std::cin));
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
		SQLHandler handler(std::cout, configuration);
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
		reader->addHandler(&handler);
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
		reader->process();
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    51
		resultCode = CLI::EXIT_CODE_SUCCESS;
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
	} catch (RelpipeCLIException e) {
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    54
		fwprintf(stderr, L"Caught CLI exception: %ls\n", e.getMessage().c_str());
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    55
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    56
		resultCode = e.getExitCode();
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
	} catch (RelpipeReaderException e) {
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
		fwprintf(stderr, L"Caught Reader exception: %ls\n", e.getMessage().c_str());
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		fwprintf(stderr, L"Debug: Input stream: eof=%ls, lastRead=%d\n", (cin.eof() ? L"true" : L"false"), cin.gcount());
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    60
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
	} catch (RelpipeSQLWriterException e) {
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		fwprintf(stderr, L"Error while generating SQL: %ls\n", e.getMessage().c_str());
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
		resultCode = CLI::EXIT_CODE_DATA_ERROR;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
	}
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    65
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    66
	return resultCode;
7727b4d5560d new module: relpipe-out-sql
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    67
}