our $AUTOLOAD;
if($AUTOLOAD =~ /::fetch(_all)?(?:_by_(\w+?))?(?:_HASHED_FROM_(\w+?))?(?:_TO_(\w+?))?$/) {
my $all = $1;
my $filter_string = $2;
my $key_string = $3;
my $value_column = $4;
my ($self) = @_;
my $column_set = $self->column_set();
my $filter_components = $filter_string && [ split(/_AND_/i, $filter_string) ];
foreach my $column_name ( @$filter_components ) {
unless($column_set->{$column_name}) {
die "unknown column '$column_name'";
}
}
my $key_components = $key_string && [ split(/_AND_/i, $key_string) ];
foreach my $column_name ( @$key_components ) {
unless($column_set->{$column_name}) {
die "unknown column '$column_name'";
}
}
if($value_column && !$column_set->{$value_column}) {
die "unknown column '$value_column'";
}
# warn "Setting up '$AUTOLOAD' method\n";
$AUTOLOAD = sub {
my $self = shift @_;
return $self->fetch_all(
join(' AND ', map { "$filter_components->[$_]='$_[$_]'" } 0..scalar(@$filter_components)-1),
!$all,
$key_components,
$value_column
);
};
goto &$AUTOLOAD; # restart the new method
} elsif($AUTOLOAD =~ /::count_all_by_(\w+)$/) {
my $filter_string = $1;
my ($self) = @_;
my $column_set = $self->column_set();
my $filter_components = $filter_string && [ split(/_AND_/i, $filter_string) ];
foreach my $column_name ( @$filter_components ) {
unless($column_set->{$column_name}) {
die "unknown column '$column_name'";
}
}
# warn "Setting up '$AUTOLOAD' method\n";
$AUTOLOAD = sub {
my $self = shift @_;
return $self->count_all(
join(' AND ', map { "$filter_components->[$_]='$_[$_]'" } 0..scalar(@$filter_components)-1),
);
};
goto &$AUTOLOAD; # restart the new method
} elsif($AUTOLOAD =~ /::remove_all_by_(\w+)$/) {
my $filter_name = $1;
my ($self) = @_;
my $column_set = $self->column_set();
if($column_set->{$filter_name}) {
# warn "Setting up '$AUTOLOAD' method\n";
$AUTOLOAD = sub { my ($self, $filter_value) = @_; return $self->remove_all("$filter_name='$filter_value'"); };
goto &$AUTOLOAD; # restart the new method
} else {
die "unknown column '$filter_name'";
}
} elsif($AUTOLOAD =~ /::update_(\w+)$/) {
my @columns_to_update = split(/_AND_/i, $1);
# warn "Setting up '$AUTOLOAD' method\n";
$AUTOLOAD = sub { my ($self, $object) = @_; return $self->update($object, @columns_to_update); };
goto &$AUTOLOAD; # restart the new method
} else {
warn "sub '$AUTOLOAD' not implemented";
}
}