line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::BulkLoader::Mysql; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
23500
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
37
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
42
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '1.006'; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
6
|
use constant key_count=>0; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
78
|
|
9
|
1
|
|
|
1
|
|
5
|
use constant key_single_insert=>1; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
57
|
|
10
|
1
|
|
|
1
|
|
5
|
use constant key_bulk_insert=>2; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
37
|
|
11
|
1
|
|
|
1
|
|
6
|
use constant key_buffer=>3; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
43
|
|
12
|
1
|
|
|
1
|
|
4
|
use constant key_sql_insert=>4; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
13
|
1
|
|
|
1
|
|
5
|
use constant key_sql_columns=>5; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
44
|
|
14
|
1
|
|
|
1
|
|
4
|
use constant key_bulk_sql=>6; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
42
|
|
15
|
1
|
|
|
1
|
|
5
|
use constant key_single_sql=>7; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
44
|
|
16
|
1
|
|
|
1
|
|
5
|
use constant key_data=>8; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
17
|
1
|
|
|
1
|
|
5
|
use constant key_placeholder_count=>9; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
889
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# Below is stub documentation for your module. You'd better edit it! |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 NAME |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
DBIx::BulkLoader::Mysql - Perl extension for mysql bulk loading |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 SYNOPSIS |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
use DBIx::BulkLoader::Mysql; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# non repeating portion of the insert statement |
31
|
|
|
|
|
|
|
my $insert='insert into bulk_insert (col_a,col_b,col_c) values '; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# repeating portion of the insert statement |
34
|
|
|
|
|
|
|
my $placeholders='(?,?,?)'; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
# how many rows to buffer until insert is called |
37
|
|
|
|
|
|
|
my $bulk_insert_count=5; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# db connection |
40
|
|
|
|
|
|
|
my $dbh=DBI->connect(db connection info here); |
41
|
|
|
|
|
|
|
my $placeholder_count=3; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my ($bulk,$error)=DBIx::BulkLoader::Mysql->new( |
44
|
|
|
|
|
|
|
dbh=>$dbh |
45
|
|
|
|
|
|
|
,sql_insert=>$insert |
46
|
|
|
|
|
|
|
,placeholders=>$placeholders |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
die $error unless $bulk; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
for( 1 .. 50 ) { |
51
|
|
|
|
|
|
|
$bulk->insert(qw(a b c)); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
# inserted 50 rows at once |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
$bulk->insert(qw(l l x)); |
56
|
|
|
|
|
|
|
# inserted 0 rows |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
$bulk->insert(qw(l l x)); |
59
|
|
|
|
|
|
|
# inserted 0 rows |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
$bulk->flush; |
62
|
|
|
|
|
|
|
# inserted 2 rows 1 at a time |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head1 DESCRIPTION |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Simple buffering bulk loader interface for mysql. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head2 EXPORT |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
None. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 OO Methods |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
This section covers the OO methods for this package. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=over 4 |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=item * my ($bulk,$error)=DBIx::BulkLoader::Mysql->new(%hash); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Package constructor. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
$bulk is undef on error |
83
|
|
|
|
|
|
|
$error explains why $bulk is undef |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Constructor options |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
dbh=>$dbh |
88
|
|
|
|
|
|
|
Sets the DBH object |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sql_insert=>$insert |
91
|
|
|
|
|
|
|
Contains the body of the sql statement minus the |
92
|
|
|
|
|
|
|
placeholder segment. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
placeholders=>$placeholders |
95
|
|
|
|
|
|
|
Placeholder segment of the sql statement |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
placeholder_count=>3 |
98
|
|
|
|
|
|
|
Optional argument |
99
|
|
|
|
|
|
|
If you get strange insert counts or dbi bails |
100
|
|
|
|
|
|
|
set this option manually |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
bulk_insert_count=>50 |
103
|
|
|
|
|
|
|
Optional argument |
104
|
|
|
|
|
|
|
Sets the number of rows to buffer for insert. |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
prepare_args=>{} |
107
|
|
|
|
|
|
|
Optional argument |
108
|
|
|
|
|
|
|
Arguments to be passed to $dbh->prepare |
109
|
|
|
|
|
|
|
See DBD::mysql |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=cut |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub new { |
114
|
0
|
|
|
0
|
1
|
|
my ($class,%hash)=@_; |
115
|
0
|
|
|
|
|
|
my $s=bless [],$class; |
116
|
0
|
|
|
|
|
|
$s->[key_data]=[]; |
117
|
0
|
0
|
|
|
|
|
$hash{bulk_insert_count}=$hash{bulk_insert_count} |
118
|
|
|
|
|
|
|
? $hash{bulk_insert_count} : 50; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# stop here if we have some bad arguments |
121
|
0
|
0
|
|
|
|
|
return (undef,'placeholders=>"" not set!') unless $hash{placeholders}; |
122
|
0
|
0
|
|
|
|
|
return (undef,'sql_insert=>"" not set!') unless $hash{sql_insert}; |
123
|
0
|
0
|
|
|
|
|
return (undef,'dbh=>$dbh not set!') unless $hash{dbh}; |
124
|
0
|
0
|
|
|
|
|
unless($hash{placeholder_count}) { |
125
|
0
|
|
|
|
|
|
for( $hash{placeholders}=~ /\?/g){ |
126
|
0
|
|
|
|
|
|
$hash{placeholder_count}++ |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
0
|
|
|
|
|
|
$s->[key_placeholder_count]=$hash{placeholder_count} ; |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
|
$s->[key_buffer]= |
132
|
|
|
|
|
|
|
$hash{placeholder_count} * $hash{bulk_insert_count}; |
133
|
|
|
|
|
|
|
|
134
|
0
|
0
|
|
|
|
|
my $prep_args=$hash{prepare_args} ? $hash{prepare_args} : ({}); |
135
|
0
|
|
|
|
|
|
my $single=join ' ',$hash{sql_insert},$hash{placeholders}; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# run the prepare statement |
138
|
0
|
|
|
|
|
|
$s->[key_single_sql]=$single; |
139
|
0
|
|
|
|
|
|
$s->[key_single_insert]=eval { |
140
|
0
|
|
|
|
|
|
$hash{dbh}->prepare( |
141
|
|
|
|
|
|
|
$single |
142
|
|
|
|
|
|
|
,$hash{prepare_args} |
143
|
|
|
|
|
|
|
); |
144
|
|
|
|
|
|
|
}; |
145
|
0
|
0
|
|
|
|
|
return undef,"failed to prepare: $single" if $@; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
my @placeholders; |
149
|
0
|
|
|
|
|
|
for(1 .. $hash{bulk_insert_count}) { |
150
|
0
|
|
|
|
|
|
push @placeholders,$hash{placeholders}; |
151
|
|
|
|
|
|
|
} |
152
|
0
|
|
|
|
|
|
my $bulk=join(' ',$hash{sql_insert}, |
153
|
|
|
|
|
|
|
join(', ',@placeholders) |
154
|
|
|
|
|
|
|
); |
155
|
|
|
|
|
|
|
|
156
|
0
|
0
|
|
|
|
|
return undef,"failed to prepare: $bulk" if $@; |
157
|
0
|
|
|
|
|
|
$s->[key_bulk_sql]=$bulk; |
158
|
0
|
|
|
|
|
|
$s->[key_bulk_insert]=eval { |
159
|
0
|
|
|
|
|
|
$hash{dbh}->prepare( |
160
|
|
|
|
|
|
|
$bulk |
161
|
|
|
|
|
|
|
,$hash{prepare_args} |
162
|
|
|
|
|
|
|
); |
163
|
|
|
|
|
|
|
}; |
164
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
|
$s,undef; |
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item * $bulk->flush; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Empties the placeholder buffer |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=cut |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub flush () { |
175
|
0
|
|
|
0
|
1
|
|
my ($s)=@_; |
176
|
0
|
|
|
|
|
|
my $row=$s->[key_data]; |
177
|
0
|
|
|
|
|
|
while(my @single=splice(@$row,0, $s->get_placeholder_count)) { |
178
|
0
|
|
|
|
|
|
$s->get_prepared_single_sth->execute(@single); |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub DESTROY { |
183
|
0
|
|
|
0
|
|
|
@{$_[0]}=() |
|
0
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=item * $bulk->insert($x,$y,$z); |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Inserts the placeholder arguments onto the buffer stack. This does not cause an insert, unless the total number of rows is the same as the constructor call "bulk_insert_count=>50". |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=cut |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
sub insert { |
193
|
0
|
|
|
0
|
1
|
|
my ($s,@data)=@_; |
194
|
0
|
|
|
|
|
|
my $row=$s->[key_data]; |
195
|
0
|
|
|
|
|
|
push @$row,@data; |
196
|
0
|
0
|
|
|
|
|
if((1 + $#$row)==$s->get_buffer_size) { |
197
|
0
|
|
|
|
|
|
$s->get_prepared_bulk_sth->execute(@$row); |
198
|
0
|
|
|
|
|
|
@$row=(); |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
} |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=item * my $columns=$bulk->get_placeholder_count; |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
Gets the total number of column placeholders. |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=cut |
208
|
|
|
|
|
|
|
|
209
|
0
|
|
|
0
|
1
|
|
sub get_placeholder_count () { $_[0]->[key_placeholder_count] } |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=item * my $buffer_size=$bulk->get_buffer_size; |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Gets the total size of the array used for insert. |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=cut |
216
|
|
|
|
|
|
|
|
217
|
0
|
|
|
0
|
1
|
|
sub get_buffer_size () { $_[0]->[key_buffer] } |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=item * my $sql_single=$bulk->single_sql; |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Gets the raw sql statement used for single row inserts. |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=cut |
224
|
|
|
|
|
|
|
|
225
|
0
|
|
|
0
|
1
|
|
sub single_sql() { $_[0]->[key_single_sql] } |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=item * my $bulk_sql=$bulk->bulk_sql; |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
Gets the raw sql statement used for bulk row inserts. |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=cut |
232
|
|
|
|
|
|
|
|
233
|
0
|
|
|
0
|
1
|
|
sub bulk_sql() { $_[0]->[key_bulk_sql] } |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
=item * my $single_sth=$bulk->get_prepared_single_sth; |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
Gets the prepared statement handle for single row inserts. |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=cut |
240
|
|
|
|
|
|
|
|
241
|
0
|
|
|
0
|
1
|
|
sub get_prepared_single_sth () { $_[0]->[key_single_insert] } |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=item * my $bulk_sth=$bulk->get_prepared_bulk_sth; |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
Gets the prepared statement handle for bulk row inserts. |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=cut |
248
|
|
|
|
|
|
|
|
249
|
0
|
|
|
0
|
1
|
|
sub get_prepared_bulk_sth () { $_[0]->[key_bulk_insert] } |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=item * my @buffer=$bulk->get_buffered_data; |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
Returns a list containing the current buffered data |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=cut |
256
|
|
|
|
|
|
|
|
257
|
0
|
|
|
0
|
1
|
|
sub get_buffered_data () { @{$_[0]->[key_data]} } |
|
0
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=back |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=head1 SEE ALSO |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
DBI, DBD::mysql |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=head1 Source Forge Project |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
If you find this software usefil please donate to the Source Forge Project. |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
L |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=head1 AUTHOR |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
Michael Shipper |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
Copyright (C) 2010 by Michael Shipper |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
280
|
|
|
|
|
|
|
it under the same terms as Perl itself, either Perl version 5.8.4 or, |
281
|
|
|
|
|
|
|
at your option, any later version of Perl 5 you may have available. |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
=cut |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
###################################### |
287
|
|
|
|
|
|
|
# |
288
|
|
|
|
|
|
|
# End of the package |
289
|
|
|
|
|
|
|
1; |
290
|
|
|
|
|
|
|
__END__ |