package SQL::Translator::Parser::XML::XMI::SQLFairy;
# -------------------------------------------------------------------
-# $Id: SQLFairy.pm,v 1.2 2003-10-13 17:05:55 grommit Exp $
+# $Id$
# -------------------------------------------------------------------
# Copyright (C) 2003 Mark Addison <mark.addison@itn.co.uk>,
#
use strict;
use vars qw[ $DEBUG $VERSION @EXPORT_OK ];
-$VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
+$VERSION = sprintf "%d.%02d", q$Revision$ =~ /(\d+)\.(\d+)/;
$DEBUG = 0 unless defined $DEBUG;
use Exporter;
use base qw(Exporter);
$end[0]->{multiplicity}{rangeUpper} == 1
|| $end[1]->{multiplicity}{rangeUpper} == 1
) {
- # 1:m or 0:m
one2many($assoc);
}
else
{
- # m:n
- warn "Sorry, n:m associations not yet implimented for xmi.id=".$assoc->{"xmi.id"}."\n";
+ many2many($assoc);
}
}
}
# Maps a 1:M association into the schema
-sub one2many {
+sub one2many
+{
my ($assoc) = @_;
my @ends = @{$assoc->{associationEnds}};
my ($end1) = grep $_->{multiplicity}{rangeUpper} == 1, @ends;
) or die $schema->error;
}
+# Maps m:n into schema by building a link table.
+sub many2many
+{
+ my ($assoc) = @_;
+ my @end = @{$assoc->{associationEnds}};
+
+ # Create the link table
+ my $name = $end[0]->{participant}{name}."_".$end[1]->{participant}{name};
+ my $link_table = $schema->add_table( name => $name )
+ or die "Schema Error: ".$schema->error;
+
+ # Export the pkey(s) from the ends into the link table
+ my @pkeys;
+ foreach (@end) {
+ my $table = $schema->get_table($_->{participant}{name});
+ my @fkeys = $table->primary_key->fields;
+ push @pkeys,@fkeys;
+ foreach ( @fkeys ) {
+ my $fld = $table->get_field($_);
+ my %data;
+ $data{$_} = $fld->$_()
+ foreach (
+ qw/name size data_type default_value is_nullable is_unique/);
+ $data{is_auto_increment} = 0;
+ $data{extra} = { $fld->extra }; # Copy
+ $link_table->add_field(%data) or die $table->error;
+ }
+ $link_table->add_constraint(
+ type => "FOREIGN_KEY",
+ fields => [@fkeys],
+ reference_table => $table->{name},
+ reference_fields => [@fkeys],
+ ) or die $schema->error;
+
+ }
+ # Add pkey constraint
+ $link_table->add_constraint( type => "PRIMARY KEY", fields => [@pkeys] )
+ or die $link_table->error;
+
+
+ # Add fkeys to our participants
+}
1; #---------------------------------------------------------------------------
__END__
=head2 Relationships
-Modeled using UML associations. Currently only handles 0:m and 1:m joins. That
-is associations where one ends multiplicty is '1' or '0..1' and the other end's
+=head2 1:m
+
+Associations where one ends multiplicty is '1' or '0..1' and the other end's
multplicity is more than 1 e.g '*', '0..*', '1..*', '0..3', '4..42' etc.
The pkey field from the 1 end is added to the table for the class at the many
If the association is a composition then the created fkey is made part of the
many ends pkey. ie It exports the pkey to create an identity join.
+=head2 m:n
+
+Model using a standard m:n association and the parser will automatically create
+a link table for you in the Schema by exporting pkeys from the tables at
+each end.
+
=head1 EXAMPLE
TODO An example to help make sense of the above! Probably based on the test.
=head1 TODO
-1:1 and m:m joins.
+1:1 joins.
+
+Use Role names from associations as field names for exported keys when building
+relationships.
Generalizations.
-Support for the format_X_name subs in the Translator.
+Support for the format_X_name subs in the Translator and format subs for
+generating the link table name in m:n joins.
Lots more...