| 1 | package SQL::Maker::Plugin::InsertMulti; | ||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||
| 7 | 1 | our @EXPORT = qw/insert_multi/; | |||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||
| 9 | # for mysql | ||||||||||||||||||||||||||||||||||
| 10 | 8 | ▶ | sub insert_multi { | ||||||||||||||||||||||||||||||||
|
Subroutine
insert_multi: called CC=20 SCAR=30.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 11 | # my ($self, $table, $cols, $binds, $opts) = @_; | ||||||||||||||||||||||||||||||||||
| 12 | # my ($self, $table, $colvals, $opts) = @_; | ||||||||||||||||||||||||||||||||||
| 13 | 8 | my ( $self, $table, @args ) = @_; | |||||||||||||||||||||||||||||||||
| 14 | 8 | ▶ | return unless @{$args[0]}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | 8 | my (@cols, @bind, @values, $opts); | |||||||||||||||||||||||||||||||||
| 17 | 8 | my $first_arg = $args[0]->[0]; | |||||||||||||||||||||||||||||||||
| 18 | 8 | ▶ | my $is_colvals = ( ref $first_arg ) ? 1 : 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 8 | ▶ | if ( $is_colvals ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 21 | 5 | @cols = keys %{$first_arg}; | |||||||||||||||||||||||||||||||||
| 22 | 5 | @values = map { [ @$_{@cols} ] } @{$args[0]}; | |||||||||||||||||||||||||||||||||
| 23 | 5 | ▶ | $opts = $args[1] || +{}; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args[1] || {}
MC/DC100%1/1
$args[1] || {}
$args[1]
Truth table100%2/2
A
A= $args[1]
| |||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||
| 25 | else { | ||||||||||||||||||||||||||||||||||
| 26 | 3 | @cols = @{$args[0]}; | |||||||||||||||||||||||||||||||||
| 27 | 3 | @values = @{$args[1]}; | |||||||||||||||||||||||||||||||||
| 28 | 3 | ▶ | $opts = $args[2] || +{}; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args[2] || {}
MC/DC100%1/1
$args[2] || {}
$args[2]
Truth table100%2/2
A
A= $args[2]
| |||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | 8 | ▶ | my $prefix = $opts->{prefix} || 'INSERT INTO'; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts->{'prefix'} || 'INSERT INTO'
MC/DC100%1/1
$opts->{'prefix'} || 'INSERT INTO'
$opts->{'prefix'}
Truth table100%2/2
A
A= $opts->{'prefix'}
| |||||||||||||||||||||||||||||||||||
| 32 | 8 | my $quoted_table = $self->_quote($table); | |||||||||||||||||||||||||||||||||
| 33 | 8 | my @quoted_cols = map { $self->_quote($_) } @cols; | |||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | 8 | my $sql = "$prefix $quoted_table" . $self->new_line; | |||||||||||||||||||||||||||||||||
| 36 | 8 | $sql .= '(' . join(', ', @quoted_cols) . ')' . $self->new_line . "VALUES "; | |||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | 8 | for my $value ( @values ) { | |||||||||||||||||||||||||||||||||
| 39 | 16 | my @value_stmt; | |||||||||||||||||||||||||||||||||
| 40 | 16 | for my $val (@$value) { | |||||||||||||||||||||||||||||||||
| 41 | 46 | ▶ | if (Scalar::Util::blessed($val)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 42 | 6 | ▶ | if ($val->can('as_sql')) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | push @value_stmt, $val->as_sql(undef, sub { $self->_quote($_[0]) }); | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||
| 44 | 4 | push @bind, $val->bind(); | |||||||||||||||||||||||||||||||||
| 45 | } else { | ||||||||||||||||||||||||||||||||||
| 46 | 2 | push @value_stmt, '?'; | |||||||||||||||||||||||||||||||||
| 47 | 2 | push @bind, $val; | |||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||
| 49 | } else { | ||||||||||||||||||||||||||||||||||
| 50 | 40 | ▶ | Carp::croak("cannot pass in an unblessed ref as an argument in strict mode") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $val and $self->strict
MC/DC0%0/2
ref $val and $self->strict
ref $val
$self->strict
Truth table66%2/3
A and B
A= ref $valB= $self->strict
| |||||||||||||||||||||||||||||||||||
| 51 | if ref($val) && $self->strict; | ||||||||||||||||||||||||||||||||||
| 52 | 40 | ▶ | if (! $self->strict && ref $val eq 'SCALAR') { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
not $self->strict and ref $val eq "SCALAR"
MC/DC50%1/2
not $self->strict and ref $val eq "SCALAR"
not $self->strict
ref $val eq "SCALAR"
Truth table66%2/3
not(A) and B
A= not $self->strictB= ref $val eq "SCALAR"
| |||||||||||||||||||||||||||||||||||
| 53 | # $val = \'NOW()' | ||||||||||||||||||||||||||||||||||
| 54 | 4 | push @value_stmt, $$val; | |||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||
| 56 | ▶ | elsif (! $self->strict && ref $val eq 'REF' && ref $$val eq 'ARRAY') { | |||||||||||||||||||||||||||||||||
|
Condition66%4/6
not $self->strict and ref $val eq "REF" and ref $$val eq "ARRAY"
not $self->strict and ref $val eq "REF"
MC/DC33%1/3
not $self->strict and ref $val eq "REF" and ref $$val eq "ARRAY"
not $self->strict
ref $val eq "REF"
ref $$val eq "ARRAY"
Truth table50%2/4
not(A) and B and C
A= not $self->strictB= ref $val eq "REF"C= ref $$val eq "ARRAY"
| |||||||||||||||||||||||||||||||||||
| 57 | # $val = \['UNIX_TIMESTAMP(?)', '2011-04-20 00:30:00'] | ||||||||||||||||||||||||||||||||||
| 58 | 4 | my ( $stmt, @sub_bind ) = @{$$val}; | |||||||||||||||||||||||||||||||||
| 59 | 4 | push @value_stmt, $stmt; | |||||||||||||||||||||||||||||||||
| 60 | 4 | push @bind, @sub_bind; | |||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||
| 62 | else { | ||||||||||||||||||||||||||||||||||
| 63 | # normal values | ||||||||||||||||||||||||||||||||||
| 64 | 32 | push @value_stmt, '?'; | |||||||||||||||||||||||||||||||||
| 65 | 32 | push @bind, $val; | |||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||
| 69 | 16 | $sql .= '(' . join(', ', @value_stmt) . '),' . $self->new_line; | |||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | 8 | $sql =~ s/,$self->{new_line}$/$self->{new_line}/; | |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | 8 | ▶ | if ( $self->{driver} eq 'mysql' && exists $opts->{update} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"driver"} eq "mysql" and exists $opts->{"update"}
MC/DC50%1/2
$self->{"driver"} eq "mysql" and exists $opts->{"update"}
$self->{"driver"} eq "mysql"
exists $opts->{"update"}
Truth table66%2/3
A and B
A= $self->{"driver"} eq "mysql"B= exists $opts->{"update"}
| |||||||||||||||||||||||||||||||||||
| 75 | 3 | my ($update_cols, $update_vals) = $self->make_set_clause($opts->{update}); | |||||||||||||||||||||||||||||||||
| 76 | 3 | $sql .= "ON DUPLICATE KEY UPDATE " . join(', ', @$update_cols) . $self->{new_line}; | |||||||||||||||||||||||||||||||||
| 77 | 3 | push @bind, @$update_vals; | |||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||
| 80 | 8 | $sql =~ s/$self->{new_line}+$//; | |||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | 8 | return ($sql, @bind); | |||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||
| 85 | 1 | 1; | |||||||||||||||||||||||||||||||||
| 86 | __END__ | ||||||||||||||||||||||||||||||||||