commit 7335d9359a02a47598d5d7e36a595b1662dc8c53 Author: James Eagan Date: Fri Sep 29 16:45:37 2023 +0200 initial commit diff --git a/sqlite-csv.fish b/sqlite-csv.fish new file mode 100644 index 0000000..5dd1faa --- /dev/null +++ b/sqlite-csv.fish @@ -0,0 +1,58 @@ + +function sqlite-csv + argparse --exclusive csv,md,json json csv md 'sep=?' help -- $argv + if set -q _flag_help + echo "\ +Usage: sqlite-csv [options] [] [] + +If no file is specified, it will be read from stdin. +If no is specified, uses 'select * from data'. +If no output format is specified, uses ASCII box rendering. + +Options: + --csv output as CSV using a , separator + --md ouput as markdown + --json output as JSON + --sep=SEP use SEP as the column separator when reading the CSV file (e.g. --sep=';') + --help show this message and exit + +The imported CSV table is named `data`. + +Example: sqlite-csv --md summary.csv 'select * from data order by name'" + return + end + + if set -q _flag_csv + set outputMode "csv" + else if set -q _flag_md + set outputMode "markdown" + else if set -q _flag_json + set outputMode "json" + else + set outputMode "box" + end + + if set -q _flag_sep + set insep $_flag_sep + else + set insep "," + end + + # Check if first arg is a file that exists; + # if it isn't, read from stdin + if test "x$argv[1]" != "x" + and test -e $argv[1] + set db $argv[1] + set query $argv[2..] + else + set db "|cat -" + set query $argv[1..] + end + + # If no query is specified, dump all columns + if test -z $query + set query 'select * from data' + end + + sqlite3 :memory: -cmd '.mode csv' -cmd ".separator $insep" -cmd ".import '$db' data" -cmd ".mode $outputMode --wrap 120" "$query" +end