migrate almost all code to Generator::Role::DDL
Arthur Axel 'fREW' Schmidt [Fri, 11 Mar 2011 04:05:20 +0000 (22:05 -0600)]
lib/SQL/Translator/Generator/DDL/SQLServer.pm
lib/SQL/Translator/Generator/Role/DDL.pm
lib/SQL/Translator/Producer/SQLServer.pm

index c417844..4fc4466 100644 (file)
@@ -160,15 +160,17 @@ sub constraints {
 
 sub table {
    my ($self, $table) = @_;
+   join ( "\n", $self->table_comments($table), '' ) .
    join ( "\n\n",
-      $self->table_comments($table),
       'CREATE TABLE ' . $self->quote($table->name) . " (\n".
         join( ",\n",
            map { "  $_" }
            $self->fields($table),
            $self->constraints($table),
         ) .
-        "\n);"
+        "\n);",
+        $self->unique_constraints_multiple($table),
+        $self->indices($table),
    )
 }
 
@@ -230,5 +232,14 @@ sub foreign_key_constraints {
      $schema->get_tables )
 }
 
+sub schema {
+   my ($self, $schema) = @_;
+
+   $self->header_comments .
+      $self->drop_tables($schema) .
+      join("\n\n", map $self->table($_), grep { $_->name } $schema->get_tables) .
+      "\n" . join "\n", $self->foreign_key_constraints($schema)
+}
+
 1;
 
index f7c2964..5533a01 100644 (file)
@@ -50,11 +50,11 @@ sub table_comments {
    my ($self, $table) = @_;
    if ($self->add_comments) {
       return (
-         "\n",
-         "--\n",
-         "-- Table: " . $self->quote($table->name) . "\n",
-         "--\n",
-         map "-- $_\n", $table->comments
+         "",
+         "--",
+         "-- Table: " . $self->quote($table->name) . "",
+         "--",
+         map "-- $_", $table->comments
       )
    } else {
       return ()
index 010a742..cbab3c0 100644 (file)
@@ -11,29 +11,11 @@ use SQL::Translator::Utils qw(debug header_comment);
 use SQL::Translator::Generator::DDL::SQLServer;
 
 sub produce {
-    my $translator     = shift;
-    my $future = SQL::Translator::Generator::DDL::SQLServer->new(
-      add_comments    => !$translator->no_comments,
-      add_drop_tables => $translator->add_drop_table,
-    );
-
-    my $schema         = $translator->schema;
-
-    my $output = $future->header_comments
-      . $future->drop_tables($schema);
-
-    for my $table ( grep { $_->name } $schema->get_tables ) {
-        $output .= join( "\n\n",
-            $future->table($table),
-            $future->unique_constraints_multiple($table),
-            $future->indices($table),
-        );
-    }
-
-    my @foreign_constraints = $future->foreign_key_constraints($schema);
-    $output .= join ("\n", '', @foreign_constraints) if @foreign_constraints;
-
-    return $output;
+  my $translator = shift;
+  SQL::Translator::Generator::DDL::SQLServer->new(
+    add_comments    => !$translator->no_comments,
+    add_drop_tables => $translator->add_drop_table,
+  )->schema($translator->schema)
 }
 
 1;