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)

This commit is contained in:
Paul Trowbridge 2020-10-22 12:24:10 -04:00
parent ae1a32baa7
commit 82f7a8fff2
2 changed files with 10 additions and 10 deletions

View File

@ -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);

View File

@ -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) <> ''