=cut
-
sub _ident_cond {
my ($class) = @_;
return join(" AND ", map { "$_ = ?" } keys %{$class->_primaries});
sub set_primary_key {
my ($class, @cols) = @_;
my %pri;
- tie %pri, 'Tie::IxHash';
- %pri = map { $_ => {} } @cols;
+ tie %pri, 'Tie::IxHash', map { $_ => {} } @cols;
$class->_primaries(\%pri);
}
if (ref $vals[0] eq 'HASH') {
$query = $vals[0];
} elsif (@pk == @vals) {
- my $ret = ($class->search_literal($class->_ident_cond, @vals, $attrs))[0];
+ $query = {};
+ @{$query}{@pk} = @vals;
+ #my $ret = ($class->search_literal($class->_ident_cond, @vals, $attrs))[0];
#warn "$class: ".join(', ', %{$ret->{_column_data}});
- return $ret;
+ #return $ret;
} else {
$query = {@vals};
}
$class->throw( "Can't find unless all primary keys are specified" )
unless (keys %$query >= @pk); # If we check 'em we run afoul of uc/lc
# column names etc. Not sure what to do yet
- my $ret = ($class->search($query))[0];
- #warn "$class: ".join(', ', %{$ret->{_column_data}});
- return $ret;
+ #return $class->search($query)->next;
+ my @cols = $class->_select_columns;
+ my @row = $class->storage->select_single($class->_table_name, \@cols, $query);
+ return (@row ? $class->_row_to_object(\@cols, \@row) : ());
}
sub discard_changes {
my ($self) = @_;
delete $self->{_dirty_columns};
- return unless $self->in_database; # Don't reload if we aren't real!
+ return unless $self->in_storage; # Don't reload if we aren't real!
my ($reload) = $self->find($self->id);
unless ($reload) { # If we got deleted in the mean-time
- $self->in_database(0);
+ $self->in_storage(0);
return $self;
}
delete @{$self}{keys %$self};
return keys %{shift->_primaries};
}
+sub ID {
+ my ($self) = @_;
+ $self->throw( "Can't call ID() as a class method" ) unless ref $self;
+ return undef unless $self->in_storage;
+ return $self->_create_ID(map { $_ => $self->{_column_data}{$_} } keys %{$self->_primaries});
+}
+
+sub _create_ID {
+ my ($class,%vals) = @_;
+ $class = ref $class || $class;
+ return join '|', $class, map { $_ . '=' . $vals{$_} } sort keys %vals;
+}
+
1;
=back
=head1 AUTHORS
-Matt S. Trout <perl-stuff@trout.me.uk>
+Matt S. Trout <mst@shadowcatsystems.co.uk>
=head1 LICENSE