1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
#!/usr/local/bin/perl -w
# POD docs at end of file
use strict;
use Carp;
use FileHandle;
use Getopt::Long;
my $debug;
my $help;
GetOptions(
"help|h"=>\$help,
);
foreach my $fn (@ARGV) {
sl2sql($fn);
}
exit 0;
sub sl2sql {
my $fn = shift;
my $fh = FileHandle->new($fn) || die("can't open $fn");
my @domains = ();
while(<$fh>) {
chomp;
s/\%.*//;
s/\s+$//;
next unless $_;
if (/^:(\w+)\s+(.*)/) {
my $cmd = $1;
my $rest = $2;
my $cmt = '';
if ($rest =~ /\#\s*(.*)/) {
$cmt = $1;
$rest =~ s/\#.*//;
}
my @args = split(' ', $rest);
if ($cmd eq 'domain') {
my $d = [shift @args, join(' ', @args)];
push(@$d,
$args[0] =~ /^[A-Z]/ ?
'primitive' : 'relation');
push(@$d, $cmt);
push(@domains, $d);
}
if ($cmd eq 'import') {
print "-- $_\n";
push(@domains, [$args[0], $args[0], 'relation', '']);
}
}
elsif (/^\#/) {
s/\#/\-\-/;
print "$_\n";
}
else {
if (/^(\w+)\((.*)\)\s*(.*)/) {
my $rel = $1;
my $argstr = $2;
my $sep = $3;
my @cmts = ();
my @constraints = ();
if ($sep eq ':-') {
while(<$fh>) {
chomp;
s/\s+$//;
last unless $_;
if (/\s+(.*)/) {
my $line = $1;
my $last;
if ($line =~ /\#\s*(.*)/) {
push(@cmts, $1);
$line =~ s/\#.*//;
}
$line =~ s/\,$//;
if ($line =~ /\.$/) {
$line =~ s/\.$//;
$last = 1;
}
if ($line =~ /:(.*)/) {
# push(@cmts, $1);
}
else {
push(@constraints, $line) if $line;
}
last if $last;
}
else {
last; # must have indent
}
}
if (@cmts) {
push(@cmts, '');
}
}
my @args = split(/,\s*/, $argstr);
push(@domains, [$rel, $rel, 'relation']);
my @cols =
map {
my $col = $_;
my $nullable = 0;
if ($col =~ /\?$/) {
$nullable = 1;
$col =~ s/\?$//;
}
my $type = 'TEXT';
my $qual = '';
my $cmt = '';
if ($col eq '*') {
$col = $rel .'_id';
$type = "SERIAL";
$nullable = 0;
}
else {
my ($d) = grep {$_->[0] eq $col} @domains;
if ($d) {
$cmt = $d->[3];
$type = $d->[1];
if ($d->[2] eq 'relation') {
$col .= '_id';
$qual =
sprintf("REFERENCES $type(%s)",
$type.'_id');
$type = "INTEGER";
}
}
else {
warn $col;
}
}
unless ($nullable) {
$qual = "NOT NULL $qual";
}
my $all = sprintf("%-20s %-12s %s",
$col, $type, $qual);
push(@cmts,
sprintf("%-20s: $cmt",
$col)) if $cmt;
# if ($cmt) {
# # $all = "-- $cmt\n $all";
# $all = sprintf("%-60s -- $cmt", $all);
# }
$all;
} @args;
# blank line
$constraints[0] = "\n$constraints[0]" if @constraints;
createtable($rel, [@cols, @constraints], [@cmts]);
}
else {
warn $_;
}
}
}
$fh->close;
}
sub createtable {
my $tbl = shift;
my $cols = shift;
my $cmts = shift || [];
my $cmtstr =
join('', map {"-- $_\n"} @$cmts);
printf "\n\n-- RELATION: $tbl\n--\n$cmtstr--\n";
printf("CREATE TABLE $tbl (\n%s\n);\n",
join(",\n", map { s/\s+$//;s/(\n*)\s*(.*)/$1 $2/;$_} @$cols));
print "-- ****************************************\n";
}
__END__
=head1 NAME
stag-sl2sql.pl
=head1 SYNOPSIS
=head1 DESCRIPTION
=cut
|