From 82f7a8fff2ce5b759136298418b5d44148432196 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 22 Oct 2020 12:24:10 -0400 Subject: [PATCH] format the column name as an identity (%I) and only build statements where any fkey for a func is not null (dont build a master table for funcs with no primary key identified) --- sql/build_master_tables.sql | 10 +++++----- sql/temp.sql | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/sql/build_master_tables.sql b/sql/build_master_tables.sql index 85b4ace..bcbbbac 100644 --- a/sql/build_master_tables.sql +++ b/sql/build_master_tables.sql @@ -9,16 +9,16 @@ BEGIN FOR f IN SELECT 'DROP TABLE IF EXISTS fc.'||func||'; CREATE TABLE IF NOT EXISTS fc.'||func||' (' || - string_agg(cname || ' ' || dtype,', ' ORDER BY opos ASC) || - ', PRIMARY KEY ('||string_agg(cname,', ') FILTER (WHERE fkey = func)||'));' AS ddl, + string_agg(format('%I',cname) || ' ' || dtype,', ' ORDER BY opos ASC) || + ', PRIMARY KEY ('||string_agg(format('%I',cname),', ') FILTER (WHERE fkey = func)||'));' AS ddl, ---need to add a clause to exclude where the key is null - 'INSERT INTO fc.'||func||' SELECT DISTINCT ' || string_agg(cname,', ' ORDER BY opos ASC) || ' FROM rlarp.osm_dev WHERE '||string_agg(cname,'||') FILTER (WHERE fkey = func)||' IS NOT NULL ON CONFLICT DO NOTHING' AS pop + 'INSERT INTO fc.'||func||' SELECT DISTINCT ' || string_agg(format('%I',cname),', ' ORDER BY opos ASC) || ' FROM rlarp.osm_dev WHERE '||string_agg(format('%I',cname)||' IS NOT NULL ',' AND ') FILTER (WHERE fkey = func)||' ON CONFLICT DO NOTHING' AS pop FROM fc.target_meta - WHERE - func <> 'doc' GROUP BY func + HAVING + string_agg(cname,', ') FILTER (WHERE fkey = func) <> '' loop EXECUTE format('%s',f.ddl); EXECUTE format('%s',f.pop); diff --git a/sql/temp.sql b/sql/temp.sql index 0c6db84..ee6f28a 100644 --- a/sql/temp.sql +++ b/sql/temp.sql @@ -1,12 +1,12 @@ SELECT 'DROP TABLE IF EXISTS fc.'||func||'; CREATE TABLE IF NOT EXISTS fc.'||func||' (' || - string_agg(cname || ' ' || dtype,', ' ORDER BY opos ASC) || - ', PRIMARY KEY ('||string_agg(cname,', ') FILTER (WHERE fkey = func)||'));' AS ddl, + string_agg(format('%I',cname) || ' ' || dtype,', ' ORDER BY opos ASC) || + ', PRIMARY KEY ('||string_agg(format('%I',cname),', ') FILTER (WHERE fkey = func)||'));' AS ddl, ---need to add a clause to exclude where the key is null - 'INSERT INTO fc.'||func||' SELECT DISTINCT ' || string_agg(cname,', ' ORDER BY opos ASC) || ' FROM rlarp.osm_dev WHERE '||string_agg(cname,'||') FILTER (WHERE fkey = func)||' IS NOT NULL ON CONFLICT DO NOTHING' AS populate + 'INSERT INTO fc.'||func||' SELECT DISTINCT ' || string_agg(format('%I',cname),', ' ORDER BY opos ASC) || ' FROM rlarp.osm_dev WHERE '||string_agg(format('%I',cname)||' IS NOT NULL ',' AND ') FILTER (WHERE fkey = func)||' ON CONFLICT DO NOTHING' AS pop FROM fc.target_meta - WHERE - func <> 'doc' GROUP BY func + HAVING + string_agg(cname,', ') FILTER (WHERE fkey = func) <> ''