line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SQL::NamedPlaceholder; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
51107
|
use strict; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
108
|
|
4
|
2
|
|
|
2
|
|
13
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
81
|
|
5
|
2
|
|
|
2
|
|
1368
|
use Exporter::Lite; |
|
2
|
|
|
|
|
1661
|
|
|
2
|
|
|
|
|
12
|
|
6
|
2
|
|
|
2
|
|
130
|
use Scalar::Util qw(reftype); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
358
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
14
|
use Carp; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
1005
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw(bind_named); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub bind_named { |
14
|
32
|
|
|
32
|
1
|
23249
|
my ($sql, $hash) = @_; |
15
|
32
|
100
|
|
|
|
233
|
$sql or croak 'my ($sql, $bind) = bind_named($sql, $hash) requires $sql'; |
16
|
31
|
100
|
100
|
|
|
288
|
(reftype($hash) || '') eq 'HASH' or croak 'must specify HASH as bind values'; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# replace question marks as placeholder. e.g. [`hoge` = ?] to [`hoge` = :hoge] |
19
|
29
|
|
|
|
|
806
|
$sql =~ s{(([`"]?)(\S+?)\2\s*(=|<=?|>=?|<>|!=|<=>)\s*)\?}{$1:$3}g; |
20
|
|
|
|
|
|
|
|
21
|
29
|
|
|
|
|
42
|
my $bind = []; |
22
|
|
|
|
|
|
|
|
23
|
29
|
|
|
|
|
86
|
$sql =~ s{:(\w+)}{ |
24
|
53
|
100
|
|
|
|
210
|
croak("'$1' does not exist in bind hash") if !exists $hash->{$1}; |
25
|
52
|
|
|
|
|
70
|
my $type = ref($hash->{$1}); |
26
|
52
|
100
|
|
|
|
72
|
if ($type eq 'ARRAY') { |
27
|
2
|
50
|
|
|
|
2
|
if (@{ $hash->{$1} }) { |
|
2
|
|
|
|
|
6
|
|
28
|
2
|
|
|
|
|
3
|
push @$bind, @{ $hash->{$1} }; |
|
2
|
|
|
|
|
4
|
|
29
|
2
|
|
|
|
|
3
|
join ', ', map { '?' } @{ $hash->{$1} }; |
|
4
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
5
|
|
30
|
|
|
|
|
|
|
} else { |
31
|
0
|
|
|
|
|
0
|
push @$bind, undef; |
32
|
0
|
|
|
|
|
0
|
'?'; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
} else { |
35
|
50
|
|
|
|
|
74
|
push @$bind, $hash->{$1}; |
36
|
50
|
|
|
|
|
120
|
'?'; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
}eg; |
39
|
|
|
|
|
|
|
|
40
|
28
|
100
|
|
|
|
100
|
wantarray ? ($sql, $bind) : [$sql, $bind]; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
1; |
44
|
|
|
|
|
|
|
__END__ |