Deprecate SQL::Translator::Schema::Graph, undocument as_graph()
[dbsrgits/SQL-Translator.git] / lib / SQL / Translator / Schema.pm
index 79fb443..2c725ba 100644 (file)
@@ -1,25 +1,5 @@
 package SQL::Translator::Schema;
 
-# ----------------------------------------------------------------------
-# $Id: Schema.pm 1440 2009-01-17 16:31:57Z jawnsy $
-# ----------------------------------------------------------------------
-# Copyright (C) 2002-2009 SQLFairy Authors
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License as
-# published by the Free Software Foundation; version 2.
-#
-# This program is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-# 02111-1307  USA
-# -------------------------------------------------------------------
-
 =pod
 
 =head1 NAME
@@ -47,6 +27,7 @@ returns the database structure.
 =cut
 
 use strict;
+use warnings;
 use SQL::Translator::Schema::Constants;
 use SQL::Translator::Schema::Procedure;
 use SQL::Translator::Schema::Table;
@@ -54,22 +35,36 @@ use SQL::Translator::Schema::Trigger;
 use SQL::Translator::Schema::View;
 
 use SQL::Translator::Utils 'parse_list_arg';
+use Carp;
 
 use base 'SQL::Translator::Schema::Object';
-use vars qw[ $TABLE_ORDER $VIEW_ORDER $TRIGGER_ORDER $PROC_ORDER ];
+our $VERSION = '1.59';
 
 __PACKAGE__->_attributes(qw/name database translator/);
 
-# ----------------------------------------------------------------------
-sub as_graph {
+sub new {
+  my $class = shift;
+  my $self = $class->SUPER::new (@_)
+    or return;
 
-=pod
+  $self->{_order} = { map { $_ => 0 } qw/
+    table
+    view
+    trigger
+    proc
+  /};
 
-=head2 as_graph
+  return $self;
+}
 
-Returns the schema as an L<SQL::Translator::Schema::Graph> object.
+# FIXME - to be removed, together with the SQL::Translator::Schema::Graph* stuff
+# looks like a remnant of the Turnkey project integration back in 2003-4
+# Appears to be quite dead
+sub as_graph {
+
+    eval { require Class::MakeMethods }
+      or croak 'You need to install the CPAN dependency Class::MakeMethods to use as_graph()';
 
-=cut
     require  SQL::Translator::Schema::Graph;
 
     my $self = shift;
@@ -78,7 +73,6 @@ Returns the schema as an L<SQL::Translator::Schema::Graph> object.
         translator => $self->translator );
 }
 
-# ----------------------------------------------------------------------
 sub as_graph_pm {
 
 =pod
@@ -93,11 +87,11 @@ Returns a Graph::Directed object with the table names for nodes.
 
     my $self = shift;
     my $g    = Graph::Directed->new;
-    
-    for my $table ( $self->get_tables ) { 
+
+    for my $table ( $self->get_tables ) {
         my $tname  = $table->name;
         $g->add_vertex( $tname );
-    
+
         for my $field ( $table->get_fields ) {
             if ( $field->is_foreign_key ) {
                 my $fktable = $field->foreign_key_reference->reference_table;
@@ -110,7 +104,6 @@ Returns a Graph::Directed object with the table names for nodes.
     return $g;
 }
 
-# ----------------------------------------------------------------------
 sub add_table {
 
 =pod
@@ -119,7 +112,7 @@ sub add_table {
 
 Add a table object.  Returns the new SQL::Translator::Schema::Table object.
 The "name" parameter is required.  If you try to create a table with the
-same name as an existing table, you will get an error and the table will 
+same name as an existing table, you will get an error and the table will
 not be created.
 
   my $t1 = $schema->add_table( name => 'foo' ) or die $schema->error;
@@ -137,13 +130,13 @@ not be created.
         $table->schema($self);
     }
     else {
-        my %args = @_;
+        my %args = ref $_[0] eq 'HASH' ? %{ $_[0] } : @_;
         $args{'schema'} = $self;
         $table = $table_class->new( \%args )
           or return $self->error( $table_class->error );
     }
 
-    $table->order( ++$TABLE_ORDER );
+    $table->order( ++$self->{_order}{table} );
 
     # We know we have a name as the Table->new above errors if none given.
     my $table_name = $table->name;
@@ -158,7 +151,6 @@ not be created.
     return $table;
 }
 
-# ----------------------------------------------------------------------
 sub drop_table {
 
 =pod
@@ -203,7 +195,6 @@ can be set to 1 to also drop all triggers on the table, default is 0.
     return $table;
 }
 
-# ----------------------------------------------------------------------
 sub add_procedure {
 
 =pod
@@ -230,14 +221,14 @@ procedure will not be created.
         $procedure->schema($self);
     }
     else {
-        my %args = @_;
+        my %args = ref $_[0] eq 'HASH' ? %{ $_[0] } : @_;
         $args{'schema'} = $self;
         return $self->error('No procedure name') unless $args{'name'};
         $procedure = $procedure_class->new( \%args )
           or return $self->error( $procedure_class->error );
     }
 
-    $procedure->order( ++$PROC_ORDER );
+    $procedure->order( ++$self->{_order}{proc} );
     my $procedure_name = $procedure->name
       or return $self->error('No procedure name');
 
@@ -252,7 +243,6 @@ procedure will not be created.
     return $procedure;
 }
 
-# ----------------------------------------------------------------------
 sub drop_procedure {
 
 =pod
@@ -289,7 +279,6 @@ object.
     return $proc;
 }
 
-# ----------------------------------------------------------------------
 sub add_trigger {
 
 =pod
@@ -298,7 +287,7 @@ sub add_trigger {
 
 Add a trigger object.  Returns the new SQL::Translator::Schema::Trigger object.
 The "name" parameter is required.  If you try to create a trigger with the
-same name as an existing trigger, you will get an error and the trigger will 
+same name as an existing trigger, you will get an error and the trigger will
 not be created.
 
   my $t1 = $schema->add_trigger( name => 'foo' );
@@ -316,14 +305,14 @@ not be created.
         $trigger->schema($self);
     }
     else {
-        my %args = @_;
+        my %args = ref $_[0] eq 'HASH' ? %{ $_[0] } : @_;
         $args{'schema'} = $self;
         return $self->error('No trigger name') unless $args{'name'};
         $trigger = $trigger_class->new( \%args )
           or return $self->error( $trigger_class->error );
     }
 
-    $trigger->order( ++$TRIGGER_ORDER );
+    $trigger->order( ++$self->{_order}{trigger} );
 
     my $trigger_name = $trigger->name or return $self->error('No trigger name');
     if ( defined $self->{'triggers'}{$trigger_name} ) {
@@ -336,7 +325,6 @@ not be created.
     return $trigger;
 }
 
-# ----------------------------------------------------------------------
 sub drop_trigger {
 
 =pod
@@ -372,7 +360,6 @@ trigger name or an C<SQL::Translator::Schema::Trigger> object.
     return $trigger;
 }
 
-# ----------------------------------------------------------------------
 sub add_view {
 
 =pod
@@ -381,7 +368,7 @@ sub add_view {
 
 Add a view object.  Returns the new SQL::Translator::Schema::View object.
 The "name" parameter is required.  If you try to create a view with the
-same name as an existing view, you will get an error and the view will 
+same name as an existing view, you will get an error and the view will
 not be created.
 
   my $v1 = $schema->add_view( name => 'foo' );
@@ -399,13 +386,13 @@ not be created.
         $view->schema($self);
     }
     else {
-        my %args = @_;
+        my %args = ref $_[0] eq 'HASH' ? %{ $_[0] } : @_;
         $args{'schema'} = $self;
         return $self->error('No view name') unless $args{'name'};
         $view = $view_class->new( \%args ) or return $view_class->error;
     }
 
-    $view->order( ++$VIEW_ORDER );
+    $view->order( ++$self->{_order}{view} );
     my $view_name = $view->name or return $self->error('No view name');
 
     if ( defined $self->{'views'}{$view_name} ) {
@@ -418,7 +405,6 @@ not be created.
     return $view;
 }
 
-# ----------------------------------------------------------------------
 sub drop_view {
 
 =pod
@@ -453,7 +439,6 @@ name or an C<SQL::Translator::Schema::View> object.
     return $view;
 }
 
-# ----------------------------------------------------------------------
 sub database {
 
 =pod
@@ -471,7 +456,6 @@ Get or set the schema's database.  (optional)
     return $self->{'database'} || '';
 }
 
-# ----------------------------------------------------------------------
 sub is_valid {
 
 =pod
@@ -495,7 +479,6 @@ Returns true if all the tables and views are valid.
     return 1;
 }
 
-# ----------------------------------------------------------------------
 sub get_procedure {
 
 =pod
@@ -515,7 +498,6 @@ Returns a procedure by the name provided.
     return $self->{'procedures'}{$procedure_name};
 }
 
-# ----------------------------------------------------------------------
 sub get_procedures {
 
 =pod
@@ -543,7 +525,6 @@ Returns all the procedures as an array or array reference.
     }
 }
 
-# ----------------------------------------------------------------------
 sub get_table {
 
 =pod
@@ -560,18 +541,17 @@ Returns a table by the name provided.
     my $table_name = shift or return $self->error('No table name');
     my $case_insensitive = shift;
     if ( $case_insensitive ) {
-       $table_name = uc($table_name);
-       foreach my $table ( keys %{$self->{tables}} ) {
-               return $self->{tables}{$table} if $table_name eq uc($table);
-       }
-       return $self->error(qq[Table "$table_name" does not exist]);
+      $table_name = uc($table_name);
+      foreach my $table ( keys %{$self->{tables}} ) {
+         return $self->{tables}{$table} if $table_name eq uc($table);
+      }
+      return $self->error(qq[Table "$table_name" does not exist]);
     }
     return $self->error(qq[Table "$table_name" does not exist])
       unless exists $self->{'tables'}{$table_name};
     return $self->{'tables'}{$table_name};
 }
 
-# ----------------------------------------------------------------------
 sub get_tables {
 
 =pod
@@ -599,7 +579,6 @@ Returns all the tables as an array or array reference.
     }
 }
 
-# ----------------------------------------------------------------------
 sub get_trigger {
 
 =pod
@@ -619,7 +598,6 @@ Returns a trigger by the name provided.
     return $self->{'triggers'}{$trigger_name};
 }
 
-# ----------------------------------------------------------------------
 sub get_triggers {
 
 =pod
@@ -647,7 +625,6 @@ Returns all the triggers as an array or array reference.
     }
 }
 
-# ----------------------------------------------------------------------
 sub get_view {
 
 =pod
@@ -667,7 +644,6 @@ Returns a view by the name provided.
     return $self->{'views'}{$view_name};
 }
 
-# ----------------------------------------------------------------------
 sub get_views {
 
 =pod
@@ -695,7 +671,6 @@ Returns all the views as an array or array reference.
     }
 }
 
-# ----------------------------------------------------------------------
 sub make_natural_joins {
 
 =pod
@@ -709,14 +684,14 @@ tables.  Accepts the following arguments:
 
 =item * join_pk_only
 
-A True or False argument which determins whether or not to perform 
+A True or False argument which determins whether or not to perform
 the joins from primary keys to fields of the same name in other tables
 
 =item * skip_fields
 
 A list of fields to skip in the joins
 
-=back 4
+=back
 
   $schema->make_natural_joins(
       join_pk_only => 1,
@@ -767,7 +742,6 @@ A list of fields to skip in the joins
     return 1;
 }
 
-# ----------------------------------------------------------------------
 sub name {
 
 =pod
@@ -785,7 +759,6 @@ Get or set the schema's name.  (optional)
     return $self->{'name'} || '';
 }
 
-# ----------------------------------------------------------------------
 sub translator {
 
 =pod
@@ -801,7 +774,6 @@ Get the SQL::Translator instance that instantiated the parser.
     return $self->{'translator'};
 }
 
-# ----------------------------------------------------------------------
 sub DESTROY {
     my $self = shift;
     undef $_ for values %{ $self->{'tables'} };
@@ -810,8 +782,6 @@ sub DESTROY {
 
 1;
 
-# ----------------------------------------------------------------------
-
 =pod
 
 =head1 AUTHOR