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