繁体   English   中英

有什么方法可以使用Perl的DBIx :: Class中的SQL函数来过滤列?

[英]Is there any way to filter a column using SQL functions in Perl's DBIx::Class?

我正在使用带有PostGIS几何列的PostgreSQL数据库。

我想配置Result类,以便使用ST_AsEWKT函数对几何列进行膨胀,并使用ST_GeomFromEWKT函数对几何列进行缩小。

有没有一种方法可以使“查找”方法正常工作,并且使“更新”和“创建”方法也正常工作。 如果可以避免的话,我宁愿不必为每个表编写专门的查询。

我可以使用hack来扩大列,例如

__PACKAGE__->inflate_column( 'geo', {
  inflate => sub {
    my ($raw_value, $result) = @_;
    my $col = $result->result_source->resultset->get_column("geo")->func("ST_AsEWKT");
  },
});

但我不确定如何实施通货紧缩。

提前致谢。

我有一个使用DBIx :: Class :: InflateColumn的有效解决方案。 这是不理想的,因为它会针对每个几何列向数据库进行单独的查询。 (理想情况下,如果甚至可以在不更改DBIC的情况下完成操作,则应该告诉DBIC仅使用适当的函数来查询该字段。)

答案如下。

__PACKAGE__->load_components("InflateColumn");

__PACKAGE__->inflate_column( 'geo', {

  inflate => sub {
    my ($value, $result) = @_;

    my $dbh = $result->result_source->storage->dbh;
    my $sth = $dbh->prepare( q{SELECT ST_AsEWKT(?)} );
    $sth->execute( $value );
    my @row = $sth->fetchrow;

    return $row[0];
  },

  deflate => sub {
    my ($value, $result) = @_;

    my $dbh = $result->result_source->storage->dbh;
    my $sth = $dbh->prepare( q{SELECT ST_GeomFromEWKT(?)} );
    $sth->execute( $value );
    my @row = $sth->fetchrow;

    return $row[0];
  },

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM