line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBomb::GluedUpdate; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
DBomb::GluedUpdate - An update glued to a DBomb::Base object. |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=cut |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
10731
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
49
|
|
12
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
50
|
|
13
|
|
|
|
|
|
|
our $VERSION = '$Revision: 1.11 $'; |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
106
|
use DBomb; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use DBomb::Conf; |
17
|
|
|
|
|
|
|
use Carp::Assert; |
18
|
|
|
|
|
|
|
use Class::MethodMaker |
19
|
|
|
|
|
|
|
'new_with_init' => 'new', |
20
|
|
|
|
|
|
|
'get_set' => [ qw(columns_list), # [ column_value_object, ... ] |
21
|
|
|
|
|
|
|
qw(peer), # a DBomb::Base object |
22
|
|
|
|
|
|
|
qw(dbh), |
23
|
|
|
|
|
|
|
qw(sth), |
24
|
|
|
|
|
|
|
qw(where_obj), # expr |
25
|
|
|
|
|
|
|
], |
26
|
|
|
|
|
|
|
; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
## init($peer) |
29
|
|
|
|
|
|
|
## init($peer,$dbh,[$columns_list]) |
30
|
|
|
|
|
|
|
sub init |
31
|
|
|
|
|
|
|
{ |
32
|
|
|
|
|
|
|
my $self = shift; |
33
|
|
|
|
|
|
|
$self->columns_list([]); |
34
|
|
|
|
|
|
|
$self->where_obj(new DBomb::Query::Expr()); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
## First argument might be a dbh or peer object |
37
|
|
|
|
|
|
|
for(@_){ |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
if (UNIVERSAL::isa($_,'DBI::db')){ |
40
|
|
|
|
|
|
|
$self->dbh($_); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
elsif (UNIVERSAL::isa($_,'DBomb::Base')){ |
43
|
|
|
|
|
|
|
$self->peer($_); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
elsif (UNIVERSAL::isa($_,'ARRAY')){ |
46
|
|
|
|
|
|
|
for (@$_){ |
47
|
|
|
|
|
|
|
assert(UNIVERSAL::isa(ref($_),'DBomb::Value::Column'), 'GluedUpdate requires column value objs'); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
$self->columns_list($_); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
assert(defined($self->peer), 'GluedUpdate requires a peer'); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
if (@{$self->columns_list} == 0){ |
55
|
|
|
|
|
|
|
## Default is all updatable columns. |
56
|
|
|
|
|
|
|
$self->columns_list([ |
57
|
|
|
|
|
|
|
grep { $_->has_value |
58
|
|
|
|
|
|
|
&& $_->is_modified |
59
|
|
|
|
|
|
|
&& (not $_->column_info->is_in_primary_key) |
60
|
|
|
|
|
|
|
} values %{$self->peer->_dbo_values}]); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
## Same as prepare->execute |
65
|
|
|
|
|
|
|
## update() |
66
|
|
|
|
|
|
|
## update(@bind_values) |
67
|
|
|
|
|
|
|
## update($dbh,@bind_values) |
68
|
|
|
|
|
|
|
sub update |
69
|
|
|
|
|
|
|
{ |
70
|
|
|
|
|
|
|
my $self = shift; |
71
|
|
|
|
|
|
|
my @bv; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
for (@_){ |
74
|
|
|
|
|
|
|
if (UNIVERSAL::isa($_,'DBI::db')){ $self->dbh($_) } |
75
|
|
|
|
|
|
|
else { push @bv, $_ } |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
assert(defined($self->dbh), 'update requires a dbh'); |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
if (not $self->sth){ $self->prepare } |
80
|
|
|
|
|
|
|
return $self->execute(@bv); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
## execute() |
84
|
|
|
|
|
|
|
## execute(@bind_values) |
85
|
|
|
|
|
|
|
## execute($dbh,@bind_values) |
86
|
|
|
|
|
|
|
sub execute |
87
|
|
|
|
|
|
|
{ |
88
|
|
|
|
|
|
|
my $self = shift; |
89
|
|
|
|
|
|
|
my @bv; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
for (@_){ |
92
|
|
|
|
|
|
|
if (UNIVERSAL::isa($_,'DBI::db')){ $self->dbh($_) } |
93
|
|
|
|
|
|
|
else { push @bv, $_ } |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
assert(defined($self->dbh), 'update requires a dbh'); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
if (not $self->sth){ $self->prepare } |
98
|
|
|
|
|
|
|
return $self->sth->execute((@{$self->bind_values},@bv)); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
## prepare() |
103
|
|
|
|
|
|
|
## prepare($dbh) |
104
|
|
|
|
|
|
|
sub prepare |
105
|
|
|
|
|
|
|
{ |
106
|
|
|
|
|
|
|
my $self = shift; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
for (@_){ |
109
|
|
|
|
|
|
|
$self->dbh($_) if UNIVERSAL::isa($_, 'DBI::db'); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
assert(defined($self->dbh), 'prepare requires a dbh'); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
if ($DBomb::Conf::prepare_cached){ |
114
|
|
|
|
|
|
|
$self->sth($self->dbh->prepare_cached(scalar $self->sql)); |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
else{ |
117
|
|
|
|
|
|
|
$self->sth($self->dbh->prepare(scalar $self->sql)); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
return $self; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
## where(EXPR) |
123
|
|
|
|
|
|
|
sub where |
124
|
|
|
|
|
|
|
{ |
125
|
|
|
|
|
|
|
my $self = shift; |
126
|
|
|
|
|
|
|
$self->where_obj->append( new DBomb::Query::Expr(@_)); |
127
|
|
|
|
|
|
|
return $self; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub sql |
131
|
|
|
|
|
|
|
{ |
132
|
|
|
|
|
|
|
my $self = shift; |
133
|
|
|
|
|
|
|
$self->dbh(shift) if @_; |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
assert($self->dbh, 'sql requires a dbh'); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
my $sql = "UPDATE "; |
138
|
|
|
|
|
|
|
assert(@{$self->columns_list} > 0, 'update attempted but no columns have been modified'); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
my $col = $self->columns_list->[0]; |
141
|
|
|
|
|
|
|
$sql .= $col->column_info->table_info->name; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
$sql .= " SET "; |
144
|
|
|
|
|
|
|
$sql .= join ", ", map { "$_ = ?" } map { $_->column_info->name } @{$self->columns_list}; |
145
|
|
|
|
|
|
|
$sql .= " WHERE "; |
146
|
|
|
|
|
|
|
$sql .= $self->where_obj->sql($self->dbh); |
147
|
|
|
|
|
|
|
return $sql; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub bind_values |
151
|
|
|
|
|
|
|
{ |
152
|
|
|
|
|
|
|
my $self = shift; |
153
|
|
|
|
|
|
|
my $bv = []; |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
for my $col_val (@{$self->columns_list}){ |
156
|
|
|
|
|
|
|
next unless $col_val->has_value; |
157
|
|
|
|
|
|
|
push @$bv, $col_val->get_value_for_update; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
push @$bv, @{$self->where_obj->bind_values}; |
161
|
|
|
|
|
|
|
return $bv; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
1; |
166
|
|
|
|
|
|
|
__END__ |