0

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

我想配置 Result 类,以便几何列使用 ST_AsEWKT 函数膨胀并使用 ST_GeomFromEWKT 函数放气。

Is there a way to do this so that the "find" method works as normal, and so that the "update" and "create" methods also work as normal. I'd rather not have to write specialized queries for each table, if I can avoid it.

I can use a hack for inflating the column, e.g.

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

but I am unsure how to implement deflation.

Thanks in advance.

4

1 回答 1

0

我有一个使用 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];
  },

});
于 2013-01-03T22:42:44.900 回答