@@ -9,7 +9,7 @@ use Exporter 'import';
9
9
10
10
=head1 NAME
11
11
12
- SQL::Inserter - Fast insert SQL builder, buffered DBI inserting
12
+ SQL::Inserter - Efficient buffered DBI inserter and fast INSERT SQL builder
13
13
14
14
=head1 VERSION
15
15
@@ -35,8 +35,12 @@ our @EXPORT_OK = qw(simple_insert multi_insert_sql);
35
35
# Fastest method: pass single or multiple rows of data as an array
36
36
$sql->insert($col1_row1, $col2_row1, $col1_row2...);
37
37
38
- # Alternative, pass a single row as a hash, allows SQL code
39
- # instead of values (pass reference)
38
+ # You can manually flush the buffer at any time with no argument on insert
39
+ # (otherwise there is auto-flush on the object's destruction)
40
+ $sql->insert();
41
+
42
+ # Alternative, pass a single row as a hash, allows SQL code passed as references
43
+ # instead of values
40
44
$sql->insert({
41
45
column1 => $data1,
42
46
column2 => \'NOW()',
@@ -152,15 +156,18 @@ sub new {
152
156
=head2 insert
153
157
154
158
# Fastest array method. Only bind data is passed.
155
- $sql->insert(@column_data_array);
159
+ my $ret = $sql->insert(@column_data_array);
156
160
157
161
# Alternative, allows SQL code as values in addition to bind variables
158
- $sql->insert(\%row_data);
162
+ my $ret = $sql->insert(\%row_data);
159
163
160
164
# No parameters will force emtying of buffer (db write)
161
- $sql->insert();
165
+ my $ret = $sql->insert();
166
+
167
+ The main insert method. Returns the return value of the last C<execute > statement
168
+ if there was one called, 0 otherwise (buffer not full.
162
169
163
- The main insert method. It works in two modes, by passing an array or a hashref:
170
+ It works in two main modes, by passing an array or a hashref:
164
171
165
172
=over 4
166
173
@@ -185,7 +192,8 @@ automatically converted to C<NULL> if the C<null_undef> option was set).
185
192
=item Flushing the buffer
186
193
187
194
Calling C<insert > with no arguments forces a write to the db, flushing the buffer.
188
- You don't have to call this manually, the same will happen when the object is destroyed.
195
+ You don't have to call this manually as the buffer will be flushed when the object
196
+ is destroyed (e.g. your object falls out of scope).
189
197
190
198
=item Mixing modes
191
199
@@ -203,7 +211,7 @@ sub insert {
203
211
204
212
return $self -> _hash_insert(@_ ) if $_ [0] and ref ($_ [0]);
205
213
206
-
214
+ my $ret = 0;
207
215
if (@_ ) {
208
216
209
217
croak(" Calling insert without a hash requires cols defined in constructor" )
@@ -227,11 +235,12 @@ sub insert {
227
235
push @{$self -> {bind }}, splice (@_ );
228
236
$self -> {buffer_counter } += $rows ;
229
237
}
230
- $self -> _write_full_buffer() if $self -> {buffer_counter } == $self -> {buffer };
238
+ $ret = $ self-> _write_full_buffer() if $self -> {buffer_counter } == $self -> {buffer };
231
239
}
232
240
} elsif ($self -> {buffer_counter }) { # Empty the buffer
233
- $self -> _empty_buffer();
241
+ $ret = $ self-> _empty_buffer();
234
242
}
243
+ return $ret ;
235
244
}
236
245
237
246
=head1 ATTRIBUTES
@@ -365,7 +374,7 @@ sub multi_insert_sql {
365
374
return unless $table && $columns && @$columns ;
366
375
367
376
my $placeholders =
368
- join (' , ' , (' (' . join (' ,' , (' ?' ) x @$columns ) . ' )' ) x $num_rows );
377
+ join (" , \n " , (' (' . join (' ,' , (' ?' ) x @$columns ) . ' )' ) x $num_rows );
369
378
370
379
return _create_insert_sql($table , $columns , $placeholders , $dupe );
371
380
}
@@ -375,6 +384,7 @@ sub multi_insert_sql {
375
384
sub _hash_insert {
376
385
my $self = shift ;
377
386
my $fields = shift ;
387
+ my $ret = 0;
378
388
379
389
croak(" Insert was previously called with an array argument (still in buffer)" )
380
390
if $self -> {buffer_counter } && !$self -> {hash_buffer };
@@ -385,7 +395,9 @@ sub _hash_insert {
385
395
push @{$self -> {hash_buffer }}, $row ;
386
396
push @{$self -> {bind }}, @bind ;
387
397
388
- $self -> _write_hash_buffer() if $self -> {buffer_counter } == $self -> {buffer };
398
+ $ret = $self -> _write_hash_buffer() if $self -> {buffer_counter } == $self -> {buffer };
399
+
400
+ return $ret ;
389
401
}
390
402
391
403
sub _write_full_buffer {
@@ -396,6 +408,8 @@ sub _write_full_buffer {
396
408
397
409
$self -> _execute($self -> {full_buffer_insert });
398
410
$self -> _cleanup();
411
+
412
+ return $self -> {last_retval };
399
413
}
400
414
401
415
sub _prepare_full_buffer_insert {
@@ -411,7 +425,7 @@ sub _empty_buffer {
411
425
return $self -> _write_hash_buffer() if $self -> {hash_buffer };
412
426
413
427
my $rows = scalar (@{$self -> {bind }}) / scalar @{$self -> {cols }};
414
- my $sth = $self -> {dbh }-> prepare(
428
+ my $sth = $self -> {dbh }-> prepare(
415
429
multi_insert_sql(
416
430
$self -> {table },
417
431
$self -> {cols },
@@ -421,6 +435,8 @@ sub _empty_buffer {
421
435
);
422
436
$self -> _execute($sth );
423
437
$self -> _cleanup();
438
+
439
+ return $self -> {last_retval };
424
440
}
425
441
426
442
sub _write_hash_buffer {
@@ -434,6 +450,8 @@ sub _write_hash_buffer {
434
450
);
435
451
$self -> _execute($sth );
436
452
$self -> _cleanup();
453
+
454
+ return $self -> {last_retval };
437
455
}
438
456
439
457
sub _execute {
@@ -472,7 +490,7 @@ sub _create_insert_sql {
472
490
473
491
$sql .= _on_duplicate_key_update($columns ) if $dupe eq ' update' ;
474
492
475
- return " $sql ; " ;
493
+ return $sql ;
476
494
}
477
495
478
496
sub _row_placeholders {
@@ -534,6 +552,18 @@ Note that as of MySQL 8.0.20 the C<VALUES> in C<UPDATE> is deprecated (row alias
534
552
used instead), so this functionality might need to be updated some day if C<VALUES > is
535
553
removed completely.
536
554
555
+ =head2 Output whitespace
556
+
557
+ No spaces are added to the output string beyond the minimum. However, there is a new
558
+ line (C<\n > ) added for each row of value placeholders - mainly to easily count the
559
+ number of rows from the string.
560
+ Also, the C<ON DUPLICATE KEY UPDATE > clause is on a new line.
561
+
562
+ =head2 Error handling
563
+
564
+ The module does not do any error handling on C<prepare > /C<execute > statements,
565
+ you should use L<DBI> 's C<RaiseError > and C<HandleErrror > .
566
+
537
567
=head2 Performance
538
568
539
569
The OO interface has minimal overhead. The only consideration is that if your rows
@@ -581,7 +611,7 @@ L<https://metacpan.org/pod/SQL::Inserter>
581
611
582
612
=head1 LICENSE AND COPYRIGHT
583
613
584
- Copyright (C) 2023, SpareRoom.com
614
+ Copyright (C) 2023, SpareRoom
585
615
586
616
This is free software; you can redistribute it and/or modify it under
587
617
the same terms as the Perl 5 programming language system itself.
0 commit comments