From 73e6b2b5ad1d49f303e58c085fedf425bbde010b Mon Sep 17 00:00:00 2001 From: Takuya Tsuchida Date: Thu, 18 May 2017 19:38:57 +0900 Subject: [PATCH] no ping before fetch mysql_insertid --- cpanfile | 1 + lib/DBIx/Otogiri.pm | 2 +- t/17_last_insert_id_in_mysql.t | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 t/17_last_insert_id_in_mysql.t diff --git a/cpanfile b/cpanfile index 31aad4f..3fd97b6 100644 --- a/cpanfile +++ b/cpanfile @@ -16,4 +16,5 @@ on 'test' => sub { on 'develop' => sub { requires 'Test::PostgreSQL'; + requires 'Test::mysqld'; }; diff --git a/lib/DBIx/Otogiri.pm b/lib/DBIx/Otogiri.pm index 99a32a7..1a9a752 100644 --- a/lib/DBIx/Otogiri.pm +++ b/lib/DBIx/Otogiri.pm @@ -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 { diff --git a/t/17_last_insert_id_in_mysql.t b/t/17_last_insert_id_in_mysql.t new file mode 100644 index 0000000..3c7b2a3 --- /dev/null +++ b/t/17_last_insert_id_in_mysql.t @@ -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;