indent-sql-sh (900B)
1 #!/bin/bash 2 3 set -eu 4 5 # This script indents the output of Exposed SQL logger. 6 7 # Usage: ./indent.sh filename 8 9 # Remove leading "^SQL: " that Exposed uses. 10 crop_leading_sql () { 11 sed 's/^SQL: //' 12 } 13 14 # Inserts new line & two spaces before the first "(" 15 # and last ")", and before each comma. Only triggers on 16 # "CREATE TABLE"-lines. 17 indent_create_table () { 18 sed '/^CREATE/s/, /,/g' \ 19 | sed '/^CREATE/s/\(,\|)$\)/\n \1/g' \ 20 | sed '/^CREATE/s/(/\n (/' 21 } 22 23 24 # Inserts new line & two spaces before each "ALTER TABLE" 25 # statement 26 indent_alter_table () { 27 sed 's/^ALTER TABLE \(.*\)/ALTER TABLE\n \1/' 28 } 29 30 # Inserts a blank line after between each CREATE/ALTER TABLE statement. 31 blank_line_after_statement () { 32 sed '/^CREATE TABLE/s/\(.*\)/\n\1/' \ 33 | sed '/^ALTER TABLE/s/\(.*\)/\n\1/' 34 } 35 36 crop_leading_sql < $1 \ 37 | indent_create_table \ 38 | indent_alter_table \ 39 | blank_line_after_statement