Files
observatory-extension/src/pg/sql/42_observatory_exploration.sql
2016-04-20 16:49:41 -04:00

76 lines
2.1 KiB
PL/PgSQL

-- Functions used to search the observatory for measures
--------------------------------------------------------------------------------
-- TODO allow the user to specify the boundary to search for measures
--
CREATE OR REPLACE FUNCTION cdb_observatory.OBS_Search(
search_term text,
relevant_boundary text DEFAULT null
)
RETURNS TABLE(id text, description text, name text, aggregate text, source text) as $$
DECLARE
boundary_term text;
BEGIN
IF relevant_boundary then
boundary_term = '';
else
boundary_term = '';
END IF;
RETURN QUERY
EXECUTE format($string$
SELECT id, description,
name,
aggregate,
replace(split_part(id,'".', 1),'"', '') source
FROM observatory.OBS_column
where name ilike '%%' || %L || '%%'
or description ilike '%%' || %L || '%%'
%s
$string$, search_term, search_term,boundary_term);
RETURN;
END
$$ LANGUAGE plpgsql;
-- Functions to return the geometry levels that a point is part of
--------------------------------------------------------------------------------
-- TODO add test response
CREATE OR REPLACE FUNCTION OBS_GetAvailableBoundaries(
geom geometry,
timespan text DEFAULT null)
RETURNS TABLE(boundary_id text, description text, time_span text, tablename text) as $$
DECLARE
timespan_query TEXT DEFAULT '';
BEGIN
IF time_span != null THEN
timespan_query = format('AND timespan = %L', time_span);
END IF;
RETURN QUERY
EXECUTE
$string$
select
column_id,
obs_column.description,
timespan,
tablename
FROM
observatory.OBS_table,
observatory.OBS_column_table,
observatory.OBS_column
WHERE
observatory.OBS_column_table.column_id = observatory.obs_column.id
observatory.OBS_column_table.table_id = observatory.obs_table.id
AND
observatory.OBS_column.type='Geometry'
AND
$1 && bounds::box2d
$string$ || timespan_query
USING geom
RETURN;
END
$$ LANGUAGE plpgsql;