簡體   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