Factor out calling of normal diff-production functions
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Producer / PostgreSQL.pm
index 551f9b1..7fd7159 100644 (file)
@@ -21,15 +21,18 @@ Does not yet support PostGIS Views.
 
 use strict;
 use warnings;
-use vars qw[ $DEBUG $WARN $VERSION ];
-$VERSION = '1.59';
+our ( $DEBUG, $WARN );
+our $VERSION = '1.59';
 $DEBUG = 0 unless defined $DEBUG;
 
 use base qw(SQL::Translator::Producer);
 use SQL::Translator::Schema::Constants;
-use SQL::Translator::Utils qw(debug header_comment parse_dbms_version);
+use SQL::Translator::Utils qw(debug header_comment parse_dbms_version batch_alter_table_statements);
+use SQL::Translator::Generator::DDL::PostgreSQL;
 use Data::Dumper;
 
+my $generator = SQL::Translator::Generator::DDL::PostgreSQL->new;
+
 my ( %translate, %index_name );
 my $max_id_length;
 
@@ -170,6 +173,7 @@ sub produce {
 
     my $qt = $translator->quote_table_names ? q{"} : q{};
     my $qf = $translator->quote_field_names ? q{"} : q{};
+    $generator->quote_chars([$qt]);
 
     my @output;
     push @output, header_comment unless ($no_comments);
@@ -201,6 +205,13 @@ sub produce {
       });
     }
 
+    for my $trigger ( $schema->get_triggers ) {
+      push @table_defs, create_trigger( $trigger, {
+          add_drop_trigger => $add_drop_table,
+          no_comments      => $no_comments,
+        });
+    }
+
     push @output, map { "$_;\n\n" } values %type_defs;
     push @output, map { "$_;\n\n" } @table_defs;
     if ( @fks ) {
@@ -276,6 +287,7 @@ sub create_table
 
     my $qt = $options->{quote_table_names} || '';
     my $qf = $options->{quote_field_names} || '';
+    $generator->quote_chars([$qt]);
     my $no_comments = $options->{no_comments} || 0;
     my $add_drop_table = $options->{add_drop_table} || 0;
     my $postgres_version = $options->{postgres_version} || 0;
@@ -283,10 +295,9 @@ sub create_table
 
     my $table_name = $table->name or next;
     my ( $fql_tbl_name ) = ( $table_name =~ s/\W(.*)$// ) ? $1 : q{};
-    my $table_name_ur = $qt ? $table_name
+    my $table_name_ur = $qt ? join('.', $table_name, $fql_tbl_name)
         : $fql_tbl_name ? join('.', $table_name, $fql_tbl_name)
         : $table_name;
-    $table->name($table_name_ur);
 
 # print STDERR "$table_name table_name\n";
     my ( @comments, @field_defs, @sequence_defs, @constraint_defs, @fks );
@@ -347,20 +358,20 @@ sub create_table
 
     my $temporary = "";
 
-    if(exists $table->{extra}{temporary}) {
-        $temporary = $table->{extra}{temporary} ? "TEMPORARY " : "";
+    if(exists $table->extra->{temporary}) {
+        $temporary = $table->extra->{temporary} ? "TEMPORARY " : "";
     }
 
     my $create_statement;
     $create_statement = join("\n", @comments);
     if ($add_drop_table) {
         if ($postgres_version >= 8.002) {
-            $create_statement .= qq[DROP TABLE IF EXISTS $qt$table_name_ur$qt CASCADE;\n];
+            $create_statement .= 'DROP TABLE IF EXISTS ' . $generator->quote($table_name_ur) . " CASCADE;\n";
         } else {
-            $create_statement .= qq[DROP TABLE $qt$table_name_ur$qt CASCADE;\n];
+            $create_statement .= 'DROP TABLE ' . $generator->quote($table_name_ur) . " CASCADE;\n";
         }
     }
-    $create_statement .= qq[CREATE ${temporary}TABLE $qt$table_name_ur$qt (\n].
+    $create_statement .= "CREATE ${temporary}TABLE " . $generator->quote($table_name_ur) . " (\n" .
                             join( ",\n", map { "  $_" } @field_defs, @constraint_defs ).
                             "\n)"
                             ;
@@ -386,6 +397,7 @@ sub create_view {
     my ($view, $options) = @_;
     my $qt = $options->{quote_table_names} || '';
     my $qf = $options->{quote_field_names} || '';
+    $generator->quote_chars([$qt]);
     my $postgres_version = $options->{postgres_version} || 0;
     my $add_drop_view = $options->{add_drop_view};
 
@@ -393,23 +405,23 @@ sub create_view {
     debug("PKG: Looking at view '${view_name}'\n");
 
     my $create = '';
-    $create .= "--\n-- View: ${qt}${view_name}${qt}\n--\n"
+    $create .= "--\n-- View: " . $generator->quote($view_name) . "\n--\n"
         unless $options->{no_comments};
     if ($add_drop_view) {
         if ($postgres_version >= 8.002) {
-            $create .= "DROP VIEW IF EXISTS ${qt}${view_name}${qt};\n";
+            $create .= "DROP VIEW IF EXISTS " . $generator->quote($view_name) . ";\n";
         } else {
-            $create .= "DROP VIEW ${qt}${view_name}${qt};\n";
+            $create .= "DROP VIEW " . $generator->quote($view_name) . ";\n";
         }
     }
     $create .= 'CREATE';
 
     my $extra = $view->extra;
     $create .= " TEMPORARY" if exists($extra->{temporary}) && $extra->{temporary};
-    $create .= " VIEW ${qt}${view_name}${qt}";
+    $create .= " VIEW " . $generator->quote($view_name);
 
     if ( my @fields = $view->fields ) {
-        my $field_list = join ', ', map { "${qf}${_}${qf}" } @fields;
+        my $field_list = join ', ', map { $generator->quote($_) } @fields;
         $create .= " ( ${field_list} )";
     }
 
@@ -434,6 +446,7 @@ sub create_view {
 
         my $qt = $options->{quote_table_names} || '';
         my $qf = $options->{quote_field_names} || '';
+        $generator->quote_chars([$qt]);
         my $table_name = $field->table->name;
         my $constraint_defs = $options->{constraint_defs} || [];
         my $postgres_version = $options->{postgres_version} || 0;
@@ -445,7 +458,7 @@ sub create_view {
             ? "-- " . $field->comments . "\n  "
             : '';
 
-        my $field_def     = $field_comments.qq[$qf$field_name$qf];
+        my $field_def     = $field_comments . $generator->quote($field_name);
 
         #
         # Datatype
@@ -515,20 +528,20 @@ sub create_geometry_constraints{
    my @constraints;
    push @constraints, SQL::Translator::Schema::Constraint->new(
                      name       => "enforce_dims_".$field->name,
-                     expression => "(ST_NDims($field) = ".$field->{extra}{dimensions}.")",
+                     expression => "(ST_NDims($field) = ".$field->extra->{dimensions}.")",
                      table       => $field->table,
                      type       => CHECK_C,
                   );
 
    push @constraints, SQL::Translator::Schema::Constraint->new(
                      name       => "enforce_srid_".$field->name,
-                     expression => "(ST_SRID($field) = ".$field->{extra}{srid}.")",
+                     expression => "(ST_SRID($field) = ".$field->extra->{srid}.")",
                      table       => $field->table,
                      type       => CHECK_C,
                   );
    push @constraints, SQL::Translator::Schema::Constraint->new(
                      name       => "enforce_geotype_".$field->name,
-                     expression => "(GeometryType($field) = '".$field->{extra}{geometry_type}."'::text OR $field IS NULL)",
+                     expression => "(GeometryType($field) = '".$field->extra->{geometry_type}."'::text OR $field IS NULL)",
                      table       => $field->table,
                      type       => CHECK_C,
                   );
@@ -542,6 +555,7 @@ sub create_index
 
     my $qt = $options->{quote_table_names} ||'';
     my $qf = $options->{quote_field_names} ||'';
+    $generator->quote_chars([$qt]);
     my $table_name = $index->table->name;
 
     my ($index_def, @constraint_defs);
@@ -552,10 +566,10 @@ sub create_index
 
     my $type = $index->type || NORMAL;
     my @fields     =  $index->fields;
-    next unless @fields;
+    return unless @fields;
 
-    my $def_start = qq[CONSTRAINT ${qf}$name${qf} ];
-    my $field_names = '(' . join(", ", (map { $_ =~ /\(.*\)/ ? $_ : ($qf . $_ . $qf ) } @fields)) . ')';
+    my $def_start = 'CONSTRAINT ' . $generator->quote($name) . ' ';
+    my $field_names = '(' . join(", ", (map { $_ =~ /\(.*\)/ ? $_ : ( $generator->quote($_) ) } @fields)) . ')';
     if ( $type eq PRIMARY_KEY ) {
         push @constraint_defs, "${def_start}PRIMARY KEY ".$field_names;
     }
@@ -564,7 +578,7 @@ sub create_index
     }
     elsif ( $type eq NORMAL ) {
         $index_def =
-            "CREATE INDEX ${qf}${name}${qf} on ${qt}${table_name}${qt} ".$field_names
+            'CREATE INDEX ' . $generator->quote($name) . ' on ' . $generator->quote($table_name) . ' ' . $field_names
             ;
     }
     else {
@@ -581,6 +595,7 @@ sub create_constraint
 
     my $qf = $options->{quote_field_names} ||'';
     my $qt = $options->{quote_table_names} ||'';
+    $generator->quote_chars([$qt]);
     my $table_name = $c->table->name;
     my (@constraint_defs, @fks);
 
@@ -591,8 +606,8 @@ sub create_constraint
     my @rfields = grep { defined } $c->reference_fields;
 
     next if !@fields && $c->type ne CHECK_C;
-    my $def_start = $name ? qq[CONSTRAINT ${qf}$name${qf} ] : '';
-    my $field_names = '(' . join(", ", (map { $_ =~ /\(.*\)/ ? $_ : ($qf . $_ . $qf ) } @fields)) . ')';
+    my $def_start = $name ? 'CONSTRAINT ' . $generator->quote($name) . ' ' : '';
+    my $field_names = '(' . join(", ", (map { $_ =~ /\(.*\)/ ? $_ : ( $generator->quote($_) ) } @fields)) . ')';
     if ( $c->type eq PRIMARY_KEY ) {
         push @constraint_defs, "${def_start}PRIMARY KEY ".$field_names;
     }
@@ -604,11 +619,11 @@ sub create_constraint
         push @constraint_defs, "${def_start}CHECK ($expression)";
     }
     elsif ( $c->type eq FOREIGN_KEY ) {
-        my $def .= "ALTER TABLE $qt$table_name$qt ADD ${def_start}FOREIGN KEY $field_names"
-            . "\n  REFERENCES " . $qt . $c->reference_table . $qt;
+        my $def .= "ALTER TABLE " . $generator->quote($table_name) . " ADD ${def_start}FOREIGN KEY $field_names"
+            . "\n  REFERENCES " . $generator->quote($c->reference_table);
 
         if ( @rfields ) {
-            $def .= ' ('.$qf . join( $qf.', '.$qf, @rfields ) . $qf.')';
+            $def .= ' (' . join( ', ', map { $generator->quote($_) } @rfields ) . ')';
         }
 
         if ( $c->match_type ) {
@@ -634,6 +649,30 @@ sub create_constraint
     return \@constraint_defs, \@fks;
 }
 
+sub create_trigger {
+  my ($trigger,$options) = @_;
+
+  my @statements;
+
+  push @statements, sprintf( 'DROP TRIGGER IF EXISTS %s', $trigger->name )
+    if $options->{add_drop_trigger};
+
+  my $scope = $trigger->scope || '';
+  $scope = " FOR EACH $scope" if $scope;
+
+  push @statements, sprintf(
+    'CREATE TRIGGER %s %s %s ON %s%s %s',
+    $trigger->name,
+    $trigger->perform_action_when,
+    join( ' OR ', @{ $trigger->database_events } ),
+    $trigger->on_table,
+    $scope,
+    $trigger->action,
+  );
+
+  return @statements;
+}
+
 sub convert_datatype
 {
     my ($field) = @_;
@@ -647,7 +686,7 @@ sub convert_datatype
 #        $len = ($len < length($_)) ? length($_) : $len for (@$list);
 #        my $chk_name = mk_name( $table_name.'_'.$field_name, 'chk' );
 #        push @$constraint_defs,
-#        qq[CONSTRAINT "$chk_name" CHECK ($qf$field_name$qf ].
+#        'CONSTRAINT "$chk_name" CHECK (' . $generator->quote(field_name) .
 #           qq[IN ($commalist))];
         $data_type = 'character varying';
     }
@@ -716,7 +755,7 @@ sub convert_datatype
     # Geography
     #
     if($data_type eq 'geography'){
-        $data_type .= '('.$field->{extra}{geography_type}.','. $field->{extra}{srid} .')'
+        $data_type .= '('.$field->extra->{geography_type}.','. $field->extra->{srid} .')'
     }
 
     return $data_type;
@@ -792,7 +831,7 @@ sub alter_field
                        $to_field->name)
         if ( !defined $new_default && defined $old_default );
 
-    # add geometry column and contraints
+    # add geometry column and constraints
     push @out, add_geometry_column($to_field) if is_geometry($to_field);
     push @out, add_geometry_constraints($to_field) if is_geometry($to_field);
 
@@ -820,10 +859,11 @@ sub drop_field
 
     my $qt = $options->{quote_table_names} ||'';
     my $qf = $options->{quote_field_names} ||'';
+    $generator->quote_chars([$qt]);
 
     my $out = sprintf('ALTER TABLE %s DROP COLUMN %s',
-                      $qt . $old_field->table->name . $qt,
-                      $qf . $old_field->name . $qf);
+                      $generator->quote($old_field->table->name),
+                      $generator->quote($old_field->name));
         $out .= "\n".drop_geometry_column($old_field) if is_geometry($old_field);
     return $out;
 }
@@ -836,9 +876,9 @@ sub add_geometry_column{
                   $field->table->schema->name,
                   $options->{table} ? $options->{table} : $field->table->name,
                   $field->name,
-                  $field->{extra}{dimensions},
-                  $field->{extra}{srid},
-                  $field->{extra}{geometry_type});
+                  $field->extra->{dimensions},
+                  $field->extra->{srid},
+                  $field->extra->{geometry_type});
     return $out;
 }
 
@@ -876,8 +916,9 @@ sub drop_geometry_constraints{
 sub alter_table {
     my ($to_table, $options) = @_;
     my $qt = $options->{quote_table_names} || '';
+    $generator->quote_chars([$qt]);
     my $out = sprintf('ALTER TABLE %s %s',
-                      $qt . $to_table->name . $qt,
+                      $generator->quote($to_table->name),
                       $options->{alter_table_action});
     $out .= "\n".$options->{geometry_changes} if $options->{geometry_changes};
     return $out;
@@ -886,7 +927,8 @@ sub alter_table {
 sub rename_table {
     my ($old_table, $new_table, $options) = @_;
     my $qt = $options->{quote_table_names} || '';
-    $options->{alter_table_action} = "RENAME TO $qt$new_table$qt";
+    $generator->quote_chars([$qt]);
+    $options->{alter_table_action} = "RENAME TO " . $generator->quote($new_table);
 
    my @geometry_changes;
    push @geometry_changes, map { drop_geometry_column($_); } grep { is_geometry($_) } $old_table->get_fields;
@@ -901,6 +943,7 @@ sub alter_create_index {
     my ($index, $options) = @_;
     my $qt = $options->{quote_table_names} || '';
     my $qf = $options->{quote_field_names} || '';
+    $generator->quote_chars([$qt]);
     my ($idef, $constraints) = create_index($index, {
         quote_field_names => $qf,
         quote_table_names => $qt,
@@ -908,7 +951,7 @@ sub alter_create_index {
     });
     return $index->type eq NORMAL ? $idef
         : sprintf('ALTER TABLE %s ADD %s',
-              $qt . $index->table->name . $qt,
+              $generator->quote($index->table->name),
               join(q{}, @$constraints)
           );
 }
@@ -923,24 +966,34 @@ sub alter_drop_constraint {
     my ($c, $options) = @_;
     my $qt = $options->{quote_table_names} || '';
     my $qc = $options->{quote_field_names} || '';
+    $generator->quote_chars([$qt]);
+
+    # attention: Postgres  has a very special naming structure for naming
+    # foreign keys and primary keys.  It names them using the name of the
+    # table as prefix and fkey or pkey as suffix, concatenated by an underscore
+    my $c_name;
+    if( $c->name ) {
+        # Already has a name, just quote it
+        $c_name = $qc . $c->name . $qc;
+    } elsif ( $c->type eq FOREIGN_KEY ) {
+        # Doesn't have a name, and is foreign key, append '_fkey'
+        $c_name = $qc . $c->table->name . '_' .
+                    ($c->fields)[0] . '_fkey' . $qc;
+    } elsif ( $c->type eq PRIMARY_KEY ) {
+        # Doesn't have a name, and is primary key, append '_pkey'
+        $c_name = $qc . $c->table->name . '_pkey' . $qc;
+    }
 
     return sprintf(
         'ALTER TABLE %s DROP CONSTRAINT %s',
-        $qt . $c->table->name . $qt,
-        # attention: Postgres  has a very special naming structure
-        # for naming foreign keys, it names them uses the name of
-        # the table as prefix and fkey as suffix, concatenated by a underscore
-        $c->type eq FOREIGN_KEY
-            ? $c->name
-                ? $qc . $c->name . $qc
-                : $qc . $c->table->name . '_' . ($c->fields)[0] . '_fkey' . $qc
-            : $qc . $c->name . $qc
+        $qt . $c->table->name . $qt, $c_name
     );
 }
 
 sub alter_create_constraint {
     my ($index, $options) = @_;
     my $qt = $options->{quote_table_names} || '';
+    $generator->quote_chars([$qt]);
     my ($defs, $fks) = create_constraint(@_);
 
     # return if there are no constraint definitions so we don't run
@@ -949,7 +1002,7 @@ sub alter_create_constraint {
 
     return unless(@{$defs} || @{$fks});
     return $index->type eq FOREIGN_KEY ? join(q{}, @{$fks})
-        : join( ' ', 'ALTER TABLE', $qt.$index->table->name.$qt,
+        : join( ' ', 'ALTER TABLE', $generator->quote($index->table->name),
               'ADD', join(q{}, @{$defs}, @{$fks})
           );
 }
@@ -957,7 +1010,8 @@ sub alter_create_constraint {
 sub drop_table {
     my ($table, $options) = @_;
     my $qt = $options->{quote_table_names} || '';
-    my $out = "DROP TABLE $qt$table$qt CASCADE";
+    $generator->quote_chars([$qt]);
+    my $out = "DROP TABLE " . $generator->quote($table) . " CASCADE";
 
     my @geometry_drops = map { drop_geometry_column($_); } grep { is_geometry($_) } $table->get_fields;
 
@@ -965,6 +1019,47 @@ sub drop_table {
     return $out;
 }
 
+sub batch_alter_table {
+  my ( $table, $diff_hash, $options ) = @_;
+  my $qt = $options->{quote_table_names} || '';
+  $generator->quote_chars([$qt]);
+
+  # as long as we're not renaming the table we don't need to be here
+  if ( @{$diff_hash->{rename_table}} == 0 ) {
+    return batch_alter_table_statements($diff_hash, $options);
+  }
+
+  # first we need to perform drops which are on old table
+  my @sql = batch_alter_table_statements($diff_hash, $options, qw(
+    alter_drop_constraint
+    alter_drop_index
+    drop_field
+  ));
+
+  # next comes the rename_table
+  my $old_table = $diff_hash->{rename_table}[0][0];
+  push @sql, rename_table( $old_table, $table, $options );
+
+  # for alter_field (and so also rename_field) we need to make sure old
+  # field has table name set to new table otherwise calling alter_field dies
+  $diff_hash->{alter_field} =
+    [map { $_->[0]->table($table) && $_ } @{$diff_hash->{alter_field}}];
+  $diff_hash->{rename_field} =
+    [map { $_->[0]->table($table) && $_ } @{$diff_hash->{rename_field}}];
+
+  # now add everything else
+  push @sql, batch_alter_table_statements($diff_hash, $options, qw(
+    add_field
+    alter_field
+    rename_field
+    alter_create_index
+    alter_create_constraint
+    alter_table
+  ));
+
+  return @sql;
+}
+
 1;
 
 # -------------------------------------------------------------------