line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SQL::OOP::Dataset; |
2
|
9
|
|
|
9
|
|
1941
|
use strict; |
|
9
|
|
|
|
|
17
|
|
|
9
|
|
|
|
|
312
|
|
3
|
9
|
|
|
9
|
|
44
|
use warnings; |
|
9
|
|
|
|
|
13
|
|
|
9
|
|
|
|
|
253
|
|
4
|
9
|
|
|
9
|
|
41
|
use SQL::OOP::Base; |
|
9
|
|
|
|
|
14
|
|
|
9
|
|
|
|
|
169
|
|
5
|
9
|
|
|
9
|
|
2624
|
use SQL::OOP::ID; |
|
9
|
|
|
|
|
16
|
|
|
9
|
|
|
|
|
240
|
|
6
|
9
|
|
|
9
|
|
43
|
use Scalar::Util qw(blessed); |
|
9
|
|
|
|
|
21
|
|
|
9
|
|
|
|
|
423
|
|
7
|
9
|
|
|
9
|
|
44
|
use base qw(SQL::OOP::Base); |
|
9
|
|
|
|
|
15
|
|
|
9
|
|
|
|
|
8152
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub MODE_INSERT() {1} ## no critic |
10
|
|
|
|
|
|
|
sub MODE_UPDATE() {2} ## no critic |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
### --- |
13
|
|
|
|
|
|
|
### Constructor |
14
|
|
|
|
|
|
|
### --- |
15
|
|
|
|
|
|
|
sub new { |
16
|
21
|
|
|
21
|
1
|
14669
|
my $class = shift @_; |
17
|
21
|
100
|
|
|
|
92
|
my $data_ref = (scalar @_ == 1) ? shift @_ : [@_]; |
18
|
21
|
50
|
|
|
|
85
|
if (ref $data_ref eq 'HASH') { |
19
|
0
|
|
|
|
|
0
|
warn 'Deprecated: call the method with key-value array instead of hash'; |
20
|
0
|
|
|
|
|
0
|
$data_ref = [%$data_ref]; |
21
|
|
|
|
|
|
|
} |
22
|
21
|
|
|
|
|
98
|
my $self = bless { |
23
|
|
|
|
|
|
|
gen => undef, |
24
|
|
|
|
|
|
|
array => [], |
25
|
|
|
|
|
|
|
}, $class; |
26
|
|
|
|
|
|
|
|
27
|
21
|
|
|
|
|
153
|
return $self->append($data_ref); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
### --- |
31
|
|
|
|
|
|
|
### append elements |
32
|
|
|
|
|
|
|
### --- |
33
|
|
|
|
|
|
|
sub append { |
34
|
38
|
|
|
38
|
1
|
76
|
my $self = shift @_; |
35
|
38
|
100
|
|
|
|
114
|
my $data_ref = (scalar @_ == 1) ? shift @_ : [@_]; |
36
|
38
|
50
|
|
|
|
102
|
if (ref $data_ref eq 'HASH') { |
37
|
0
|
|
|
|
|
0
|
warn 'Deprecated: call the method with key-value array instead of hash'; |
38
|
0
|
|
|
|
|
0
|
$data_ref = [%$data_ref]; |
39
|
|
|
|
|
|
|
} |
40
|
38
|
|
|
|
|
54
|
my @copied = @{$data_ref}; |
|
38
|
|
|
|
|
86
|
|
41
|
38
|
|
|
|
|
184
|
$self->_init_gen; |
42
|
|
|
|
|
|
|
|
43
|
38
|
|
|
|
|
156
|
while (my($key, $val) = splice @copied, 0, 2) { |
44
|
30
|
|
|
|
|
42
|
push(@{$self->{array}}, SQL::OOP::ID->new($key)->to_string, $val); |
|
30
|
|
|
|
|
179
|
|
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
38
|
|
|
|
|
170
|
return $self; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
### --- |
51
|
|
|
|
|
|
|
### Get binded values in array |
52
|
|
|
|
|
|
|
### --- |
53
|
|
|
|
|
|
|
sub bind { |
54
|
20
|
|
|
20
|
1
|
64
|
my $self = shift; |
55
|
20
|
|
|
|
|
31
|
my @copy = @{$self->{array}}; |
|
20
|
|
|
|
|
66
|
|
56
|
20
|
|
|
|
|
28
|
my @vals; |
57
|
20
|
|
|
|
|
89
|
while (my ($k, $v) = splice @copy, 0, 2) { |
58
|
28
|
|
|
|
|
94
|
push(@vals, $v); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
my @out = map { |
61
|
20
|
100
|
|
|
|
38
|
if (blessed($_)) { |
|
28
|
|
|
|
|
92
|
|
62
|
6
|
|
|
|
|
20
|
$_->bind; |
63
|
|
|
|
|
|
|
} else { |
64
|
22
|
|
|
|
|
52
|
$_; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} @vals; |
67
|
20
|
50
|
|
|
|
115
|
return @out if (wantarray); |
68
|
0
|
|
|
|
|
0
|
return scalar @out; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
### --- |
72
|
|
|
|
|
|
|
### Get SQL for UPDATE command in string |
73
|
|
|
|
|
|
|
### --- |
74
|
|
|
|
|
|
|
sub to_string_for_update { |
75
|
8
|
|
|
8
|
1
|
17
|
my ($self, $prefix) = @_; |
76
|
8
|
|
|
|
|
29
|
local $SQL::OOP::Base::quote_char = $self->quote_char; |
77
|
8
|
|
|
|
|
25
|
$self->generate(MODE_UPDATE); |
78
|
8
|
50
|
33
|
|
|
41
|
if ($self->{gen} && $prefix) { |
79
|
0
|
|
|
|
|
0
|
return $prefix. ' '. $self->{gen}; |
80
|
|
|
|
|
|
|
} else { |
81
|
8
|
|
|
|
|
38
|
return $self->{gen}; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
### --- |
86
|
|
|
|
|
|
|
### Get SQL for INSERT command in string |
87
|
|
|
|
|
|
|
### --- |
88
|
|
|
|
|
|
|
sub to_string_for_insert { |
89
|
5
|
|
|
5
|
1
|
19
|
my ($self, $prefix) = @_; |
90
|
5
|
|
|
|
|
29
|
local $SQL::OOP::Base::quote_char = $self->quote_char; |
91
|
5
|
|
|
|
|
17
|
$self->generate(MODE_INSERT); |
92
|
5
|
50
|
33
|
|
|
28
|
if ($self->{gen} && $prefix) { |
93
|
0
|
|
|
|
|
0
|
return $prefix. ' '. $self->{gen}; |
94
|
|
|
|
|
|
|
} else { |
95
|
5
|
|
|
|
|
29
|
return $self->{gen}; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub generate { |
100
|
24
|
|
|
24
|
1
|
48
|
my ($self, $type) = @_; |
101
|
|
|
|
|
|
|
|
102
|
24
|
|
|
|
|
33
|
my @copy = @{$self->{array}}; |
|
24
|
|
|
|
|
78
|
|
103
|
24
|
|
|
|
|
36
|
my @key; |
104
|
|
|
|
|
|
|
my @val; |
105
|
24
|
|
|
|
|
101
|
while (my($k, $v) = splice @copy, 0, 2) { |
106
|
36
|
|
|
|
|
51
|
push(@key, $k); |
107
|
36
|
|
|
|
|
121
|
push(@val, $v); |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
24
|
100
|
|
|
|
95
|
if ($type eq MODE_INSERT) { |
|
|
50
|
|
|
|
|
|
111
|
15
|
100
|
|
|
|
104
|
$self->{gen} = sprintf('(%s) VALUES (%s)', |
112
|
|
|
|
|
|
|
join(', ', @key), |
113
|
9
|
|
|
|
|
24
|
join(', ', map {blessed($_) ? $_->to_string : '?'} @val)); |
114
|
|
|
|
|
|
|
} elsif ($type eq MODE_UPDATE) { |
115
|
15
|
|
|
|
|
32
|
$self->{gen} = ''; |
116
|
15
|
|
|
|
|
47
|
for my $idx (0 .. (scalar @key) - 1) { |
117
|
21
|
100
|
|
|
|
157
|
$self->{gen} .= ', '. sprintf('%s = %s', $key[$idx], |
118
|
|
|
|
|
|
|
blessed($val[$idx]) ? $val[$idx]->to_string : '?'); |
119
|
|
|
|
|
|
|
} |
120
|
15
|
|
|
|
|
85
|
$self->{gen} =~ s{^, }{}; |
121
|
|
|
|
|
|
|
} |
122
|
24
|
|
|
|
|
69
|
return $self; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub retrieve { |
126
|
2
|
|
|
2
|
0
|
7
|
my ($self, $key) = @_; |
127
|
2
|
|
|
|
|
3
|
my %tmp = (@{$self->{array}}); |
|
2
|
|
|
|
|
8
|
|
128
|
2
|
|
33
|
|
|
14
|
return $tmp{$key} || $tmp{$self->quote($key)}; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
1; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
__END__ |