diff --git a/src/pg/sql/42_observatory_exploration.sql b/src/pg/sql/42_observatory_exploration.sql new file mode 100644 index 0000000..fd6961f --- /dev/null +++ b/src/pg/sql/42_observatory_exploration.sql @@ -0,0 +1,33 @@ +-- 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(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 description, + name, + aggregate, + replace(split_part(id,'".', 1),'"', '') source + FROM observatory.OBS_column + where name ilike '%%%s%%' + or description ilike '%%%s%%' + %s + $string$, search_term, search_term,boundary_term); + RETURN; +END +$$ LANGUAGE plpgsql; diff --git a/src/pg/test/expected/42_observatroy_exploration_test.out b/src/pg/test/expected/42_observatroy_exploration_test.out new file mode 100644 index 0000000..b95c1f4 --- /dev/null +++ b/src/pg/test/expected/42_observatroy_exploration_test.out @@ -0,0 +1,29 @@ +\i test/sql/load_fixtures.sql +SET client_min_messages TO WARNING; +\set ECHO none +Loading obs_table.sql fixture file... +Done. +Loading obs_column.sql fixture file... +Done. +Loading obs_column_table.sql fixture file... +Done. +Loading obs_column_to_column.sql fixture file... +Done. +Loading obs_85328201013baa14e8e8a4a57a01e6f6fbc5f9b1.sql fixture file... +Done. +Loading obs_3e7cc9cfd403b912c57b42d5f9195af9ce2f3cdb.sql fixture file... +Done. +Loading obs_ab038198aaab3f3cb055758638ee4de28ad70146.sql fixture file... +Done. +Loading obs_a92e1111ad3177676471d66bb8036e6d057f271b.sql fixture file... +Done. +Loading obs_11ee8b82c877c073438bc935a91d3dfccef875d1.sql fixture file... +Done. +Loading obs_d34555209878e8c4b37cf0b2b3d072ff129ec470.sql fixture file... +Done. +obs_search_stu +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +("The total number of all people living in a geographic area.","Total Population",sum,es.ine) +("The total number of all people living in a given geographic area. This is a very useful catch-all denominator when calculating rates.","Total Population",sum,us.census.acs) +("The total number of all people living in a given geographic area. This is a very useful catch-all denominator when calculating rates.","Quantile:Total Population",quantile,us.census.acs) +(3 rows) diff --git a/src/pg/test/sql/42_observatory_exploration_test.sql b/src/pg/test/sql/42_observatory_exploration_test.sql new file mode 100644 index 0000000..22c811e --- /dev/null +++ b/src/pg/test/sql/42_observatory_exploration_test.sql @@ -0,0 +1,3 @@ +\i test/sql/load_fixtures.sql + +SELECT cdb_observatory.OBS_Search('total_pop');