File: Mysql.pm

package info (click to toggle)
libdbix-bulkloader-mysql-perl 1.006-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 76 kB
  • sloc: perl: 85; makefile: 2
file content (290 lines) | stat: -rw-r--r-- 6,092 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package DBIx::BulkLoader::Mysql;

use strict;
use warnings;

our $VERSION = '1.006';

use constant key_count=>0;
use constant key_single_insert=>1;
use constant key_bulk_insert=>2;
use constant key_buffer=>3;
use constant key_sql_insert=>4;
use constant key_sql_columns=>5;
use constant key_bulk_sql=>6;
use constant key_single_sql=>7;
use constant key_data=>8;
use constant key_placeholder_count=>9;


# Below is stub documentation for your module. You'd better edit it!

=head1 NAME

DBIx::BulkLoader::Mysql - Perl extension for mysql bulk loading

=head1 SYNOPSIS

  use DBIx::BulkLoader::Mysql;

  # non repeating portion of the insert statement
  my $insert='insert into bulk_insert (col_a,col_b,col_c) values ';

  # repeating portion of the insert statement
  my $placeholders='(?,?,?)';

  # how many rows to buffer until insert is called
  my $bulk_insert_count=5;

  # db connection
  my $dbh=DBI->connect(db connection info here);
  my $placeholder_count=3;

  my ($bulk,$error)=DBIx::BulkLoader::Mysql->new(
  		dbh=>$dbh
		,sql_insert=>$insert
		,placeholders=>$placeholders
   );
   die $error unless $bulk;

   for( 1 .. 50 ) {
     $bulk->insert(qw(a b c));
   }
   # inserted 50 rows at once

   $bulk->insert(qw(l l x));
   # inserted 0 rows

   $bulk->insert(qw(l l x));
   # inserted 0 rows

   $bulk->flush;
   # inserted 2 rows 1 at a time

=head1 DESCRIPTION

Simple buffering bulk loader interface for mysql.

=head2 EXPORT

None.

=head2 OO Methods

This section covers the OO methods for this package.

=over 4

=item * my ($bulk,$error)=DBIx::BulkLoader::Mysql->new(%hash);

Package constructor.

	$bulk is undef on error
	$error explains why $bulk is undef

Constructor options

                dbh=>$dbh
		 Sets the DBH object

                sql_insert=>$insert
		 Contains the body of the sql statement minus the
		 placeholder segment.

                placeholders=>$placeholders
		 Placeholder segment of the sql statement

                placeholder_count=>3
		 Optional argument
		  If you get strange insert counts or dbi bails
		  set this option manually

                bulk_insert_count=>50
		 Optional argument
		  Sets the number of rows to buffer for insert.

                prepare_args=>{}
		 Optional argument
		  Arguments to be passed to $dbh->prepare
		  See DBD::mysql

=cut

sub new {
	my ($class,%hash)=@_;
	my $s=bless [],$class;
	$s->[key_data]=[];
	$hash{bulk_insert_count}=$hash{bulk_insert_count} 
		? $hash{bulk_insert_count} : 50;

	# stop here if we have some bad arguments
	return (undef,'placeholders=>"" not set!') unless $hash{placeholders};
	return (undef,'sql_insert=>"" not set!') unless $hash{sql_insert};
	return (undef,'dbh=>$dbh not set!') unless $hash{dbh};
	unless($hash{placeholder_count}) {
	  for( $hash{placeholders}=~ /\?/g){ 
	   $hash{placeholder_count}++
	  }
	}
	$s->[key_placeholder_count]=$hash{placeholder_count} ;

	$s->[key_buffer]=
		$hash{placeholder_count} * $hash{bulk_insert_count};

	my $prep_args=$hash{prepare_args} ? $hash{prepare_args} : ({});
	my $single=join ' ',$hash{sql_insert},$hash{placeholders};

	# run the prepare statement
	$s->[key_single_sql]=$single;
	$s->[key_single_insert]=eval {
		$hash{dbh}->prepare(
			$single
			,$hash{prepare_args}
		);
	};
	return undef,"failed to prepare: $single" if $@;
	

	my @placeholders;
	for(1 .. $hash{bulk_insert_count}) { 
		push @placeholders,$hash{placeholders};
	}
	my $bulk=join(' ',$hash{sql_insert},
		join(', ',@placeholders)
	);

	return undef,"failed to prepare: $bulk" if $@;
	$s->[key_bulk_sql]=$bulk;
	$s->[key_bulk_insert]=eval {
		$hash{dbh}->prepare(
			$bulk
			,$hash{prepare_args}
		);
	};
	
	$s,undef;
}

=item * $bulk->flush;

Empties the placeholder buffer

=cut

sub flush () {
	my ($s)=@_;
	my $row=$s->[key_data];
	while(my @single=splice(@$row,0, $s->get_placeholder_count)) {
		$s->get_prepared_single_sth->execute(@single);
	}
}

sub DESTROY { 
	@{$_[0]}=() 
}

=item * $bulk->insert($x,$y,$z);

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".

=cut

sub insert {
	my ($s,@data)=@_;
	my $row=$s->[key_data];
	push @$row,@data;
	if((1 + $#$row)==$s->get_buffer_size) {
		$s->get_prepared_bulk_sth->execute(@$row);
		@$row=();
	}

}

=item * my $columns=$bulk->get_placeholder_count;

Gets the total number of column placeholders.

=cut

sub get_placeholder_count () { $_[0]->[key_placeholder_count] }

=item * my $buffer_size=$bulk->get_buffer_size;

Gets the total size of the array used for insert.

=cut

sub get_buffer_size () { $_[0]->[key_buffer] }

=item * my $sql_single=$bulk->single_sql;

Gets the raw sql statement used for single row inserts.

=cut

sub single_sql() { $_[0]->[key_single_sql] }

=item * my $bulk_sql=$bulk->bulk_sql;

Gets the raw sql statement used for bulk row inserts.

=cut

sub bulk_sql() { $_[0]->[key_bulk_sql] }

=item * my $single_sth=$bulk->get_prepared_single_sth;

Gets the prepared statement handle for single row inserts.

=cut

sub get_prepared_single_sth () { $_[0]->[key_single_insert] }

=item * my $bulk_sth=$bulk->get_prepared_bulk_sth;

Gets the prepared statement handle for bulk row inserts.

=cut

sub get_prepared_bulk_sth () { $_[0]->[key_bulk_insert] }

=item * my @buffer=$bulk->get_buffered_data;

Returns a list containing the current buffered data

=cut

sub get_buffered_data () { @{$_[0]->[key_data]} }

=back
	
=head1 SEE ALSO

DBI, DBD::mysql

=head1 Source Forge Project

If you find this software usefil please donate to the Source Forge Project.

L<DBIx BulkLoader Mysql|https://sourceforge.net/projects/dbix-bulkloader/>

=head1 AUTHOR

Michael Shipper

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2010 by Michael Shipper

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.4 or,
at your option, any later version of Perl 5 you may have available.


=cut

######################################
#
# End of the package
1;
__END__