support use of dynamic tables (API-generated) in obs_getdata

This commit is contained in:
John Krauss 2017-01-10 21:44:49 +00:00
parent 267af19911
commit bc4f1b5909

View File

@ -418,14 +418,21 @@ BEGIN
END || ' || ''}'')::JSON', ', ') END || ' || ''}'')::JSON', ', ')
AS colspecs, AS colspecs,
(SELECT String_Agg(tablename, ', ') FROM (SELECT JSONB_Object_Keys(JSONB_Object( (SELECT String_Agg(DISTINCT CASE
Array_Cat(Array_Agg('observatory.' || numer_tablename) FILTER (WHERE numer_tablename IS NOT NULL), -- External API
Array_Cat(Array_Agg('observatory.' || geom_tablename), WHEN tablename LIKE 'cdb_observatory.%' THEN
Array_Agg('observatory.' || denom_tablename) FILTER (WHERE denom_tablename IS NOT NULL))), 'LATERAL (SELECT * FROM ' || tablename ||
Array_Cat(Array_Agg(numer_tablename) FILTER (WHERE numer_tablename IS NOT NULL), '(_geoms.geom)) ' || REPLACE(tablename, 'cdb_observatory.', '')
Array_Cat(Array_Agg(geom_tablename), -- Internal obs_ table
Array_Agg(denom_tablename) FILTER (WHERE denom_tablename IS NOT NULL))) ELSE 'observatory.' || tablename
)) tablename) bar) tablenames, END, ', ') FROM (
SELECT DISTINCT UNNEST(tablenames_ary) tablename FROM (
SELECT ARRAY_AGG(numer_tablename) ||
ARRAY_AGG(denom_tablename) ||
ARRAY_AGG(geom_tablename)
tablenames_ary
) tablenames_inner
) tablenames_outer) tablenames,
String_Agg(numer_tablename || '.' || numer_geomref_colname || ' = ' || String_Agg(numer_tablename || '.' || numer_geomref_colname || ' = ' ||
geom_tablename || '.' || geom_geomref_colname || geom_tablename || '.' || geom_geomref_colname ||
@ -506,7 +513,14 @@ BEGIN
(unnest($1))->>'normalization' normalization (unnest($1))->>'normalization' normalization
) )
SELECT String_Agg( SELECT String_Agg(
'(''{' ||CASE WHEN LOWER(numer_type) LIKE 'numeric' THEN '(''{' || CASE
-- api-delivered values
WHEN numer_tablename LIKE 'cdb_observatory.%' THEN
'"value": "'' || ' ||
'(cdb_observatory.FIRST( ' ||
REPLACE(numer_tablename, 'cdb_observatory.', '') || '.' || numer_colname || '))::TEXT || ''"'''
-- numeric internal values
WHEN LOWER(numer_type) LIKE 'numeric' THEN
'"value": '' || ' || CASE '"value": '' || ' || CASE
-- denominated -- denominated
WHEN LOWER(normalization) LIKE 'denom%' OR (normalization IS NULL AND denom_id IS NOT NULL) WHEN LOWER(normalization) LIKE 'denom%' OR (normalization IS NULL AND denom_id IS NOT NULL)
@ -593,18 +607,26 @@ BEGIN
END || ' || ''}'')::JSON', ', ') END || ' || ''}'')::JSON', ', ')
AS colspecs, AS colspecs,
STRING_AGG(geom_tablename || '.' || geom_geomref_colname, ', ') AS geomrefs, STRING_AGG(REPLACE(geom_tablename, 'cdb_observatory.', '') ||
'.' || geom_geomref_colname, ', ') AS geomrefs,
(SELECT String_Agg(tablename, ', ') FROM (SELECT JSONB_Object_Keys(JSONB_Object( (SELECT String_Agg(DISTINCT CASE
Array_Cat(Array_Agg('observatory.' || numer_tablename) FILTER (WHERE numer_tablename IS NOT NULL), -- External API
Array_Cat(Array_Agg('observatory.' || geom_tablename), WHEN tablename LIKE 'cdb_observatory.%' THEN
Array_Agg('observatory.' || denom_tablename) FILTER (WHERE denom_tablename IS NOT NULL))), 'LATERAL (SELECT * FROM ' || tablename ||
Array_Cat(Array_Agg(numer_tablename) FILTER (WHERE numer_tablename IS NOT NULL), '(_geoms.geom)) ' || REPLACE(tablename, 'cdb_observatory.', '')
Array_Cat(Array_Agg(geom_tablename), -- Internal obs_ table
Array_Agg(denom_tablename) FILTER (WHERE denom_tablename IS NOT NULL))) ELSE 'observatory.' || tablename
)) tablename) bar) tablenames, END, ', ') FROM (
SELECT DISTINCT UNNEST(tablenames_ary) tablename FROM (
SELECT ARRAY_AGG(numer_tablename) ||
ARRAY_AGG(denom_tablename) ||
ARRAY_AGG(geom_tablename)
tablenames_ary
) tablenames_inner
) tablenames_outer) tablenames,
String_Agg(numer_tablename || '.' || numer_geomref_colname || ' = ' || String_Agg(DISTINCT numer_tablename || '.' || numer_geomref_colname || ' = ' ||
geom_tablename || '.' || geom_geomref_colname || geom_tablename || '.' || geom_geomref_colname ||
Coalesce(' AND ' || numer_tablename || '.' || numer_geomref_colname || ' = ' || Coalesce(' AND ' || numer_tablename || '.' || numer_geomref_colname || ' = ' ||
denom_tablename || '.' || denom_geomref_colname, ''), denom_tablename || '.' || denom_geomref_colname, ''),
@ -623,12 +645,13 @@ BEGIN
WITH _geoms AS (SELECT WITH _geoms AS (SELECT
(UNNEST($1)).val as id, (UNNEST($1)).val as id,
(UNNEST($1)).geom AS geom) (UNNEST($1)).geom AS geom)
SELECT _geoms.id::INT, Array_to_JSON(ARRAY[%s]) SELECT _geoms.id::INT, Array_to_JSON(ARRAY[%s]::JSON[])
FROM %s, _geoms FROM _geoms, %s
WHERE %s %s %s
GROUP BY _geoms.id %s GROUP BY _geoms.id %s
ORDER BY _geoms.id ORDER BY _geoms.id
$query$, colspecs, tables, NULLIF(obs_wheres, '') || ' AND ', user_wheres, $query$, colspecs, tables,
'WHERE ' || NULLIF(ARRAY_TO_STRING(ARRAY[obs_wheres, user_wheres], ' AND '), ''),
CASE WHEN merge IS False THEN ', ' || geomrefs ELSE '' END) CASE WHEN merge IS False THEN ', ' || geomrefs ELSE '' END)
USING geomvals; USING geomvals;
RETURN; RETURN;