From: Robert Bohne Date: Wed, 17 Feb 2010 13:58:26 +0000 (+0100) Subject: Add datetime_timezone/datetime_locale loader options X-Git-Tag: 0.05003~5^2~6 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=42e785fae547f52e8212d89086eec625dcfdb2bd;p=dbsrgits%2FDBIx-Class-Schema-Loader.git Add datetime_timezone/datetime_locale loader options --- diff --git a/lib/DBIx/Class/Schema/Loader/Base.pm b/lib/DBIx/Class/Schema/Loader/Base.pm index e304c7c..6895279 100644 --- a/lib/DBIx/Class/Schema/Loader/Base.pm +++ b/lib/DBIx/Class/Schema/Loader/Base.pm @@ -55,6 +55,8 @@ __PACKAGE__->mk_group_ro_accessors('simple', qw/ monikers dynamic naming + datetime_timezone + datetime_locale /); @@ -418,6 +420,16 @@ Example: Add to all columns with type DATE the attribute timezone => "Europe/Berlin". +=head2 datetime_timezone + +Set timezone attribute for L +to all columns with the type DATE. + +=head2 datetime_locale + +Set local attribute for L +to all columns with the type DATE. + =head1 METHODS None of these methods are intended for direct invocation by regular @@ -1607,6 +1619,28 @@ sub _custom_column_info { return {}; } +sub _datetime_column_info { + my ( $self, $info, $column_info) = @_; + # warn "_datetime_column_info XXXXXXXXX"; + # use Data::Dumper; + # warn Dumper $info; + # warn Dumper $column_info; + my $return = {}; + my $type = lc ( $info->{TYPE_NAME} ); + + if ( + ( defined $column_info->{inflate_datetime} and $column_info->{inflate_datetime} ) + or ( defined $column_info->{inflate_date} and $column_info->{inflate_date} ) + or ( $type eq 'date') + or ( $type eq 'datetime') + or ( $type eq 'timestamp') + ){ + $return->{timezone} = $self->datetime_timezone if $self->datetime_timezone; + $return->{locale} = $self->datetime_locale if $self->datetime_locale; + } + return $return; +} + # remove the dump dir from @INC on destruction sub DESTROY { my $self = shift; diff --git a/lib/DBIx/Class/Schema/Loader/DBI.pm b/lib/DBIx/Class/Schema/Loader/DBI.pm index 7347b61..df76ae5 100644 --- a/lib/DBIx/Class/Schema/Loader/DBI.pm +++ b/lib/DBIx/Class/Schema/Loader/DBI.pm @@ -292,7 +292,9 @@ sub _columns_info_for { my $extra_info = $self->_extra_column_info($info) || {}; my $custom_info = $self->_custom_column_info($info) || {}; - $result{$col_name} = { %column_info, %$extra_info, %$custom_info }; + my $datetime_info = $self->_datetime_column_info($info, + { %column_info, %$extra_info, %$custom_info }) || {}; + $result{$col_name} = { %column_info, %$extra_info, %$custom_info, %$datetime_info }; } $sth->finish; }; diff --git a/t/lib/dbixcsl_common_tests.pm b/t/lib/dbixcsl_common_tests.pm index ebc12c1..641f702 100644 --- a/t/lib/dbixcsl_common_tests.pm +++ b/t/lib/dbixcsl_common_tests.pm @@ -71,7 +71,7 @@ sub _custom_column_info { sub run_tests { my $self = shift; - plan tests => 146 + ($self->{extra}->{count} || 0); + plan tests => 149 + ($self->{extra}->{count} || 0); $self->create(); @@ -117,6 +117,8 @@ sub setup_schema { debug => $debug, use_namespaces => 0, dump_directory => $DUMP_DIR, + datetime_timezone => 'Europe/Berlin', + datetime_locale => 'de_DE' ); $loader_opts{db_schema} = $self->{db_schema} if $self->{db_schema}; @@ -137,7 +139,7 @@ sub setup_schema { my $file_count; find sub { return if -d; $file_count++ }, $DUMP_DIR; - my $expected_count = 35; + my $expected_count = 36; $expected_count += grep /CREATE (?:TABLE|VIEW)/i, @{ $self->{extra}{create} || [] }; @@ -444,6 +446,10 @@ sub test_schema { my $class34 = $classes->{loader_test34}; my $rsobj34 = $conn->resultset($moniker34); + my $moniker36 = $monikers->{loader_test36}; + my $class36 = $classes->{loader_test36}; + my $rsobj36 = $conn->resultset($moniker36); + isa_ok( $rsobj3, "DBIx::Class::ResultSet" ); isa_ok( $rsobj4, "DBIx::Class::ResultSet" ); isa_ok( $rsobj5, "DBIx::Class::ResultSet" ); @@ -467,6 +473,7 @@ sub test_schema { isa_ok( $rsobj32, "DBIx::Class::ResultSet" ); isa_ok( $rsobj33, "DBIx::Class::ResultSet" ); isa_ok( $rsobj34, "DBIx::Class::ResultSet" ); + isa_ok( $rsobj36, "DBIx::Class::ResultSet" ); # basic rel test my $obj4 = $rsobj4->find(123); @@ -623,6 +630,9 @@ sub test_schema { # Added by custom_column_info ok($class11->column_info('loader_test10')->{is_numeric}, 'is_numeric detected'); + is($class36->column_info('a_date')->{locale},'de_DE','locale is correct'); + is($class36->column_info('a_date')->{timezone},'Europe/Berlin','locale is correct'); + my $obj10 = $rsobj10->create({ subject => 'xyzzy' }); $obj10->update(); @@ -860,6 +870,13 @@ sub create { a_function $self->{default_function_def} ) $self->{innodb} }, + + qq{ + CREATE TABLE loader_test36 ( + id INTEGER NOT NULL PRIMARY KEY, + a_date DATE + ) $self->{innodb} + }, ); @statements_reltests = ( @@ -1257,6 +1274,7 @@ sub drop_tables { LOADER_TEST23 LoAdEr_test24 loader_test35 + loader_test36 /; my @tables_auto_inc = (