bash-completion.sh
author František Kučera <franta-hg@frantovo.cz>
Sun, 28 Aug 2022 18:03:13 +0200
branchv_0
changeset 59 a1775ba6d056
parent 55 3a120e64bb37
permissions -rw-r--r--
optional UNION ALL: CLI option '--on-duplicate-relation' with values 'fail' and 'insert' (later more modes) examples, shortcuts: relpipe-tr-unionall() { relpipe-tr-sql --copy '.+' --on-duplicate-relation insert; } # does UNION ALL for all tables with same name (including already existing ones – when non-empty database is used) relpipe-tr-unionall() { relpipe-tr-serialize | relpipe-tr-deserialize; } # does UNION ALL only for tables with same name immediately following each other (interleaved duplicates will stay unaffected)

# Relational pipes
# Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

_relpipe_tr_sql_completion_dsn() {
	if type relpipe-out-nullbyte &> /dev/null; then
		relpipe-tr-sql --list-data-sources true | relpipe-out-nullbyte | while read -r -d '' name; do read -r -d '' description; echo $name; done
	fi
}

_relpipe_tr_sql_completion() {
	local w0 w1 w2

	COMPREPLY=()
	w0=${COMP_WORDS[COMP_CWORD]}
	w1=${COMP_WORDS[COMP_CWORD-1]}
	w2=${COMP_WORDS[COMP_CWORD-2]}

	DATA_TYPE=(
		"string"
		"integer"
		"boolean"
	)

	BOOLEAN_VALUES=(
		"true"
		"false"
	)

	ON_DUPLICATE_RELATION=(
		"fail"
		"insert"
		# "extend"
		# "drop"
		# "skip"
		# "rename-new"
		# "rename-old"
	)

	DATA_SOURCE_STRING=(
		"Driver=SQLite3;Database=file::memory:"
		"Driver=SQLite3;Database=file:temp-relpipe.sqlite"
		"Driver=SQLite3;Database=file:/tmp/relpipe.sqlite"
	)

	if   [[ "$w1" == "--relation"      && "x$w0" == "x" ]];    then COMPREPLY=("''")
	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" == "--on-duplicate-relation"          ]];    then COMPREPLY=($(compgen -W "${ON_DUPLICATE_RELATION[*]}" -- "$w0"))
	elif [[ "$w1" == "--copy"          && "x$w0" == "x" ]];    then COMPREPLY=("'.+'")
	elif [[ "$w1" == "--copy-renamed"  && "x$w0" == "x" ]];    then COMPREPLY=("'.+'")
	elif [[ "$w2" == "--copy-renamed"  && "x$w0" == "x" ]];    then COMPREPLY=("'copy_of_\$0'")
	elif [[ "$w1" == "--data-source-name"               ]];    then COMPREPLY=($(compgen -W "$(_relpipe_tr_sql_completion_dsn)" -- "$w0"))
	elif [[ "$w1" == "--data-source-string"             ]];    then COMPREPLY=($(compgen -W "${DATA_SOURCE_STRING[*]}" -- "$w0"))
	else
		OPTIONS=(
			"--relation"
			"--type-cast"
			"--parameter"
			"--on-duplicate-relation"
			"--copy"
			"--copy-renamed"
			"--list-data-sources"
			"--data-source-name"
			"--data-source-string"
		)
		COMPREPLY=($(compgen -W "${OPTIONS[*]}" -- "$w0"))
	fi
}

complete -F _relpipe_tr_sql_completion relpipe-tr-sql
complete -F _relpipe_tr_sql_completion relpipe-in-sql