scwrypts/zsh/db/run-sql/postgres
yage 96992e9344 v2.3.0
=====================================================================

--- New Scripts --------------------------

zsh )
  latex + latex template engine
   - latex/build-pdf
   - latex/cleanup
   - latex/create-new
   - latex/get-pdf
   - latex/open-pdf

  beta SQL script -- got tired of floating this; works, but only OK
   - db/run-sql/postgres

--- Changes ------------------------------

- Added 'math', 'basic', and 'times-new-roman' templates to latex
- Added 'readlink' to list of required coreutils
- Added __INPUT to read into a variable with prompt (zsh/utils/io)
- Added $EXECUTION_DIR to interact with the user's working directory

--- Bug Fixes ----------------------------

- subscwrypts no longer force stdout/stderr to tty
2022-08-09 21:51:31 -06:00

73 lines
1.7 KiB
Bash
Executable File

#!/bin/zsh
_DEPENDENCIES+=(
psql
)
_REQUIRED_ENV+=()
source ${0:a:h}/common.zsh
#####################################################################
_RUN_SQL_POSTGRES() {
local _HOST _NAME _PASS _PORT _USER INPUT_FILE
while [[ $# -gt 0 ]]
do
case $1 in
--host | -h ) _HOST="$2"; shift 2 ;;
--name | -d ) _NAME="$2"; shift 2 ;;
--pass | -w ) _PASS="$2"; shift 2 ;;
--port | -p ) _PORT="$2"; shift 2 ;;
--user | -U ) _USER="$2"; shift 2 ;;
--file | -i ) INPUT_FILE="$2"; shift 2 ;;
* ) shift 1 ;;
esac
done
[ ! $_HOST ] && _HOST=127.0.0.1
[ ! $_NAME ] && _NAME=postgres
[ ! $_PORT ] && _PORT=5432
[ ! $_USER ] && _USER=postgres
local SQL_DIR="$SCWRYPTS_DATA_PATH/sql"
[ ! -d $SQL_DIR ] && mkdir -p $SQL_DIR
cd $SQL_DIR
[[ $(ls "*.sql" 2>&1 | wc -l) -eq 0 ]] && {
__ERROR "you haven't made any SQL commands yet"
__REMINDER "add '.sql' files here: '$SQL_DIR/'"
exit 1
}
[ ! $INPUT_FILE ] && INPUT_FILE=$(\
__FZF 'select a sql file to run'
)
[ ! $INPUT_FILE ] && __ABORT
[ ! -f $INPUT_FILE ] && {
__FAIL 2 "no such sql file '$SQL_DIR/$INPUT_FILE'"
}
__STATUS "loading $INPUT_FILE preview..."
_LESS $INPUT_FILE
__STATUS "login : $_USER@$_HOST:$_PORT/$_NAME"
__STATUS "command : ./$INPUT_FILE"
__yN 'run this command?' || __ABORT
__STATUS "running './$INPUT_FILE'"
PGPASSWORD="$_PASS" psql \
-h $_HOST \
-p $_PORT \
-U $_USER \
-d $_NAME \
< $INPUT_FILE \
&& __SUCCESS "finished running './$INPUT_FILE'" \
|| __FAIL 3 "something went wrong running './$INPUT_FILE' (see above)"
}
#####################################################################
__WARNING
__WARNING 'this function is in a beta state'
__WARNING
_RUN_SQL_POSTGRES $@