1 package SQL::Translator::Parser::XML::XMI::Rational;
3 # -------------------------------------------------------------------
4 # $Id: Rational.pm,v 1.4 2003-10-06 13:29:45 grommit Exp $
5 # -------------------------------------------------------------------
6 # Copyright (C) 2003 Mark Addison <mark.addison@itn.co.uk>,
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; version 2.
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 # -------------------------------------------------------------------
25 SQL::Translator::Parser::XML::XMI::Rational - Create Schema using Rational's UML
26 Data Modeling Profile.
31 use SQL::Translator::Parser::XML::XMI;
32 use SQL::Translator::Utils 'debug';
34 # Set the parg for the conversion sub then use the XMI parser
36 my ( $translator ) = @_;
37 my $pargs = $translator->parser_args;
38 $pargs->{classes2schema} = \&classes2schema;
39 return SQL::Translator::Parser::XML::XMI::parse(@_);
44 return grep {$_->{kind} ne "return"} @$params;
48 my ($schema, $classes) = @_;
50 foreach my $class (@$classes) {
51 next unless $class->{stereotype} eq "Table";
54 debug "Adding class: $class->{name}";
55 my $table = $schema->add_table( name => $class->{name} )
56 or die "Schema Error: ".$schema->error;
59 # Fields from Class attributes
61 foreach my $attr ( @{$class->{attributes}} ) {
62 next unless $attr->{stereotype} eq "Column"
63 or $attr->{stereotype} eq "PK"
64 or $attr->{stereotype} eq "FK"
65 or $attr->{stereotype} eq "PFK";
68 $attr->{stereotype} eq "PK" or $attr->{stereotype} eq "PFK"
71 name => $attr->{name},
72 data_type => $attr->{datatype},
73 is_primary_key => $ispk,
75 $data{default_value} = $attr->{initialValue}
76 if exists $attr->{initialValue};
77 $data{data_type} = $attr->{_map_taggedValues}{dataType}{dataValue}
79 $data{size} = $attr->{_map_taggedValues}{size}{dataValue};
80 $data{is_nullable}=$attr->{_map_taggedValues}{nullable}{dataValue};
82 my $field = $table->add_field( %data ) or die $schema->error;
83 $table->primary_key( $field->name ) if $data{'is_primary_key'};
87 # Constraints and indexes from Operations
89 foreach my $op ( @{$class->{operations}} ) {
90 next unless my $stereo = $op->{stereotype};
91 my @fields = map {$_->{name}} grep {$_->{kind} ne "return"} @{$op->{parameters}};
98 # Work out type and any other data
99 if ( $stereo eq "Unique" ) {
100 $data{type} = "UNIQUE";
102 elsif ( $stereo eq "PK" ) {
103 $data{type} = "PRIMARY_KEY";
105 # TODO We need to work out the ref table
106 elsif ( $stereo eq "FK" ) {
107 $data{type} = "FOREIGN_KEY";
108 _add_fkey_refs($class,$op,\%data);
111 # Add the constraint or index
113 $table->add_constraint( %data ) or die $schema->error;
115 elsif ( $stereo eq "Index" ) {
116 $data{type} = "NORMAL";
117 $table->add_index( %data ) or die $schema->error;
127 my ($class,$op,$data) = @_;
129 # Find the association ends
130 my ($end) = grep { $_->{name} eq $op->{name} } @{$class->{associationEnds}};
133 my ($refop) = grep { $_->{name} eq $end->{otherEnd}{name} }
134 @{$end->{otherEnd}{participant}{operations}};
135 return unless $refop;
137 $data->{reference_table} = $end->{otherEnd}{participant}{name};
138 $data->{reference_fields} = [ map("$_->{name}", _parameters_in($refop->{parameters})) ];
142 1; #---------------------------------------------------------------------------
151 use SQL::Translator::Parser::XML::XMI;
153 my $translator = SQL::Translator->new(
154 from => 'XML-XMI-Rational',
156 filename => 'schema.xmi',
161 print $obj->translate;
165 Translates Schema described using Rational Software's UML Data Modeling Profile.
166 Finding good information on this profile seems to be very difficult so this
167 is based on a vague white paper and notes in vendors docs!
169 Below is a summary of what this parser thinks the profile looks like.
171 B<Tables> Are classes marked with <<Table>> stereotype.
173 B<Fields> Attributes stereotyped with <<Column>> or one of the key stereotypes.
174 Additional info is added using tagged values of C<dataType>, C<size> and
175 C<nullable>. Default value is given using normal UML default value for the
178 B<Keys> Key fields are marked with <<PK>>, <<FK>> or <<PFK>>. Note that this is
179 really to make it obvious on the diagram, you must still add the constraints.
180 (This parser will also automatically add the constraint for single field pkeys
181 for attributes marked with PK but I think this is out of spec.)
183 B<Constraints> Stereotyped operations, with the names of the parameters
184 indicating which fields it applies to. Can use <<PK>>, <<FK>>, <<Unique>> or
187 B<Relationships> You can model the relationships in the diagram and have the
188 translator add the foreign key constraints for you. The forign keys are defined
189 as <<FK>> operations as show above. To show which table they point to join the
190 class to the taget classwith an association where the role names are the names
191 of the constraints to join.
195 +------------------------------------------------------+
198 +------------------------------------------------------+
199 | <<PK>> fooID { dataType=INT size=10 nullable=0 } |
200 | <<Column>> name { dataType=VARCHAR size=255 } |
201 | <<Column>> description { dataType=TEXT } |
202 +------------------------------------------------------+
203 | <<PK>> pkcon( fooID ) |
204 | <<Unique>> con2( name ) |
205 +------------------------------------------------------+
214 +------------------------------------------------------+
217 +------------------------------------------------------+
218 | <<PK>> barID { dataType=INT size=10 nullable=0 } |
219 | <<FK>> fooID { dataType=INT size=10 nullable=0 } |
220 | <<Column>> name { dataType=VARCHAR size=255 } |
221 +------------------------------------------------------+
222 | <<PK>> pkcon( barID ) |
223 | <<FK>> fkcon( fooID ) |
224 +------------------------------------------------------+
227 fooID INT(10) NOT NULL,
235 barID INT(10) NOT NULL,
236 fooID INT(10) NOT NULL,
239 FOREIGN KEY fkcon (fooID) REFERENCES Foo (fooID)
248 The Rational profile also defines ways to model stuff above tables such as the
253 Mark D. Addison E<lt>mark.addison@itn.co.ukE<gt>.
257 perl(1), SQL::Translator::Parser::XML::XMI