Skip to content

Commit

Permalink
Add psql-jq helper script to see psql query as json
Browse files Browse the repository at this point in the history
  • Loading branch information
tkareine committed Feb 28, 2017
1 parent f3d2983 commit 05a2a35
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
65 changes: 65 additions & 0 deletions bin/psql-jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env bash

set -uo pipefail

print_usage() {
local this_script
this_script=${0##*/}

cat << EOF
Usage: $this_script [-d db_name] -c command
-c, --command command SQL query to execute
-h, --help display this message and exit
Other options are passed to psql.
Example: $this_script -d mydb -c 'select(1)'
EOF
}

command=
passthrough_args=()

while :; do
case ${1:-} in
-h|-\?|--help)
print_usage
exit
;;
-c|--command)
if [[ -n ${2:-} ]]; then
command=$2
shift
else
printf 'ERROR: "--command" requires a non-empty argument.\n' >&2
exit 1
fi
;;
--)
shift
break
;;
-?*)
passthrough_args+=($1)

if [[ -n ${2:-} && $2 != -* ]]; then
passthrough_args+=($2)
shift
fi
;;
*)
break
esac

shift
done

if [[ -z $command ]]; then
print_usage
exit 1
fi

psql -t "${passthrough_args[@]}" << EOF | jq -C '.'
select row_to_json(t) from ($command) t
EOF
1 change: 1 addition & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ INSTALL_BY_SYMLINKING_ALL=(
.xmodmap
bin/git-create-patch
bin/git-remote-hg
bin/psql-jq
bin/well
)

Expand Down

0 comments on commit 05a2a35

Please sign in to comment.