make search_tables a table returning function

This commit is contained in:
Andy Eschbacher
2016-04-21 15:13:02 -04:00
parent e72583e15c
commit e119e0ddda

View File

@@ -2,29 +2,43 @@
-- return a table that contains a string match based on input
-- TODO: implement search for timespan
CREATE OR REPLACE FUNCTION OBS_SearchTables(
CREATE OR REPLACE FUNCTION _OBS_SearchTables(
search_term text,
time_span text DEFAULT '2009 - 2013'
time_span text DEFAULT NULL
)
RETURNS text[]
RETURNS table(tablename text, timespan text)
As $$
DECLARE
out_var text[];
BEGIN
EXECUTE
'SELECT array_agg(tablename)
FROM observatory.obs_table t
JOIN observatory.obs_column_table ct
ON ct.table_id = t.id
JOIN observatory.obs_column c
ON ct.column_id = c.id
WHERE c.type ILIKE ''geometry''
AND c.id = $1'
INTO out_var
USING search_term;
RETURN out_var;
IF time_span IS NULL
THEN
RETURN QUERY
EXECUTE
'SELECT tablename, timespan
FROM observatory.obs_table t
JOIN observatory.obs_column_table ct
ON ct.table_id = t.id
JOIN observatory.obs_column c
ON ct.column_id = c.id
WHERE c.type ILIKE ''geometry''
AND c.id = $1'
USING search_term;
ELSE
RETURN QUERY
EXECUTE
'SELECT tablename, timespan
FROM observatory.obs_table t
JOIN observatory.obs_column_table ct
ON ct.table_id = t.id
JOIN observatory.obs_column c
ON ct.column_id = c.id
WHERE c.type ILIKE ''geometry''
AND c.id = $1
AND t.timespan = $2'
USING search_term, time_span;
END IF;
END;
$$ LANGUAGE plpgsql;
$$ LANGUAGE plpgsql IMMUTABLE;