Last active
August 7, 2019 20:36
-
-
Save joshbooks/abcba759c9ef8cbd113ffcf38e9ecc3b to your computer and use it in GitHub Desktop.
turn a postgres table into a kotlin data class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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