refactor table into more methods
Arthur Axel 'fREW' Schmidt [Fri, 11 Mar 2011 02:44:10 +0000 (20:44 -0600)]
lib/SQL/Translator/Generator/DDL/SQLServer.pm
lib/SQL/Translator/Generator/Role/DDL.pm

index 70782a2..e9433eb 100644 (file)
@@ -142,25 +142,29 @@ sub enum_constraint {
   )
 }
 
+sub constraints {
+  my ($self, $table) = @_;
+
+  (map $self->enum_constraint($_->name, { $_->extra }->{list} || []),
+     grep { 'enum' eq lc $_->data_type } $table->get_fields),
+
+  (map $self->primary_key_constraint($_),
+     grep { $_->type eq PRIMARY_KEY } $table->get_constraints),
+
+  (map $self->unique_constraint_single($_),
+     grep {
+       $_->type eq UNIQUE &&
+       !grep { $_->is_nullable } $_->fields
+     } $table->get_constraints),
+}
+
 sub table {
    my ($self, $table) = @_;
    'CREATE TABLE ' . $self->quote($table->name) . " (\n".
      join( ",\n",
         map { "  $_" }
-        # field defs
-        ( map $self->field($_), $table->get_fields ),
-        # constraint defs
-        (map $self->enum_constraint($_->name, { $_->extra }->{list} || []),
-           grep { 'enum' eq lc $_->data_type } $table->get_fields),
-
-        (map $self->primary_key_constraint($_),
-           grep { $_->type eq PRIMARY_KEY } $table->get_constraints),
-
-        (map $self->unique_constraint_single($_),
-           grep {
-             $_->type eq UNIQUE &&
-             !grep { $_->is_nullable } $_->fields
-           } $table->get_constraints),
+        $self->fields($table),
+        $self->constraints($table),
      ) .
      "\n);",
 }
index 7b6726f..e8ddcf9 100644 (file)
@@ -54,6 +54,11 @@ sub field_type {
    ($self->type_map->{$field_type} || $field_type).$self->field_type_size($field)
 }
 
+sub fields {
+  my ($self, $table) = @_;
+  ( map $self->field($_), $table->get_fields )
+}
+
 sub nullable { 'NULL' }
 
 1;