404 motivation not found | t_ishidaのブログ

7月/08

17

DBDesignerの吐いたcreate table文をスクラップするスクリプト

#!/usr/bin/perl
use strict;
sub say{ print "$_[0]n" }

my @tbls = ();
my $cur  = undef;
my ( $fn, $sep ) = @ARGV;
$fn || die('ファイルの指定無いよ');
$sep = $sep || "t";
open my $f, $fn;
while( <$f> ){
/(?:^s+$|^))/ and next;
/create +table +([^( ]+)/i and push @tbls, $cur and $cur = { nm => $1, flds => [], pk => [], idx => [] } and next;
/primary +key((.+?))/i    and push @{$cur->{pk}}, $1 and next;
/INDEX +([^(]+)((.+?))/  and push @{$cur->{idx}}, { nm => $1, fld_nm => $2 } and next;
my %fld = ();
@fld{ 'dum','nm','dat_type'} = split( / +/ );
delete $fld{'dum'};
push @{$cur->{flds}}, %fld;
}
push @tbls, $cur;
close $f;
shift @tbls;
for( @tbls ){
say( 'table name' );
say( $_->{nm} );

say( 'columns' );
foreach my $fld( @{$_->{flds}} ){
say( join $sep, ( $fld->{nm}, $fld->{dat_type} ) );
}

say( 'primary_key' );
foreach my $pk( @{$_->{pk}} ){
say( $pk );
}

say( 'index' );
foreach my $idx( @{$_->{idx}} ){
say( $idx->{nm} . $sep . $idx->{fld_nm} );
}
say('');
}

多分、もっと短くなると思う

Share and Enjoy:
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Tumblr
  • email
  • Facebook
  • FriendFeed

RSS Feed

コメントはまだありません。

Leave a comment!

<< 先日のCreateTable文をスクラップする奴を改良

ゆの in javascript ってこうですか?分りません(><) >>

Find it!

Theme Design by devolux.org

Tag Cloud