Moose Identity Round Up. Part the Second
A little more identity clean-up here in the Moose-Pen today.
As a final clean up I would like this exception test to pass
{
caption => 'on an identity element blocked',
type => 'exception',
key => 'elements',
elements => [
{ name => 'id',
identity =>{'DBI::db'=>{'ORACLE' => {
name => 'NEXTVAL',
view => 'products_seq'}
}}
},
{ name => 'first_name', },
{ name => 'last_name', },
],
update => {
container => {
id => '101',
last_name => 'Bloggings',
first_name => 'Bill',
},
message => 'Attempt to use identity element: id in an update'
},
create => {
container => {
id => '101',
last_name => 'Bloggings',
first_name => 'Bill',
},
message => 'Attempt to use identity element: id in an create'
},
},
in other words you cannot, at least in SQL, update or insert (create) an 'element/field' that is flagged as identity.
Looking at the code I see that I can accomplish this in the '_insert_update_container' sub where I iterrate over all the items in the container looking for field matches. As I do this iteration in on either an array-ref or a hash-ref I might end up duplication code if I am not careful.
The first thing I did was create this internal sub;
sub _die_on_identity {
my $self = shift;
my ($action, $field) = @_;
die "Database::Accessor::Driver::DBI::Error->"
. "Attempt to use identity element: "
. $field
. " in an "
. lc($action);
}
and then I just call it if my field is an identity;
...
if ( ref($container) eq "ARRAY" ) {
my @fields = ();
$self->is_exe_array(1);
my $fields = $container->[0];
foreach my $key ( sort( keys( %{$fields} ) ) ) {
my $field = $self->get_element_by_name($key);
next
if ( !$field );
++ $self->_die_on_identity($action,$key)
++ if ($field->identity);
...
}
else {
foreach my $key ( sort( keys( %{$container} ) ) ) {
my $field = $self->get_element_by_name($key);
next
if ( !$field );
++ $self->_die_on_identity($action,$key)
++ if ($field->identity);
…
and my tests both pass
...
ok 16 - have identity option but do not use create params correct
ok 17 - create on an identity element blocked
ok 18 - update on an identity element blocked
and that is today's post-ette.
Leave a comment