Subject: | insert() will not call create() overrides |
3.0.9 changed create() to insert() and left in an alias for create().
create() and insert() should be synonyms. Unfortunately the way it was
done, calling insert() bypasses a subclass' create() override. This
means older CDBI classes may misbehave when insert() is called.
The attached patch fixes the problem.
Subject: | create_alias.patch |
--- lib/Class/DBI.pm (revision 54479)
+++ lib/Class/DBI.pm (local)
@@ -426,7 +426,7 @@
return defined($exists) ? $exists : $class->insert($hash);
}
-sub insert {
+sub create {
my $class = shift;
return $class->_croak("insert needs a hashref") unless ref $_[0] eq 'HASH';
my $info = { %{ +shift } }; # make sure we take a copy
@@ -445,7 +445,12 @@
return $class->_insert($data);
}
-*create = \&insert;
+# Ensure that calls to insert() call old create() overrides.
+sub insert {
+ my $class = shift;
+ $class->create(@_);
+}
+
#----------------------------------------------------------------------
# Low Level Data Access
--- t/create_alias.t (revision 54479)
+++ t/create_alias.t (local)
@@ -0,0 +1,57 @@
+#!/usr/bin/perl -w
+
+# Test the alias between create() and insert() for the purposes of backwards
+# compatibility
+
+use Test::More;
+use strict;
+
+BEGIN {
+ eval "use DBD::SQLite";
+ plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : ('no_plan');
+}
+
+INIT {
+ use lib 't/testlib';
+ use Film;
+}
+
+
+{
+ my $film = Film->create({
+ Title => 'Impulse',
+ });
+
+ isa_ok $film, 'Film';
+ is $film->Title, 'Impulse', 'create() works';
+
+ ok $film->delete;
+}
+
+{
+ my $create_called = 0;
+ my $film;
+
+ {
+ package Film;
+ no warnings 'once';
+ local *create = sub {
+ my $class = shift;
+
+ $create_called++;
+ $class->SUPER::create(@_);
+ };
+
+ $film = Film->insert({
+ Title => 'Inframan',
+ });
+ }
+
+ isa_ok $film, 'Film';
+ is $film->Title, 'Inframan';
+
+ is $create_called, 1, 'insert() calls a create() override';
+
+ ok $film->delete;
+}
+