Skip to content

Commit

Permalink
Merge pull request #16 from tsucchi/no_ping_before_fetch_mysql_insertid
Browse files Browse the repository at this point in the history
no ping before fetch mysql_insertid
  • Loading branch information
ytnobody committed May 18, 2017
2 parents 710484e + 73e6b2b commit ccfa434
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ on 'test' => sub {

on 'develop' => sub {
requires 'Test::PostgreSQL';
requires 'Test::mysqld';
};
2 changes: 1 addition & 1 deletion lib/DBIx/Otogiri.pm
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ sub last_insert_id {
my @rows = $self->search_by_sql('SELECT LASTVAL() AS lastval');
return $rows[0]->{lastval};
}
return $self->dbh->last_insert_id($catalog, $schema, $table, $field, $attr_href);
return $self->{dbh}->last_insert_id($catalog, $schema, $table, $field, $attr_href);
}

sub reconnect {
Expand Down
43 changes: 43 additions & 0 deletions t/17_last_insert_id_in_mysql.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use strict;
use warnings;
use Test::More;
use Otogiri;

use Test::Requires 'Test::mysqld';

my $mysql = Test::mysqld->new(
my_cnf => {
'skip-networking' => '',
}
) or plan skip_all => $Test::mysqld::errstr;


my $db = Otogiri->new( connect_info => [$mysql->dsn(dbname => 'test'), '', '', { RaiseError => 1, PrintError => 0 }] );

my $sql_person = <<'EOF';
CREATE TABLE person (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(48) NOT NULL,
age INTEGER
);
EOF

$db->dbh->do($sql_person);

subtest 'last_insert_id with sequence name', sub {
$db->fast_insert('person', {
name => 'Sherlock Shellingford',
age => 15,
});
$db->fast_insert('person', {
name => 'Nero Yuzurizaki',
age => 15,
});

my ($row) = $db->search_by_sql('SELECT MAX(id) AS max_id FROM person');
my $lastval = $row->{max_id};

is( $db->last_insert_id, $lastval);
};

done_testing;

0 comments on commit ccfa434

Please sign in to comment.