=cut
-use strict;
-use warnings;
+use Moo;
use SQL::Translator::Schema::Constants;
use SQL::Translator::Schema::Procedure;
use SQL::Translator::Schema::Table;
use SQL::Translator::Utils 'parse_list_arg';
use Carp;
-use base 'SQL::Translator::Schema::Object';
-our $VERSION = '1.59';
+with qw(
+ SQL::Translator::Schema::Role::Extra
+ SQL::Translator::Schema::Role::Error
+ SQL::Translator::Schema::Role::Compare
+);
-__PACKAGE__->_attributes(qw/name database translator/);
+our $VERSION = '1.59';
-sub new {
- my $class = shift;
- my $self = $class->SUPER::new (@_)
- or return;
- $self->{_order} = { map { $_ => 0 } qw/
+has _order => (is => 'ro', default => sub { +{ map { $_ => 0 } qw/
table
view
trigger
proc
- /};
-
- return $self;
-}
+ /} },
+);
# 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
return $g;
}
+has _tables => ( is => 'ro', init_arg => undef, default => sub { +{} } );
+
sub add_table {
=pod
or return $self->error( $table_class->error );
}
- $table->order( ++$self->{_order}{table} );
+ $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;
- if ( defined $self->{'tables'}{$table_name} ) {
+ if ( defined $self->_tables->{$table_name} ) {
return $self->error(qq[Can't create table: "$table_name" exists]);
}
else {
- $self->{'tables'}{$table_name} = $table;
+ $self->_tables->{$table_name} = $table;
}
return $table;
my %args = @_;
my $cascade = $args{'cascade'};
- if ( !exists $self->{'tables'}{$table_name} ) {
+ if ( !exists $self->_tables->{$table_name} ) {
return $self->error(qq[Can't drop table: $table_name" doesn't exist]);
}
- my $table = delete $self->{'tables'}{$table_name};
+ my $table = delete $self->_tables->{$table_name};
if ($cascade) {
# Drop all triggers on this table
$self->drop_trigger()
- for ( grep { $_->on_table eq $table_name } @{ $self->{'triggers'} } );
+ for ( grep { $_->on_table eq $table_name } values %{ $self->_triggers } );
}
return $table;
}
+has _procedures => ( is => 'ro', init_arg => undef, default => sub { +{} } );
+
sub add_procedure {
=pod
or return $self->error( $procedure_class->error );
}
- $procedure->order( ++$self->{_order}{proc} );
+ $procedure->order( ++$self->_order->{proc} );
my $procedure_name = $procedure->name
or return $self->error('No procedure name');
- if ( defined $self->{'procedures'}{$procedure_name} ) {
+ if ( defined $self->_procedures->{$procedure_name} ) {
return $self->error(
qq[Can't create procedure: "$procedure_name" exists] );
}
else {
- $self->{'procedures'}{$procedure_name} = $procedure;
+ $self->_procedures->{$procedure_name} = $procedure;
}
return $procedure;
$proc_name = shift;
}
- if ( !exists $self->{'procedures'}{$proc_name} ) {
+ if ( !exists $self->_procedures->{$proc_name} ) {
return $self->error(
qq[Can't drop procedure: $proc_name" doesn't exist]);
}
- my $proc = delete $self->{'procedures'}{$proc_name};
+ my $proc = delete $self->_procedures->{$proc_name};
return $proc;
}
+has _triggers => ( is => 'ro', init_arg => undef, default => sub { +{} } );
+
sub add_trigger {
=pod
or return $self->error( $trigger_class->error );
}
- $trigger->order( ++$self->{_order}{trigger} );
+ $trigger->order( ++$self->_order->{trigger} );
my $trigger_name = $trigger->name or return $self->error('No trigger name');
- if ( defined $self->{'triggers'}{$trigger_name} ) {
+ if ( defined $self->_triggers->{$trigger_name} ) {
return $self->error(qq[Can't create trigger: "$trigger_name" exists]);
}
else {
- $self->{'triggers'}{$trigger_name} = $trigger;
+ $self->_triggers->{$trigger_name} = $trigger;
}
return $trigger;
$trigger_name = shift;
}
- if ( !exists $self->{'triggers'}{$trigger_name} ) {
+ if ( !exists $self->_triggers->{$trigger_name} ) {
return $self->error(
qq[Can't drop trigger: $trigger_name" doesn't exist]);
}
- my $trigger = delete $self->{'triggers'}{$trigger_name};
+ my $trigger = delete $self->_triggers->{$trigger_name};
return $trigger;
}
+has _views => ( is => 'ro', init_arg => undef, default => sub { +{} } );
+
sub add_view {
=pod
$view = $view_class->new( \%args ) or return $view_class->error;
}
- $view->order( ++$self->{_order}{view} );
+ $view->order( ++$self->_order->{view} );
my $view_name = $view->name or return $self->error('No view name');
- if ( defined $self->{'views'}{$view_name} ) {
+ if ( defined $self->_views->{$view_name} ) {
return $self->error(qq[Can't create view: "$view_name" exists]);
}
else {
- $self->{'views'}{$view_name} = $view;
+ $self->_views->{$view_name} = $view;
}
return $view;
$view_name = shift;
}
- if ( !exists $self->{'views'}{$view_name} ) {
+ if ( !exists $self->_views->{$view_name} ) {
return $self->error(qq[Can't drop view: $view_name" doesn't exist]);
}
- my $view = delete $self->{'views'}{$view_name};
+ my $view = delete $self->_views->{$view_name};
return $view;
}
-sub database {
-
-=pod
-
=head2 database
Get or set the schema's database. (optional)
=cut
- my $self = shift;
- $self->{'database'} = shift if @_;
- return $self->{'database'} || '';
-}
+has database => ( is => 'rw', default => sub { '' } );
sub is_valid {
my $self = shift;
my $procedure_name = shift or return $self->error('No procedure name');
return $self->error(qq[Table "$procedure_name" does not exist])
- unless exists $self->{'procedures'}{$procedure_name};
- return $self->{'procedures'}{$procedure_name};
+ unless exists $self->_procedures->{$procedure_name};
+ return $self->_procedures->{$procedure_name};
}
sub get_procedures {
my @procedures =
map { $_->[1] }
sort { $a->[0] <=> $b->[0] }
- map { [ $_->order, $_ ] } values %{ $self->{'procedures'} };
+ map { [ $_->order, $_ ] } values %{ $self->_procedures };
if (@procedures) {
return wantarray ? @procedures : \@procedures;
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);
+ 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};
+ unless exists $self->_tables->{$table_name};
+ return $self->_tables->{$table_name};
}
sub get_tables {
my @tables =
map { $_->[1] }
sort { $a->[0] <=> $b->[0] }
- map { [ $_->order, $_ ] } values %{ $self->{'tables'} };
+ map { [ $_->order, $_ ] } values %{ $self->_tables };
if (@tables) {
return wantarray ? @tables : \@tables;
my $self = shift;
my $trigger_name = shift or return $self->error('No trigger name');
return $self->error(qq[Table "$trigger_name" does not exist])
- unless exists $self->{'triggers'}{$trigger_name};
- return $self->{'triggers'}{$trigger_name};
+ unless exists $self->_triggers->{$trigger_name};
+ return $self->_triggers->{$trigger_name};
}
sub get_triggers {
my @triggers =
map { $_->[1] }
sort { $a->[0] <=> $b->[0] }
- map { [ $_->order, $_ ] } values %{ $self->{'triggers'} };
+ map { [ $_->order, $_ ] } values %{ $self->_triggers };
if (@triggers) {
return wantarray ? @triggers : \@triggers;
my $self = shift;
my $view_name = shift or return $self->error('No view name');
return $self->error('View "$view_name" does not exist')
- unless exists $self->{'views'}{$view_name};
- return $self->{'views'}{$view_name};
+ unless exists $self->_views->{$view_name};
+ return $self->_views->{$view_name};
}
sub get_views {
my @views =
map { $_->[1] }
sort { $a->[0] <=> $b->[0] }
- map { [ $_->order, $_ ] } values %{ $self->{'views'} };
+ map { [ $_->order, $_ ] } values %{ $self->_views };
if (@views) {
return wantarray ? @views : \@views;
return 1;
}
-sub name {
-
-=pod
-
=head2 name
Get or set the schema's name. (optional)
=cut
- my $self = shift;
- $self->{'name'} = shift if @_;
- return $self->{'name'} || '';
-}
-
-sub translator {
+has name => ( is => 'rw', default => sub { '' } );
=pod
=cut
- my $self = shift;
- $self->{'translator'} = shift if @_;
- return $self->{'translator'};
-}
+has translator => ( is => 'rw' );
sub DESTROY {
my $self = shift;
- undef $_ for values %{ $self->{'tables'} };
- undef $_ for values %{ $self->{'views'} };
+ undef $_ for values %{ $self->_tables };
+ undef $_ for values %{ $self->_views };
}
1;
--- /dev/null
+package SQL::Translator::Schema::Role::Extra;
+use Moo::Role;
+
+
+=head1 Methods
+
+The following methods are defined here, therefore all schema objects
+using this role will have them.
+
+=head2 extra
+
+Get or set the objects "extra" attibutes (e.g., "ZEROFILL" for MySQL fields).
+Call with no args to get all the extra data.
+Call with a single name arg to get the value of the named extra attribute,
+returned as a scalar. Call with a hash or hashref to set extra attributes.
+Returns a hash or a hashref.
+
+ $field->extra( qualifier => 'ZEROFILL' );
+
+ $qualifier = $field->extra('qualifier');
+
+ %extra = $field->extra;
+ $extra = $field->extra;
+
+=cut
+
+has extra => ( is => 'rwp', default => sub { +{} } );
+
+around extra => sub {
+ my ($orig, $self) = (shift, shift);
+
+ @_ = %{$_[0]} if ref $_[0] eq "HASH";
+ my $extra = $self->$orig;
+
+ if (@_==1) {
+ return exists($extra->{$_[0]}) ? $extra->{$_[0]} : undef ;
+ }
+ elsif (@_) {
+ my %args = @_;
+ while ( my ( $key, $value ) = each %args ) {
+ $extra->{$key} = $value;
+ }
+ }
+
+ return wantarray ? %$extra : $extra;
+};
+
+=head2 remove_extra
+
+L</extra> can only be used to get or set "extra" attributes but not to
+remove some. Call with no args to remove all extra attributes that
+have been set before. Call with a list of key names to remove
+certain extra attributes only.
+
+ # remove all extra attributes
+ $field->remove_extra();
+
+ # remove timezone and locale attributes only
+ $field->remove_extra(qw/timezone locale/);
+
+=cut
+
+sub remove_extra {
+ my ( $self, @keys ) = @_;
+ unless (@keys) {
+ $self->_set_extra({});
+ }
+ else {
+ delete @{$self->extra}{@keys};
+ }
+}
+
+1;