use strict;
use warnings;
-use base qw/Class::Data::Inheritable/;
use NEXT;
-__PACKAGE__->mk_classdata('_accessor_group_deleted' => { });
-
sub mk_group_accessors {
my($self, $group, @fields) = @_;
};
}
-sub delete_accessor {
- my ($class, $accessor) = @_;
- $class = ref $class if ref $class;
- my $sym = "${class}::${accessor}";
- undef &$sym;
- delete $DB::sub{$sym};
- #$class->_accessor_group_deleted->{"${class}::${accessor}"} = 1;
-}
-
1;
my $wo_meth = ($class->can('mutator_name')
? $class->mutator_name($col)
: $col);
+ #warn "$col $ro_meth $wo_meth";
if ($ro_meth eq $wo_meth) {
$class->NEXT::ACTUAL::mk_group_accessors($group => [ $ro_meth => $col ]);
} else {
sub has_a {
my ($class, $col, @rest) = @_;
$class->NEXT::ACTUAL::has_a(lc($col), @rest);
- $class->delete_accessor($col);
$class->mk_group_accessors('has_a' => $col);
return 1;
}
my @extra;
foreach (@fields) {
my ($acc, $field) = ref $_ ? @$_ : ($_, $_);
+ #warn "$acc ".lc($acc)." $field";
next if defined &{"${class}::${acc}"};
push(@extra, [ lc $acc => $field ]);
}
$self->add_relationship($col, $f_class,
{ "foreign.${pri}" => "self.${col}" },
{ _type => 'has_a' } );
- $self->delete_accessor($col);
$self->mk_group_accessors('has_a' => $col);
return 1;
}
sub retrieve {
my ($class, @vals) = @_;
+ my $attrs = (@vals > 1 && ref $vals[$#vals] eq 'HASH' ? pop(@vals) : {});
my @pk = keys %{$class->_primaries};
die "Can't retrieve unless primary columns are defined" unless @pk;
my $query;
if (ref $vals[0] eq 'HASH') {
$query = $vals[0];
} elsif (@pk == @vals) {
- my $ret = ($class->retrieve_from_sql($class->_ident_cond, @vals))[0];
+ my $ret = ($class->retrieve_from_sql($class->_ident_cond, @vals, $attrs))[0];
#warn "$class: ".join(', ', %{$ret->{_column_data}});
return $ret;
} else {
unless ($value =~ s/^self\.//) {
die "Unable to convert relationship to WHERE clause: invalid value ${value}";
}
- unless ($self->can($value)) {
+ unless ($self->_columns->{$value}) {
die "Unable to convert relationship to WHERE clause: no such accessor ${value}";
}
push(@{$attrs->{bind}}, $self->get_column($value));
$attrs->{_action} = 'convert';
my ($cond) = $self->_cond_resolve($rel_obj->{cond}, $attrs);
$cond = "${s_cond} AND ${cond}" if $s_cond;
- return $rel_obj->{class}->retrieve_from_sql($cond, @{$attrs->{bind} || {}});
+ return $rel_obj->{class}->retrieve_from_sql($cond, @{$attrs->{bind} || []},
+ $attrs);
}
sub create_related {
sub insert {
my ($self) = @_;
- return if $self->{_in_database};
+ return if $self->in_database;
my $sth = $self->_get_sth('insert', [ keys %{$self->{_column_data}} ],
$self->_table_name, undef);
$sth->execute(values %{$self->{_column_data}});
$sth->finish;
- $self->{_in_database} = 1;
+ $self->in_database(1);
$self->{_dirty_columns} = {};
return $self;
}
sub in_database {
- return $_[0]->{_in_database};
+ my ($self, $val) = @_;
+ $self->{_in_database} = $val if @_ > 1;
+ return $self->{_in_database};
}
sub create {
sub update {
my ($self) = @_;
- die "Not in database" unless $self->{_in_database};
+ die "Not in database" unless $self->in_database;
my @to_update = keys %{$self->{_dirty_columns} || {}};
return -1 unless @to_update;
my $sth = $self->_get_sth('update', \@to_update,
sub delete {
my $self = shift;
if (ref $self) {
- die "Not in database" unless $self->{_in_database};
+ die "Not in database" unless $self->in_database;
#warn $self->_ident_cond.' '.join(', ', $self->_ident_values);
my $sth = $self->_get_sth('delete', undef,
$self->_table_name, $self->_ident_cond);
$sth->execute($self->_ident_values);
$sth->finish;
- delete $self->{_in_database};
+ $self->in_database(undef);
} else {
my $attrs = { };
if (@_ > 1 && ref $_[$#_] eq 'HASH') {
while (my @row = $sth->fetchrow_array) {
my $new = $class->new;
$new->store_column($_, shift @row) for @cols;
- $new->{_in_database} = 1;
+ $new->in_database(1);
push(@found, $new);
}
$sth->finish;
}
my $query = ref $_[0] eq "HASH" ? shift: {@_};
my ($cond, @param) = $class->_cond_resolve($query, $attrs);
- return $class->retrieve_from_sql($cond, @param);
+ return $class->retrieve_from_sql($cond, @param, $attrs);
}
sub search_like {