line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SQL::Composer::Insert; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
12732
|
use strict; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
121
|
|
4
|
4
|
|
|
4
|
|
14
|
use warnings; |
|
4
|
|
|
|
|
4
|
|
|
4
|
|
|
|
|
98
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
require Carp; |
7
|
4
|
|
|
4
|
|
661
|
use SQL::Composer::Quoter; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
1562
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
9
|
|
|
9
|
0
|
3774
|
my $class = shift; |
11
|
9
|
|
|
|
|
21
|
my (%params) = @_; |
12
|
|
|
|
|
|
|
|
13
|
9
|
|
|
|
|
18
|
my $self = { table => $params{into} }; |
14
|
9
|
|
|
|
|
12
|
bless $self, $class; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
$self->{quoter} = |
17
|
9
|
|
33
|
|
|
42
|
$params{quoter} || SQL::Composer::Quoter->new(driver => $params{driver}); |
18
|
|
|
|
|
|
|
|
19
|
9
|
|
|
|
|
46
|
my $sql = ''; |
20
|
9
|
|
|
|
|
7
|
my @bind; |
21
|
|
|
|
|
|
|
|
22
|
9
|
|
|
|
|
15
|
$sql .= 'INSERT INTO '; |
23
|
|
|
|
|
|
|
|
24
|
9
|
|
|
|
|
19
|
$sql .= $self->_quote($params{into}); |
25
|
|
|
|
|
|
|
|
26
|
9
|
100
|
66
|
|
|
19
|
if ($params{values} && @{$params{values}}) { |
|
9
|
|
|
|
|
27
|
|
27
|
7
|
|
|
|
|
6
|
my @columns; |
28
|
|
|
|
|
|
|
my @values; |
29
|
7
|
|
|
|
|
7
|
while (my ($key, $value) = splice @{$params{values}}, 0, 2) { |
|
17
|
|
|
|
|
41
|
|
30
|
10
|
|
|
|
|
11
|
push @columns, $key; |
31
|
|
|
|
|
|
|
|
32
|
10
|
100
|
|
|
|
17
|
if (ref $value) { |
33
|
2
|
100
|
|
|
|
5
|
if (ref $value eq 'SCALAR') { |
|
|
50
|
|
|
|
|
|
34
|
1
|
|
|
|
|
2
|
push @values, $$value; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
elsif (ref $value eq 'REF') { |
37
|
1
|
50
|
|
|
|
2
|
if (ref $$value eq 'ARRAY') { |
38
|
1
|
|
|
|
|
2
|
push @values, $$value->[0]; |
39
|
1
|
|
|
|
|
1
|
push @bind, @$$value[1 .. $#{$$value}]; |
|
1
|
|
|
|
|
2
|
|
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
else { |
42
|
0
|
|
|
|
|
0
|
Carp::croak('unexpected reference'); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
else { |
46
|
0
|
|
|
|
|
0
|
Carp::croak('unexpected reference'); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
else { |
50
|
8
|
|
|
|
|
7
|
push @values, '?'; |
51
|
8
|
|
|
|
|
12
|
push @bind, $value; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
7
|
50
|
|
|
|
36
|
if (@columns) { |
56
|
7
|
|
|
|
|
10
|
$sql .= ' (' . (join ',', map { $self->_quote($_) } @columns) . ')'; |
|
10
|
|
|
|
|
17
|
|
57
|
7
|
|
|
|
|
11
|
$sql .= ' VALUES ('; |
58
|
7
|
|
|
|
|
9
|
$sql .= join ',', @values; |
59
|
7
|
|
|
|
|
7
|
$sql .= ')'; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# save this for later ... |
63
|
7
|
|
|
|
|
12
|
$self->{columns} = \@columns; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
else { |
66
|
2
|
|
|
|
|
2
|
my $driver = $params{driver}; |
67
|
2
|
100
|
66
|
|
|
13
|
if ($driver && $driver =~ m/(?:sqlite|pg)/i) { |
68
|
1
|
|
|
|
|
2
|
$sql .= ' DEFAULT VALUES'; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
else { |
71
|
1
|
|
|
|
|
1
|
$sql .= ' () VALUES ()'; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
9
|
|
|
|
|
13
|
$self->{sql} = $sql; |
76
|
9
|
|
|
|
|
9
|
$self->{bind} = \@bind; |
77
|
|
|
|
|
|
|
|
78
|
9
|
|
|
|
|
28
|
return $self; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
1
|
|
|
1
|
0
|
324
|
sub table { shift->{table} } |
82
|
|
|
|
|
|
|
|
83
|
8
|
|
|
8
|
0
|
27
|
sub to_sql { shift->{sql} } |
84
|
8
|
50
|
|
8
|
0
|
2276
|
sub to_bind { @{shift->{bind} || []} } |
|
8
|
|
|
|
|
33
|
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub _quote { |
87
|
21
|
|
|
21
|
|
14
|
my $self = shift; |
88
|
21
|
|
|
|
|
20
|
my ($column, $prefix) = @_; |
89
|
|
|
|
|
|
|
|
90
|
21
|
|
|
|
|
36
|
return $self->{quoter}->quote($column, $prefix); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
__END__ |