cleanup
Justin Hunter [Wed, 19 Aug 2009 10:13:40 +0000 (03:13 -0700)]
lib/SQL/Translator/Parser/DDL/YAML.pm

index 23b5dd3..cb60564 100644 (file)
@@ -1,67 +1,57 @@
 use MooseX::Declare;
 role SQL::Translator::Parser::DDL::YAML { 
-    use MooseX::Types::Moose qw(Any Str);
+    use MooseX::Types::Moose qw(Str);
     use SQL::Translator::Types qw(Schema);
     use aliased 'SQL::Translator::Object::Column';
     use aliased 'SQL::Translator::Object::Constraint';
     use aliased 'SQL::Translator::Object::Index';
+    use aliased 'SQL::Translator::Object::Procedure';
     use aliased 'SQL::Translator::Object::Table';
-    use aliased 'SQL::Translator::Object::Schema' => 'SchemaObj';
+    use aliased 'SQL::Translator::Object::Trigger';
+    use aliased 'SQL::Translator::Object::View';
     use YAML qw(Load);
     use MooseX::MultiMethods;
 
-#    multi method parse(Any $data) { use Data::Dumper; die Dumper($data); }
     multi method parse(Schema $data) { return $data }
 
     multi method parse(Str $data) {
-        return $data if blessed $data && $data->isa('SQL::Translator::Object::Schema');
         $data = Load($data);
         $data = $data->{schema};
     
-#        warn "YAML data:",Dumper( $data ) if $self->debug;
-
-        my $schema = SchemaObj->new; #$self->schema;
+        my $schema = $self->schema;
+        my $translator = $self->translator;
     
         #
         # Tables
         #
         my @tables = 
             map   { $data->{'tables'}{ $_->[1] } }
-#            sort  { $a->[0] <=> $b->[0] }
             map   { [ $data->{'tables'}{ $_ }{'order'} || 0, $_ ] }
-            keys %{ $data->{'tables'} } ;
+            keys %{ $data->{'tables'} };
     
         for my $tdata ( @tables ) {
             my $table = Table->new({ map { $tdata->{$_} ? ($_ => $tdata->{$_}) : () } qw/name extra options/ });    
             $schema->add_table($table);
-#            my $table = $schema->add_table(
-#                map {
-#                  $tdata->{$_} ? ($_ => $tdata->{$_}) : ()
-#                } (qw/name extra options/)
-#            ) or die $schema->error;
     
             my @fields = 
                 map   { $tdata->{'fields'}{ $_->[1] } }
-#                sort  { $a->[0] <=> $b->[0] }
                 map   { [ $tdata->{'fields'}{ $_ }{'order'}, $_ ] }
-                keys %{ $tdata->{'fields'} } ;
+                keys %{ $tdata->{'fields'} };
     
             for my $fdata ( @fields ) {
-#                $table->add_field( %$fdata ) or die $table->error;
                 $fdata->{sql_data_type} = $self->data_type_mapping->{$fdata->{data_type}} || -99999;
+
                 my $column = Column->new($fdata);
                 $table->add_column($column);
                 $table->primary_key($column->name) if $fdata->{is_primary_key};
             }
     
             for my $idata ( @{ $tdata->{'indices'} || [] } ) {
-#                $table->add_index( %$idata ) or die $table->error;
                  my $index = Index->new($idata);
                  $table->add_index($index);
             }
     
             for my $cdata ( @{ $tdata->{'constraints'} || [] } ) {
-#                $table->add_constraint( %$cdata ) or die $table->error;
                  my $constraint = Constraint->new($cdata);
                  $table->add_constraint($constraint);
             }
@@ -72,12 +62,12 @@ role SQL::Translator::Parser::DDL::YAML {
         #
         my @views = 
             map   { $data->{'views'}{ $_->[1] } }
-            sort  { $a->[0] <=> $b->[0] }
             map   { [ $data->{'views'}{ $_ }{'order'}, $_ ] }
-            keys %{ $data->{'views'} } ;
+            keys %{ $data->{'views'} };
     
         for my $vdata ( @views ) {
-#            $schema->add_view( %$vdata ) or die $schema->error;
+            my $view = View->new($vdata);
+            $schema->add_view($view);
         }
     
         #
@@ -85,13 +75,14 @@ role SQL::Translator::Parser::DDL::YAML {
         #
         my @triggers = 
             map   { $data->{'triggers'}{ $_->[1] } }
-            sort  { $a->[0] <=> $b->[0] }
             map   { [ $data->{'triggers'}{ $_ }{'order'}, $_ ] }
-            keys %{ $data->{'triggers'} }
-        ;
+            keys %{ $data->{'triggers'} };
     
         for my $tdata ( @triggers ) {
-#            $schema->add_trigger( %$tdata ) or die $schema->error;
+            my @columns = delete $tdata->{fields} || ();
+            my $trigger = Trigger->new($tdata);
+            $trigger->add_column($schema->get_table($tdata->{on_table})->get_column($_)) for @columns; 
+            $schema->add_trigger($trigger);
         }
     
         #
@@ -99,25 +90,24 @@ role SQL::Translator::Parser::DDL::YAML {
         #
         my @procedures = 
             map   { $data->{'procedures'}{ $_->[1] } }
-            sort  { $a->[0] <=> $b->[0] }
             map   { [ $data->{'procedures'}{ $_ }{'order'}, $_ ] }
-            keys %{ $data->{'procedures'} }
-        ;
+            keys %{ $data->{'procedures'} };
     
         for my $tdata ( @procedures ) {
-#            $schema->add_procedure( %$tdata ) or die $schema->error;
+             my $procedure = Procedure->new($tdata);
+             $schema->add_procedure($procedure);
         }
     
         if ( my $tr_data = $data->{'translator'} ) {
-            $self->add_drop_table( $tr_data->{'add_drop_table'} );
-            $self->filename( $tr_data->{'filename'} );
-            $self->no_comments( $tr_data->{'no_comments'} );
-            $self->parser_args( $tr_data->{'parser_args'} );
-            $self->producer_args( $tr_data->{'producer_args'} );
-            $self->parser_type( $tr_data->{'parser_type'} );
-            $self->producer_type( $tr_data->{'producer_type'} );
-            $self->show_warnings( $tr_data->{'show_warnings'} );
-            $self->trace( $tr_data->{'trace'} );
+            $translator->add_drop_table( $tr_data->{'add_drop_table'} );
+            $translator->filename( $tr_data->{'filename'} );
+            $translator->no_comments( $tr_data->{'no_comments'} );
+            $translator->parser_args( $tr_data->{'parser_args'} );
+            $translator->producer_args( $tr_data->{'producer_args'} );
+            $translator->parser_type( $tr_data->{'parser_type'} );
+            $translator->producer_type( $tr_data->{'producer_type'} );
+            $translator->show_warnings( $tr_data->{'show_warnings'} );
+            $translator->trace( $tr_data->{'trace'} );
         }
     
         return $schema;