add boolean parameter to --list-data-sources + allow listing and SELECTing at the same time v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 09 May 2021 18:19:09 +0200
branchv_0
changeset 54 bc6e11cccdf4
parent 53 cc6ffeba0fe5
child 55 3a120e64bb37
add boolean parameter to --list-data-sources + allow listing and SELECTing at the same time
bash-completion.sh
src/CLIParser.h
src/relpipe-tr-sql.cpp
--- a/bash-completion.sh	Mon Oct 26 00:00:35 2020 +0100
+++ b/bash-completion.sh	Sun May 09 18:19:09 2021 +0200
@@ -33,6 +33,11 @@
 		"boolean"
 	)
 
+	BOOLEAN_VALUES=(
+		"true"
+		"false"
+	)
+
 	DATA_SOURCE_STRING=(
 		"Driver=SQLite3;Database=file::memory:"
 		"Driver=SQLite3;Database=file:temp-relpipe.sqlite"
@@ -43,6 +48,7 @@
 	elif [[ "$w2" == "--relation"      && "x$w0" == "x" ]];    then COMPREPLY=('"SELECT * FROM "')
 	elif [[ "$w1" == "--type-cast"     && "x$w0" == "x" ]];    then COMPREPLY=("''")
 	elif [[ "$w2" == "--type-cast"                      ]];    then COMPREPLY=($(compgen -W "${DATA_TYPE[*]}" -- "$w0"))
+	elif [[ "$w1" == "--list-data-sources"              ]];    then COMPREPLY=($(compgen -W "${BOOLEAN_VALUES[*]}" -- "$w0"))
 	elif [[ "$w1" == "--parameter"     && "x$w0" == "x" ]];    then COMPREPLY=("''")
 	elif [[ "$w1" == "--copy"          && "x$w0" == "x" ]];    then COMPREPLY=("'.+'")
 	elif [[ "$w1" == "--copy-renamed"  && "x$w0" == "x" ]];    then COMPREPLY=("'.+'")
--- a/src/CLIParser.h	Mon Oct 26 00:00:35 2020 +0100
+++ b/src/CLIParser.h	Sun May 09 18:19:09 2021 +0200
@@ -38,6 +38,15 @@
 		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);
 	}
 
+	/**
+	 * TODO: use a common method
+	 */
+	bool parseBoolean(const string_t& value) {
+		if (value == L"true") return true;
+		else if (value == L"false") return false;
+		else throw relpipe::cli::RelpipeCLIException(L"Unable to parse boolean value: " + value + L" (expecting true or false)", relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
+	}
+
 	void addQuery(Configuration& c, Statement& currentQuery) {
 		if (currentQuery.sql.size()) {
 			c.statements.push_back(currentQuery);
@@ -85,7 +94,7 @@
 			} else if (option == OPTION_DATA_SOURCE_STRING) {
 				c.dataSourceString = readNext(arguments, i);
 			} else if (option == OPTION_LIST_DATA_SOURCES) {
-				c.listDataSources = true;
+				c.listDataSources = parseBoolean(readNext(arguments, i));
 			} else throw relpipe::cli::RelpipeCLIException(L"Unsupported CLI option: " + option, relpipe::cli::CLI::EXIT_CODE_BAD_CLI_ARGUMENTS);
 		}
 		addQuery(c, currentQuery); // last relation
--- a/src/relpipe-tr-sql.cpp	Mon Oct 26 00:00:35 2020 +0100
+++ b/src/relpipe-tr-sql.cpp	Sun May 09 18:19:09 2021 +0200
@@ -59,7 +59,10 @@
 		if (configuration.listDataSources) {
 			// --list-data-sources:
 			SqlHandler::listDataSources(writer.get(), driverManager.get());
-		} else if (std::regex_match(cli.programName(), std::wregex(L"^(.*/)?relpipe-in-sql$"))) {
+			if (isatty(fileno(stdin)) && configuration.statements.empty() && configuration.copyRelations.empty()) goto end;
+		}
+
+		if (std::regex_match(cli.programName(), std::wregex(L"^(.*/)?relpipe-in-sql$"))) {
 			// relpipe-in-sql:
 			if (cli.arguments().size() == 0) configuration.copyRelations.push_back({L".*", L"", false});
 			configuration.sqlBeforeRelational = isatty(fileno(stdin)) ? nullptr : &std::wcin;
@@ -74,6 +77,7 @@
 			reader->process();
 		}
 
+end:
 		resultCode = CLI::EXIT_CODE_SUCCESS;
 
 	} catch (RelpipeCLIException& e) {