Skip to content

Instantly share code, notes, and snippets.

@pdwetz
Last active August 15, 2021 10:12
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save pdwetz/5368441 to your computer and use it in GitHub Desktop.
Save pdwetz/5368441 to your computer and use it in GitHub Desktop.
Outputs a POCO for a given MySql table. Based on http://stackoverflow.com/a/13918084/21865 with mild formatting changes and additional types added.
select 'replacewithtablename' into @table;
select 'replacewithdatabasename' into @schema;
select concat('public class ',@table,'{')
union
select concat('public ',tps.dest,' ',column_name,'{get;set;}')
from information_schema.columns c
join (
select 'char' as orign ,'string' as dest union all
select 'varchar' ,'string' union all
select 'datetime' ,'DateTime' union all
select 'date' ,'DateTime' union all
select 'text' ,'string' union all
select 'binary' ,'byte[]' union all
select 'int' ,'int' union all
select 'decimal' ,'decimal' union all
select 'float' ,'float' union all
select 'tinyint' ,'bool'
) tps on c.data_type like tps.orign
where table_schema=@schema and table_name=@table union
select '}';
@ztkemsley
Copy link

Sheer brilliance. Thanks to both you and MeelStorm for this :)

@ztkemsley
Copy link

I added The following line to manage binary fields such as md5/sha hashes etc.

select 'binary' ,'byte[]' union all

@vishnoor
Copy link

please add select 'float' ,'float' union all for float datatype

@atomass
Copy link

atomass commented Feb 13, 2018

Timestamp datatype is missing.
Please add select 'timestamp' ,'DateTime' union all

@shukebeta
Copy link

I have written a bash script to make your life easier. It was in the comments area on https://gist.github.com/shukebeta/b315c940f63277fa982e6c93c0f97040

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment