DS/Transformer/Update.pm untested

filter: Statementstmt 0.0% 0 / 43 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 61 CC 4 SCAR 19.1
CC
4
Cov
0%
process
12.0
new
6.0
BEGIN
2.0
CRAP 6.8
1 #!perl
2
3 # ########################################################################## #
4 # Title: Update rows - processor
5 # Creation date: 2007-03-05
6 # Author: Michael Zedeler
7 # Description: Updates rows found in stream
8 # Data Stream class
9 # File: $Source: /data/cvs/lib/DSlib/lib/DS/Transformer/Update.pm,v $
10 # Repository: kronhjorten
11 # State: $State: Exp $
12 # Documentation: inline
13 # Recepient: -
14 # ########################################################################## #
15
16 package DS::Transformer::Update;
17
18 use base qw { DS::Transformer };
19
20 use strict;
21 use Carp::Assert;
22
23 our ($VERSION) = $DS::VERSION;
24 our ($REVISION) = '$Revision: 1.1 $' =~ /(\d+\.\d+)/;
25
26
27 sub new {
28 my( $class, $dbh, $typespec, $source ) = @_;
29
30 my $self = $class->SUPER::new( $typespec, $source );
31
32 assert($dbh->isa('DBI::db'));
33
34 $self->{dbh} = $dbh;
35
36 my $p_num = 1;
37 my %bind_map = ();
38 my @ands;
39 my @sets;
40 foreach my $field (@{$self->{typespec}->{fields}}) {
41 next if $self->{typespec}->{pk_lookup}->{$field};
42 push @sets, "$field = ?";
43 $bind_map{$field} = $p_num++;
44 }
45 foreach my $pk (@{$self->{typespec}->{pks}}) {
46 push @ands, "$pk = ?";
47 $bind_map{$pk} = $p_num++;
48 }
49 my $update = join(' ',
50 'UPDATE',
51 $self->{typespec}->{name},
52 'SET',
53 join(', ', @sets),
54 'WHERE',
55 join(' AND ', @ands)
56 );
57
58 my $sth = $self->{dbh}->prepare_cached( $update, undef, 3 );
59 assert(defined($sth));
60
61 $self->{update_sth} = $sth;
62 $self->{bind_map} = \%bind_map;
63
64 return $self;
65 }
66
67
68 sub process {
69 my( $self, $row ) = @_;
70
71 foreach my $field (keys %{$self->{bind_map}}) {
72 assert(exists($row->{$field}));
73 if( $self->{typespec}->{pk_lookup}->{$field} and not defined( $row->{$field} )) {
74 die "The field $field is undefined but also primary key. Refusing to do an UPDATE ... WHERE ... $field IS NULL";
75 }
76 $self->{update_sth}->bind_param( $self->{bind_map}->{$field}, $row->{$field} );
77 }
78
79 $row = $self->{update_sth}->execute() ? $row : undef;
80
81 return $row;
82 }
83
84 1;