#!perl -w
use strict;
use utf8;
use open ':encoding(gbk)', ':std';
use open ':encoding(utf8)';
use Win32::OLE;
use Win32::OLE::Const 'Microsoft.Word'; # wd constants
my $word = Win32::OLE->new("Word.Application");
$word->{Visible} = 1;
my $document = $word->Documents->Add;
my $selection = $word->Selection;
my %thistablecolumns = ();
my %thistablerefrecords = ();
&main();
sub PrintTableinfo(){
my ($tablename) = @_;
my ($columnname,$columntype);
#-----------------------------
print ("$tablename \n");
my @columnname = keys %thistablecolumns;
print @columnname;
@columnname = sort { $a cmp $b } @columnname ;
print @columnname;
while(($columnname,$columntype) = each(%thistablecolumns)) {
print ("$columnname,$columntype");
my $columndesc = "";
if(exists $thistablerefrecords{$columnname}){
$columndesc = $thistablerefrecords{$columnname};
}
print ("$columndesc\n");
}
$selection->Font->{size} = 20;
$selection->TypeText("tablename : $tablename \n");
$selection->Font->{size} = 10;
$selection->TypeText("mark:");
my $Range = $document->ActiveWindow->Selection->{Range};
my $rownum = keys %thistablecolumns;
$rownum ++;
my $table = make_table( $document, $Range,$rownum,3);
$table->Columns(2)->{Width} = '50';
$document->ActiveWindow->Selection->EndKey( { unit => wdStory } );
enter($document);
$table->cell( 1, 1 )->Range->InsertAfter("fieldname");
$table->cell( 1, 2 )->Range->InsertAfter("fieldtype");
$table->cell( 1, 3 )->Range->InsertAfter("desc");
my $rowindex = 2;
foreach $columnname (@columnname) {
$columntype = $thistablecolumns{$columnname};
my $columndesc = "";
if(exists $thistablerefrecords{$columnname}){
$columndesc = $thistablerefrecords{$columnname};
}
print (" row : $rowindex\n");
$table->cell( $rowindex, 1 )->Range->InsertAfter($columnname);
$table->cell( $rowindex, 2 )->Range->InsertAfter($columntype);
$table->cell( $rowindex, 3 )->Range->InsertAfter($columndesc);
$rowindex++;
}
}
sub main(){
my $line ;
my $tablename = "";
my $tablecontext = 0;
my %tableref = ();
while($line = <>){
#print $line;
if ($line =~ /CREATE TABLE\s+(\w+)/){
$tablename = $1;
$tablecontext = 1;
next;
}elsif ($line =~ /ENGINE=InnoDB/){
&PrintTableinfo($tablename);
$tablename = "";
$tablecontext = 0;
%thistablecolumns = ();
%thistablerefrecords = ();
next;
}
if ($tablecontext == 1){
# print $line;
&ParseTableDesc($line);
}
}
$document->SaveAs('E:/ffpsdatadic.doc');
$document->close;
$word->quit;
}
sub ParseTableDesc(){
my ($line) = @_;
if ($line =~ /^\s*PRIMARY KEY/ || $line =~ /^\s*KEY/ ){
return;
}elsif ($line =~ /FOREIGN\s+KEY\s+\((\w+)\)\s+REFERENCES\s+(\w+)\s+\((\w+)\)/){
my $columnname = $1;
my $reftable = $2;
my $refcolumnname = $3;
$thistablerefrecords{$columnname} = "refkey : ${reftable} -- ${refcolumnname}"
}elsif ($line =~ /^\s*(\w+)\s+(\w+)/){
my $columnname = $1;
my $columntype = $2;
$thistablecolumns{$columnname} = $columntype;
}
return;
}
sub make_table {
my ( $document, $Range ,$cols,$rows) = @_;
my $objTa = $document->Tables->Add( $Range, $cols, $rows );
#$objTa->Columns(1)->{Width} = '5';
$objTa->Borders->{InsideLineStyle} = wdLineStyleSingle;
$objTa->Borders->{OutsideLineStyle} = wdLineStyleSingle;
#$objTa->Cell( 1, 1 )->Merge( { MergeTo => $objTa->Cell( 3, 1 ) } );
return $objTa;
}
sub enter {
my $document = shift;
$document->ActiveWindow->Selection->TypeParagraph;
}
use strict;
use utf8;
use open ':encoding(gbk)', ':std';
use open ':encoding(utf8)';
use Win32::OLE;
use Win32::OLE::Const 'Microsoft.Word'; # wd constants
my $word = Win32::OLE->new("Word.Application");
$word->{Visible} = 1;
my $document = $word->Documents->Add;
my $selection = $word->Selection;
my %thistablecolumns = ();
my %thistablerefrecords = ();
&main();
sub PrintTableinfo(){
my ($tablename) = @_;
my ($columnname,$columntype);
#-----------------------------
print ("$tablename \n");
my @columnname = keys %thistablecolumns;
print @columnname;
@columnname = sort { $a cmp $b } @columnname ;
print @columnname;
while(($columnname,$columntype) = each(%thistablecolumns)) {
print ("$columnname,$columntype");
my $columndesc = "";
if(exists $thistablerefrecords{$columnname}){
$columndesc = $thistablerefrecords{$columnname};
}
print ("$columndesc\n");
}
$selection->Font->{size} = 20;
$selection->TypeText("tablename : $tablename \n");
$selection->Font->{size} = 10;
$selection->TypeText("mark:");
my $Range = $document->ActiveWindow->Selection->{Range};
my $rownum = keys %thistablecolumns;
$rownum ++;
my $table = make_table( $document, $Range,$rownum,3);
$table->Columns(2)->{Width} = '50';
$document->ActiveWindow->Selection->EndKey( { unit => wdStory } );
enter($document);
$table->cell( 1, 1 )->Range->InsertAfter("fieldname");
$table->cell( 1, 2 )->Range->InsertAfter("fieldtype");
$table->cell( 1, 3 )->Range->InsertAfter("desc");
my $rowindex = 2;
foreach $columnname (@columnname) {
$columntype = $thistablecolumns{$columnname};
my $columndesc = "";
if(exists $thistablerefrecords{$columnname}){
$columndesc = $thistablerefrecords{$columnname};
}
print (" row : $rowindex\n");
$table->cell( $rowindex, 1 )->Range->InsertAfter($columnname);
$table->cell( $rowindex, 2 )->Range->InsertAfter($columntype);
$table->cell( $rowindex, 3 )->Range->InsertAfter($columndesc);
$rowindex++;
}
}
sub main(){
my $line ;
my $tablename = "";
my $tablecontext = 0;
my %tableref = ();
while($line = <>){
#print $line;
if ($line =~ /CREATE TABLE\s+(\w+)/){
$tablename = $1;
$tablecontext = 1;
next;
}elsif ($line =~ /ENGINE=InnoDB/){
&PrintTableinfo($tablename);
$tablename = "";
$tablecontext = 0;
%thistablecolumns = ();
%thistablerefrecords = ();
next;
}
if ($tablecontext == 1){
# print $line;
&ParseTableDesc($line);
}
}
$document->SaveAs('E:/ffpsdatadic.doc');
$document->close;
$word->quit;
}
sub ParseTableDesc(){
my ($line) = @_;
if ($line =~ /^\s*PRIMARY KEY/ || $line =~ /^\s*KEY/ ){
return;
}elsif ($line =~ /FOREIGN\s+KEY\s+\((\w+)\)\s+REFERENCES\s+(\w+)\s+\((\w+)\)/){
my $columnname = $1;
my $reftable = $2;
my $refcolumnname = $3;
$thistablerefrecords{$columnname} = "refkey : ${reftable} -- ${refcolumnname}"
}elsif ($line =~ /^\s*(\w+)\s+(\w+)/){
my $columnname = $1;
my $columntype = $2;
$thistablecolumns{$columnname} = $columntype;
}
return;
}
sub make_table {
my ( $document, $Range ,$cols,$rows) = @_;
my $objTa = $document->Tables->Add( $Range, $cols, $rows );
#$objTa->Columns(1)->{Width} = '5';
$objTa->Borders->{InsideLineStyle} = wdLineStyleSingle;
$objTa->Borders->{OutsideLineStyle} = wdLineStyleSingle;
#$objTa->Cell( 1, 1 )->Merge( { MergeTo => $objTa->Cell( 3, 1 ) } );
return $objTa;
}
sub enter {
my $document = shift;
$document->ActiveWindow->Selection->TypeParagraph;
}