blib/lib/DBIx/BulkLoader/Mysql.pm

filter: Statementstmt 44.7% 38 / 85 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 52.1% 12 / 23 Podpod 100% 10 / 10 totaltotal 43.4% 60 / 138 CC 13 SCAR 37.6
CC
13
Cov
36%
new
132.0
flush
6.0
insert
6.0
CRAP 43.0
↑ Summary
1 package DBIx::BulkLoader::Mysql;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = '1.006';
7  
8 1 ▶use constant key_count=>0;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use constant key_single_insert=>1;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use constant key_bulk_insert=>2;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use constant key_buffer=>3;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant key_sql_insert=>4;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use constant key_sql_columns=>5;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use constant key_bulk_sql=>6;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant key_single_sql=>7;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use constant key_data=>8;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use constant key_placeholder_count=>9;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 0 ▶sub new {
Subroutine new: not called CC=11 SCAR=48.8
Pod documented
114 0 my ($class,%hash)=@_;
115 0 my $s=bless [],$class;
116 0 $s->[key_data]=[];
117 $hash{bulk_insert_count}=$hash{bulk_insert_count}
118 0 ▶ ? $hash{bulk_insert_count} : 50;
Branch: 1 branch
BranchTrueFalseTotal
$hash{'bulk_insert_count'} ? : 0 0 0
119  
120 # stop here if we have some bad arguments
121 0 ▶ return (undef,'placeholders=>"" not set!') unless $hash{placeholders};
Branch: 1 branch
BranchTrueFalseTotal
unless $hash{"placeholders"} 0 0 0
122 0 ▶ return (undef,'sql_insert=>"" not set!') unless $hash{sql_insert};
Branch: 1 branch
BranchTrueFalseTotal
unless $hash{"sql_insert"} 0 0 0
123 0 ▶ return (undef,'dbh=>$dbh not set!') unless $hash{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $hash{"dbh"} 0 0 0
124 0 ▶ unless($hash{placeholder_count}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hash{"placeholder_count"}) 0 0 0
125 0 for( $hash{placeholders}=~ /\?/g){
126 0 $hash{placeholder_count}++
127 }
128 }
129 0 $s->[key_placeholder_count]=$hash{placeholder_count} ;
130  
131 $s->[key_buffer]=
132 0 $hash{placeholder_count} * $hash{bulk_insert_count};
133  
134 0 ▶ my $prep_args=$hash{prepare_args} ? $hash{prepare_args} : ({});
Branch: 1 branch
BranchTrueFalseTotal
$hash{'prepare_args'} ? : 0 0 0
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 $hash{dbh}->prepare(
141 $single
142 ,$hash{prepare_args}
143 0 );
144 };
145 0 ▶ return undef,"failed to prepare: $single" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
146
147  
148 0 my @placeholders;
149 0 for(1 .. $hash{bulk_insert_count}) {
150 0 push @placeholders,$hash{placeholders};
151 }
152 my $bulk=join(' ',$hash{sql_insert},
153 0 join(', ',@placeholders)
154 );
155  
156 0 ▶ return undef,"failed to prepare: $bulk" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
157 0 $s->[key_bulk_sql]=$bulk;
158 0 $s->[key_bulk_insert]=eval {
159 $hash{dbh}->prepare(
160 $bulk
161 ,$hash{prepare_args}
162 0 );
163 };
164
165 0 $s,undef;
166 }
167  
168 =item * $bulk->flush;
169  
170 Empties the placeholder buffer
171  
172 =cut
173  
174 0 ▶sub flush () {
Subroutine flush: not called CC=2 SCAR=17.9
Pod documented
175 0 my ($s)=@_;
176 0 my $row=$s->[key_data];
177 0 ▶ while(my @single=splice(@$row,0, $s->get_placeholder_count)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@single) = splice(@$row, 0, $s->get_placeholder_count)) 0 0 0
178 0 $s->get_prepared_single_sth->execute(@single);
179 }
180 }
181  
182 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
183 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 0 ▶sub insert {
Subroutine insert: not called CC=2 SCAR=17.9
Pod documented
193 0 my ($s,@data)=@_;
194 0 my $row=$s->[key_data];
195 0 push @$row,@data;
196 0 ▶ if((1 + $#$row)==$s->get_buffer_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 + $row->$#* == $s->get_buffer_size) 0 0 0
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 ▶sub get_placeholder_count () { $_[0]->[key_placeholder_count] }
Subroutine get_placeholder_count: not called CC=1 SCAR=6.9
Pod documented
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 ▶sub get_buffer_size () { $_[0]->[key_buffer] }
Subroutine get_buffer_size: not called CC=1 SCAR=6.9
Pod documented
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 ▶sub single_sql() { $_[0]->[key_single_sql] }
Subroutine single_sql: not called CC=1 SCAR=6.9
Pod documented
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 ▶sub bulk_sql() { $_[0]->[key_bulk_sql] }
Subroutine bulk_sql: not called CC=1 SCAR=6.9
Pod documented
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 ▶sub get_prepared_single_sth () { $_[0]->[key_single_insert] }
Subroutine get_prepared_single_sth: not called CC=1 SCAR=6.9
Pod documented
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 ▶sub get_prepared_bulk_sth () { $_[0]->[key_bulk_insert] }
Subroutine get_prepared_bulk_sth: not called CC=1 SCAR=6.9
Pod documented
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 ▶sub get_buffered_data () { @{$_[0]->[key_data]} }
Subroutine get_buffered_data: not called CC=1 SCAR=6.9
Pod documented
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<DBIx BulkLoader Mysql|https://sourceforge.net/projects/dbix-bulkloader/>
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 1;
290 __END__
↑ Summary