From 68d75205c6994eb347f51726cea9bf3dbf28b498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dagfinn=20Ilmari=20Manns=C3=A5ker?= Date: Wed, 15 Aug 2012 18:39:19 +0200 Subject: [PATCH] Use quote_sub for trivial defaults --- Makefile.PL | 1 + lib/SQL/Translator.pm | 11 ++++++----- lib/SQL/Translator/Role/Error.pm | 3 ++- lib/SQL/Translator/Role/ListAttr.pm | 3 ++- lib/SQL/Translator/Schema.pm | 17 +++++++++-------- lib/SQL/Translator/Schema/Constraint.pm | 17 +++++++++-------- lib/SQL/Translator/Schema/Field.pm | 11 ++++++----- lib/SQL/Translator/Schema/Index.pm | 5 +++-- lib/SQL/Translator/Schema/Procedure.pm | 9 +++++---- lib/SQL/Translator/Schema/Role/Extra.pm | 3 ++- lib/SQL/Translator/Schema/Table.pm | 13 +++++++------ lib/SQL/Translator/Schema/Trigger.pm | 7 ++++--- lib/SQL/Translator/Schema/View.pm | 7 ++++--- 13 files changed, 60 insertions(+), 47 deletions(-) diff --git a/Makefile.PL b/Makefile.PL index 8e476ba77..7466ea965 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -21,6 +21,7 @@ my $deps = { 'XML::Writer' => '0.500', 'Moo' => '1.000003', 'Package::Variant' => '1.001001', + 'Sub::Quote' => '0', 'Try::Tiny' => '0.04', }, recommends => { diff --git a/lib/SQL/Translator.pm b/lib/SQL/Translator.pm index 53867ea50..4e299f51d 100644 --- a/lib/SQL/Translator.pm +++ b/lib/SQL/Translator.pm @@ -16,6 +16,7 @@ use File::Find; use File::Spec::Functions qw(catfile); use File::Basename qw(dirname); use IO::Dir; +use Sub::Quote qw(quote_sub); use SQL::Translator::Producer; use SQL::Translator::Schema; use SQL::Translator::Utils qw(throw ex2err); @@ -90,7 +91,7 @@ sub BUILD { has $_ => ( is => 'rw', - default => sub { 0 }, + default => quote_sub(q{ 0 }), coerce => sub { $_[0] ? 1 : 0 }, ) foreach qw(add_drop_table no_comments show_warnings trace validate); @@ -98,7 +99,7 @@ has $_ => ( # so we can allow individual producers to change the default has quote_identifiers => ( is => 'rw', - default => sub { '0E0' }, + default => quote_sub(q{ '0E0' }), coerce => sub { $_[0] || 0 }, ); @@ -137,7 +138,7 @@ around producer => sub { has producer_type => ( is => 'rwp', init_arg => undef ); -has producer_args => ( is => 'rw', default => sub { +{} } ); +has producer_args => ( is => 'rw', default => quote_sub(q{ +{} }) ); around producer_args => sub { my $orig = shift; @@ -158,7 +159,7 @@ around parser => sub { has parser_type => ( is => 'rwp', init_arg => undef ); -has parser_args => ( is => 'rw', default => sub { +{} } ); +has parser_args => ( is => 'rw', default => quote_sub(q{ +{} }) ); around parser_args => sub { my $orig = shift; @@ -167,7 +168,7 @@ around parser_args => sub { has filters => ( is => 'rw', - default => sub { [] }, + default => quote_sub(q{ [] }), coerce => sub { my @filters; # Set. Convert args to list of [\&code,@args] diff --git a/lib/SQL/Translator/Role/Error.pm b/lib/SQL/Translator/Role/Error.pm index 40319e4af..df1a58c40 100644 --- a/lib/SQL/Translator/Role/Error.pm +++ b/lib/SQL/Translator/Role/Error.pm @@ -1,11 +1,12 @@ package SQL::Translator::Role::Error; use Moo::Role; +use Sub::Quote qw(quote_sub); has _ERROR => ( is => 'rw', accessor => 'error', init_arg => undef, - default => sub { '' }, + default => quote_sub(q{ '' }), ); around error => sub { diff --git a/lib/SQL/Translator/Role/ListAttr.pm b/lib/SQL/Translator/Role/ListAttr.pm index e1878815c..5635f49c9 100644 --- a/lib/SQL/Translator/Role/ListAttr.pm +++ b/lib/SQL/Translator/Role/ListAttr.pm @@ -2,6 +2,7 @@ package SQL::Translator::Role::ListAttr; use strictures 1; use SQL::Translator::Utils qw(parse_list_arg ex2err); use List::MoreUtils qw(uniq); +use Sub::Quote qw(quote_sub); use Package::Variant ( importing => { @@ -24,7 +25,7 @@ sub make_variant { has($name => ( is => 'rw', (!$arguments{builder} ? ( - default => sub { [] }, + default => quote_sub(q{ [] }), ) : ()), coerce => $coerce, %arguments, diff --git a/lib/SQL/Translator/Schema.pm b/lib/SQL/Translator/Schema.pm index 756ec1a04..f77c630c9 100644 --- a/lib/SQL/Translator/Schema.pm +++ b/lib/SQL/Translator/Schema.pm @@ -32,6 +32,7 @@ use SQL::Translator::Schema::Procedure; use SQL::Translator::Schema::Table; use SQL::Translator::Schema::Trigger; use SQL::Translator::Schema::View; +use Sub::Quote qw(quote_sub); use SQL::Translator::Utils 'parse_list_arg'; use Carp; @@ -41,12 +42,12 @@ extends 'SQL::Translator::Schema::Object'; our $VERSION = '1.59'; -has _order => (is => 'ro', default => sub { +{ map { $_ => 0 } qw/ +has _order => (is => 'ro', default => quote_sub(q{ +{ map { $_ => 0 } qw/ table view trigger proc - /} }, + /} }), ); # FIXME - to be removed, together with the SQL::Translator::Schema::Graph* stuff @@ -96,7 +97,7 @@ Returns a Graph::Directed object with the table names for nodes. return $g; } -has _tables => ( is => 'ro', init_arg => undef, default => sub { +{} } ); +has _tables => ( is => 'ro', init_arg => undef, default => quote_sub(q{ +{} }) ); sub add_table { @@ -189,7 +190,7 @@ can be set to 1 to also drop all triggers on the table, default is 0. return $table; } -has _procedures => ( is => 'ro', init_arg => undef, default => sub { +{} } ); +has _procedures => ( is => 'ro', init_arg => undef, default => quote_sub(q{ +{} }) ); sub add_procedure { @@ -275,7 +276,7 @@ object. return $proc; } -has _triggers => ( is => 'ro', init_arg => undef, default => sub { +{} } ); +has _triggers => ( is => 'ro', init_arg => undef, default => quote_sub(q{ +{} }) ); sub add_trigger { @@ -358,7 +359,7 @@ trigger name or an C object. return $trigger; } -has _views => ( is => 'ro', init_arg => undef, default => sub { +{} } ); +has _views => ( is => 'ro', init_arg => undef, default => quote_sub(q{ +{} }) ); sub add_view { @@ -447,7 +448,7 @@ Get or set the schema's database. (optional) =cut -has database => ( is => 'rw', default => sub { '' } ); +has database => ( is => 'rw', default => quote_sub(q{ '' }) ); sub is_valid { @@ -743,7 +744,7 @@ Get or set the schema's name. (optional) =cut -has name => ( is => 'rw', default => sub { '' } ); +has name => ( is => 'rw', default => quote_sub(q{ '' }) ); =pod diff --git a/lib/SQL/Translator/Schema/Constraint.pm b/lib/SQL/Translator/Schema/Constraint.pm index 516ba5525..0fd43c130 100644 --- a/lib/SQL/Translator/Schema/Constraint.pm +++ b/lib/SQL/Translator/Schema/Constraint.pm @@ -28,6 +28,7 @@ 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 Sub::Quote qw(quote_sub); extends 'SQL::Translator::Schema::Object'; @@ -87,7 +88,7 @@ False, so the following are eqivalent: =cut -has deferrable => ( is => 'rw', coerce => sub { $_[0] ? 1 : 0 }, default => sub { 1 } ); +has deferrable => ( is => 'rw', coerce => sub { $_[0] ? 1 : 0 }, default => quote_sub(q{ 1 }) ); =head2 expression @@ -97,7 +98,7 @@ Gets and set the expression used in a CHECK constraint. =cut -has expression => ( is => 'rw', default => sub { '' } ); +has expression => ( is => 'rw', default => quote_sub(q{ '' }) ); around expression => sub { my ($orig, $self, $arg) = @_; @@ -220,7 +221,7 @@ Get or set the constraint's match_type. Only valid values are "full" has match_type => ( is => 'rw', - default => sub { '' }, + default => quote_sub(q{ '' }), coerce => sub { lc $_[0] }, isa => sub { my $arg = $_[0]; @@ -239,7 +240,7 @@ Get or set the constraint's name. =cut -has name => ( is => 'rw', default => sub { '' } ); +has name => ( is => 'rw', default => quote_sub(q{ '' }) ); around name => sub { my ($orig, $self, $arg) = @_; @@ -266,7 +267,7 @@ Get or set the constraint's "on delete" action. =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) = @_; @@ -281,7 +282,7 @@ Get or set the constraint's "on update" action. =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) = @_; @@ -338,7 +339,7 @@ Get or set the table referred to by the constraint. =cut -has reference_table => ( is => 'rw', default => sub { '' } ); +has reference_table => ( is => 'rw', default => quote_sub(q{ '' }) ); =head2 table @@ -362,7 +363,7 @@ Get or set the constraint's type. has type => ( is => 'rw', - default => sub { '' }, + default => quote_sub(q{ '' }), isa => sub { throw("Invalid constraint type: $_[0]") if $_[0] && !$VALID_CONSTRAINT_TYPE{ $_[0] }; diff --git a/lib/SQL/Translator/Schema/Field.pm b/lib/SQL/Translator/Schema/Field.pm index 25e8074fd..4adc293c3 100644 --- a/lib/SQL/Translator/Schema/Field.pm +++ b/lib/SQL/Translator/Schema/Field.pm @@ -26,6 +26,7 @@ use Moo; use SQL::Translator::Schema::Constants; use SQL::Translator::Types qw(schema_obj); use SQL::Translator::Utils qw(parse_list_arg ex2err throw); +use Sub::Quote qw(quote_sub); extends 'SQL::Translator::Schema::Object'; @@ -98,7 +99,7 @@ all the comments joined on newlines. has comments => ( is => 'rw', coerce => sub { ref($_[0]) eq 'ARRAY' ? $_[0] : [$_[0]] }, - default => sub { [] }, + default => quote_sub(q{ [] }), ); around comments => sub { @@ -124,7 +125,7 @@ Get or set the field's data type. =cut -has data_type => ( is => 'rw', default => sub { '' } ); +has data_type => ( is => 'rw', default => quote_sub(q{ '' }) ); =head2 sql_data_type @@ -273,7 +274,7 @@ foreign keys; checks) are represented as table constraints. has is_nullable => ( is => 'rw', coerce => sub { $_[0] ? 1 : 0 }, - default => sub { 1 }, + default => quote_sub(q{ 1 }), ); around is_nullable => sub { @@ -406,7 +407,7 @@ Get or set the field's order. =cut -has order => ( is => 'rw', default => sub { 0 } ); +has order => ( is => 'rw', default => quote_sub(q{ 0 }) ); around order => sub { my ( $orig, $self, $arg ) = @_; @@ -451,7 +452,7 @@ numbers and returns a string. has size => ( is => 'rw', - default => sub { [0] }, + default => quote_sub(q{ [0] }), coerce => sub { my @sizes = grep { defined && m/^\d+(?:\.\d+)?$/ } @{parse_list_arg($_[0])}; @sizes ? \@sizes : [0]; diff --git a/lib/SQL/Translator/Schema/Index.pm b/lib/SQL/Translator/Schema/Index.pm index 77d9c5ed3..8af8417b8 100644 --- a/lib/SQL/Translator/Schema/Index.pm +++ b/lib/SQL/Translator/Schema/Index.pm @@ -30,6 +30,7 @@ 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 Sub::Quote qw(quote_sub); extends 'SQL::Translator::Schema::Object'; @@ -100,7 +101,7 @@ Get or set the index's name. =cut -has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => sub { '' } ); +has name => ( is => 'rw', coerce => sub { defined $_[0] ? $_[0] : '' }, default => quote_sub(q{ '' }) ); =head2 options @@ -147,7 +148,7 @@ has type => ( throw("Invalid index type: $type") unless $VALID_INDEX_TYPE{$type}; }, coerce => sub { uc $_[0] }, - default => sub { 'NORMAL' }, + default => quote_sub(q{ 'NORMAL' }), ); around type => \&ex2err; diff --git a/lib/SQL/Translator/Schema/Procedure.pm b/lib/SQL/Translator/Schema/Procedure.pm index a0efe4690..5f7a08912 100644 --- a/lib/SQL/Translator/Schema/Procedure.pm +++ b/lib/SQL/Translator/Schema/Procedure.pm @@ -31,6 +31,7 @@ use Moo; use SQL::Translator::Utils qw(ex2err); use SQL::Translator::Role::ListAttr; use SQL::Translator::Types qw(schema_obj); +use Sub::Quote qw(quote_sub); extends 'SQL::Translator::Schema::Object'; @@ -69,7 +70,7 @@ Get or set the procedure's name. =cut -has name => ( is => 'rw', default => sub { '' } ); +has name => ( is => 'rw', default => quote_sub(q{ '' }) ); =head2 sql @@ -80,7 +81,7 @@ Get or set the procedure's SQL. =cut -has sql => ( is => 'rw', default => sub { '' } ); +has sql => ( is => 'rw', default => quote_sub(q{ '' }) ); =head2 order @@ -103,7 +104,7 @@ Get or set the owner of the procedure. =cut -has owner => ( is => 'rw', default => sub { '' } ); +has owner => ( is => 'rw', default => quote_sub(q{ '' }) ); =head2 comments @@ -118,7 +119,7 @@ Get or set the comments on a procedure. has comments => ( is => 'rw', coerce => sub { ref($_[0]) eq 'ARRAY' ? $_[0] : [$_[0]] }, - default => sub { [] }, + default => quote_sub(q{ [] }), ); around comments => sub { diff --git a/lib/SQL/Translator/Schema/Role/Extra.pm b/lib/SQL/Translator/Schema/Role/Extra.pm index 5e21da2ef..6525abd3d 100644 --- a/lib/SQL/Translator/Schema/Role/Extra.pm +++ b/lib/SQL/Translator/Schema/Role/Extra.pm @@ -1,5 +1,6 @@ package SQL::Translator::Schema::Role::Extra; use Moo::Role; +use Sub::Quote qw(quote_sub); =head1 Methods @@ -24,7 +25,7 @@ Returns a hash or a hashref. =cut -has extra => ( is => 'rwp', default => sub { +{} } ); +has extra => ( is => 'rwp', default => quote_sub(q{ +{} }) ); around extra => sub { my ($orig, $self) = (shift, shift); diff --git a/lib/SQL/Translator/Schema/Table.pm b/lib/SQL/Translator/Schema/Table.pm index 38c7ffef9..ee921580b 100644 --- a/lib/SQL/Translator/Schema/Table.pm +++ b/lib/SQL/Translator/Schema/Table.pm @@ -30,6 +30,7 @@ use SQL::Translator::Schema::Index; use Carp::Clan '^SQL::Translator'; use List::Util 'max'; +use Sub::Quote qw(quote_sub); extends 'SQL::Translator::Schema::Object'; @@ -74,7 +75,7 @@ C object. has _constraints => ( is => 'ro', init_arg => undef, - default => sub { +[] }, + default => quote_sub(q{ +[] }), predicate => 1, lazy => 1, ); @@ -197,7 +198,7 @@ C object. has _indices => ( is => 'ro', init_arg => undef, - default => sub { [] }, + default => quote_sub(q{ [] }), predicate => 1, lazy => 1, ); @@ -281,7 +282,7 @@ existing field, you will get an error and the field will not be created. has _fields => ( is => 'ro', init_arg => undef, - default => sub { +{} }, + default => quote_sub(q{ +{} }), predicate => 1, lazy => 1 ); @@ -405,7 +406,7 @@ all the comments joined on newlines. has comments => ( is => 'rw', coerce => sub { ref($_[0]) eq 'ARRAY' ? $_[0] : [$_[0]] }, - default => sub { [] }, + default => quote_sub(q{ [] }), ); around comments => sub { @@ -596,7 +597,7 @@ Determine whether the table can link two arg tables via many-to-many. =cut -has _can_link => ( is => 'ro', init_arg => undef, default => sub { +{} } ); +has _can_link => ( is => 'ro', init_arg => undef, default => quote_sub(q{ +{} }) ); sub can_link { my ( $self, $table1, $table2 ) = @_; @@ -807,7 +808,7 @@ Get or set the table's order. =cut -has order => ( is => 'rw', default => sub { 0 } ); +has order => ( is => 'rw', default => quote_sub(q{ 0 }) ); around order => sub { my ( $orig, $self, $arg ) = @_; diff --git a/lib/SQL/Translator/Schema/Trigger.pm b/lib/SQL/Translator/Schema/Trigger.pm index 1381c7782..c0d8d38ae 100644 --- a/lib/SQL/Translator/Schema/Trigger.pm +++ b/lib/SQL/Translator/Schema/Trigger.pm @@ -32,6 +32,7 @@ use Moo; use SQL::Translator::Utils qw(parse_list_arg ex2err throw); use SQL::Translator::Types qw(schema_obj); use List::MoreUtils qw(uniq); +use Sub::Quote qw(quote_sub); extends 'SQL::Translator::Schema::Object'; @@ -212,7 +213,7 @@ Gets or set the action of the trigger. =cut -has action => ( is => 'rw', default => sub { '' } ); +has action => ( is => 'rw', default => quote_sub(q{ '' }) ); sub is_valid { @@ -248,7 +249,7 @@ Get or set the trigger's name. =cut -has name => ( is => 'rw', default => sub { '' } ); +has name => ( is => 'rw', default => quote_sub(q{ '' }) ); =head2 order @@ -258,7 +259,7 @@ Get or set the trigger's order. =cut -has order => ( is => 'rw', default => sub { 0 } ); +has order => ( is => 'rw', default => quote_sub(q{ 0 }) ); around order => sub { my ( $orig, $self, $arg ) = @_; diff --git a/lib/SQL/Translator/Schema/View.pm b/lib/SQL/Translator/Schema/View.pm index 070d31baf..c19501bf1 100644 --- a/lib/SQL/Translator/Schema/View.pm +++ b/lib/SQL/Translator/Schema/View.pm @@ -27,6 +27,7 @@ use Moo; use SQL::Translator::Utils qw(ex2err); use SQL::Translator::Types qw(schema_obj); use SQL::Translator::Role::ListAttr; +use Sub::Quote qw(quote_sub); extends 'SQL::Translator::Schema::Object'; @@ -114,7 +115,7 @@ Get or set the view's name. =cut -has name => ( is => 'rw', default => sub { '' } ); +has name => ( is => 'rw', default => quote_sub(q{ '' }) ); =head2 order @@ -124,7 +125,7 @@ Get or set the view's order. =cut -has order => ( is => 'rw', default => sub { 0 } ); +has order => ( is => 'rw', default => quote_sub(q{ 0 }) ); around order => sub { my ( $orig, $self, $arg ) = @_; @@ -144,7 +145,7 @@ Get or set the view's SQL. =cut -has sql => ( is => 'rw', default => sub { '' } ); +has sql => ( is => 'rw', default => quote_sub(q{ '' }) ); =head2 schema