From: Dagfinn Ilmari Mannsåker Date: Wed, 15 Aug 2012 16:39:19 +0000 (+0200) Subject: Use quote_sub for trivial defaults X-Git-Tag: v0.11013_01~10 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=68d75205c6994eb347f51726cea9bf3dbf28b498;p=dbsrgits%2FSQL-Translator.git Use quote_sub for trivial defaults --- diff --git a/Makefile.PL b/Makefile.PL index 8e476ba..7466ea9 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 53867ea..4e299f5 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 40319e4..df1a58c 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 e187881..5635f49 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 756ec1a..f77c630 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 516ba55..0fd43c1 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 25e8074..4adc293 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 77d9c5e..8af8417 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 a0efe46..5f7a089 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 5e21da2..6525abd 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 38c7ffe..ee92158 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 1381c77..c0d8d38 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 070d31b..c19501b 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