1 package DBIx::Class::InflateColumn;
7 use base qw/DBIx::Class::Row/;
11 DBIx::Class::InflateColumn - Automatically create objects from column data
15 # In your table classes
16 __PACKAGE__->inflate_column('column_name', {
17 inflate => sub { ... },
18 deflate => sub { ... },
23 This component translates column data into objects, i.e. "inflating"
24 the column data. It also "deflates" objects into an appropriate format
27 It can be used, for example, to automatically convert to and from
28 L<DateTime> objects for your date and time fields.
34 Instruct L<DBIx::Class> to inflate the given column.
36 In addition to the column name, you must provide C<inflate> and
37 C<deflate> methods. The C<inflate> method is called when you access
38 the field, while the C<deflate> method is called when the field needs
39 to used by the database.
41 For example, if you have a table C<events> with a timestamp field
42 named C<insert_time>, you could inflate the column in the
43 corresponding table class using something like:
45 __PACKAGE__->inflate_column('insert_time', {
46 inflate => sub { DateTime::Format::Pg->parse_datetime(shift); },
47 deflate => sub { DateTime::Format::Pg->format_datetime(shift); },
50 (Replace L<DateTime::Format::Pg> with the appropriate module for your
51 database, or consider L<DateTime::Format::DBI>.)
53 The coderefs you set for inflate and deflate are called with two parameters,
54 the first is the value of the column to be inflated/deflated, the second is the
55 row object itself. Thus you can call C<< ->result_source->schema->storage->dbh >> on
56 it, to feed to L<DateTime::Format::DBI>.
58 In this example, calls to an event's C<insert_time> accessor return a
59 L<DateTime> object. This L<DateTime> object is later "deflated" when
60 used in the database layer.
65 my ($self, $col, $attrs) = @_;
66 $self->throw_exception("No such column $col to inflate")
67 unless $self->has_column($col);
68 $self->throw_exception("inflate_column needs attr hashref")
69 unless ref $attrs eq 'HASH';
70 $self->column_info($col)->{_inflate_info} = $attrs;
71 $self->mk_group_accessors('inflated_column' => $col);
75 sub _inflated_column {
76 my ($self, $col, $value) = @_;
77 return $value unless defined $value; # NULL is NULL is NULL
78 my $info = $self->column_info($col)
79 or $self->throw_exception("No column info for $col");
80 return $value unless exists $info->{_inflate_info};
81 my $inflate = $info->{_inflate_info}{inflate};
82 $self->throw_exception("No inflator for $col") unless defined $inflate;
83 return $inflate->($value, $self);
87 my ($self, $col) = @_;
88 return if exists $self->{_column_data}{$col};
89 my $value = $self->{_inflated_column}{$col};
91 my $info = $self->column_info($col) or
92 $self->throw_exception("No column info for $col");
93 if (exists $info->{_inflate_info}) {
94 my $deflate = $info->{_inflate_info}{deflate};
95 $self->throw_exception("No deflator for $col") unless defined $deflate;
96 $value = $deflate->($value, $self);
99 $self->store_column($col, $value);
102 =head2 get_inflated_column
104 my $val = $obj->get_inflated_column($col);
106 Fetch a column value in its inflated state. This is directly
107 analogous to L<DBIx::Class::Row/get_column> in that it only fetches a
108 column already retreived from the database, and then inflates it.
109 Throws an exception if the column requested is not an inflated column.
113 sub get_inflated_column {
114 my ($self, $col) = @_;
115 $self->throw_exception("$col is not an inflated column")
116 unless exists $self->column_info($col)->{_inflate_info};
118 return $self->{_inflated_column}{$col}
119 if exists $self->{_inflated_column}{$col};
120 return $self->{_inflated_column}{$col} =
121 $self->_inflated_column($col, $self->get_column($col));
124 =head2 set_inflated_column
126 my $copy = $obj->set_inflated_column($col => $val);
128 Sets a column value from an inflated value. This is directly
129 analogous to L<DBIx::Class::Row/set_column>.
133 sub set_inflated_column {
134 my ($self, $col, $obj) = @_;
135 my $old = $self->get_inflated_column($col);
136 my $ret = $self->store_inflated_column($col, $obj);
137 $self->{_dirty_columns}{$col} = 1
138 if (defined $old ^ defined $ret) || (defined $old && $old ne $ret);
142 =head2 store_inflated_column
144 my $copy = $obj->store_inflated_column($col => $val);
146 Sets a column value from an inflated value without marking the column
147 as dirty. This is directly analogous to L<DBIx::Class::Row/store_column>.
151 sub store_inflated_column {
152 my ($self, $col, $obj) = @_;
154 delete $self->{_column_data}{$col};
155 return $self->{_inflated_column}{$col} = $obj;
157 delete $self->{_inflated_column}{$col};
158 return $self->store_column($col, $obj);
164 Gets a column value in the same way as L<DBIx::Class::Row/get_column>. If there
165 is an inflated value stored that has not yet been deflated, it is deflated
166 when the method is invoked.
171 my ($self, $col) = @_;
172 $self->_deflate_column($col);
173 return $self->next::method($col);
178 if (exists $self->{_inflated_column}) {
179 $self->_deflate_column($_) for keys %{$self->{_inflated_column}};
181 return $self->next::method;
184 sub has_column_loaded {
185 my ($self, $col) = @_;
186 return 1 if exists $self->{_inflated_column}{$col};
187 return $self->next::method($col);
192 Updates a row in the same way as L<DBIx::Class::Row/update>, handling
193 inflation and deflation of columns appropriately.
198 my ($class, $attrs, @rest) = @_;
199 foreach my $key (keys %{$attrs||{}}) {
200 if (ref $attrs->{$key}
201 && exists $class->column_info($key)->{_inflate_info}) {
202 $class->set_inflated_column($key, delete $attrs->{$key});
205 return $class->next::method($attrs, @rest);
210 Creates a row in the same way as L<DBIx::Class::Row/new>, handling
211 inflation and deflation of columns appropriately.
216 my ($class, $attrs, @rest) = @_;
218 foreach my $key (keys %{$attrs||{}}) {
219 $inflated->{$key} = delete $attrs->{$key}
220 if ref $attrs->{$key} && exists $class->column_info($key)->{_inflate_info};
222 my $obj = $class->next::method($attrs, @rest);
223 $obj->{_inflated_column} = $inflated if $inflated;
231 =item L<DBIx::Class::Core> - This component is loaded as part of the
232 "core" L<DBIx::Class> components; generally there is no need to
239 Matt S. Trout <mst@shadowcatsystems.co.uk>
243 Daniel Westermann-Clark <danieltwc@cpan.org> (documentation)
247 You may distribute this code under the same terms as Perl itself.