From: Daniel Ruoso Date: Mon, 14 Apr 2008 14:03:47 +0000 (+0000) Subject: implements options in oracle indexes X-Git-Tag: v0.11008~327 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bdd8e79a7b6abb123363ff8006bc21b0bd33d698;p=dbsrgits%2FSQL-Translator.git implements options in oracle indexes --- diff --git a/lib/SQL/Translator/Producer/Oracle.pm b/lib/SQL/Translator/Producer/Oracle.pm index 285ee12..59e499a 100644 --- a/lib/SQL/Translator/Producer/Oracle.pm +++ b/lib/SQL/Translator/Producer/Oracle.pm @@ -482,6 +482,28 @@ sub create_table { $index->fields; next unless @fields; + my @index_options; + for my $opt ( $index->options ) { + if ( ref $opt eq 'HASH' ) { + my ( $key, $value ) = each %$opt; + if ( ref $value eq 'ARRAY' ) { + push @table_options, "$key\n(\n". join ("\n", + map { " $_->[0]\t$_->[1]" } + map { [ each %$_ ] } + @$value + )."\n)"; + } + elsif ( !defined $value ) { + push @index_options, $key; + } + else { + push @index_options, "$key $value"; + } + } + } + my $index_options = @index_options + ? "\n".join("\n", @index_options) : ''; + if ( $index_type eq PRIMARY_KEY ) { $index_name = $index_name ? mk_name( $index_name ) : mk_name( $table_name, 'pk' ); @@ -494,7 +516,7 @@ sub create_table { push @index_defs, "CREATE INDEX $index_name on $table_name_ur (". join( ', ', @fields ). - ");"; + ")$index_options;"; } elsif ( $index_type eq UNIQUE ) { $index_name = $index_name ? mk_name( $index_name ) @@ -502,7 +524,7 @@ sub create_table { push @index_defs, "CREATE UNIQUE INDEX $index_name on $table_name_ur (". join( ', ', @fields ). - ");"; + ")$index_options;"; } else { warn "Unknown index type ($index_type) on table $table_name.\n" diff --git a/lib/SQL/Translator/Schema/Index.pm b/lib/SQL/Translator/Schema/Index.pm index 583a2d9..4a5237e 100644 --- a/lib/SQL/Translator/Schema/Index.pm +++ b/lib/SQL/Translator/Schema/Index.pm @@ -64,7 +64,7 @@ my %VALID_INDEX_TYPE = ( # ---------------------------------------------------------------------- __PACKAGE__->_attributes( qw/ - name type fields table + name type fields table options /); =pod diff --git a/t/52-oracle-options.t b/t/52-oracle-options.t new file mode 100644 index 0000000..d93de6e --- /dev/null +++ b/t/52-oracle-options.t @@ -0,0 +1,34 @@ +#!/usr/bin/perl +use strict; + +use FindBin qw/$Bin/; +use Test::More; +use Test::SQL::Translator; +use Test::Exception; +use Data::Dumper; +use SQL::Translator; +use SQL::Translator::Schema::Constants; + +BEGIN { + maybe_plan(3, 'SQL::Translator::Parser::YAML', + 'SQL::Translator::Producer::Oracle'); +} + +my $yamlfile = "$Bin/data/oracle/schema_with_options.yaml"; + +my $sqlt; +$sqlt = SQL::Translator->new( + show_warnings => 0, + add_drop_table => 0, +); + +my $sql_string = $sqlt->translate( + from => 'YAML', + to => 'Oracle', + filename => $yamlfile, +); + +warn $sql_string; +ok($sql_string, 'Translation successfull'); +ok($sql_string =~ /TABLESPACE\s+DATA/, 'Table options'); +ok($sql_string =~ /TABLESPACE\s+INDX/, 'Index options'); diff --git a/t/data/oracle/schema_with_options.yaml b/t/data/oracle/schema_with_options.yaml new file mode 100644 index 0000000..ac5ff60 --- /dev/null +++ b/t/data/oracle/schema_with_options.yaml @@ -0,0 +1,53 @@ +--- +schema: + procedures: {} + tables: + d_operator: + constraints: + - deferrable: 1 + expression: '' + fields: + - id_operator + match_type: '' + name: '' + on_delete: '' + on_update: '' + options: [] + reference_fields: [] + reference_table: '' + type: PRIMARY KEY + fields: + id_operator: + data_type: integer + default_value: ~ + extra: {} + is_auto_increment: 1 + is_nullable: 0 + is_primary_key: 1 + is_unique: 0 + name: id_operator + order: 58 + size: + - 0 + name: + data_type: varchar + default_value: ~ + extra: {} + is_nullable: 0 + is_primary_key: 0 + is_unique: 0 + name: name + order: 59 + size: + - 0 + indices: + - fields: + - name + name: operator_name + options: + - TABLESPACE: INDX + type: NORMAL + name: d_operator + options: + - TABLESPACE: DATA + order: 11