我有 20 个表,我正在尝试向我的“模型”添加一个搜索函数,该函数返回用户在“视图”部分中选择的表的行中的值。
sub search {
my ( $table, $page_no, $search_value, $where, $order ) =
@args{qw/table page_no search_value where order/};
my $option = {};
if ( defined $order || defined $self->order ) {
$option->{order_by} = $order // $self->order;
}
my @rows = $self->ds->select( $where, $option );
return ( \@rows );
}
不幸的是,我的“ds”方法(它依赖于 SQL::Abstract 进行查询)太笼统了:
sub ds { DataSource->new(); }
我有使用 DataSource.pm(我的通用 DBI 模块)作为父级并指定 ds 的特定模块。例如,如果我想修改表 A,我需要调用:
sub ds { DataSource->TableA->new(); }
我的问题是,在这种情况下,我先验地不知道用户想要修改哪个表,因此我不知道$table
会采用什么值。我想写一些东西$table
作为输入并给出相应的 ds。如果$table
是TableA
,则 ds 将需要成为sub ds { DataSource->TableA->new(); }
,如果$table
是TableB
,则 ds 将需要成为sub ds { DataSource->TableB->new(); }
,依此类推。我想知道如何在不依赖大量条件的情况下编写这个?
不幸的是,我无法修改 ds. 我很想依靠腾来做这个……