Skip to content

Commit fb0fc79

Browse files
authored
Merge pull request #2 from SpareRoom/last_insert_id
last_insert_id added
2 parents d0baa91 + 3584f5e commit fb0fc79

File tree

4 files changed

+75
-19
lines changed

4 files changed

+75
-19
lines changed

Changes

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
Revision history for SQL-Inserter
22

3+
0.04 2023-12-17
4+
- Add last_insert_id.
5+
36
0.03 2023-10-29
47
- Add support for Oracle Database.
58

README.pod

+22-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ SQL::Inserter - Efficient buffered DBI inserter and fast INSERT SQL builder
44

55
=head1 VERSION
66

7-
Version 0.03
7+
Version 0.04
88

99
=head1 SYNOPSIS
1010

@@ -17,21 +17,22 @@ Version 0.03
1717
buffer => 100? # Default buffer is 100 rows
1818
);
1919

20-
# Fastest method: pass single or multiple rows of data as an array
20+
# Pass single or multiple rows of data as an array (fastest method):
2121
$sql->insert($col1_row1, $col2_row1, $col1_row2...);
2222

23-
# You can manually flush the buffer at any time with no argument on insert
24-
# (otherwise there is auto-flush on the object's destruction)
25-
$sql->insert();
26-
27-
# Alternative, pass a single row as a hash, allows SQL code passed as
28-
# references instead of values (no need to define cols in constructor)
23+
# Alternatively, pass a single row as a hash, allows SQL code passed as
24+
# references instead of values (no need to define cols in constructor):
2925
$sql->insert({
3026
column1 => $data1,
3127
column2 => \'NOW()',
3228
...
3329
});
3430

31+
# Since the inserts are buffered, they might not have been executed yet.
32+
# You can manually flush the buffer at any time with no argument on insert
33+
# (otherwise there is auto-flush on the object's destruction):
34+
$sql->insert();
35+
3536
# There are also functions to just get the SQL statement and its bind vars
3637
# similar to SQL::Abstract or SQL::Maker insert, but with much less overhead:
3738
my ($sql, @bind) = simple_insert($table, {col1=>$val...});
@@ -179,6 +180,19 @@ from the very first hash), and after flushing the buffer you can switch to array
179180

180181
=back
181182

183+
=head2 last_insert_id
184+
185+
# MySQL
186+
my $id = $sql->last_insert_id;
187+
188+
# Depending on the driver you might need parameters
189+
my $id = $sql->last_insert_id($catalog, $schema, $table, $field, \%attr);
190+
191+
Returns the id of the last insert row, if available, after emptying the buffer.
192+
193+
Convenience wrapper around L<DBI>'s database handle method of the same name. See
194+
that method's documentation for details and caveats depending on your DB driver.
195+
182196
=head1 ATTRIBUTES
183197

184198
=head2 C<last_retval>

lib/SQL/Inserter.pm

+33-9
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ SQL::Inserter - Efficient buffered DBI inserter and fast INSERT SQL builder
1313
1414
=head1 VERSION
1515
16-
Version 0.03
16+
Version 0.04
1717
1818
=cut
1919

20-
our $VERSION = '0.03';
20+
our $VERSION = '0.04';
2121

2222
our @EXPORT_OK = qw(simple_insert multi_insert_sql);
2323

@@ -32,21 +32,22 @@ our @EXPORT_OK = qw(simple_insert multi_insert_sql);
3232
buffer => 100? # Default buffer is 100 rows
3333
);
3434
35-
# Fastest method: pass single or multiple rows of data as an array
35+
# Pass single or multiple rows of data as an array (fastest method):
3636
$sql->insert($col1_row1, $col2_row1, $col1_row2...);
3737
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
43-
# references instead of values (no need to define cols in constructor)
38+
# Alternatively, pass a single row as a hash, allows SQL code passed as
39+
# references instead of values (no need to define cols in constructor):
4440
$sql->insert({
4541
column1 => $data1,
4642
column2 => \'NOW()',
4743
...
4844
});
4945
46+
# Since the inserts are buffered, they might not have been executed yet.
47+
# You can manually flush the buffer at any time with no argument on insert
48+
# (otherwise there is auto-flush on the object's destruction):
49+
$sql->insert();
50+
5051
# There are also functions to just get the SQL statement and its bind vars
5152
# similar to SQL::Abstract or SQL::Maker insert, but with much less overhead:
5253
my ($sql, @bind) = simple_insert($table, {col1=>$val...});
@@ -263,6 +264,29 @@ sub insert {
263264
return $ret;
264265
}
265266

267+
=head2 last_insert_id
268+
269+
# MySQL
270+
my $id = $sql->last_insert_id;
271+
272+
# Depending on the driver you might need parameters
273+
my $id = $sql->last_insert_id($catalog, $schema, $table, $field, \%attr);
274+
275+
Returns the id of the last insert row, if available, after emptying the buffer.
276+
277+
Convenience wrapper around L<DBI>'s database handle method of the same name. See
278+
that method's documentation for details and caveats depending on your DB driver.
279+
280+
=cut
281+
282+
sub last_insert_id {
283+
my $self = shift;
284+
285+
$self->_empty_buffer() if $self->{buffer_counter};
286+
287+
return $self->{dbh}->last_insert_id(@_);
288+
}
289+
266290
=head1 ATTRIBUTES
267291
268292
=head2 C<last_retval>

t/simple.t

+17-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ my @execute = ();
88

99
my $dbh = mock {} => (
1010
add => [
11-
prepare => sub { my $self = shift; push @prepare, @_ ; return $self},
12-
execute => sub { shift; @execute = @_ ; return 1},
11+
prepare => sub {my $self = shift; push @prepare, @_ ; return $self},
12+
execute => sub {shift; @execute = @_ ; return 1},
13+
last_insert_id => sub {1},
1314
]
1415
);
1516

@@ -197,4 +198,18 @@ subtest 'new' => sub {
197198
ok(!$sql->{oracle}, "Oracle not detected");
198199
};
199200

201+
subtest 'last_insert_id' => sub {
202+
@execute = ();
203+
@prepare = ();
204+
my $sql = SQL::Inserter->new(dbh=>$dbh,table=>'table',cols=>[qw/col1 col2/],buffer=>3);
205+
$sql->last_insert_id;
206+
is([@prepare],[], "No prepared statement");
207+
is($sql->insert(1,2),0, "No execute");
208+
is($sql->{row_total}, undef, "No row_total");
209+
210+
is($sql->last_insert_id, 1, "Last insert id");
211+
is([@execute],[1,2], "Last execute bind vars");
212+
is($sql->{row_total}, 1, "1 execute");
213+
};
214+
200215
done_testing;

0 commit comments

Comments
 (0)