| 1 | package SQL::Bind; | ||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 2 | ▶ | use base 'Exporter'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 2 | our @EXPORT_OK = qw(sql); | |||||||||||||
| 6 | |||||||||||||||
| 7 | 2 | our $VERSION = '1.01'; | |||||||||||||
| 8 | |||||||||||||||
| 9 | sub sql { | ||||||||||||||
| 10 | 11 | ▶ | my ($sql, %params) = @_; | ||||||||||||
|
Subroutine
sql: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 11 | my @bind; | |||||||||||||
| 13 | |||||||||||||||
| 14 | 11 | $sql =~ s{:([a-z_][a-z0-9_]*)(!|\*)?}{ | |||||||||||||
| 15 | my $options = $2 | ||||||||||||||
| 16 | ? { | ||||||||||||||
| 17 | { | ||||||||||||||
| 18 | '!' => 'raw', | ||||||||||||||
| 19 | '*' => 'recursive' | ||||||||||||||
| 20 | 13 | ▶ | }->{$2} => 1 | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 21 | } | ||||||||||||||
| 22 | : {}; | ||||||||||||||
| 23 | 13 | my ($replacement, @subbind) = _replace($1, $options, %params); | |||||||||||||
| 24 | |||||||||||||||
| 25 | 13 | push @bind, @subbind; | |||||||||||||
| 26 | |||||||||||||||
| 27 | 13 | $replacement; | |||||||||||||
| 28 | }gei; | ||||||||||||||
| 29 | |||||||||||||||
| 30 | 11 | return ($sql, @bind); | |||||||||||||
| 31 | } | ||||||||||||||
| 32 | |||||||||||||||
| 33 | sub _replace { | ||||||||||||||
| 34 | 13 | ▶ | my ($placeholder, $options, %params) = @_; | ||||||||||||
|
Subroutine
_replace: called CC=8 SCAR=20.8
| |||||||||||||||
| 35 | |||||||||||||||
| 36 | 13 | my @bind; | |||||||||||||
| 37 | |||||||||||||||
| 38 | 13 | my $replacement = ''; | |||||||||||||
| 39 | |||||||||||||||
| 40 | 13 | ▶ | if (!exists $params{$placeholder}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | 0 | die sprintf 'unknown placeholder: %s', $placeholder; | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | 13 | ▶ | if (ref $params{$placeholder} eq 'HASH') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 45 | 2 | ▶ | if ($options->{raw}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | 1 | $replacement = join ', ', map { $_ . '=' . $params{$placeholder}->{$_} } | |||||||||||||
| 47 | 1 | keys %{$params{$placeholder}}; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | else { | ||||||||||||||
| 50 | 1 | $replacement = join ', ', map { $_ . '=?' } keys %{$params{$placeholder}}; | |||||||||||||
| 51 | 1 | push @bind, values %{$params{$placeholder}}; | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | } | ||||||||||||||
| 54 | elsif (ref $params{$placeholder} eq 'ARRAY') { | ||||||||||||||
| 55 | 4 | ▶ | if ($options->{raw}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 2 | $replacement = join ', ', @{$params{$placeholder}}; | |||||||||||||
| 57 | } | ||||||||||||||
| 58 | else { | ||||||||||||||
| 59 | 2 | $replacement = join ', ', map { '?' } 1 .. @{$params{$placeholder}}; | |||||||||||||
| 60 | 2 | push @bind, @{$params{$placeholder}}; | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | } | ||||||||||||||
| 63 | else { | ||||||||||||||
| 64 | 7 | ▶ | if ($options->{raw}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 65 | 1 | $replacement = $params{$placeholder}; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | elsif ($options->{recursive}) { | ||||||||||||||
| 68 | 1 | my ($subsql, @subbind) = sql($params{$placeholder}, %params); | |||||||||||||
| 69 | |||||||||||||||
| 70 | 1 | $replacement = $subsql; | |||||||||||||
| 71 | 1 | push @bind, @subbind; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | else { | ||||||||||||||
| 74 | 5 | $replacement = '?'; | |||||||||||||
| 75 | 5 | push @bind, $params{$placeholder}; | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 13 | return ($replacement, @bind); | |||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 2 | 1; | |||||||||||||
| 83 | __END__ | ||||||||||||||