my $tables = q~
#A Person is independent of everything else.
CREATE TABLE Person (
Id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
Name VARCHAR(255),
KEY( Name ),
PRIMARY KEY( Id )
);
#An Actor is a Person.
#(An Actor plays Roles).
CREATE TABLE Actor (
Id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
PersonId INT(11) UNSIGNED NOT NULL REFERENCES Person(Id),
KEY( PersonId ),
PRIMARY KEY( Id )
);
#A Director is a Person.
#A Director directs Movies.
#(A Director can also be an Actor).
CREATE TABLE Director (
Id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
PersonId INT(11) UNSIGNED NOT NULL REFERENCES Person(Id),
MovieId INT(11) UNSIGNED NOT NULL REFERENCES Movie(Id),
KEY( MovieId ),
KEY( PersonId ),
PRIMARY KEY( Id )
);
#Roles are played by Actors.
#Roles occur in Movies.
#A Role is a Person.
CREATE TABLE Role (
Id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
ActorId INT(11) UNSIGNED NOT NULL REFERENCES Actor(Id),
MovieId INT(11) UNSIGNED NOT NULL REFERENCES Movie(Id),
PersonId INT(11) UNSIGNED NOT NULL REFERENCES Person(Id),
KEY( PersonId ),
KEY( ActorId ),
KEY( MovieId ),
PRIMARY KEY( Id )
);
#A movie has a Title.
#(A movie may have: Roles, Directors, Actors, Writers).
CREATE TABLE Movie (
Id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
Title VARCHAR(255),
PRIMARY KEY( Id )
);
~;
use strict;
use warnings;
use SQL::Translator;
my %To = (
POD => 'pod',
XML => 'xml',
HTML => 'html',
Raw => 'Raw',
GraphViz => 'png',
ClassDBI => 'pm',
MySQL => 'sql',
SQLite => 'sql',
Oracle => 'sql',
PostgreSQL => 'sql',
Sybase => 'sql',
);
for my $to ( sort keys %To ){
print STDERR "Translating to $to ...\n";
my $translator = SQL::Translator->new(
debug => 0, # PrINT debug info
trace => 0, # PrINT Parse::RecDescent trace
no_comments => 1, # Don't include comments in output
show_warnings => 1, # PrINT name mutations, conflicts
add_drop_table => 1, # Add "drop table" statements
data => \$tables,
producer_args => {
layout => 'dot',
node_shape => 'trapezium', #plaintext diamond house
},
);
my $output = $translator->translate(
from => "MySQL",
to => $to,
) or warn "\n\nFATAL ERROR: ", $translator->error
and next;
my $file = __FILE__ ."_$to.$To{$to}";
open my $fh, '>', $file or warn "Can't clobber $file : $!" &&
+next;
binmode $fh;
print {$fh} $output;
close $fh;
}
__END__
Description of Schema
| Field Name | Data Type | Size | Default | Other | Foreign Key |
|---|
| Id | INT | 11 | | PRIMARY KEY, NOT NULL | |
| Name | VARCHAR | 255 | | | |
Indices
| Field Name | Data Type | Size | Default | Other | Foreign Key |
|---|
| Id | INT | 11 | | PRIMARY KEY, NOT NULL | |
| PersonId | INT | 11 | | NOT NULL | Person.Id |
Indices
Indices
| Name | Fields |
|---|
| MovieId |
| PersonId |
Indices
| Name | Fields |
|---|
| PersonId |
| ActorId |
| MovieId |
| Field Name | Data Type | Size | Default | Other | Foreign Key |
|---|
| Id | INT | 11 | | PRIMARY KEY, NOT NULL | |
| Title | VARCHAR | 255 | | | |
Created by
SQL::Translator