blib/lib/SQL/Maker/Plugin/InsertMulti.pm

filter: Statementstmt 98.4% 64 / 65 Branchbran 88.8% 16 / 18 Conditioncond 76.1% 16 / 21 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 83.3% 5 / 6 Podpod 0.0% 0 / 1 totaltotal 86.9% 107 / 123 SCAR 30.0
CC
20
Cov
92%
insert_multi
20.3
__ANON__
1.1
BEGIN
1.0
CRAP 20.2
« blib/lib/SQL/Maker/Condition.pm ↑ Summary blib/lib/SQL/Maker/SelectSet.pm »
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 sub insert_multi {
11 # my ($self, $table, $cols, $binds, $opts) = @_;
12 # my ($self, $table, $colvals, $opts) = @_;
13 8 ▶ my ( $self, $table, @args ) = @_;
Subroutine insert_multi: called CC=20 SCAR=30.1
Pod undocumented
14 8 ▶ return unless @{$args[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$args[0];} 0 8 8
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
BranchTrueFalseTotal
ref $first_arg ? : 5 3 8
19  
20 8 ▶ if ( $is_colvals ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_colvals) { } 5 3 8
21 5 @cols = keys %{$first_arg};
22 5 @values = map { [ @$_{@cols} ] } @{$args[0]};
23 5 ▶ $opts = $args[1] || +{};
Condition100%2/2
$args[1] || {}
l!l
32
MC/DC100%1/1
$args[1] || {}
$args[1]
Truth table100%2/2
A
A= $args[1]
Aresult
11
00
24 }
25 else {
26 3 @cols = @{$args[0]};
27 3 @values = @{$args[1]};
28 3 ▶ $opts = $args[2] || +{};
Condition100%2/2
$args[2] || {}
l!l
21
MC/DC100%1/1
$args[2] || {}
$args[2]
Truth table100%2/2
A
A= $args[2]
Aresult
11
00
29 }
30  
31 8 ▶ my $prefix = $opts->{prefix} || 'INSERT INTO';
Condition100%2/2
$opts->{'prefix'} || 'INSERT INTO'
l!l
26
MC/DC100%1/1
$opts->{'prefix'} || 'INSERT INTO'
$opts->{'prefix'}
Truth table100%2/2
A
A= $opts->{'prefix'}
Aresult
11
00
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
BranchTrueFalseTotal
if (builtin::blessed($val)) { } 6 40 46
42 6 ▶ if ($val->can('as_sql')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val->can('as_sql')) { } 4 2 6
43 4 ▶ 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
BranchTrueFalseTotal
if ref $val and $self->strict 0 40 40
Condition66%2/3
ref $val and $self->strict
!ll&&!rl&&r
3280
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
ABresult
0X0
100
111
51 if ref($val) && $self->strict;
52 40 ▶ if (! $self->strict && ref $val eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->strict and ref $val eq 'SCALAR') { } 4 36 40
elsif (not $self->strict and ref $val eq 'REF' and ref $$val eq 'ARRAY') { } 4 32 36
Condition66%2/3
not $self->strict and ref $val eq "SCALAR"
!ll&&!rl&&r
0364
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"
ABresult
0X0
100
111
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"
!ll&&!rl&&r
3204
not $self->strict and ref $val eq "REF"
!ll&&!rl&&r
0324
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"
ABCresult
0XX0
10X0
1100
1111
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
BranchTrueFalseTotal
if ($self->{"driver"} eq "mysql" and exists $opts->{"update"}) 3 5 8
Condition66%2/3
$self->{"driver"} eq "mysql" and exists $opts->{"update"}
!ll&&!rl&&r
053
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"}
ABresult
0X0
100
111
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__
87  
88 =for test_synopsis
89 my ($table, @rows);
90  
91 =head1 NAME
92  
93 SQL::Maker::Plugin::InsertMulti - insert multiple rows at once on MySQL
94  
95 =head1 SYNOPSIS
96  
97 use SQL::Maker;
98  
99 SQL::Maker->load_plugin('InsertMulti');
100  
101 my $table = 'foo';
102 my @rows = ( +{ bar => 'baz', john => 'man' }, +{ bar => 'bee', john => 'row' } );
103 my $builder = SQL::Maker->new( driver => 'mysql' );
104 my ($sql, @binds);
105 ### INSERT INTO `foo` (`bar`, `john`) VALUES (?, ?), (?, ?)
106 ( $sql, @binds ) = $builder->insert_multi($table, \@rows);
107 ( $sql, @binds ) = $builder->insert_multi($table, [qw/bar john/], [ map { @$_{qw/bar john/} } @rows ]);
108 ### INSERT IGNORE `foo` (`bar`, `john`) VALUES (?, ?), (?, ?)
109 ( $sql, @binds ) = $builder->insert_multi($table, [qw/bar john/], [ map { @$_{qw/bar john/} } @rows ], +{ prefix => 'INSERT IGNORE' });
110 ### INSERT INTO `foo` (`bar`. `john`) VALUES (?, ?), (?, ?) ON DUPLICATE KEY UPDATE `bar` => ?
111 ( $sql, @binds ) = $builder->insert_multi($table, \@rows, +{ update => +{ bar => 'updated' } });
112 ( $sql, @binds ) = $builder->insert_multi($table, [qw/bar john/], [ map { @$_{qw/bar john/} } @rows ], +{ update => +{ bar => 'updated' } });
113  
114 =head1 DESCRIPTION
115  
116 This is a plugin to generate MySQL's INSERT-multi statement.
« blib/lib/SQL/Maker/Condition.pm ↑ Summary blib/lib/SQL/Maker/SelectSet.pm »