File Coverage

blib/lib/DBIx/Lite/Row.pm
Criterion Covered Total %
statement 33 63 52.3
branch 8 24 33.3
condition 0 2 0.0
subroutine 8 15 53.3
pod 5 5 100.0
total 54 109 49.5


line stmt bran cond sub pod time code
1             package DBIx::Lite::Row;
2             $DBIx::Lite::Row::VERSION = '0.31';
3 3     3   11 use strict;
  3         5  
  3         75  
4 3     3   10 use warnings;
  3         3  
  3         85  
5              
6 3     3   11 use Carp qw(croak);
  3         4  
  3         196  
7 3     3   13 use Clone qw(clone);
  3         6  
  3         103  
8 3     3   11 use vars qw($AUTOLOAD);
  3         3  
  3         1735  
9             $Carp::Internal{$_}++ for __PACKAGE__;
10              
11             sub _pk {
12 0     0   0 my $self = shift;
13 0         0 my $selfs = $self->__dbix_lite_row_storage;
14            
15             my @keys = $selfs->{table}->pk
16 0 0       0 or croak "No primary key defined for table " . $selfs->{table}{name};
17            
18             grep(!exists $selfs->{data}{$_}, @keys)
19 0 0       0 and croak "No primary key data retrieved for table " . $selfs->{table}{name};
20            
21 0         0 return { map +($_ => $selfs->{data}{$_}), @keys };
22             }
23              
24 12     12   22 sub __dbix_lite_row_storage { $_[0] }
25              
26             sub hashref {
27 0     0 1 0 my $self = shift;
28 0         0 my $selfs = $self->__dbix_lite_row_storage;
29            
30 0         0 return clone $selfs->{data};
31             }
32              
33             sub update {
34 0     0 1 0 my $self = shift;
35 0 0       0 my $update_cols = shift or croak "update() requires a hashref";
36 0         0 my $selfs = $self->__dbix_lite_row_storage;
37            
38 0         0 $selfs->{dbix_lite}->table($selfs->{table}{name})->search($self->_pk)->update($update_cols);
39 0         0 $selfs->{data}{$_} = $update_cols->{$_} for keys %$update_cols;
40 0         0 $self;
41             }
42              
43             sub delete {
44 0     0 1 0 my $self = shift;
45 0         0 my $selfs = $self->__dbix_lite_row_storage;
46            
47 0         0 $selfs->{dbix_lite}->table($selfs->{table}{name})->search($self->_pk)->delete;
48 0         0 undef $self;
49             }
50              
51             sub insert_related {
52 0     0 1 0 my $self = shift;
53 0         0 my ($rel_name, $insert_cols) = @_;
54 0 0       0 $rel_name or croak "insert_related() requires a table name";
55 0   0     0 $insert_cols //= {};
56 0         0 my $selfs = $self->__dbix_lite_row_storage;
57            
58             my ($table_name, $my_key, $their_key) = $self->_relationship($rel_name)
59 0 0       0 or croak "No $rel_name relationship defined for " . $selfs->{table}{name};
60            
61             return $selfs->{dbix_lite}
62             ->table($table_name)
63 0         0 ->insert({ $their_key => $selfs->{data}{$my_key}, %$insert_cols });
64             }
65              
66             sub _relationship {
67 4     4   8 my $self = shift;
68 4         8 my ($rel_name) = @_;
69 4         9 my $selfs = $self->__dbix_lite_row_storage;
70            
71 4 50       29 my ($rel_type) = grep $selfs->{table}{$_}{$rel_name}, qw(has_one has_many)
72             or return ();
73            
74 4         11 my $rel = $selfs->{table}{$rel_type}{$rel_name};
75 4         7 my ($table_name, $my_key, $their_key) = ($rel->[0], %{ $rel->[1] });
  4         19  
76            
77             exists $selfs->{data}{$my_key}
78 4 50       12 or croak "No $my_key key retrieved from " . $selfs->{table}{name};
79            
80 4         21 return ($table_name, $my_key, $their_key, $rel_type);
81             }
82              
83             sub get {
84 0     0 1 0 my $self = shift;
85 0 0       0 my $key = shift or croak "get() requires a column name";
86 0         0 my $selfs = $self->__dbix_lite_row_storage;
87            
88 0         0 return $selfs->{data}{$key};
89             }
90              
91             sub AUTOLOAD {
92 9 50   9   2242 my $self = shift or return undef;
93 9         27 my $selfs = $self->__dbix_lite_row_storage;
94            
95             # Get the called method name and trim off the namespace
96 9         67 (my $method = $AUTOLOAD) =~ s/.*:://;
97            
98 9 100       33 if (exists $selfs->{data}{$method}) {
99 5         31 return $selfs->{data}{$method};
100             }
101            
102 4 50       16 if (my ($table_name, $my_key, $their_key, $rel_type) = $self->_relationship($method)) {
103             my $rs = $selfs->{dbix_lite}
104             ->table($table_name)
105 4         22 ->search({ "me.$their_key" => $selfs->{data}{$my_key} });
106 4 100       32 return $rel_type eq 'has_many' ? $rs : $rs->single;
107             }
108            
109             croak sprintf "No %s method is provided by this %s (%s) object",
110 0           $method, ref($self), $selfs->{table}{name};
111             }
112              
113       0     sub DESTROY {}
114              
115             1;
116              
117             __END__