Skip to content

Instantly share code, notes, and snippets.

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 thomasw-mitutoyo-ctl/9dc43e0ec0d41af970d3a5c755c1cb74 to your computer and use it in GitHub Desktop.
Save thomasw-mitutoyo-ctl/9dc43e0ec0d41af970d3a5c755c1cb74 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 changes for ShaoLinq.
select 'replacewithtablename' into @table;
select 'replacewithdatabasename' into @schema;
select GROUP_CONCAT(r.x SEPARATOR '\n') from
(
select '[DataAccessObject]'as x,'grp'as grp union
select concat('public abstract class ',@table,'{') as x, 'grp' as grp
union
select concat('[PersistedMember]\npublic abstract ',tps.dest,IF(tps.dest = 'string', ' ', IF(is_nullable = 'NO', ' ', '? ')), REPLACE(column_name, ' ', ''),' {get;set;}'), 'grp'
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 'tinytext' ,'string' union all
select 'mediumtext' ,'string' union all
select 'longtext' ,'string' union all
select 'enum' ,'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 '}', 'grp') r GROUP BY grp;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment