1 package SQL::Translator::Producer::Oracle;
3 #-----------------------------------------------------
4 # $Id: Oracle.pm,v 1.1.1.1 2002-03-01 02:26:25 kycl4rk Exp $
6 # File : SQL/Translator/Producer/Oracle.pm
7 # Programmer : Ken Y. Clark, kclark@logsoft.com
9 # Purpose : Oracle SQL producer
10 #-----------------------------------------------------
13 use SQL::Translator::Producer;
14 use vars qw( $VERSION );
15 $VERSION = (qw$Revision: 1.1.1.1 $)[-1];
17 use base qw[ SQL::Translator::Producer ];
19 my $max_identifier_length = 30;
20 my %used_identifiers = ();
28 mediumint => 'number',
34 varchar => 'varchar2',
59 my ( $self, $data ) = @_;
61 #print "got ", scalar keys %$data, " tables:\n";
62 #print join(', ', keys %$data), "\n";
63 #print Dumper( $data );
68 my $output = $self->header;
71 # Print create for each table
73 my ( $index_i, $trigger_i ) = ( 1, 1 );
74 for my $table_name ( sort keys %$data ) {
75 check_identifier( $table_name );
77 my ( @comments, @field_decs, @trigger_decs );
79 my $table = $data->{ $table_name };
80 push @comments, "#\n# Table: $table_name\n#";
84 sort { $a->[0] <=> $b->[0] }
85 map { [ $_->{'order'}, $_ ] }
86 values %{ $table->{'fields'} }
91 my $field_str = check_identifier( $field->{'name'} );
96 my $data_type = $field->{'data_type'};
97 $data_type = defined $translate{ $data_type } ?
98 $translate{ $data_type } :
99 die "Unknown datatype: $data_type\n";
100 $field_str .= ' '.$data_type;
101 $field_str .= '('.$field->{'size'}.')' if defined $field->{'size'};
106 if ( $field->{'default'} ) {
107 # next if $field->{'default'} eq 'NULL';
108 $field_str .= sprintf(
110 $field->{'default'} =~ m/null/i ? 'NULL' :
111 "'".$field->{'default'}."'"
116 # Not null constraint
118 unless ( $field->{'null'} ) {
119 my $constraint_name = make_identifier($field->{'name'}, '_nn');
120 $field_str .= ' CONSTRAINT ' . $constraint_name . ' NOT NULL';
126 if ( $field->{'is_auto_inc'} ) {
127 my $trigger_no = $trigger_i++;
128 my $trigger_sequence =
129 join( '_', 'seq' , $field->{'name'}, $trigger_no );
131 join( '_', 'autoinc', $field->{'name'}, $trigger_no );
134 'CREATE SEQUENCE ' . $trigger_sequence . ";" .
135 'CREATE OR REPLACE TRIGGER ' . $trigger_name .
136 ' BEFORE INSERT ON ' . $table_name .
137 ' FOR EACH ROW WHEN (new.' . $field->{'name'} . ' is null) ' .
139 ' SELECT ' . $trigger_sequence . '.nextval ' .
140 ' INTO :new.' . $field->{'name'} .
142 ' END ' . $trigger_name . ";/"
146 push @field_decs, $field_str;
153 for my $index ( @{ $table->{'indeces'} } ) {
154 my $index_name = $index->{'name'} || '';
155 my $index_type = $index->{'type'} || 'normal';
156 my @fields = @{ $index->{'fields'} } or next;
158 if ( $index_type eq 'primary_key' ) {
159 if ( !$index_name ) {
160 $index_name = make_identifier( $table_name, 'i_', '_pk' );
162 elsif ( $index_name !~ m/^i_/ ) {
163 $index_name = make_identifier( $table_name, 'i_' );
165 elsif ( $index_name !~ m/_pk$/ ) {
166 $index_name = make_identifier( $table_name, '_pk' );
169 $index_name = make_identifier( $index_name );
172 push @field_decs, 'CONSTRAINT ' . $index_name . ' PRIMARY KEY ' .
173 '(' . join( ', ', @fields ) . ')';
176 elsif ( $index_type eq 'unique' ) {
177 if ( !$index_name ) {
178 $index_name = make_identifier( join( '_', @fields ), 'u_' );
180 elsif ( $index_name !~ m/^u_/ ) {
181 $index_name = make_identifier( $index_name, 'u_' );
184 $index_name = make_identifier( $index_name );
187 push @field_decs, 'CONSTRAINT ' . $index_name . ' UNIQUE ' .
188 '(' . join( ', ', @fields ) . ')';
191 elsif ( $index_type eq 'normal' ) {
192 if ( !$index_name ) {
194 make_identifier($table_name, 'i_', '_'.$index_i++ );
196 elsif ( $index_name !~ m/^i_/ ) {
197 $index_name = make_identifier( $index_name, 'i_' );
200 $index_name = make_identifier( $index_name );
203 push @index_decs, "CREATE INDEX $index_name on $table_name (".
204 join( ', ', @{ $index->{'fields'} } ).
210 warn "On table $table_name, unknown index type: $index_type\n";
214 my $create_statement = "CREATE TABLE $table_name (\n".
215 join( ",\n", map { " $_" } @field_decs ).
219 $output .= join( "\n\n",
228 $output .= "#\n# End\n#\n";
232 # Used to make index names
234 sub make_identifier {
235 my ( $identifier, @mutations ) = @_;
236 my $length_of_mutations;
237 for my $mutation ( @mutations ) {
238 $length_of_mutations += length( $mutation );
242 length( $identifier ) + $length_of_mutations >
243 $max_identifier_length
245 $identifier = substr(
248 $max_identifier_length - $length_of_mutations
252 for my $mutation ( @mutations ) {
253 if ( $mutation =~ m/.+_$/ ) {
254 $identifier = $mutation.$identifier;
256 elsif ( $mutation =~ m/^_.+/ ) {
257 $identifier = $identifier.$mutation;
261 if ( $used_identifiers{ $identifier } ) {
263 if ( $identifier =~ m/_(\d+)$/ ) {
265 $identifier = substr(
268 length( $identifier ) - ( length( $index ) + 1 )
272 return make_identifier( $identifier, '_'.$index );
275 $used_identifiers{ $identifier } = 1;
281 # Checks to see if an identifier is not too long
283 sub check_identifier {
284 my $identifier = shift;
285 die "Identifier '$identifier' is too long, unrecoverable error.\n"
286 if length( $identifier ) > $max_identifier_length;
292 #-----------------------------------------------------
293 # All bad art is the result of good intentions.
295 #-----------------------------------------------------
299 SQL::Translator::Producer::Oracle - Oracle SQL producer
303 use SQL::Translator::Producer::Oracle;
311 Ken Y. Clark, kclark@logsoft.com