From: Justin Hunter Date: Thu, 21 Oct 2010 21:54:49 +0000 (-0700) Subject: change usage of Tie::IxHash X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a499b4bdb67f16a5a7f0934c52104895f7321f97;p=dbsrgits%2FSQL-Translator-2.0-ish.git change usage of Tie::IxHash --- diff --git a/lib/SQL/Translator/Object/Column.pm b/lib/SQL/Translator/Object/Column.pm index 4cf4f81..d9f8a3b 100644 --- a/lib/SQL/Translator/Object/Column.pm +++ b/lib/SQL/Translator/Object/Column.pm @@ -3,6 +3,7 @@ class SQL::Translator::Object::Column extends SQL::Translator::Object is dirty { use MooseX::Types::Moose qw(Int Maybe ScalarRef Str); use MooseX::MultiMethods; use SQL::Translator::Types qw(Bit Constraint Table Trigger); + use Tie::IxHash; clean; use overload diff --git a/lib/SQL/Translator/Object/Constraint.pm b/lib/SQL/Translator/Object/Constraint.pm index f8913c9..505b2a8 100644 --- a/lib/SQL/Translator/Object/Constraint.pm +++ b/lib/SQL/Translator/Object/Constraint.pm @@ -2,7 +2,8 @@ use MooseX::Declare; class SQL::Translator::Object::Constraint extends SQL::Translator::Object { use MooseX::Types::Moose qw(ArrayRef Bool HashRef Int Maybe Str Undef); use MooseX::MultiMethods; - use SQL::Translator::Types qw(Column MatchType Table); + use SQL::Translator::Types qw(Column MatchType Table ColumnHash IxHash); + use Tie::IxHash; has 'table' => ( is => 'rw', @@ -18,18 +19,19 @@ class SQL::Translator::Object::Constraint extends SQL::Translator::Object { ); has 'columns' => ( - traits => ['Hash'], is => 'rw', - isa => HashRef[Column], + isa => IxHash, #ColumnHash, handles => { - exists_column => 'exists', - column_ids => 'keys', - get_columns => 'values', - get_column => 'get', - add_column => 'set', - clear_columns => 'clear', + exists_column => 'EXISTS', + column_ids => 'Keys', + get_columns => 'Values', + get_column => 'FETCH', + add_column => 'STORE', + remove_column => 'DELETE', + clear_columns => 'CLEAR', }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + coerce => 1, + default => sub { Tie::IxHash->new() } ); has 'type' => ( diff --git a/lib/SQL/Translator/Object/Index.pm b/lib/SQL/Translator/Object/Index.pm index 4fad2fe..f5729a0 100644 --- a/lib/SQL/Translator/Object/Index.pm +++ b/lib/SQL/Translator/Object/Index.pm @@ -2,7 +2,8 @@ use MooseX::Declare; class SQL::Translator::Object::Index extends SQL::Translator::Object { use MooseX::Types::Moose qw(HashRef Str); use MooseX::MultiMethods; - use SQL::Translator::Types qw(Column Table); + use SQL::Translator::Types qw(Column Table ColumnHash IxHash); + use Tie::IxHash; has 'table' => ( is => 'rw', @@ -17,18 +18,19 @@ class SQL::Translator::Object::Index extends SQL::Translator::Object { ); has 'columns' => ( - traits => ['Hash'], is => 'rw', - isa => HashRef[Column], + isa => IxHash, #ColumnHash, handles => { - exists_column => 'exists', - column_ids => 'keys', - get_columns => 'values', - get_column => 'get', - add_column => 'set', - clear_columns => 'clear', + exists_column => 'EXISTS', + column_ids => 'Keys', + get_columns => 'Values', + get_column => 'FETCH', + add_column => 'STORE', + remove_column => 'DELETE', + clear_columns => 'CLEAR', }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + coerce => 1, + default => sub { Tie::IxHash->new() } ); has 'type' => ( diff --git a/lib/SQL/Translator/Object/Schema.pm b/lib/SQL/Translator/Object/Schema.pm index 80f268d..547308a 100644 --- a/lib/SQL/Translator/Object/Schema.pm +++ b/lib/SQL/Translator/Object/Schema.pm @@ -1,8 +1,9 @@ use MooseX::Declare; class SQL::Translator::Object::Schema extends SQL::Translator::Object { use MooseX::Types::Moose qw(HashRef Maybe Str); - use SQL::Translator::Types qw(Procedure Table Trigger View); use MooseX::MultiMethods; + use Tie::IxHash; + use SQL::Translator::Types qw(Procedure Table Trigger View ProcedureHash TableHash TriggerHash ViewHash IxHash); has 'name' => ( is => 'rw', diff --git a/lib/SQL/Translator/Object/Table.pm b/lib/SQL/Translator/Object/Table.pm index 878cd11..a377f29 100644 --- a/lib/SQL/Translator/Object/Table.pm +++ b/lib/SQL/Translator/Object/Table.pm @@ -2,9 +2,10 @@ use MooseX::Declare; class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { use MooseX::Types::Moose qw(Any Bool HashRef Int Str); use MooseX::MultiMethods; - use SQL::Translator::Types qw(Column Constraint Index Schema Sequence); + use SQL::Translator::Types qw(Column Constraint Index Schema Sequence ColumnHash ConstraintHash IndexHash SequenceHash IxHash); use SQL::Translator::Object::Column; use SQL::Translator::Object::Constraint; + use Tie::IxHash; clean; use overload @@ -20,64 +21,65 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { ); has 'columns' => ( - traits => ['Hash'], is => 'rw', - isa => HashRef[Column], + isa => IxHash, #ColumnHash, handles => { - exists_column => 'exists', - column_ids => 'keys', - get_columns => 'values', - get_column => 'get', - add_column => 'set', - remove_column => 'delete', - clear_columns => 'clear', + exists_column => 'EXISTS', + column_ids => 'Keys', + get_columns => 'Values', + get_column => 'FETCH', + add_column => 'STORE', + remove_column => 'DELETE', + has_columns => 'Length', + clear_columns => 'CLEAR', }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + coerce => 1, + default => sub { Tie::IxHash->new() } ); has 'indexes' => ( - traits => ['Hash'], is => 'rw', - isa => HashRef[Index], + isa => IxHash, #IndexHash, handles => { - exists_index => 'exists', - index_ids => 'keys', - get_indices => 'values', - get_index => 'get', - add_index => 'set', - remove_index => 'delete', + exists_index => 'EXISTS', + index_ids => 'Keys', + get_indices => 'Values', + get_index => 'FETCH', + add_index => 'STORE', + remove_index => 'DELETE', }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + coerce => 1, + default => sub { Tie::IxHash->new() } ); has 'constraints' => ( - traits => ['Hash'], is => 'rw', - isa => HashRef[Constraint], + isa => IxHash, #ConstraintHash, handles => { - exists_constraint => 'exists', - constraint_ids => 'keys', - get_constraints => 'values', - get_constraint => 'get', - add_constraint => 'set', - remove_constraint => 'delete', + exists_constraint => 'EXISTS', + constraint_ids => 'Keys', + get_constraints => 'Values', + get_constraint => 'FETCH', + add_constraint => 'STORE', + remove_constraint => 'DELETE', }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + coerce => 1, + default => sub { Tie::IxHash->new() } ); has 'sequences' => ( - traits => ['Hash'], is => 'rw', - isa => HashRef[Sequence], + isa => IxHash, #SequenceHash, handles => { - exists_sequence => 'exists', - sequence_ids => 'keys', - get_sequences => 'values', - get_sequence => 'get', - add_sequence => 'set', - remove_sequence => 'delete', + exists_sequence => 'EXISTS', + sequence_ids => 'Keys', + get_sequences => 'Values', + get_sequence => 'FETCH', + add_sequence => 'STORE', + remove_sequence => 'DELETE', }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + coerce => 1, + default => sub { Tie::IxHash->new() }, ); has 'schema' => ( @@ -189,7 +191,6 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { my $fields = delete $args->{fields}; - tie %{$args->{columns}}, 'Tie::IxHash'; $args->{columns}{$_} = SQL::Translator::Object::Column->new( name => $_ ) for @$fields; return $args; diff --git a/lib/SQL/Translator/Object/Trigger.pm b/lib/SQL/Translator/Object/Trigger.pm index c212be8..f0f0730 100644 --- a/lib/SQL/Translator/Object/Trigger.pm +++ b/lib/SQL/Translator/Object/Trigger.pm @@ -2,7 +2,8 @@ use MooseX::Declare; class SQL::Translator::Object::Trigger extends SQL::Translator::Object { use MooseX::Types::Moose qw(Any ArrayRef HashRef Int Str); use MooseX::MultiMethods; - use SQL::Translator::Types qw(Column Schema Table); + use SQL::Translator::Types qw(Column Schema Table ColumnHash IxHash); + use Tie::IxHash; has 'schema' => ( is => 'rw', @@ -23,18 +24,19 @@ class SQL::Translator::Object::Trigger extends SQL::Translator::Object { ); has 'columns' => ( - traits => ['Hash'], is => 'rw', - isa => HashRef[Column], + isa => IxHash, #ColumnHash, handles => { - exists_column => 'exists', - column_ids => 'keys', - get_columns => 'values', - get_column => 'get', - add_column => 'set', - clear_columns => 'clear', + exists_column => 'EXISTS', + column_ids => 'Keys', + get_columns => 'Values', + get_column => 'FETCH', + add_column => 'STORE', + remove_column => 'DELETE', + clear_columns => 'CLEAR', }, - default => sub { my %hash = (); tie %hash, 'Tie::IxHash'; return \%hash }, + coerce => 1, + default => sub { Tie::IxHash->new() } ); has 'on_table' => ( diff --git a/lib/SQL/Translator/Types.pm b/lib/SQL/Translator/Types.pm index 3a6f846..143f31b 100644 --- a/lib/SQL/Translator/Types.pm +++ b/lib/SQL/Translator/Types.pm @@ -1,8 +1,11 @@ use MooseX::Declare; class SQL::Translator::Types { use MooseX::Types::Moose qw(ArrayRef CodeRef HashRef Int Maybe Str Undef); + use MooseX::Types::Parameterizable qw(Parameterizable); use MooseX::Types -declare, [qw(Column Constraint ForeignKey Index PrimaryKey Procedure Schema Sequence Table Trigger View - Bit DBIHandle MatchType Parser Producer Translator DBICSchema)]; + Bit DBIHandle MatchType Parser Producer Translator DBICSchema IxHash + ColumnHash ConstraintHash IndexHash ProcedureHash SequenceHash TableHash TriggerHash ViewHash)]; + use Tie::IxHash; class_type DBICSchema, { class => 'DBIx::Class::Schema' }; @@ -57,5 +60,16 @@ class SQL::Translator::Types { sub coerce_dbihandle_from_str { } sub coerce_dbihandle_from_arrayref { } sub coerce_dbihandle_from_coderef { } - + + subtype IxHash, as 'Tie::IxHash'; + coerce IxHash, from HashRef, via { Tie::IxHash->new($_) }; + + subtype ColumnHash, as Parameterizable[IxHash, Maybe[Column]]; + subtype ConstraintHash, as Parameterizable[IxHash, Maybe[Constraint]]; + subtype IndexHash, as Parameterizable[IxHash, Maybe[Index]]; + subtype ProcedureHash, as Parameterizable[IxHash, Maybe[Procedure]]; + subtype SequenceHash, as Parameterizable[IxHash, Maybe[Sequence]]; + subtype TableHash, as Parameterizable[IxHash, Maybe[Table]]; + subtype TriggerHash, as Parameterizable[IxHash, Maybe[Trigger]]; + subtype ViewHash, as Parameterizable[IxHash, Maybe[View]]; }