line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::Foo::UpdateQuery; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
23
|
use strict; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
1447
|
|
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
4144
|
use Tie::IxHash; |
|
4
|
|
|
|
|
17204
|
|
|
4
|
|
|
|
|
132
|
|
6
|
4
|
|
|
4
|
|
45
|
use Log::Any qw($log); |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
38
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
#################################################### |
9
|
|
|
|
|
|
|
# Constructor |
10
|
|
|
|
|
|
|
# |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
0
|
3
|
my ($class, $table, $dbh) = @_; |
15
|
|
|
|
|
|
|
|
16
|
1
|
50
|
|
|
|
4
|
die "No Table name passed to UpdateQuery" unless $table; |
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
|
|
6
|
my $self = { |
19
|
|
|
|
|
|
|
table => $table, |
20
|
|
|
|
|
|
|
dbh => $dbh, |
21
|
|
|
|
|
|
|
fields => {}, |
22
|
|
|
|
|
|
|
keys => {}, |
23
|
|
|
|
|
|
|
debug => 0 |
24
|
|
|
|
|
|
|
}; |
25
|
|
|
|
|
|
|
|
26
|
1
|
|
|
|
|
3
|
bless $self, $class; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# use tied hashes to preserve the order of added fields |
29
|
1
|
|
|
|
|
3
|
tie(%{$self->{fields}}, 'Tie::IxHash'); |
|
1
|
|
|
|
|
13
|
|
30
|
1
|
|
|
|
|
58
|
tie(%{$self->{keys}}, 'Tie::IxHash'); |
|
1
|
|
|
|
|
5
|
|
31
|
|
|
|
|
|
|
|
32
|
1
|
|
|
|
|
12
|
return $self; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
#################################################### |
36
|
|
|
|
|
|
|
# Properties |
37
|
|
|
|
|
|
|
# |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub UpdateQuery { |
40
|
|
|
|
|
|
|
|
41
|
1
|
|
|
1
|
0
|
3
|
my $self = $_[0]; |
42
|
|
|
|
|
|
|
|
43
|
1
|
|
|
|
|
1
|
my $field_list = ""; |
44
|
1
|
|
|
|
|
2
|
my @args; |
45
|
|
|
|
|
|
|
|
46
|
1
|
|
|
|
|
1
|
for my $field (keys %{$self->{fields}}) |
|
1
|
|
|
|
|
5
|
|
47
|
|
|
|
|
|
|
{ |
48
|
3
|
100
|
|
|
|
24
|
$field_list .= ", " if $field_list; |
49
|
|
|
|
|
|
|
|
50
|
3
|
|
|
|
|
11
|
my $value = $self->{fields}->{$field}; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Temp fix for current_timestamp as a param |
53
|
3
|
50
|
33
|
|
|
32
|
if ($value && $value eq "current_timestamp()") { |
54
|
|
|
|
|
|
|
|
55
|
0
|
|
|
|
|
0
|
$field_list .= "$field = current_timestamp()"; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
} else { |
58
|
|
|
|
|
|
|
|
59
|
3
|
|
|
|
|
5
|
$field_list .= "$field = ?"; |
60
|
|
|
|
|
|
|
|
61
|
3
|
|
|
|
|
7
|
push @args, $value; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
1
|
|
|
|
|
5
|
my ($where, @keys) = $self->WhereClause(); |
66
|
|
|
|
|
|
|
|
67
|
1
|
|
|
|
|
6
|
my $query = "update " . $self->{table} . " set $field_list $where;"; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
#warn $query . " : " . join(",", @args, @keys); |
70
|
|
|
|
|
|
|
|
71
|
1
|
|
|
|
|
5
|
return $query, @args, @keys; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub InsertQuery { |
76
|
|
|
|
|
|
|
|
77
|
2
|
|
|
2
|
0
|
3
|
my $self = $_[0]; |
78
|
|
|
|
|
|
|
|
79
|
2
|
|
|
|
|
3
|
my $field_list = ""; |
80
|
2
|
|
|
|
|
5
|
my $value_list = ""; |
81
|
2
|
|
|
|
|
2
|
my @args; |
82
|
|
|
|
|
|
|
|
83
|
2
|
|
|
|
|
3
|
for my $field (keys %{$self->{fields}}) |
|
2
|
|
|
|
|
10
|
|
84
|
|
|
|
|
|
|
{ |
85
|
6
|
100
|
|
|
|
52
|
$field_list .= ", " if $field_list; |
86
|
6
|
100
|
|
|
|
9
|
$value_list .= ", " if $field_list; |
87
|
|
|
|
|
|
|
|
88
|
6
|
|
|
|
|
7
|
$field_list .= $field; |
89
|
|
|
|
|
|
|
|
90
|
6
|
|
|
|
|
22
|
my $value = $self->{fields}->{$field}; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# Temp fix for current_timestamp as a param |
93
|
6
|
50
|
33
|
|
|
53
|
if ($value && $value eq "current_timestamp()") { |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
0
|
$value_list .= "current_timestamp()"; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
} else { |
98
|
|
|
|
|
|
|
|
99
|
6
|
|
|
|
|
7
|
$value_list .= "?"; |
100
|
|
|
|
|
|
|
|
101
|
6
|
|
|
|
|
18
|
push @args, $value; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
2
|
|
|
|
|
6
|
for my $field (keys %{$self->{keys}}) |
|
2
|
|
|
|
|
7
|
|
106
|
|
|
|
|
|
|
{ |
107
|
0
|
0
|
|
|
|
0
|
$field_list .= ", " if $field_list; |
108
|
0
|
0
|
|
|
|
0
|
$value_list .= ", " if $field_list; |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
0
|
$field_list .= $field; |
111
|
0
|
|
|
|
|
0
|
$value_list .= "?"; |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
0
|
push @args, $self->{keys}->{$field}; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
2
|
|
|
|
|
27
|
my $query = "insert into " . $self->{table} . " ( $field_list ) values ( $value_list );"; |
117
|
|
|
|
|
|
|
|
118
|
2
|
|
|
|
|
7
|
return $query, @args; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub DeleteQuery { |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
0
|
0
|
0
|
my $self = $_[0]; |
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
0
|
my ($where, @keys) = $self->WhereClause(); |
126
|
|
|
|
|
|
|
|
127
|
0
|
|
|
|
|
0
|
my $query = "delete from " . $self->{table} . " $where"; |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
0
|
return $query, @keys; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub WhereClause |
133
|
|
|
|
|
|
|
{ |
134
|
1
|
|
|
1
|
0
|
2
|
my $self = $_[0]; |
135
|
|
|
|
|
|
|
|
136
|
1
|
|
|
|
|
1
|
my $where = ""; |
137
|
1
|
|
|
|
|
3
|
my @args; |
138
|
|
|
|
|
|
|
|
139
|
1
|
|
|
|
|
1
|
for my $field (keys %{$self->{keys}}) |
|
1
|
|
|
|
|
4
|
|
140
|
|
|
|
|
|
|
{ |
141
|
1
|
50
|
|
|
|
12
|
$where .= " and " if $where; |
142
|
|
|
|
|
|
|
|
143
|
1
|
|
|
|
|
3
|
$where .= "$field = ?"; |
144
|
|
|
|
|
|
|
|
145
|
1
|
|
|
|
|
15
|
push @args, $self->{keys}->{$field}; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
1
|
50
|
|
|
|
14
|
$where = "where $where" if $where; |
149
|
|
|
|
|
|
|
|
150
|
1
|
|
|
|
|
5
|
return $where, @args; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
#################################################### |
155
|
|
|
|
|
|
|
# Methods |
156
|
|
|
|
|
|
|
# |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub addKey { |
159
|
|
|
|
|
|
|
|
160
|
1
|
|
|
1
|
0
|
802
|
my ($self, $field, $value) = @_; |
161
|
|
|
|
|
|
|
|
162
|
1
|
|
|
|
|
5
|
$self->{keys}->{$field} = $value; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub addField { |
166
|
|
|
|
|
|
|
|
167
|
3
|
|
|
3
|
0
|
110
|
my ($self, $field, $value) = @_; |
168
|
|
|
|
|
|
|
|
169
|
3
|
50
|
33
|
|
|
16
|
$value = undef if $value && $value eq ''; # MT - not sure about this! Maybee remove this line? |
170
|
|
|
|
|
|
|
|
171
|
3
|
|
|
|
|
14
|
$self->{fields}->{$field} = $value; |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub addFields { |
175
|
|
|
|
|
|
|
|
176
|
2
|
|
|
2
|
0
|
611
|
my $self = shift; |
177
|
|
|
|
|
|
|
|
178
|
2
|
|
|
|
|
2
|
my $fields; |
179
|
|
|
|
|
|
|
|
180
|
2
|
50
|
|
|
|
7
|
if (ref($_[0]) eq 'HASH') { |
181
|
|
|
|
|
|
|
|
182
|
0
|
|
|
|
|
0
|
$fields = $_[0]; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
} else { |
185
|
|
|
|
|
|
|
|
186
|
2
|
|
|
|
|
10
|
$fields = {@_}; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
2
|
|
|
|
|
10
|
while (my ($field, $value) = each(%$fields)) { |
190
|
|
|
|
|
|
|
|
191
|
6
|
50
|
|
|
|
49
|
$value = undef if $value eq ''; |
192
|
|
|
|
|
|
|
|
193
|
6
|
|
|
|
|
23
|
$self->{fields}->{$field} = $value; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
sub DoInsert { |
198
|
|
|
|
|
|
|
|
199
|
2
|
|
|
2
|
0
|
26
|
my ($self, $dbh) = @_; |
200
|
|
|
|
|
|
|
|
201
|
2
|
50
|
|
|
|
7
|
$dbh = $self->{dbh} unless $dbh; |
202
|
|
|
|
|
|
|
|
203
|
2
|
|
|
|
|
6
|
my ($sql, @args) = $self->InsertQuery(); |
204
|
|
|
|
|
|
|
|
205
|
2
|
|
|
|
|
3
|
my $rows; |
206
|
|
|
|
|
|
|
|
207
|
2
|
|
|
|
|
9
|
$dbh->do($sql, {}, @args); |
208
|
|
|
|
|
|
|
|
209
|
2
|
|
|
|
|
8
|
log_query($dbh, $sql, \@args); |
210
|
|
|
|
|
|
|
|
211
|
2
|
50
|
|
|
|
9
|
return 0 if $dbh->err; |
212
|
|
|
|
|
|
|
|
213
|
2
|
|
|
|
|
7
|
my $newid = $dbh->last_insert_id(undef, undef, $self->{table}, undef); |
214
|
|
|
|
|
|
|
|
215
|
2
|
|
|
|
|
8
|
$log->info("Insert ID: $newid"); |
216
|
|
|
|
|
|
|
|
217
|
2
|
|
|
|
|
13
|
return $newid; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
sub DoUpdate { |
221
|
|
|
|
|
|
|
|
222
|
1
|
|
|
1
|
0
|
15
|
my ($self, $dbh) = @_; |
223
|
|
|
|
|
|
|
|
224
|
1
|
50
|
|
|
|
4
|
$dbh = $self->{dbh} unless $dbh; |
225
|
|
|
|
|
|
|
|
226
|
1
|
|
|
|
|
4
|
my ($sql, @args) = $self->UpdateQuery(); |
227
|
|
|
|
|
|
|
|
228
|
1
|
50
|
|
|
|
2
|
unless (keys %{$self->{keys}}) { |
|
1
|
|
|
|
|
57
|
|
229
|
|
|
|
|
|
|
|
230
|
0
|
|
|
|
|
0
|
$log->error("Update query with no keys not allowed: $sql (" . join(",", @args) . ")"); |
231
|
|
|
|
|
|
|
|
232
|
0
|
|
|
|
|
0
|
return undef; |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
1
|
|
|
|
|
17
|
my $rows = $dbh->do($sql, {}, @args); |
236
|
|
|
|
|
|
|
|
237
|
1
|
|
|
|
|
3
|
log_query($dbh, $sql, \@args); |
238
|
|
|
|
|
|
|
|
239
|
1
|
50
|
|
|
|
5
|
return 0 if $dbh->err; |
240
|
|
|
|
|
|
|
|
241
|
1
|
|
|
|
|
4
|
$log->info("Updated: $rows"); |
242
|
|
|
|
|
|
|
|
243
|
1
|
|
|
|
|
6
|
return($rows); |
244
|
|
|
|
|
|
|
} |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
sub DontUpdate { |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
# for testing: don't do anything, just show SQL update statement & its args |
249
|
|
|
|
|
|
|
|
250
|
0
|
|
|
0
|
0
|
0
|
my ($self, $dbh) = @_; |
251
|
|
|
|
|
|
|
|
252
|
0
|
|
|
|
|
0
|
my ($sql, @args) = $self->UpdateQuery(); |
253
|
|
|
|
|
|
|
|
254
|
0
|
|
|
|
|
0
|
warn "UpdateQuery->DoUpdate: $sql " . join(",", @args); |
255
|
|
|
|
|
|
|
|
256
|
0
|
|
|
|
|
0
|
return 1; |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
sub DoDelete { |
260
|
|
|
|
|
|
|
|
261
|
0
|
|
|
0
|
0
|
0
|
my ($self, $dbh) = @_; |
262
|
|
|
|
|
|
|
|
263
|
0
|
0
|
|
|
|
0
|
$dbh = $self->{dbh} unless $dbh; |
264
|
|
|
|
|
|
|
|
265
|
0
|
|
|
|
|
0
|
my ($sql, @args) = $self->DeleteQuery(); |
266
|
|
|
|
|
|
|
|
267
|
0
|
|
|
|
|
0
|
my $rows = $dbh->do($sql, {}, @args); |
268
|
|
|
|
|
|
|
|
269
|
0
|
|
|
|
|
0
|
log_query($dbh, $sql, \@args); |
270
|
|
|
|
|
|
|
|
271
|
0
|
0
|
|
|
|
0
|
return 0 if $dbh->err; |
272
|
|
|
|
|
|
|
|
273
|
0
|
|
|
|
|
0
|
$log->info("Deleted: $rows"); |
274
|
|
|
|
|
|
|
|
275
|
0
|
|
|
|
|
0
|
return $rows; |
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
sub log_query |
280
|
|
|
|
|
|
|
{ |
281
|
3
|
|
|
3
|
0
|
5
|
my ($dbh, $sql, $args) = @_; |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
# use the 'caller' function name to work out context |
284
|
3
|
|
|
|
|
4
|
my $caller = ( caller(2) )[3]; |
285
|
|
|
|
|
|
|
|
286
|
3
|
50
|
|
|
|
9
|
if ($dbh->err) { |
287
|
|
|
|
|
|
|
|
288
|
0
|
|
|
|
|
0
|
$log->error($dbh->errstr . " - $sql (" . join(",", @$args) . ") called by $caller"); |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
} else { |
291
|
|
|
|
|
|
|
|
292
|
3
|
|
|
|
|
17
|
$log->debug("$sql (" . join(",", @$args) . ") called by $caller"); |
293
|
|
|
|
|
|
|
} |
294
|
|
|
|
|
|
|
} |
295
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
#################################################### |
298
|
|
|
|
|
|
|
# End of Package |
299
|
|
|
|
|
|
|
#################################################### |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
1; |
302
|
|
|
|
|
|
|
|