mirror of
https://github.com/rasilon/ujetl.git
synced 2026-04-11 10:29:29 +00:00
Bugfix type quoting and add select generator
This commit is contained in:
parent
633fbe7391
commit
ddc67f3a41
2 changed files with 81 additions and 17 deletions
|
|
@ -14,22 +14,22 @@ declare
|
||||||
pks text;
|
pks text;
|
||||||
begin
|
begin
|
||||||
SELECT
|
SELECT
|
||||||
array_to_string(array_agg(quote_ident(pg_attribute.attname::text) ),', ') into pks
|
array_to_string(array_agg(quote_ident(pg_attribute.attname::text) ),', ') into pks
|
||||||
FROM
|
FROM
|
||||||
pg_index,
|
pg_index,
|
||||||
pg_class,
|
pg_class,
|
||||||
pg_attribute,
|
pg_attribute,
|
||||||
pg_namespace
|
pg_namespace
|
||||||
WHERE
|
WHERE
|
||||||
pg_class.relname = tabname AND
|
pg_class.relname = tabname
|
||||||
indrelid = pg_class.oid AND
|
AND indrelid = pg_class.oid
|
||||||
nspname = sch AND
|
AND nspname = sch
|
||||||
pg_class.relnamespace = pg_namespace.oid AND
|
AND pg_class.relnamespace = pg_namespace.oid
|
||||||
pg_attribute.attrelid = pg_class.oid AND
|
AND pg_attribute.attrelid = pg_class.oid
|
||||||
pg_attribute.attnum = any(pg_index.indkey)
|
AND pg_attribute.attnum = any(pg_index.indkey)
|
||||||
AND indisprimary ;
|
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
|
for colinfo in
|
||||||
select
|
select
|
||||||
*
|
*
|
||||||
|
|
@ -40,7 +40,6 @@ begin
|
||||||
and table_name = tabname
|
and table_name = tabname
|
||||||
order by ordinal_position
|
order by ordinal_position
|
||||||
loop
|
loop
|
||||||
raise info 'Working on %.% (%)',sch,tabname,colinfo::text;
|
|
||||||
if not is_first then
|
if not is_first then
|
||||||
col_list := col_list || E',\n ';
|
col_list := col_list || E',\n ';
|
||||||
vals := vals || E',\n ';
|
vals := vals || E',\n ';
|
||||||
|
|
@ -48,7 +47,7 @@ begin
|
||||||
changes := changes || E'\n OR ';
|
changes := changes || E'\n OR ';
|
||||||
end if;
|
end if;
|
||||||
col_list := col_list || quote_ident(colinfo.column_name);
|
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) ||
|
sets := sets || quote_ident(colinfo.column_name) ||
|
||||||
E' = EXCLUDED.' || quote_ident(colinfo.column_name);
|
E' = EXCLUDED.' || quote_ident(colinfo.column_name);
|
||||||
changes := changes || E't.' || quote_ident(colinfo.column_name) ||
|
changes := changes || E't.' || quote_ident(colinfo.column_name) ||
|
||||||
|
|
|
||||||
65
config_util/ujetl_select_generator.sql
Normal file
65
config_util/ujetl_select_generator.sql
Normal file
|
|
@ -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$
|
||||||
|
;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue