File Coverage

blib/lib/DBIx/ORM/Declarative/Row.pm
Criterion Covered Total %
statement 9 71 12.6
branch 0 28 0.0
condition 0 13 0.0
subroutine 3 7 42.8
pod 2 2 100.0
total 14 121 11.5


line stmt bran cond sub pod time code
1             package DBIx::ORM::Declarative::Row;
2              
3 1     1   6 use strict;
  1         2  
  1         36  
4 1     1   5 use Carp;
  1         2  
  1         389  
5              
6 1     1   7 use vars qw(@ISA);
  1         2  
  1         1243  
7             @ISA = qw(DBIx::ORM::Declarative::Table);
8              
9             # Mark a row for deletion
10             sub delete
11             {
12 0     0 1   my ($self) = @_;
13 0 0 0       carp "delete: not a class method" and return unless ref $self;
14 0           $self->{__deleted} = 1;
15 0           return $self;
16             }
17              
18             # Set the data items for this item
19             sub __set_data
20             {
21 0     0     my ($self, @data) = @_;
22 0 0 0       carp "not a class method" and return unless ref $self;
23              
24 0           @{$self->{__data}}{$self->_column_sql_names} = @data;
  0            
25 0           return $self;
26             }
27              
28             # How do we find this item?
29             sub __create_where
30             {
31 0     0     my ($self) = @_;
32 0 0 0       carp "not a class method" and return unless ref $self;
33              
34             # create the where clause, if we haven't already
35 0 0         if(not exists $self->{__whereinfo})
36             {
37             # Column info
38 0           my @cols = $self->_columns;
39 0           my %name2sql = $self->_column_map;
40 0           my @binds;
41             my @wclause;
42              
43             # We use either the first set of unique keys, or everything
44 0           my @un = $self->_unique_keys;
45 0           my @colv;
46 0 0         @colv = map { $name2sql{$_} } @{$un[0]} if @un;
  0            
  0            
47 0 0         @colv = map { $_->{sql_name} } @cols unless @colv;
  0            
48              
49             # Walk the list of columns
50 0           for my $k (@colv)
51             {
52             # If the data is defined, we use it. Otherwise, we use IS NULL
53 0 0         if(defined($self->{__data}{$k}))
54             {
55 0           push @wclause, "$k=?";
56 0           push @binds, $self->{__data}{$k};
57             }
58             else
59             {
60 0           push @wclause, "$k IS NULL";
61             }
62             }
63              
64             # Save the where clause and bind values
65 0           @{$self->{__whereinfo}} = (join(' AND ', @wclause), @binds);
  0            
66             }
67              
68 0           return @{$self->{__whereinfo}};
  0            
69             }
70              
71             # Save changes to the database
72             sub commit
73             {
74 0     0 1   my ($self) = @_;
75              
76             # Parameter checking
77 0 0 0       carp "commit: not a class method" and return unless ref $self;
78 0           my $handle = $self->handle;
79 0 0 0       carp "can't commit without a database handle" and return unless $handle;
80 0 0 0       return $self unless $self->{__deleted} or $self->{__dirty};
81              
82 0           my ($where, @binds) = $self->__create_where;
83 0           my $sql;
84              
85             # Create the SQL command
86 0 0         if($self->{__deleted})
87             {
88 0           $sql = "DELETE FROM " . $self->_sql_name . " WHERE $where";
89             }
90             else
91             {
92 0           my @cols = $self->_columns;
93 0           $sql = "UPDATE " . $self->_sql_name . " SET ";
94 0           $sql .= join(',', map { $_->{sql_name} . '=?' } @cols);
  0            
95 0           unshift @binds, map { $self->{__data}{$_->{sql_name}} } @cols;
  0            
96              
97 0           $sql .= " WHERE $where";
98             }
99              
100 0 0         unshift @binds, undef if @binds; # Avoid DBI lossage
101            
102             # Execute the SQL
103 0 0         if(not $handle->do($sql, @binds))
104             {
105 0           carp "Database error: ", $handle->errstr;
106 0           $self->__do_rollback;
107 0           return;
108             }
109              
110             # Clean up the object
111 0           delete $self->{__dirty};
112 0           delete $self->{__whereinfo};
113              
114             # If we've deleted this data, we're no longer a row object
115 0 0         if($self->{__deleted})
116             {
117 0           delete $self->{__data};
118 0           delete $self->{__deleted};
119 0           bless $self, $self->_class;
120             }
121             else
122             {
123             # Reload the "where" clause
124 0           $self->__create_where
125             }
126              
127             # Commit and return
128 0           local ($SIG{__WARN__}) = $self->w__noop;
129 0           $handle->commit;
130 0           return $self;
131             }
132             1;
133              
134             __END__