From: Andrew Rodland Date: Tue, 9 Mar 2010 19:29:50 +0000 (+0000) Subject: Support add_columns('+colname'=>{...}) syntax to augment column definitions. X-Git-Tag: v0.08121~70 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=157ce0cfdee831491de41596b44e19cd7b45dfeb;p=dbsrgits%2FDBIx-Class.git Support add_columns('+colname'=>{...}) syntax to augment column definitions. --- diff --git a/Changes b/Changes index 08728f8..b300e60 100644 --- a/Changes +++ b/Changes @@ -19,6 +19,8 @@ Revision history for DBIx::Class attribute - Fix ambiguity in default directory handling of create_ddl_dir (RT#54063) + - Support add_columns('+colname' => { ... }) to augment column + definitions. 0.08120 2010-02-24 08:58:00 (UTC) - Make sure possibly overwritten deployment_statements methods in diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm index 56f94dc..0207ec8 100644 --- a/lib/DBIx/Class.pm +++ b/lib/DBIx/Class.pm @@ -276,6 +276,8 @@ gphat: Cory G Watson groditi: Guillermo Roditi +hobbs: Andrew Rodland + ilmari: Dagfinn Ilmari MannsEker jasonmay: Jason May diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index ebbf960..d872d1c 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -139,6 +139,13 @@ The column names given will be created as accessor methods on your L objects. You can change the name of the accessor by supplying an L in the column_info hash. +If a column name beginning with a plus sign ('+col1') is provided, the +attributes provided will be merged with any existing attributes for the +column, with the new attributes taking precedence in the case that an +attribute already exists. Using this without a hashref +(C<< $source->add_columns(qw/+col1 +col2/) >>) is legal, but useless -- +it does the same thing it would do without the plus. + The contents of the column_info are not set in stone. The following keys are currently recognised/used by DBIx::Class: @@ -288,9 +295,17 @@ sub add_columns { my @added; my $columns = $self->_columns; while (my $col = shift @cols) { + my $column_info = {}; + if ($col =~ s/^\+//) { + $column_info = $self->column_info($col); + } + # If next entry is { ... } use that for the column info, if not # use an empty hashref - my $column_info = ref $cols[0] ? shift(@cols) : {}; + if (ref $cols[0]) { + my $new_info = shift(@cols); + %$column_info = (%$column_info, %$new_info); + } push(@added, $col) unless exists $columns->{$col}; $columns->{$col} = $column_info; } diff --git a/lib/DBIx/Class/ResultSourceProxy.pm b/lib/DBIx/Class/ResultSourceProxy.pm index 6af0202..feb0a59 100644 --- a/lib/DBIx/Class/ResultSourceProxy.pm +++ b/lib/DBIx/Class/ResultSourceProxy.pm @@ -37,6 +37,9 @@ sub add_columns { my $source = $class->result_source_instance; $source->add_columns(@cols); foreach my $c (grep { !ref } @cols) { + # If this is an augment definition get the real colname. + $c =~ s/^\+//; + $class->register_column($c => $source->column_info($c)); } } diff --git a/t/lib/DBICTest/Schema/Event.pm b/t/lib/DBICTest/Schema/Event.pm index 0c477c8..d5bd0be 100644 --- a/t/lib/DBICTest/Schema/Event.pm +++ b/t/lib/DBICTest/Schema/Event.pm @@ -15,12 +15,22 @@ __PACKAGE__->add_columns( starts_at => { data_type => 'date' }, created_on => { data_type => 'timestamp' }, - varchar_date => { data_type => 'varchar', inflate_date => 1, size => 20, is_nullable => 1 }, - varchar_datetime => { data_type => 'varchar', inflate_datetime => 1, size => 20, is_nullable => 1 }, + varchar_date => { data_type => 'varchar', size => 20, is_nullable => 1 }, + varchar_datetime => { data_type => 'varchar', size => 20, is_nullable => 1 }, skip_inflation => { data_type => 'datetime', inflate_datetime => 0, is_nullable => 1 }, ts_without_tz => { data_type => 'datetime', is_nullable => 1 }, # used in EventTZPg ); __PACKAGE__->set_primary_key('id'); +# Test add_columns '+colname' to augment a column definition. +__PACKAGE__->add_columns( + '+varchar_date' => { + inflate_date => 1, + }, + '+varchar_datetime' => { + inflate_datetime => 1, + }, +); + 1;