X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FTranslator%2FSchema%2FField.pm;h=93d25e29312ed557efee6e1495fbf1196535bb8d;hb=8742e4084f9e446f87223a5fdfc62de5f463c5f3;hp=f306ff407dd0c086e0e5260dd31b4fbcba6ee51c;hpb=4ee57231353e8d71dd88494429fab1a7a293632b;p=dbsrgits%2FSQL-Translator.git diff --git a/lib/SQL/Translator/Schema/Field.pm b/lib/SQL/Translator/Schema/Field.pm index f306ff4..93d25e2 100644 --- a/lib/SQL/Translator/Schema/Field.pm +++ b/lib/SQL/Translator/Schema/Field.pm @@ -1,7 +1,7 @@ package SQL::Translator::Schema::Field; # ---------------------------------------------------------------------- -# $Id: Field.pm,v 1.19 2004-07-09 00:46:28 grommit Exp $ +# $Id: Field.pm,v 1.27 2007-10-24 10:55:44 schiffbruechige Exp $ # ---------------------------------------------------------------------- # Copyright (C) 2002-4 SQLFairy Authors # @@ -43,14 +43,14 @@ C is the field object. =cut use strict; -use Class::Base; use SQL::Translator::Schema::Constants; use SQL::Translator::Utils 'parse_list_arg'; -use base 'Class::Base'; +use base 'SQL::Translator::Schema::Object'; + use vars qw($VERSION $TABLE_COUNT $VIEW_COUNT); -$VERSION = sprintf "%d.%02d", q$Revision: 1.19 $ =~ /(\d+)\.(\d+)/; +$VERSION = sprintf "%d.%02d", q$Revision: 1.27 $ =~ /(\d+)\.(\d+)/; # Stringify to our name, being careful not to pass any args through so we don't # accidentally set it to undef. We also have to tweak bool so the object is @@ -61,8 +61,45 @@ use overload fallback => 1, ; +use DBI qw(:sql_types); + +# Mapping from string to sql contstant +our %type_mapping = ( + integer => SQL_INTEGER, + int => SQL_INTEGER, + + smallint => SQL_SMALLINT, + bigint => 9999, # DBI doesn't export a constatn for this. Le suck + + double => SQL_DOUBLE, + + decimal => SQL_DECIMAL, + numeric => SQL_NUMERIC, + dec => SQL_DECIMAL, + + bit => SQL_BIT, + + date => SQL_DATE, + datetime => SQL_DATETIME, + timestamp => SQL_TIMESTAMP, + time => SQL_TIME, + + char => SQL_CHAR, + varchar => SQL_VARCHAR, + binary => SQL_BINARY, + varbinary => SQL_VARBINARY, + tinyblob => SQL_BLOB, + blob => SQL_BLOB, + text => SQL_LONGVARCHAR + +); # ---------------------------------------------------------------------- -sub init { + +__PACKAGE__->_attributes( qw/ + table name data_type size is_primary_key is_nullable + is_auto_increment default_value comments is_foreign_key + is_unique order sql_data_type +/); =pod @@ -77,21 +114,6 @@ Object constructor. =cut - my ( $self, $config ) = @_; - - for my $arg ( - qw[ - table name data_type size is_primary_key is_nullable - is_auto_increment default_value comments extra - ] - ) { - next unless defined $config->{ $arg }; - defined $self->$arg( $config->{ $arg } ) or return; - } - - return $self; -} - # ---------------------------------------------------------------------- sub comments { @@ -142,10 +164,28 @@ Get or set the field's data type. =cut my $self = shift; - $self->{'data_type'} = shift if @_; + if (@_) { + $self->{'data_type'} = $_[0]; + $self->{'sql_data_type'} = $type_mapping{lc $_[0]} || SQL_UNKNOWN_TYPE unless exists $self->{sql_data_type}; + } return $self->{'data_type'} || ''; } +sub sql_data_type { + +=head2 sql_data_type + +Constant from DBI package representing this data type. See L +for more details. + +=cut + + my $self = shift; + $self->{sql_data_type} = shift if @_; + return $self->{sql_data_type} || 0; + +} + # ---------------------------------------------------------------------- sub default_value { @@ -167,8 +207,6 @@ assume an error like other methods. } # ---------------------------------------------------------------------- -sub extra { - =pod =head2 extra @@ -181,15 +219,6 @@ Accepts a hash(ref) of name/value pairs to store; returns a hash. =cut - my $self = shift; - my $args = ref $_[0] eq 'HASH' ? shift : { @_ }; - - while ( my ( $key, $value ) = each %$args ) { - $self->{'extra'}{ $key } = $value; - } - - return %{ $self->{'extra'} || {} }; -} # ---------------------------------------------------------------------- sub foreign_key_reference { @@ -569,6 +598,64 @@ also be used to get the table name. return $self->{'table'}; } +sub parsed_field { + +=head2 + +Returns the field exactly as the parser found it + +=cut + + my $self = shift; + + if (@_) { + my $value = shift; + $self->{parsed_field} = $value; + return $value || $self; + } + return $self->{parsed_field} || $self; +} + +# ---------------------------------------------------------------------- +sub equals { + +=pod + +=head2 equals + +Determines if this field is the same as another + + my $isIdentical = $field1->equals( $field2 ); + +=cut + + my $self = shift; + my $other = shift; + my $case_insensitive = shift; + + return 0 unless $self->SUPER::equals($other); + return 0 unless $case_insensitive ? uc($self->name) eq uc($other->name) : $self->name eq $other->name; + + # Comparing types: use sql_data_type if both are not 0. Else use string data_type + if ($self->sql_data_type && $other->sql_data_type) { + return 0 unless $self->sql_data_type == $other->sql_data_type + } else { + return 0 unless lc($self->data_type) eq lc($other->data_type) + } + + return 0 unless $self->size eq $other->size; + return 0 unless (!defined $self->default_value || $self->default_value eq 'NULL') eq (!defined $other->default_value || $other->default_value eq 'NULL'); + return 0 if defined $self->default_value && $self->default_value ne $other->default_value; + return 0 unless $self->is_nullable eq $other->is_nullable; +# return 0 unless $self->is_unique eq $other->is_unique; + return 0 unless $self->is_primary_key eq $other->is_primary_key; +# return 0 unless $self->is_foreign_key eq $other->is_foreign_key; + return 0 unless $self->is_auto_increment eq $other->is_auto_increment; +# return 0 unless $self->comments eq $other->comments; + return 0 unless $self->_compare_objects(scalar $self->extra, scalar $other->extra); + return 1; +} + # ---------------------------------------------------------------------- sub DESTROY { #