File Coverage

blib/lib/DBIx/Lite/Row.pm
Criterion Covered Total %
statement 33 65 50.7
branch 8 24 33.3
condition 0 2 0.0
subroutine 8 16 50.0
pod 6 6 100.0
total 55 113 48.6


line stmt bran cond sub pod time code
1             package DBIx::Lite::Row;
2             $DBIx::Lite::Row::VERSION = '0.32';
3 3     3   23 use strict;
  3         6  
  3         93  
4 3     3   16 use warnings;
  3         5  
  3         130  
5              
6 3     3   21 use Carp qw(croak);
  3         5  
  3         188  
7 3     3   19 use Clone qw(clone);
  3         6  
  3         134  
8 3     3   18 use vars qw($AUTOLOAD);
  3         6  
  3         2840  
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   21 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         8 my $selfs = $self->__dbix_lite_row_storage;
70            
71 4 50       48 my ($rel_type) = grep $selfs->{table}{$_}{$rel_name}, qw(has_one has_many)
72             or return ();
73            
74 4         9 my $rel = $selfs->{table}{$rel_type}{$rel_name};
75 4         7 my ($table_name, $my_key, $their_key) = ($rel->[0], %{ $rel->[1] });
  4         16  
76            
77             exists $selfs->{data}{$my_key}
78 4 50       11 or croak "No $my_key key retrieved from " . $selfs->{table}{name};
79            
80 4         19 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   1945 my $self = shift or return undef;
93 9         25 my $selfs = $self->__dbix_lite_row_storage;
94            
95             # Get the called method name and trim off the namespace
96 9         61 (my $method = $AUTOLOAD) =~ s/.*:://;
97            
98 9 100       36 if (exists $selfs->{data}{$method}) {
99 5         28 return $selfs->{data}{$method};
100             }
101            
102 4 50       11 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         15 ->search({ "me.$their_key" => $selfs->{data}{$my_key} });
106 4 100       24 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             sub db {
114 0     0 1   my ($self) = @_;
115              
116 0           return $self->__dbix_lite_row_storage->{dbix_lite};
117             }
118              
119       0     sub DESTROY {}
120              
121             1;
122              
123             __END__