From 6fd8a43e44e67b48699476340c3191a04a9864dc Mon Sep 17 00:00:00 2001 From: John Krauss Date: Thu, 21 Apr 2016 14:17:31 -0400 Subject: [PATCH] adding standardizemeasurename --- src/pg/sql/40_observatory_utility.sql | 20 +++++++++++++++++++ .../expected/40_observatory_utility_test.out | 5 +++++ .../test/sql/40_observatory_utility_test.sql | 3 +++ 3 files changed, 28 insertions(+) diff --git a/src/pg/sql/40_observatory_utility.sql b/src/pg/sql/40_observatory_utility.sql index 6eb22c1..69ea27c 100644 --- a/src/pg/sql/40_observatory_utility.sql +++ b/src/pg/sql/40_observatory_utility.sql @@ -173,3 +173,23 @@ BEGIN return result; END; $$ LANGUAGE plpgsql; + +-- Function that replaces all non digits or letters with _ trims and lowercases the +-- passed measure name + +CREATE OR REPLACE FUNCTION cdb_observatory._OBS_StandardizeMeasureName(measure_name text) +RETURNS text +AS $$ +DECLARE + result text; +BEGIN + -- Turn non letter or digits to _ + result = regexp_replace(measure_name, '[^\dA-Za-z]+','_', 'g'); + -- Remove duplicate _'s + result = regexp_replace(result,'_{2,}','_', 'g'); + -- Trim _'s from beginning and end + result = trim(both '_' from result); + result = lower(result); + RETURN result; +END; +$$ LANGUAGE plpgsql; diff --git a/src/pg/test/expected/40_observatory_utility_test.out b/src/pg/test/expected/40_observatory_utility_test.out index 5fb3a2f..309f35e 100644 --- a/src/pg/test/expected/40_observatory_utility_test.out +++ b/src/pg/test/expected/40_observatory_utility_test.out @@ -140,6 +140,11 @@ SELECT cdb_observatory._OBS_GetRelatedColumn( {"\"es.ine\".total_pop",NULL,"\"us.census.acs\".B01001001"} (1 row) + _obs_standardizemeasurename +----------------------------- + test_343_2_qqq +(1 row) + \i test/sql/drop_fixtures.sql SET client_min_messages TO NOTICE; \set ECHO none diff --git a/src/pg/test/sql/40_observatory_utility_test.sql b/src/pg/test/sql/40_observatory_utility_test.sql index 37f0648..9fc7d63 100644 --- a/src/pg/test/sql/40_observatory_utility_test.sql +++ b/src/pg/test/sql/40_observatory_utility_test.sql @@ -84,4 +84,7 @@ SELECT cdb_observatory._OBS_GetRelatedColumn( 'denominator' ); +-- should give back a standardized measure name +SELECT cdb_observatory._OBS_StandardizeMeasureName('test 343 %% 2 qqq }}{{}}'); + \i test/sql/drop_fixtures.sql