package SQL::Translator::Schema;
-# ----------------------------------------------------------------------
-# 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
=cut
use strict;
+use warnings;
use SQL::Translator::Schema::Constants;
use SQL::Translator::Schema::Procedure;
use SQL::Translator::Schema::Table;
use SQL::Translator::Schema::View;
use SQL::Translator::Utils 'parse_list_arg';
+use Carp;
use base 'SQL::Translator::Schema::Object';
-use vars qw[ $VERSION $TABLE_ORDER $VIEW_ORDER $TRIGGER_ORDER $PROC_ORDER ];
-
-$VERSION = '1.99';
+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
+ /};
+
+ return $self;
+}
-=head2 as_graph
+# 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 {
-Returns the schema as an L<SQL::Translator::Schema::Graph> object.
+ 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;
translator => $self->translator );
}
-# ----------------------------------------------------------------------
sub as_graph_pm {
=pod
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;
return $g;
}
-# ----------------------------------------------------------------------
sub add_table {
=pod
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;
$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;
return $table;
}
-# ----------------------------------------------------------------------
sub drop_table {
=pod
return $table;
}
-# ----------------------------------------------------------------------
sub add_procedure {
=pod
$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');
return $procedure;
}
-# ----------------------------------------------------------------------
sub drop_procedure {
=pod
return $proc;
}
-# ----------------------------------------------------------------------
sub add_trigger {
=pod
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' );
$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} ) {
return $trigger;
}
-# ----------------------------------------------------------------------
sub drop_trigger {
=pod
return $trigger;
}
-# ----------------------------------------------------------------------
sub add_view {
=pod
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' );
$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} ) {
return $view;
}
-# ----------------------------------------------------------------------
sub drop_view {
=pod
return $view;
}
-# ----------------------------------------------------------------------
sub database {
=pod
return $self->{'database'} || '';
}
-# ----------------------------------------------------------------------
sub is_valid {
=pod
return 1;
}
-# ----------------------------------------------------------------------
sub get_procedure {
=pod
return $self->{'procedures'}{$procedure_name};
}
-# ----------------------------------------------------------------------
sub get_procedures {
=pod
}
}
-# ----------------------------------------------------------------------
sub get_table {
=pod
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
}
}
-# ----------------------------------------------------------------------
sub get_trigger {
=pod
return $self->{'triggers'}{$trigger_name};
}
-# ----------------------------------------------------------------------
sub get_triggers {
=pod
}
}
-# ----------------------------------------------------------------------
sub get_view {
=pod
return $self->{'views'}{$view_name};
}
-# ----------------------------------------------------------------------
sub get_views {
=pod
}
}
-# ----------------------------------------------------------------------
sub make_natural_joins {
=pod
=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,
return 1;
}
-# ----------------------------------------------------------------------
sub name {
=pod
return $self->{'name'} || '';
}
-# ----------------------------------------------------------------------
sub translator {
=pod
return $self->{'translator'};
}
-# ----------------------------------------------------------------------
sub DESTROY {
my $self = shift;
undef $_ for values %{ $self->{'tables'} };
1;
-# ----------------------------------------------------------------------
-
=pod
=head1 AUTHOR