我正在尝试将我们的mapserver上mapfile中的查询功能重新创建为plpgsql存储过程。
以下是查询:
geom from (select g.gid, g.geom, g.basin, a.\"DATE\", a.\"VALUE\" from sarffg_basins_00_regional_3sec as g join \"%prod_table%\" as a on g.basin = a.\"BASIN\" where a.\"DATE\" = '%prod_date%') as subquery using unique gid using srid=4326
在我的存储过程中,我有:
RETURN QUERY
EXECUTE 'SELECT geom FROM (
SELECT g.gid,
g.geom,
g.basin,
a.date,
a.value
FROM sarffg_basins_00_regional_3sec AS g
JOIN '||tablename_ts||' AS a
ON g.basin = a.basin
WHERE a.date = '''||adj_timestamp||''')
AS subquery USING UNIQUE gid USING srid=4326';
在我的mapfile中找到的上述查询工作正常。当我尝试在psql中调用我的存储过程时,我得到:
ERROR: syntax error at or near "USING"
LINE 11: AS subquery USING UNIQUE gid USING srid=4326
^
QUERY: SELECT geom FROM (
SELECT g.gid,
g.geom,
g.basin,
a.date,
a.value
FROM sarffg_basins_00_regional_3sec AS g
JOIN temp_table_ts AS a
ON g.basin = a.basin
WHERE a.date = '2017-01-15 00:00:00+00')
AS subquery USING UNIQUE gid USING srid=4326
CONTEXT: PL/pgSQL function ingest_ffgs_prod_composite_csv(text,bigint,boolean,boolean) line 239 at RETURN QUERY
我也尝试在我的函数中省略“using”子句,而是在调用存储过程后将该部分保留在mapfile中,即:
DATA "select * from ingest_ffgs_prod_composite_csv('%prod_table%', 1484438400) as subquery using unique gid using srid=4326"
使用包含以下内容的存储过程
RETURN QUERY
EXECUTE 'SELECT geom FROM (
SELECT g.gid,
g.geom,
g.basin,
a.date,
a.value
FROM sarffg_basins_00_regional_3sec AS g
JOIN '||tablename_ts||' AS a
ON g.basin = a.basin
WHERE a.date = '''||adj_timestamp||''');
但这让我在mapserver错误日志中出现错误:
[Wed Jan 25 02:28:17 2017].593733 msDrawMap(): Image handling error. Failed to draw layer named 'regional_basin_values'.
[Wed Jan 25 02:28:17 2017].659656 msPostGISLayerWhichShapes(): Query error. Error executing query: ERROR: syntax error at or near "select"
LINE 1: ..._BASIN_TIMESERIES', 1484438400) as subquery where select * &...
^
[Wed Jan 25 02:28:17 2017].659862 msDrawMap(): Image handling error. Failed to draw layer named 'regional_basin_product'.
[Wed Jan 25 02:28:22 2017].836950 msPostGISLayerWhichShapes(): Query error. Error executing query: ERROR: syntax error at or near "select"
LINE 1: ..._BASIN_TIMESERIES', 1484438400) as subquery where select * &...
最后,我尝试将查询的前部留在mapfile中,只将子查询转换为存储过程:
映射文件:
DATA "geom from (select * from ingest_ffgs_prod_composite_csv('%prod_table%', 1484438400)) as subquery using unique gid using srid=4326"
存储过程:
RETURN QUERY
EXECUTE 'SELECT g.gid,
g.geom,
g.basin,
a.date,
a.value
FROM sarffg_basins_00_regional_3sec AS g
JOIN '||tablename_ts||' AS a
ON g.basin = a.basin
WHERE a.date = '''||adj_timestamp||''');
这让我失望:
[Wed Jan 25 02:35:36 2017].527302 msDrawMap(): Image handling error. Failed to draw layer named 'regional_basin_values'.
[Wed Jan 25 02:35:36 2017].617289 msPostGISLayerWhichShapes(): Query error. Error executing query: ERROR: column "VALUE" does not exist
LINE 1: select "VALUE",encode(ST_AsBinary(ST_Force2D("geom"),'NDR'),...
^
[Wed Jan 25 02:35:36 2017].617511 msDrawMap(): Image handling error. Failed to draw layer named 'regional_basin_product'.
[Wed Jan 25 02:35:42 2017].103566 msPostGISLayerWhichShapes(): Query error. Error executing query: ERROR: column "VALUE" does not exist
LINE 1: select "VALUE",encode(ST_AsBinary(ST_Force2D("geom"),'NDR'),...
这里执行的return语句是:
RETURN QUERY
EXECUTE 'SELECT g.'||quote_ident('gid')||',
g.'||quote_ident('geom')||',
g.'||quote_ident('basin')||',
a.'||quote_ident('DATE')||',
a.'||quote_ident('VALUE')||'
FROM sarffg_basins_00_regional_3sec AS g JOIN '||quote_ident(prod_table)||' AS a
ON g.'||quote_ident('basin')||' = a.'||quote_ident('BASIN')||'
WHERE a.'||quote_ident('DATE')||' = '''||adj_timestamp||'''';
我已经确认prod_table有一个名为“VALUE”的列,所以我不确定为什么会看到这个错误。同样重要的是要注意从psql中调用我的过程不会产生错误。
(我有两个非常相似的return语句,因为我的代码查询一个带有大写列名称的表,如果没有该表,它会从一个没有大写名称的CSV中创建一个。)
也不确定它是否相关,但这是我的函数返回的内容:
RETURNS table (
gid integer,
geom geometry(MultiPolygon,4326),
basin double precision,
date timestamptz,
value double precision
)
非常感谢任何帮助
答案 0 :(得分:0)
我想,你在过滤器中使用字段VALUE或在mapfile中使用类似的东西(很难说没有mapfile)。 此过滤器必须具有大写列名称,这就是原始查询也大写列名称的原因:
select g.gid, g.geom, g.basin, a.\"DATE\", a.\"VALUE\" from....
如果是这样,您只需要将过程返回的列大写:
RETURNS table (
gid integer,
geom geometry(MultiPolygon,4326),
basin double precision,
"DATE" timestamptz,
"VALUE" double precision
)
请记住,在PostgreSql中,如果您使用双引号括起来,则列和表名称的情况很重要。 这个查询:
SELECT VALUE from ...
与案例无关,而这一个:
SELECT "VALUE" from ...
确实需要一个包含大写列名的表。具有大写列名称的表需要双引号:
CREATE TABLE test ("VALUE" text, .....