| 1 | package SQL::NamedPlaceholder; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use Exporter::Lite; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use Scalar::Util qw(reftype); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 2 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 2 | our $VERSION = '0.10'; | |||||||||||||||||||
| 11 | 2 | our @EXPORT_OK = qw(bind_named); | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 33 | ▶ | sub bind_named { | ||||||||||||||||||
|
Subroutine
bind_named: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||
| 14 | 33 | my ($sql, $hash) = @_; | |||||||||||||||||||
| 15 | 33 | ▶ | $sql or croak 'my ($sql, $bind) = bind_named($sql, $hash) requires $sql'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 16 | 32 | ▶ | (reftype($hash) || '') eq 'HASH' or croak 'must specify HASH as bind values'; | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
builtin::reftype($hash) || ''
MC/DC100%1/1
builtin::reftype($hash) || ''
builtin::reftype($hash)
Truth table100%2/2
A
A= builtin::reftype($hash)
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | # replace question marks as placeholder. e.g. [`hoge` = ?] to [`hoge` = :hoge] | ||||||||||||||||||||
| 19 | 30 | $sql =~ s{(([`"]?)(\S+?)\2\s*(=|<=?|>=?|<>|!=|<=>)\s*)\?}{$1:$3}g; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 30 | my $bind = []; | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 30 | $sql =~ s{:([A-Za-z_][A-Za-z0-9_]*)}{ | |||||||||||||||||||
| 24 | 53 | ▶ | croak("'$1' does not exist in bind hash") if !exists $hash->{$1}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | 52 | my $type = ref($hash->{$1}); | |||||||||||||||||||
| 26 | 52 | ▶ | if ($type eq 'ARRAY') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 27 | 2 | ▶ | if (@{ $hash->{$1} }) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | 2 | push @$bind, @{ $hash->{$1} }; | |||||||||||||||||||
| 29 | 2 | join ', ', map { '?' } @{ $hash->{$1} }; | |||||||||||||||||||
| 30 | } else { | ||||||||||||||||||||
| 31 | 0 | push @$bind, undef; | |||||||||||||||||||
| 32 | 0 | '?'; | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | } else { | ||||||||||||||||||||
| 35 | 50 | push @$bind, $hash->{$1}; | |||||||||||||||||||
| 36 | 50 | '?'; | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | }eg; | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 29 | ▶ | wantarray ? ($sql, $bind) : [$sql, $bind]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 2 | 1; | |||||||||||||||||||
| 44 | __END__ | ||||||||||||||||||||