File Coverage

blib/lib/DBIx/Class/CDBICompat/ColumnsAsHash.pm
Criterion Covered Total %
statement 15 44 34.0
branch 0 10 0.0
condition n/a
subroutine 5 11 45.4
pod 0 2 0.0
total 20 67 29.8


line stmt bran cond sub pod time code
1             package
2             DBIx::Class::CDBICompat::ColumnsAsHash;
3              
4 2     2   1067 use strict;
  2         4  
  2         48  
5 2     2   9 use warnings;
  2         3  
  2         43  
6              
7 2     2   9 use base 'DBIx::Class';
  2         3  
  2         466  
8              
9             =head1 NAME
10              
11             DBIx::Class::CDBICompat::ColumnsAsHash - Emulates the behavior of Class::DBI where the object can be accessed as a hash of columns.
12              
13             =head1 SYNOPSIS
14              
15             See DBIx::Class::CDBICompat for usage directions.
16              
17             =head1 DESCRIPTION
18              
19             Emulates the I behavior of Class::DBI where the object can be accessed as a hash of columns. This is often used as a performance hack.
20              
21             my $column = $result->{column};
22              
23             =head2 Differences from Class::DBI
24              
25             If C is true it will warn when a column is accessed as a hash key.
26              
27             =cut
28              
29             sub new {
30 0     0 0   my $class = shift;
31              
32 0           my $new = $class->next::method(@_);
33              
34 0           $new->_make_columns_as_hash;
35              
36 0           return $new;
37             }
38              
39             sub inflate_result {
40 0     0 0   my $class = shift;
41              
42 0           my $new = $class->next::method(@_);
43              
44 0           $new->_make_columns_as_hash;
45              
46 0           return $new;
47             }
48              
49              
50             sub _make_columns_as_hash {
51 0     0     my $self = shift;
52              
53 0           for my $col ($self->columns) {
54 0 0         if( exists $self->{$col} ) {
55 0           warn "Skipping mapping $col to a hash key because it exists";
56             }
57              
58 0           tie $self->{$col}, 'DBIx::Class::CDBICompat::Tied::ColumnValue',
59             $self, $col;
60             }
61             }
62              
63              
64             package DBIx::Class::CDBICompat::Tied::ColumnValue;
65              
66 2     2   12 use Carp;
  2         4  
  2         122  
67 2     2   12 use Scalar::Util qw(weaken isweak);
  2         4  
  2         485  
68              
69              
70             sub TIESCALAR {
71 0     0     my($class, $obj, $col) = @_;
72 0           my $self = [$obj, $col];
73 0           weaken $self->[0];
74              
75 0           return bless $self, $_[0];
76             }
77              
78             sub FETCH {
79 0     0     my $self = shift;
80 0           my($obj, $col) = @$self;
81              
82 0           my $class = ref $obj;
83 0           my $id = $obj->id;
84             carp "Column '$col' of '$class/$id' was fetched as a hash"
85 0 0         if $ENV{DBIC_CDBICOMPAT_HASH_WARN};
86              
87             return $obj->column_info($col)->{_inflate_info}
88 0 0         ? $obj->get_inflated_column($col)
89             : $obj->get_column($col);
90             }
91              
92             sub STORE {
93 0     0     my $self = shift;
94 0           my($obj, $col) = @$self;
95              
96 0           my $class = ref $obj;
97 0           my $id = $obj->id;
98             carp "Column '$col' of '$class/$id' was stored as a hash"
99 0 0         if $ENV{DBIC_CDBICOMPAT_HASH_WARN};
100              
101             return $obj->column_info($col)->{_inflate_info}
102 0 0         ? $obj->set_inflated_column($col => shift)
103             : $obj->set_column($col => shift);
104             }
105              
106             =head1 FURTHER QUESTIONS?
107              
108             Check the list of L.
109              
110             =head1 COPYRIGHT AND LICENSE
111              
112             This module is free software L
113             by the L. You can
114             redistribute it and/or modify it under the same terms as the
115             L.
116              
117             =cut
118              
119             1;