X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FSchema%2FField.pm;h=a88263233452ba1a7fd1b3f99942bbb3746942ab;hb=b6fda1d15abec5e34f793a266b139d374bdfa4c3;hp=a4e411017bf7961418583d4751ab0b87826299fb;hpb=c804300c39626143dea1a4295d178fabe5d7c991;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Schema/Field.pm b/lib/SQL/Translator/Schema/Field.pm index a4e4110..a882632 100644 --- a/lib/SQL/Translator/Schema/Field.pm +++ b/lib/SQL/Translator/Schema/Field.pm @@ -22,11 +22,12 @@ C is the field object. =cut -use Moo 1.000003; +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 SQL::Translator::Utils qw(parse_list_arg ex2err throw carp_ro); use Sub::Quote qw(quote_sub); +use Scalar::Util (); extends 'SQL::Translator::Schema::Object'; @@ -43,19 +44,24 @@ use overload use DBI qw(:sql_types); -# Mapping from string to sql contstant +# Mapping from string to sql constant our %type_mapping = ( integer => SQL_INTEGER, int => SQL_INTEGER, + tinyint => SQL_TINYINT, smallint => SQL_SMALLINT, - bigint => 9999, # DBI doesn't export a constatn for this. Le suck + bigint => SQL_BIGINT, double => SQL_DOUBLE, + 'double precision' => SQL_DOUBLE, decimal => SQL_DECIMAL, - numeric => SQL_NUMERIC, dec => SQL_DECIMAL, + numeric => SQL_NUMERIC, + + real => SQL_REAL, + float => SQL_FLOAT, bit => SQL_BIT, @@ -74,6 +80,16 @@ our %type_mapping = ( ); +has _numeric_sql_data_types => ( is => 'lazy' ); + +sub _build__numeric_sql_data_types { + return { + map { $_ => 1 } + (SQL_INTEGER, SQL_TINYINT, SQL_SMALLINT, SQL_BIGINT, SQL_DOUBLE, + SQL_NUMERIC, SQL_DECIMAL, SQL_FLOAT, SQL_REAL) + }; +} + =head2 new Object constructor. @@ -152,16 +168,6 @@ assume an error like other methods. has default_value => ( is => 'rw' ); -=head2 extra - -Get or set the field's "extra" attibutes (e.g., "ZEROFILL" for MySQL). -Accepts a hash(ref) of name/value pairs to store; returns a hash. - - $field->extra( qualifier => 'ZEROFILL' ); - my %extra = $field->extra; - -=cut - =head2 foreign_key_reference Get or set the field's foreign key reference; @@ -258,7 +264,7 @@ sub _build_is_foreign_key { Get or set whether the field can be null. If not defined, then returns "1" (assumes the field can be null). The argument is evaluated -by Perl for True or False, so the following are eqivalent: +by Perl for True or False, so the following are equivalent: $is_nullable = $field->is_nullable(0); $is_nullable = $field->is_nullable(''); @@ -321,6 +327,8 @@ Determine whether the field has a UNIQUE constraint or not. has is_unique => ( is => 'lazy', init_arg => undef ); +around is_unique => carp_ro('is_unique'); + sub _build_is_unique { my ( $self ) = @_; @@ -494,7 +502,7 @@ has table => ( is => 'rw', isa => schema_obj('Table'), weak_ref => 1 ); around table => \&ex2err; -=head2 +=head2 parsed_field Returns the field exactly as the parser found it @@ -550,7 +558,14 @@ around equals => sub { my $effective_lhs = $lhs_is_ref ? $$lhs : $lhs; my $effective_rhs = $rhs_is_ref ? $$rhs : $rhs; - return 0 if $effective_lhs ne $effective_rhs; + if ( $self->_is_numeric_data_type + && Scalar::Util::looks_like_number($effective_lhs) + && Scalar::Util::looks_like_number($effective_rhs) ) { + return 0 if ($effective_lhs + 0) != ($effective_rhs + 0); + } + else { + return 0 if $effective_lhs ne $effective_rhs; + } } return 0 unless $self->is_nullable eq $other->is_nullable; @@ -566,6 +581,11 @@ around equals => sub { # Must come after all 'has' declarations around new => \&ex2err; +sub _is_numeric_data_type { + my $self = shift; + return $self->_numeric_sql_data_types->{ $self->sql_data_type }; +} + 1; =pod