use SQL::Translator::Schema::Constants;
use SQL::Translator::Utils qw(ex2err throw);
use SQL::Translator::Role::ListAttr;
-use SQL::Translator::Types qw(schema_obj);
+use SQL::Translator::Types qw(schema_obj enum);
+use Sub::Quote qw(quote_sub);
extends 'SQL::Translator::Schema::Object';
Get or set whether the constraint is deferrable. If not defined,
then returns "1." The argument is evaluated by Perl for True or
-False, so the following are eqivalent:
+False, so the following are equivalent:
$deferrable = $field->deferrable(0);
$deferrable = $field->deferrable('');
=cut
-has deferrable => ( is => 'rw', coerce => sub { $_[0] ? 1 : 0 }, default => sub { 1 } );
+has deferrable => (
+ is => 'rw',
+ coerce => quote_sub(q{ $_[0] ? 1 : 0 }),
+ default => quote_sub(q{ 1 }),
+);
=head2 expression
=cut
-has expression => ( is => 'rw', default => sub { '' } );
+has expression => ( is => 'rw', default => quote_sub(q{ '' }) );
around expression => sub {
my ($orig, $self, $arg) = @_;
The fields are returned as Field objects if they exist or as plain
names if not. (If you just want the names and want to avoid the Field's overload
-magic use L<field_names>).
+magic use L</field_names>).
Returns undef or an empty list if the constraint has no fields set.
sub fields {
my $self = shift;
my $table = $self->table;
- my @tables = map { $table->get_field($_) || $_ } @{$self->field_names(@_) || []};
- return wantarray ? @tables
- : @tables ? \@tables
+ my @fields = map { $table->get_field($_) || $_ } @{$self->field_names(@_) || []};
+ return wantarray ? @fields
+ : @fields ? \@fields
: undef;
}
has match_type => (
is => 'rw',
- default => sub { '' },
- coerce => sub { lc $_[0] },
- isa => sub {
- my $arg = $_[0];
- throw("Invalid match type: $arg")
- if $arg && !($arg eq 'full' || $arg eq 'partial' || $arg eq 'simple');
- },
+ default => quote_sub(q{ '' }),
+ coerce => quote_sub(q{ lc $_[0] }),
+ isa => enum([qw(full partial simple)], {
+ msg => "Invalid match type: %s", allow_false => 1,
+ }),
);
around match_type => \&ex2err;
=cut
-has name => ( is => 'rw', default => sub { '' } );
+has name => ( is => 'rw', default => quote_sub(q{ '' }) );
around name => sub {
my ($orig, $self, $arg) = @_;
=cut
-has on_delete => ( is => 'rw', default => sub { '' } );
+has on_delete => ( is => 'rw', default => quote_sub(q{ '' }) );
around on_delete => sub {
my ($orig, $self, $arg) = @_;
=cut
-has on_update => ( is => 'rw', default => sub { '' } );
+has on_update => ( is => 'rw', default => quote_sub(q{ '' }) );
around on_update => sub {
my ($orig, $self, $arg) = @_;
=cut
-has reference_table => ( is => 'rw', default => sub { '' } );
+has reference_table => ( is => 'rw', default => quote_sub(q{ '' }) );
=head2 table
has type => (
is => 'rw',
- default => sub { '' },
- isa => sub {
- throw("Invalid constraint type: $_[0]")
- if $_[0] && !$VALID_CONSTRAINT_TYPE{ $_[0] };
- },
- coerce => sub { (my $t = $_[0]) =~ s/_/ /g; uc $t },
+ default => quote_sub(q{ '' }),
+ coerce => quote_sub(q{ (my $t = $_[0]) =~ s/_/ /g; uc $t }),
+ isa => enum([keys %VALID_CONSTRAINT_TYPE], {
+ msg => "Invalid constraint type: %s", allow_false => 1,
+ }),
);
around type => \&ex2err;