Add support for materialized views in Oracle producer
Alexander Hartmaier [Thu, 7 Apr 2016 14:50:23 +0000 (16:50 +0200)]
Changes
lib/SQL/Translator/Producer/Oracle.pm
t/55-oracle-producer.t

diff --git a/Changes b/Changes
index e70308f..b022e5b 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,7 @@
 Changes for SQL::Translator
 
  * Add support for parsing PostgreSQL dollar-quoted strings
+ * Add support for materialized views in Oracle producer
  * switched JSON backend from JSON.pm to JSON::MaybeXS
 
 0.11024 2018-01-09
index d3f7a12..929fc8e 100644 (file)
@@ -732,13 +732,23 @@ sub create_view {
     my ($view, $options) = @_;
     my $qt = $options->{quote_table_names};
     my $view_name = quote($view->name,$qt);
+    my $extra = $view->extra;
+
+    my $view_type = 'VIEW';
+    my $view_options = '';
+    if ( my $materialized = $extra->{materialized} ) {
+        $view_type = 'MATERIALIZED VIEW';
+        $view_options .= ' '.$materialized;
+    }
 
     my @create;
-    push @create, qq[DROP VIEW $view_name]
+    push @create, qq[DROP $view_type $view_name]
         if $options->{add_drop_view};
 
-    push @create, sprintf("CREATE VIEW %s AS\n%s",
+    push @create, sprintf("CREATE %s %s%s AS\n%s",
+                      $view_type,
                       $view_name,
+                      $view_options,
                       $view->sql);
 
     return \@create;
index 8f90c31..57e25ea 100644 (file)
@@ -7,6 +7,7 @@ use Test::More;
 
 use SQL::Translator::Schema::Constants;
 use SQL::Translator::Schema::Table;
+use SQL::Translator::Schema::View;
 use SQL::Translator::Schema::Field;
 use SQL::Translator::Schema::Constraint;
 use SQL::Translator::Producer::Oracle;
@@ -78,6 +79,25 @@ use SQL::Translator::Producer::Oracle;
         ],
         'correct "CREATE CONSTRAINT" SQL'
     );
+
+    my $materialized_view = SQL::Translator::Schema::View->new(
+        name    => 'matview',
+        sql     => 'SELECT id, name FROM table3',
+        fields  => 'id, name',
+        extra   => {
+            materialized =>
+                'REFRESH START WITH SYSDATE NEXT SYSDATE + 5/1440 FORCE WITH ROWID'
+        }
+    );
+
+    my ($materialized_view_def) = SQL::Translator::Producer::Oracle::create_view($materialized_view);
+    is_deeply(
+        $materialized_view_def,
+        [   "CREATE MATERIALIZED VIEW matview REFRESH START WITH SYSDATE NEXT SYSDATE + 5/1440 FORCE WITH ROWID AS\nSELECT id, name FROM table3"
+        ],
+        'correct "CREATE MATERIALZED VIEW" SQL'
+    );
+
 }
 
 done_testing();