JDBI绑定值在数据库函数的引号内

时间:2015-09-14 14:20:56

标签: java postgresql postgis jdbi

我正在尝试使用jdbi对PostGIS数据库进行空间查询,我想要的查询是

@SqlQuery(
    "SELECT ST_asText( way ) " +
    "FROM planet_osm_roads " +
    "WHERE ST_DWithin( way, ST_GeomFromText( \'POINT( :lat :lng )\', ST_SRID( way ) ), :radius )" )
public List<Road> getRoadsNearPoint( @Bind("lat") double lat, @Bind("lng") double lng, @Bind("radius") int radius );

尝试这样做

    DBI dbi = new DBI( m_dataSource );
    RoadDAO dao = dbi.onDemand( RoadDAO.class );
    System.out.println( dao.getRoadsNearPoint( 16839484.09, -3997003.76, 10 ) );

此异常中的结果

Exception in thread "main" org.skife.jdbi.v2.exceptions.UnableToExecuteStatementException: org.postgresql.util.PSQLException: ERROR: parse error - invalid geometry
  Hint: "POINT( :l" <-- parse error at position 9 within geometry [statement:"SELECT ST_asText( way ) FROM planet_osm_roads WHERE ST_DWithin( way, ST_GeomFromText( 'POINT( :lat :lng )', ST_SRID( way ) ), :radius )", located:"SELECT ST_asText( way ) FROM planet_osm_roads WHERE ST_DWithin( way, ST_GeomFromText( 'POINT( :lat :lng )', ST_SRID( way ) ), :radius )", rewritten:"SELECT ST_asText( way ) FROM planet_osm_roads WHERE ST_DWithin( way, ST_GeomFromText( 'POINT( :lat :lng )', ST_SRID( way ) ), ? )", arguments:{ positional:{}, named:{lng:-3997003.76,radius:10,lat:1.683948409E7}, finder:[]}]
    at org.skife.jdbi.v2.SQLStatement.internalExecute(SQLStatement.java:1334)
    at org.skife.jdbi.v2.Query.fold(Query.java:173)
    at org.skife.jdbi.v2.Query.list(Query.java:82)
    at org.skife.jdbi.v2.sqlobject.ResultReturnThing$IterableReturningThing.result(ResultReturnThing.java:255)
    at org.skife.jdbi.v2.sqlobject.ResultReturnThing.map(ResultReturnThing.java:48)
    at org.skife.jdbi.v2.sqlobject.QueryHandler.invoke(QueryHandler.java:45)
    at org.skife.jdbi.v2.sqlobject.SqlObject.invoke(SqlObject.java:175)
    at org.skife.jdbi.v2.sqlobject.SqlObject$1.intercept(SqlObject.java:75)
    at org.skife.jdbi.v2.sqlobject.CloseInternalDoNotUseThisClass$$EnhancerByCGLIB$$1c23157f.getRoadsNearPoint(<generated>)
    at com.elevations.Application.main(Application.java:36)
Caused by: org.postgresql.util.PSQLException: ERROR: parse error - invalid geometry
  Hint: "POINT( :l" <-- parse error at position 9 within geometry
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2182)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1911)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:173)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:615)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:465)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:458)
    at org.skife.jdbi.v2.SQLStatement.internalExecute(SQLStatement.java:1328)
    ... 9 more

我怀疑问题是试图绑定到引用内的某些内容,我尝试使用“POINT(val val)”作为我的绑定值并将其绑定到某些:point,但这也不起作用。你是怎么做到的?

2 个答案:

答案 0 :(得分:0)

我认为这是因为字符串中的参数替换。我们可以尝试传递这样的字符串。

public abstract class RoadDao {
  @SqlQuery(
        "SELECT ST_asText( way ) " +
                "FROM planet_osm_roads " +
                "WHERE ST_DWithin( way, ST_GeomFromText( :point, ST_SRID( way ) ), :radius )" )
  public abstract List<Road> getRoadsNearPoint( @Bind("point") String point, @Bind("radius") int radius );

  public List<Road> getRoadsNearPoint( double lat, double lng, int radius ) {
    String point = "POINT(" + lat + lng + ")" ;
    return getRoadsNearPoint(point, radius);
  };
}

答案 1 :(得分:0)

我确定它为时已晚,但我设法使用

DAO:

@SqlQuery("select \"local-authority-code\", \"official-name\", wkb_geometry from uk_local_authorities u\n" +
    "        INNER JOIN ogrgeojson o on o.ctyua14nm LIKE u.\"alt-name-1\"\n" +
    "        where st_contains(wkb_geometry, ST_GeomFromText(:point, 4326))")
GeostoreItem getGeostoreItem(@Bind("point") String point);

来自资源:

GeostoreItem gs = geostoreDao.getGeostoreItem("POINT(" + points[0] + " " + points[1] + ")");