+sub call_sub_if {
+ my $self = shift;
+ my $sub = shift;
+ my $check = shift;
+
+ $check = $check->($self) if ref($check) eq 'CODE';
+
+ return $check ? $self->$sub(@_) : $self;
+}
+
+sub get_first_conflicting {
+ my ($self, @attributes) = @_;
+
+ my $primary_key = ($self->meta->primary_key)[0];
+ my @where = map { ($_ => $self->$_) } @attributes;
+
+ push @where, ("!$primary_key" => $self->$primary_key) if $self->$primary_key;
+
+ return $self->_get_manager_class->get_first(where => [ and => \@where ]);
+}
+
+# These three functions cannot sit in SL::DB::Object::Hooks because
+# mixins don't deal well with super classes (SUPER is the current
+# package's super class, not $self's).
+sub load {
+ my ($self, @args) = @_;
+
+ SL::DB::Object::Hooks::run_hooks($self, 'before_load');
+ my $result = $self->SUPER::load(@args);
+ SL::DB::Object::Hooks::run_hooks($self, 'after_load', $result);
+
+ return $result;
+}
+
+sub save {
+ my ($self, @args) = @_;
+
+ my ($result, $exception);
+ my $worker = sub {
+ $exception = $EVAL_ERROR unless eval {
+ SL::DB::Object::Hooks::run_hooks($self, 'before_save');
+ $result = $self->SUPER::save(@args);
+ SL::DB::Object::Hooks::run_hooks($self, 'after_save', $result);
+ 1;
+ };
+
+ return $result;
+ };
+
+ $self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker);
+
+ die $exception if $exception;
+
+ return $result;
+}
+
+sub delete {
+ my ($self, @args) = @_;
+
+ my ($result, $exception);
+ my $worker = sub {
+ $exception = $EVAL_ERROR unless eval {
+ SL::DB::Object::Hooks::run_hooks($self, 'before_delete');
+ $result = $self->SUPER::delete(@args);
+ SL::DB::Object::Hooks::run_hooks($self, 'after_delete', $result);
+ 1;
+ };
+
+ return $result;
+ };
+
+ $self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker);
+
+ die $exception if $exception;
+
+ return $result;
+}
+
+sub load_cached {
+ my $class_or_self = shift;
+ my @ids = @_;
+ my $class = ref($class_or_self) || $class_or_self;
+ my $cache = $::request->cache("::SL::DB::Object::object_cache::${class}");
+
+ croak "Missing ID" unless @ids;
+
+ my @missing_ids = grep { !exists $cache->{$_} } @ids;
+
+ return $cache->{$ids[0]} if !@missing_ids;
+
+ croak "Caching can only be used with classes with exactly one primary key column" if 1 != scalar(@{ $class->meta->primary_key_columns });
+
+ my $primary_key = $class->meta->primary_key_columns->[0]->name;
+ my $objects = $class->_get_manager_class->get_all(where => [ $primary_key => \@missing_ids ]);
+
+ $cache->{$_->$primary_key} = $_ for @{ $objects};
+
+ return $cache->{$ids[0]};
+}
+
+sub invalidate_cached {
+ my ($class_or_self, @ids) = @_;
+ my $class = ref($class_or_self) || $class_or_self;
+
+ if (ref($class_or_self) && !@ids) {
+ croak "Caching can only be used with classes with exactly one primary key column" if 1 != scalar(@{ $class->meta->primary_key_columns });
+
+ my $primary_key = $class->meta->primary_key_columns->[0]->name;
+ @ids = ($class_or_self->$primary_key);
+ }
+
+ delete @{ $::request->cache("::SL::DB::Object::object_cache::${class}") }{ @ids };
+
+ return $class_or_self;
+}
+