Created
March 5, 2018 03:39
-
-
Save smallnest/09750e57ad4ed0ec775232eabf27a6fb to your computer and use it in GitHub Desktop.
Create go types based on mysql schema #tags: lang-go, lang-perl
This file contains hidden or 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
#!/usr/bin/env perl | |
use strict; | |
use Getopt::Long; | |
use DBI; | |
use String::CamelCase qw(camelize decamelize wordsplit); | |
my $type_map = { | |
bigint => 'int64', | |
blob => 'string', | |
char => 'string', | |
datetime => 'time.Time', | |
decimal => 'int32', | |
double => 'float64', | |
enum => 'int32', | |
float => 'float64', | |
int => 'int32', | |
longblob => 'string', | |
longtext => 'string', | |
mediumint => 'int32', | |
mediumtext => 'string', | |
smallint => 'int32', | |
text => 'string', | |
time => 'string', | |
timestamp => 'time.Time', | |
tinyint => 'int32', | |
tinytext => 'string', | |
varchar => 'string', | |
}; | |
my ($package, $filename); | |
GetOptions( | |
"package:s" => \$package, | |
"filename:s" => \$filename, | |
); | |
if( ! $package || ! $filename ) { | |
print <<EOF; | |
Usage: $0 --package <p> --filename <f> | |
env vars DBUSER, DBPASS, DBHOST, and DBNAME must also be set. | |
if env var DBTABLE is set, only that table will be processed. | |
EOF | |
exit 1; | |
} | |
my $dbuser = $ENV{DBUSER} or die "env var DBUSER must be set\n"; | |
my $dbpass = $ENV{DBPASS} or die "env var DBPASS must be set\n"; | |
my $dbhost = $ENV{DBHOST} or die "env var DBHOST must be set\n"; | |
my $dbname = $ENV{DBNAME} or die "env var DBNAME must be set\n"; | |
my $dbtable = $ENV{DBTABLE}; | |
my $dsn = "dbi:mysql:information_schema;host=$dbhost"; | |
my $dbh = DBI->connect($dsn, $dbuser, $dbpass); | |
# get table info | |
my $table_info; | |
my $sth_tables = $dbh->prepare("select * from TABLES where TABLE_SCHEMA=?"); | |
$sth_tables->execute($dbname); | |
while( my $t = $sth_tables->fetchrow_hashref ) { | |
$table_info->{ $t->{TABLE_NAME} } = $t; | |
} | |
# get column info | |
my $col_info; | |
my $sth_col = $dbh->prepare("select * from COLUMNS where TABLE_SCHEMA=? and TABLE_NAME=? order by ORDINAL_POSITION"); | |
foreach my $tname (sort keys %$table_info) { | |
$sth_col->execute($dbname, $tname); | |
while(my $c = $sth_col->fetchrow_hashref) { | |
push @{$col_info->{$tname}}, $c; | |
} | |
} | |
# convert to types | |
open my $fh, '>', $filename; | |
print $fh "package $package\n\n"; | |
foreach my $tname (sort keys %$col_info) { | |
next if $dbtable && $dbtable ne $tname; | |
my $ctname = camelize($tname); | |
my $tcmt = $col_info->{TABLE_COMMENT}; | |
print $fh "// $ctname - $tcmt\n"; | |
print $fh "type $ctname struct {\n"; | |
foreach my $c (@{ $col_info->{$tname} }) { | |
my $ccmt = $c->{COLUMN_COMMENT}; | |
my $name = $c->{COLUMN_NAME}; | |
my $ccname = camelize($name); | |
my $ctype = lc( $c->{DATA_TYPE} ); | |
my $gotype = $type_map->{ $ctype } or die "Unknown type '$ctype'\n"; | |
print $fh "\t$ccname $gotype\n"; | |
} | |
print $fh <<EOF; | |
} | |
func (t $ctname) TableName() string { | |
return "$tname" | |
} | |
EOF | |
} | |
close $fh; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/jinzhu/gorm/issues/574