-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsql2dot.pl
executable file
·39 lines (37 loc) · 1012 Bytes
/
sql2dot.pl
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
#!/usr/bin/perl
print "digraph ER_diagram {\n";
print "graph [rankdir=\"BT\"];\n";
my $current_table;
while (<STDIN>) {
chomp;
if (m/CREATE TABLE ([A-Za-z0-9_]*) \(/) {
$current_table = $1;
@fields = ();
@references = ();
}
my $current_field_name;
if (m/^\s+,?([a-zA-Z0-9_]*)\s+[^\s]+/) {
$current_field_name = $1;
push @fields,$current_field_name;
}
if (m/REFERENCES ([A-Za-z0-9_]+)\(([a-zA-Z0-9_]+)\)/i) {
push @references, "\t$current_table:$current_field_name -> $1:$2;\n";
}
if (m/^\);/) {
$table_line = "";
$table_line .= "\t$current_table";
$table_line .= " [shape=none,label=<<TABLE BORDER=\"1\" CELLBORDER=\"1\" CELLSPACING=\"0\">";
$table_line .= "<TR><TD PORT=\"$current_table\"><B>$current_table</B></TD></TR>";
for $attr (@fields) {
$table_line .= "<TR><TD PORT=\"$attr\">$attr</TD></TR>";
#$table_line .= " | $attr";
}
$table_line .= "</TABLE>>]";
$table_line .= "\n";
print $table_line;
for $ref (@references) {
print $ref;
}
}
}
print "}";