Skip to content

Instantly share code, notes, and snippets.

@joshbooks
Last active August 7, 2019 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshbooks/abcba759c9ef8cbd113ffcf38e9ecc3b to your computer and use it in GitHub Desktop.
Save joshbooks/abcba759c9ef8cbd113ffcf38e9ecc3b to your computer and use it in GitHub Desktop.
turn a postgres table into a kotlin data class
psql "$postgresDb" -c "\pset pager off" -c "\d $postgresTable" | perl -pe 's/(_)./uc($&)/ge;s/_//g' | awk -F '|' '
BEGIN {shouldPrint = 0} / id / {shouldPrint = 1} /Indexes/ {shouldPrint = 0}
shouldPrint != 0 {printf "val "$1": "}
shouldPrint != 0 && $2~/integer/ {printf "Long"}
shouldPrint != 0 && $2~/boolean/ {printf "Boolean"}
shouldPrint != 0 && $2~/character varying/ {printf "String"}
shouldPrint != 0 && $2~/text/ {printf "String"}
shouldPrint != 0 && $2~/timestamp with time zone/ {printf "java.sql.Timestamp"}
shouldPrint != 0 && $4~/not null/ {print ","} shouldPrint != 0 && $4~/^ *$/ {print "?,"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment