src/CLIParser.h
author František Kučera <franta-hg@frantovo.cz>
Sat, 14 Dec 2019 14:00:36 +0100
branchv_0
changeset 24 884ece10575d
parent 17 b3c07fb178be
child 34 24c05e69d68f
permissions -rw-r--r--
add --type-cast to allow explicit specification of type for given output attributes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     1
/**
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     2
 * Relational pipes
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     3
 * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     4
 *
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     5
 * This program is free software: you can redistribute it and/or modify
c205f5d06418 project skeleton
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
10
7da7173d84b0 fix license version: GNU GPLv3
František Kučera <franta-hg@frantovo.cz>
parents: 5
diff changeset
     7
 * the Free Software Foundation, version 3.
0
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     8
 *
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    12
 * GNU General Public License for more details.
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    13
 *
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    16
 */
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    17
#pragma once
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    18
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    19
#include <vector>
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    20
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    21
#include <relpipe/writer/typedefs.h>
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    22
#include <relpipe/cli/CLI.h>
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    23
#include <relpipe/cli/RelpipeCLIException.h>
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    24
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    25
#include "Configuration.h"
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    26
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    27
namespace relpipe {
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    28
namespace tr {
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    29
namespace sql {
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    30
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    31
class CLIParser {
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    32
private:
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    33
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    34
	// FIXME: move common methods/classes to relpipe-lib-cli or relpipe-lib-helper
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    35
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    36
	string_t readNext(std::vector<string_t> arguments, int& i) {
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    37
		if (i < arguments.size()) return arguments[i++];
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    38
		else throw relpipe::cli::RelpipeCLIException(L"Missing CLI argument" + (i > 0 ? (L" after " + arguments[i - 1]) : L""), relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    39
	}
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    40
2
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    41
	void addQuery(Configuration& c, Statement& currentQuery) {
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    42
		if (currentQuery.sql.size()) {
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    43
			c.statements.push_back(currentQuery);
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    44
			currentQuery = Statement();
0
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    45
		}
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    46
	}
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    47
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    48
public:
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    49
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    50
	static const string_t OPTION_RELATION;
24
884ece10575d add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    51
	static const string_t OPTION_TYPE_CAST;
2
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    52
	static const string_t OPTION_PARAMETER;
16
3c51a2c32c86 rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    53
	static const string_t OPTION_COPY;
3c51a2c32c86 rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    54
	static const string_t OPTION_COPY_RENAMED;
5
cbc7817a3346 add option to create DB in a file and to keep that file
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    55
	static const string_t OPTION_FILE;
17
b3c07fb178be rename --keep-file to --file-keep
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    56
	static const string_t OPTION_FILE_KEEP;
0
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    57
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    58
	Configuration parse(const std::vector<string_t>& arguments) {
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    59
		Configuration c;
2
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    60
		Statement currentQuery;
0
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    61
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    62
		for (int i = 0; i < arguments.size();) {
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    63
			string_t option = readNext(arguments, i);
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    64
2
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    65
			if (option == OPTION_RELATION) {
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    66
				addQuery(c, currentQuery); // previous relation
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    67
				currentQuery.relation = readNext(arguments, i);
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    68
				currentQuery.sql = readNext(arguments, i);
24
884ece10575d add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    69
			} else if (option == OPTION_TYPE_CAST) {
884ece10575d add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    70
				TypeCast typeCast;
884ece10575d add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    71
				typeCast.name = readNext(arguments, i);
884ece10575d add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    72
				typeCast.type = readNext(arguments, i);
884ece10575d add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
    73
				currentQuery.typeCasts.push_back(typeCast);
2
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    74
			} else if (option == OPTION_PARAMETER) {
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    75
				Parameter parameter;
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    76
				parameter.value = readNext(arguments, i);
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    77
				currentQuery.parameters.push_back(parameter);
16
3c51a2c32c86 rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    78
			} else if (option == OPTION_COPY) {
3c51a2c32c86 rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    79
				c.copyRelations.push_back({readNext(arguments, i), L"", false});
3c51a2c32c86 rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    80
			} else if (option == OPTION_COPY_RENAMED) {
3c51a2c32c86 rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
    81
				c.copyRelations.push_back({readNext(arguments, i), readNext(arguments, i), true});
5
cbc7817a3346 add option to create DB in a file and to keep that file
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    82
			} else if (option == OPTION_FILE) {
cbc7817a3346 add option to create DB in a file and to keep that file
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
    83
				c.file = readNext(arguments, i);
17
b3c07fb178be rename --keep-file to --file-keep
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
    84
			} else if (option == OPTION_FILE_KEEP) {
12
0b38339b871b improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    85
				string_t value = readNext(arguments, i);
0b38339b871b improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    86
				if (value == L"auto") c.keepFile = KeepFile::Automatic;
0b38339b871b improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    87
				else if (value == L"true") c.keepFile = KeepFile::Always;
0b38339b871b improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    88
				else if (value == L"false") c.keepFile = KeepFile::Never;
0b38339b871b improve --keep-file option: default is auto = file will be kept, if it was present before the transformation
František Kučera <franta-hg@frantovo.cz>
parents: 10
diff changeset
    89
				else throw relpipe::cli::RelpipeCLIException(L"Unsupported keep-file value: " + value + L" Expecting: true, false, auto", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
0
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    90
			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    91
		}
2
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
    92
		addQuery(c, currentQuery); // last relation
0
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    93
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    94
		return c;
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    95
	}
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    96
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    97
	virtual ~CLIParser() {
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    98
	}
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
    99
};
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   100
2
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   101
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   102
// relpipe-tr-sql --relation "tabulka" "SELECT * FROM fstab WHERE id = ?" --parameter "123"
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   103
// TODO: --type string/integer/boolean (default is string)
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   104
0
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   105
const string_t CLIParser::OPTION_RELATION = L"--relation";
24
884ece10575d add --type-cast to allow explicit specification of type for given output attributes
František Kučera <franta-hg@frantovo.cz>
parents: 17
diff changeset
   106
const string_t CLIParser::OPTION_TYPE_CAST = L"--type-cast";
2
362f2689cb87 configuration and CLI parser
František Kučera <franta-hg@frantovo.cz>
parents: 0
diff changeset
   107
const string_t CLIParser::OPTION_PARAMETER = L"--parameter";
16
3c51a2c32c86 rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   108
const string_t CLIParser::OPTION_COPY = L"--copy";
3c51a2c32c86 rename --dump to --copy + add option --copy-renamed + allow multiple copy/copy-renamed commands
František Kučera <franta-hg@frantovo.cz>
parents: 14
diff changeset
   109
const string_t CLIParser::OPTION_COPY_RENAMED = L"--copy-renamed";
5
cbc7817a3346 add option to create DB in a file and to keep that file
František Kučera <franta-hg@frantovo.cz>
parents: 2
diff changeset
   110
const string_t CLIParser::OPTION_FILE = L"--file";
17
b3c07fb178be rename --keep-file to --file-keep
František Kučera <franta-hg@frantovo.cz>
parents: 16
diff changeset
   111
const string_t CLIParser::OPTION_FILE_KEEP = L"--file-keep";
0
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   112
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   113
}
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   114
}
c205f5d06418 project skeleton
František Kučera <franta-hg@frantovo.cz>
parents:
diff changeset
   115
}