From ddc67f3a4148b2fb2f0c64df3277e24c7c0cf04b Mon Sep 17 00:00:00 2001 From: Derry Hamilton Date: Fri, 12 Jul 2019 13:51:30 +0100 Subject: [PATCH] Bugfix type quoting and add select generator --- config_util/ujetl_insert_generator.sql | 33 +++++++------ config_util/ujetl_select_generator.sql | 65 ++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 17 deletions(-) create mode 100644 config_util/ujetl_select_generator.sql diff --git a/config_util/ujetl_insert_generator.sql b/config_util/ujetl_insert_generator.sql index 20cb2eb..a7375f6 100644 --- a/config_util/ujetl_insert_generator.sql +++ b/config_util/ujetl_insert_generator.sql @@ -14,22 +14,22 @@ declare pks text; begin SELECT - array_to_string(array_agg(quote_ident(pg_attribute.attname::text) ),', ') into pks - FROM - pg_index, - pg_class, - pg_attribute, - pg_namespace - WHERE - pg_class.relname = tabname AND - indrelid = pg_class.oid AND - nspname = sch AND - pg_class.relnamespace = pg_namespace.oid AND - pg_attribute.attrelid = pg_class.oid AND - pg_attribute.attnum = any(pg_index.indkey) - AND indisprimary ; + array_to_string(array_agg(quote_ident(pg_attribute.attname::text) ),', ') into pks + FROM + pg_index, + pg_class, + pg_attribute, + pg_namespace + WHERE + pg_class.relname = tabname + AND indrelid = pg_class.oid + AND nspname = sch + AND pg_class.relnamespace = pg_namespace.oid + AND pg_attribute.attrelid = pg_class.oid + AND pg_attribute.attnum = any(pg_index.indkey) + AND indisprimary ; - header := 'INSERT INTO '||quote_ident(sch)||'.'||quote_ident(tabname)||E' as t (\n '; + header := E'INSERT INTO '||quote_ident(sch)||'.'||quote_ident(tabname)||E' as t (\n '; for colinfo in select * @@ -40,7 +40,6 @@ begin and table_name = tabname order by ordinal_position loop - raise info 'Working on %.% (%)',sch,tabname,colinfo::text; if not is_first then col_list := col_list || E',\n '; vals := vals || E',\n '; @@ -48,7 +47,7 @@ begin changes := changes || E'\n OR '; end if; col_list := col_list || quote_ident(colinfo.column_name); - vals := vals || '?::' || quote_ident(colinfo.data_type); + vals := vals || '?::' || colinfo.data_type; sets := sets || quote_ident(colinfo.column_name) || E' = EXCLUDED.' || quote_ident(colinfo.column_name); changes := changes || E't.' || quote_ident(colinfo.column_name) || diff --git a/config_util/ujetl_select_generator.sql b/config_util/ujetl_select_generator.sql new file mode 100644 index 0000000..5c475af --- /dev/null +++ b/config_util/ujetl_select_generator.sql @@ -0,0 +1,65 @@ +CREATE OR REPLACE FUNCTION pg_temp.ujetl_select(sch text, tabname text) + RETURNS text + LANGUAGE plpgsql +AS $function$ +declare + s text := ''; + header text := ''; + col_list text := ''; + vals text := ''; + sets text := ''; + changes text := ''; + is_first boolean := true; + colinfo record; + pks text; +begin + SELECT + array_to_string(array_agg(quote_ident(pg_attribute.attname::text) ),', ') into pks + FROM + pg_index, + pg_class, + pg_attribute, + pg_namespace + WHERE + pg_class.relname = tabname + AND indrelid = pg_class.oid + AND nspname = sch + AND pg_class.relnamespace = pg_namespace.oid + AND pg_attribute.attrelid = pg_class.oid + AND pg_attribute.attnum = any(pg_index.indkey) + AND indisprimary ; + + header := E'SELECT\n '; + for colinfo in + select + * + from + information_schema.columns + where + table_schema = sch + and table_name = tabname + order by ordinal_position + loop + if not is_first then + col_list := col_list || E',\n '; + end if; + col_list := col_list || quote_ident(colinfo.column_name); + + is_first = false; + end loop; + + s := header || + coalesce(col_list,'col_list failed') || + E'\nFROM\n ' || + quote_ident(sch)||'.'||quote_ident(tabname)||E' as t \n '|| + E'WHERE\n insert criteria here >= ?::datatype'; + return s; +end; +$function$ +; + + + + + +