Created
June 9, 2020 09:30
-
-
Save dev-brutus/1753286c5442a07809d7424666fc8525 to your computer and use it in GitHub Desktop.
The SQL generates Java model classes by information_schema (for PostgreSQL)
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
select classs_name, | |
E'import lombok.Data;\n\n' || | |
E'@Data\npublic class ' || classs_name || E' {\n' || string_agg(field_string, '') || E'}\n' | |
from ( | |
select classs_name, | |
' private ' || type || ' ' || lower(substring(field_name, 1, 1)) || substring(field_name, 2) || | |
E';\n' | |
as field_string | |
from (select replace(initcap(table_name), '_', '') as classs_name, | |
case | |
when (data_type, column_name) = ('bigint', 'id') then 'Long' | |
when (data_type, is_nullable) = ('bigint', 'NO') then 'long' | |
when (data_type, is_nullable) = ('bigint', 'YES') then 'Long' | |
when (data_type, is_nullable) = ('integer', 'NO') then 'int' | |
when (data_type, is_nullable) = ('integer', 'YES') then 'Integer' | |
when (data_type) = ('text') then 'String' | |
when (data_type) = ('date') then 'Date' | |
else 'Void' | |
end "type", | |
replace(initcap(column_name), '_', '') as field_name | |
from information_schema | |
.columns | |
where table_schema = '!!!!_SCHEMA_NAME_!!!' | |
order by table_name, ordinal_position) t1 | |
) t2 | |
group by classs_name | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment