Removed some unneeded, commented out code.
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Parser / XML / XMI / Rational.pm
CommitLineData
b2a00f50 1package SQL::Translator::Parser::XML::XMI::Rational;
2
3# -------------------------------------------------------------------
182bc70c 4# $Id: Rational.pm,v 1.4 2003-10-06 13:29:45 grommit Exp $
b2a00f50 5# -------------------------------------------------------------------
6# Copyright (C) 2003 Mark Addison <mark.addison@itn.co.uk>,
7#
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.
11#
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.
16#
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
20# 02111-1307 USA
21# -------------------------------------------------------------------
22
23=head1 NAME
24
25SQL::Translator::Parser::XML::XMI::Rational - Create Schema using Rational's UML
26Data Modeling Profile.
27
28=cut
29
30use strict;
31use SQL::Translator::Parser::XML::XMI;
32use SQL::Translator::Utils 'debug';
33
34# Set the parg for the conversion sub then use the XMI parser
35sub parse {
36 my ( $translator ) = @_;
37 my $pargs = $translator->parser_args;
38 $pargs->{classes2schema} = \&classes2schema;
39 return SQL::Translator::Parser::XML::XMI::parse(@_);
40}
41
a8ae1e5c 42sub _parameters_in {
43 my $params = shift;
44 return grep {$_->{kind} ne "return"} @$params;
45}
46
b2a00f50 47sub classes2schema {
48 my ($schema, $classes) = @_;
49
50 foreach my $class (@$classes) {
51 next unless $class->{stereotype} eq "Table";
52
53 # Add the table
54 debug "Adding class: $class->{name}";
55 my $table = $schema->add_table( name => $class->{name} )
56 or die "Schema Error: ".$schema->error;
57
58 #
59 # Fields from Class attributes
60 #
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";
66
67 my $ispk =
68 $attr->{stereotype} eq "PK" or $attr->{stereotype} eq "PFK"
69 ? 1 : 0;
70 my %data = (
71 name => $attr->{name},
72 data_type => $attr->{datatype},
73 is_primary_key => $ispk,
74 );
75 $data{default_value} = $attr->{initialValue}
76 if exists $attr->{initialValue};
77 $data{data_type} = $attr->{_map_taggedValues}{dataType}{dataValue}
78 || $attr->{datatype};
79 $data{size} = $attr->{_map_taggedValues}{size}{dataValue};
80 $data{is_nullable}=$attr->{_map_taggedValues}{nullable}{dataValue};
81
82 my $field = $table->add_field( %data ) or die $schema->error;
83 $table->primary_key( $field->name ) if $data{'is_primary_key'};
84 }
85
86 #
87 # Constraints and indexes from Operations
88 #
89 foreach my $op ( @{$class->{operations}} ) {
90 next unless my $stereo = $op->{stereotype};
91 my @fields = map {$_->{name}} grep {$_->{kind} ne "return"} @{$op->{parameters}};
92 my %data = (
93 name => $op->{name},
94 type => "",
95 fields => [@fields],
96 );
97
98 # Work out type and any other data
99 if ( $stereo eq "Unique" ) {
100 $data{type} = "UNIQUE";
101 }
102 elsif ( $stereo eq "PK" ) {
103 $data{type} = "PRIMARY_KEY";
104 }
105 # TODO We need to work out the ref table
a8ae1e5c 106 elsif ( $stereo eq "FK" ) {
107 $data{type} = "FOREIGN_KEY";
108 _add_fkey_refs($class,$op,\%data);
109 }
b2a00f50 110
111 # Add the constraint or index
112 if ( $data{type} ) {
113 $table->add_constraint( %data ) or die $schema->error;
114 }
115 elsif ( $stereo eq "Index" ) {
116 $data{type} = "NORMAL";
117 $table->add_index( %data ) or die $schema->error;
118 }
119
b2a00f50 120 } # Ops loop
121
122 } # Classes loop
123}
124
a8ae1e5c 125use Data::Dumper;
126sub _add_fkey_refs {
127 my ($class,$op,$data) = @_;
128
129 # Find the association ends
130 my ($end) = grep { $_->{name} eq $op->{name} } @{$class->{associationEnds}};
a8ae1e5c 131 return unless $end;
132 # Find the fkey op
133 my ($refop) = grep { $_->{name} eq $end->{otherEnd}{name} }
134 @{$end->{otherEnd}{participant}{operations}};
135 return unless $refop;
136
137 $data->{reference_table} = $end->{otherEnd}{participant}{name};
138 $data->{reference_fields} = [ map("$_->{name}", _parameters_in($refop->{parameters})) ];
139 return $data;
140}
141
b2a00f50 1421; #---------------------------------------------------------------------------
143
144__END__
145
146=pod
147
148=head1 SYNOPSIS
149
150 use SQL::Translator;
151 use SQL::Translator::Parser::XML::XMI;
152
153 my $translator = SQL::Translator->new(
154 from => 'XML-XMI-Rational',
155 to => 'MySQL',
156 filename => 'schema.xmi',
157 show_warnings => 1,
158 add_drop_table => 1,
159 );
160
161 print $obj->translate;
162
163=head1 DESCRIPTION
164
165Translates Schema described using Rational Software's UML Data Modeling Profile.
166Finding good information on this profile seems to be very difficult so this
167is based on a vague white paper and notes in vendors docs!
168
169Below is a summary of what this parser thinks the profile looks like.
170
171B<Tables> Are classes marked with <<Table>> stereotype.
172
173B<Fields> Attributes stereotyped with <<Column>> or one of the key stereotypes.
174Additional info is added using tagged values of C<dataType>, C<size> and
175C<nullable>. Default value is given using normal UML default value for the
176attribute.
177
178B<Keys> Key fields are marked with <<PK>>, <<FK>> or <<PFK>>. Note that this is
179really 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
181for attributes marked with PK but I think this is out of spec.)
182
183B<Constraints> Stereotyped operations, with the names of the parameters
184indicating which fields it applies to. Can use <<PK>>, <<FK>>, <<Unique>> or
185<<Index>>.
186
a8ae1e5c 187B<Relationships> You can model the relationships in the diagram and have the
188translator add the foreign key constraints for you. The forign keys are defined
189as <<FK>> operations as show above. To show which table they point to join the
190class to the taget classwith an association where the role names are the names
191of the constraints to join.
192
b2a00f50 193e.g.
194
195 +------------------------------------------------------+
196 | <<Table>> |
197 | Foo |
198 +------------------------------------------------------+
199 | <<PK>> fooID { dataType=INT size=10 nullable=0 } |
200 | <<Column>> name { dataType=VARCHAR size=255 } |
201 | <<Column>> description { dataType=TEXT } |
202 +------------------------------------------------------+
a8ae1e5c 203 | <<PK>> pkcon( fooID ) |
b2a00f50 204 | <<Unique>> con2( name ) |
205 +------------------------------------------------------+
a8ae1e5c 206 |
207 | pkcon
208 |
209 |
210 |
211 |
212 | fkcon
213 |
214 +------------------------------------------------------+
215 | <<Table>> |
216 | Bar |
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 +------------------------------------------------------+
b2a00f50 225
226 CREATE TABLE Foo (
227 fooID INT(10) NOT NULL,
228 name VARCHAR(255),
229 description TEXT,
230 PRIMARY KEY (fooID),
a8ae1e5c 231 UNIQUE con2 (name)
232 );
233
234 CREATE TABLE Bar (
235 barID INT(10) NOT NULL,
236 fooID INT(10) NOT NULL,
237 name VARCHAR(255),
238 PRIMARY KEY (fooID),
239 FOREIGN KEY fkcon (fooID) REFERENCES Foo (fooID)
b2a00f50 240 );
241
242=head1 ARGS
243
244=head1 BUGS
245
246=head1 TODO
247
a8ae1e5c 248The Rational profile also defines ways to model stuff above tables such as the
249actuall db.
b2a00f50 250
251=head1 AUTHOR
252
253Mark D. Addison E<lt>mark.addison@itn.co.ukE<gt>.
254
255=head1 SEE ALSO
256
257perl(1), SQL::Translator::Parser::XML::XMI
258
259=cut