line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SQL::NamedPlaceholder; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
26991
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
43
|
|
4
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
37
|
|
5
|
2
|
|
|
2
|
|
716
|
use Exporter::Lite; |
|
2
|
|
|
|
|
911
|
|
|
2
|
|
|
|
|
8
|
|
6
|
2
|
|
|
2
|
|
75
|
use Scalar::Util qw(reftype); |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
136
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
8
|
use Carp; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
512
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
11
|
|
|
|
|
|
|
our @EXPORT_OK = qw(bind_named); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub bind_named { |
14
|
33
|
|
|
33
|
1
|
19057
|
my ($sql, $hash) = @_; |
15
|
33
|
100
|
|
|
|
190
|
$sql or croak 'my ($sql, $bind) = bind_named($sql, $hash) requires $sql'; |
16
|
32
|
100
|
100
|
|
|
255
|
(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
|
30
|
|
|
|
|
614
|
$sql =~ s{(([`"]?)(\S+?)\2\s*(=|<=?|>=?|<>|!=|<=>)\s*)\?}{$1:$3}g; |
20
|
|
|
|
|
|
|
|
21
|
30
|
|
|
|
|
31
|
my $bind = []; |
22
|
|
|
|
|
|
|
|
23
|
30
|
|
|
|
|
75
|
$sql =~ s{:([A-Za-z_][A-Za-z0-9_]*)}{ |
24
|
53
|
100
|
|
|
|
161
|
croak("'$1' does not exist in bind hash") if !exists $hash->{$1}; |
25
|
52
|
|
|
|
|
49
|
my $type = ref($hash->{$1}); |
26
|
52
|
100
|
|
|
|
52
|
if ($type eq 'ARRAY') { |
27
|
2
|
50
|
|
|
|
2
|
if (@{ $hash->{$1} }) { |
|
2
|
|
|
|
|
5
|
|
28
|
2
|
|
|
|
|
1
|
push @$bind, @{ $hash->{$1} }; |
|
2
|
|
|
|
|
5
|
|
29
|
2
|
|
|
|
|
1
|
join ', ', map { '?' } @{ $hash->{$1} }; |
|
4
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
5
|
|
30
|
|
|
|
|
|
|
} else { |
31
|
0
|
|
|
|
|
0
|
push @$bind, undef; |
32
|
0
|
|
|
|
|
0
|
'?'; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
} else { |
35
|
50
|
|
|
|
|
56
|
push @$bind, $hash->{$1}; |
36
|
50
|
|
|
|
|
110
|
'?'; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
}eg; |
39
|
|
|
|
|
|
|
|
40
|
29
|
100
|
|
|
|
85
|
wantarray ? ($sql, $bind) : [$sql, $bind]; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
1; |
44
|
|
|
|
|
|
|
__END__ |