1 package # hide from PAUSE
2 DBIx::Class::CDBICompat::TempColumns;
6 use base 'DBIx::Class';
11 __PACKAGE__->mk_classdata('_temp_columns' => { });
13 sub _add_column_group {
14 my ($class, $group, @cols) = @_;
16 return $class->next::method($group, @cols) unless $group eq 'TEMP';
18 my %new_cols = map { $_ => 1 } @cols;
19 my %tmp_cols = %{$class->_temp_columns};
21 for my $existing_col ( grep $new_cols{$_}, $class->columns ) {
22 # Already been declared TEMP
23 next if $tmp_cols{$existing_col};
25 carp "Declaring column $existing_col as TEMP but it already exists";
28 $class->_register_column_group($group => @cols);
29 $class->mk_group_accessors('temp' => @cols);
31 $class->_temp_columns({ %tmp_cols, %new_cols });
35 my ($class, $attrs, @rest) = @_;
37 my $temp = $class->_extract_temp_data($attrs);
39 my $new = $class->next::method($attrs, @rest);
41 $new->set_temp($_, $temp->{$_}) for keys %$temp;
46 sub _extract_temp_data {
47 my($self, $data) = @_;
50 foreach my $key (keys %$data) {
51 $temp{$key} = delete $data->{$key} if $self->_temp_columns->{$key};
58 my ($class, $col, @rest) = @_;
59 return $col if $class->_temp_columns->{$col};
60 return $class->next::method($col, @rest);
64 my($self, %data) = @_;
66 my $temp_data = $self->_extract_temp_data(\%data);
68 $self->set_temp($_, $temp_data->{$_}) for keys %$temp_data;
70 return $self->next::method(%data);
74 my ($self, $column) = @_;
75 $self->throw_exception( "Can't fetch data as class method" ) unless ref $self;
76 $self->throw_exception( "No such TEMP column '${column}'" ) unless $self->_temp_columns->{$column} ;
77 return $self->{_temp_column_data}{$column}
78 if exists $self->{_temp_column_data}{$column};
83 my ($self, $column, $value) = @_;
84 $self->throw_exception( "No such TEMP column '${column}'" )
85 unless $self->_temp_columns->{$column};
86 $self->throw_exception( "set_temp called for ${column} without value" )
88 return $self->{_temp_column_data}{$column} = $value;
92 return 1 if shift->has_column(shift);