sub insert {
my ($self) = @_;
- return $self if $self->in_database;
+ return $self if $self->in_storage;
#use Data::Dumper; warn Dumper($self);
my %in;
$in{$_} = $self->get_column($_)
my %out = %{ $self->storage->insert($self->_table_name, \%in) };
$self->store_column($_, $out{$_})
for grep { $self->get_column($_) ne $out{$_} } keys %out;
- $self->in_database(1);
+ $self->in_storage(1);
$self->{_dirty_columns} = {};
return $self;
}
-=item in_database
+=item in_storage
- $obj->in_database; # Get value
- $obj->in_database(1); # Set value
+ $obj->in_storage; # Get value
+ $obj->in_storage(1); # Set value
Indicated whether the object exists as a row in the database or not
=cut
-sub in_database {
+sub in_storage {
my ($self, $val) = @_;
- $self->{_in_database} = $val if @_ > 1;
- return $self->{_in_database};
+ $self->{_in_storage} = $val if @_ > 1;
+ return $self->{_in_storage};
}
=item create
sub update {
my ($self, $upd) = @_;
- $self->throw( "Not in database" ) unless $self->in_database;
+ $self->throw( "Not in database" ) unless $self->in_storage;
my %to_update = %{$upd || {}};
$to_update{$_} = $self->get_column($_) for $self->is_changed;
return -1 unless keys %to_update;
$obj->delete
Deletes the object from the database. The object is still perfectly usable
-accessor-wise etc. but ->in_database will now return 0 and the object must
+accessor-wise etc. but ->in_storage will now return 0 and the object must
be re ->insert'ed before it can be ->update'ed
=cut
sub delete {
my $self = shift;
if (ref $self) {
- $self->throw( "Not in database" ) unless $self->in_database;
+ $self->throw( "Not in database" ) unless $self->in_storage;
#warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
$self->storage->delete($self->_table_name, $self->ident_condition);
- $self->in_database(undef);
+ $self->in_storage(undef);
#$self->store_column($_ => undef) for $self->primary_columns;
# Should probably also arrange to trash PK if auto
# but if we do, post-delete cascade triggers fail :/
$class->_mk_column_accessors(@cols);
}
-=item retrieve_from_sql
+=item search_literal
- my @obj = $class->retrieve_from_sql($sql_where_cond, @bind);
- my $cursor = $class->retrieve_from_sql($sql_where_cond, @bind);
+ my @obj = $class->search_literal($literal_where_cond, @bind);
+ my $cursor = $class->search_literal($literal_where_cond, @bind);
=cut
-sub retrieve_from_sql {
+sub search_literal {
my ($class, $cond, @vals) = @_;
$cond =~ s/^\s*WHERE//i;
my $attrs = (ref $vals[$#vals] eq 'HASH' ? { %{ pop(@vals) } } : {});
return $class->search(\$cond, $attrs);
}
-=item count_from_sql
+=item count_literal
- my $count = $class->count($sql_where_cond);
+ my $count = $class->count_literal($literal_where_cond);
=cut
-sub count_from_sql {
+sub count_literal {
my ($class, $cond, @vals) = @_;
$cond =~ s/^\s*WHERE//i;
my $attrs = (ref $vals[$#vals] eq 'HASH' ? pop(@vals) : {});
if (@_ > 1 && ref $_[$#_] eq 'HASH') {
$attrs = { %{ pop(@_) } };
}
- my $query = ref $_[0] eq "HASH" || (@_ == 1) ? shift: {@_};
+ my $query = (@_ == 1 || ref $_[0] eq "HASH" ? shift: {@_});
my @cols = 'COUNT(*)';
my $cursor = $class->storage->select($class->_table_name, \@cols,
$query, $attrs);
my ($class, $cols, $row) = @_;
my $new = $class->new;
$new->store_column($_, shift @$row) for @$cols;
- $new->in_database(1);
+ $new->in_storage(1);
return $new;
}
return $new->insert;
}
-#sub _cond_resolve {
-# my ($self, $query, $attrs) = @_;
-# return '1 = 1' unless keys %$query;
-# my $op = $attrs->{'cmp'} || '=';
-# my $cond = join(' AND ',
-# map { (defined $query->{$_}
-# ? "$_ $op ?"
-# : (do { delete $query->{$_}; "$_ IS NULL"; }));
-# } keys %$query);
-# return ($cond, values %$query);
-#}
-
=item table
__PACKAGE__->table('tbl_name');
sub insert_or_update {
my $self = shift;
- return ($self->in_database ? $self->update : $self->insert);
-}
-
-=item retrieve_all
-
- my @all = $class->retrieve_all;
-
-=cut
-
-sub retrieve_all {
- my ($class) = @_;
- return $class->retrieve_from_sql( '1' );
+ return ($self->in_storage ? $self->update : $self->insert);
}
=item is_changed