forked from mathieubossaert/central2pg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_table_from_refcursor.sql
More file actions
77 lines (70 loc) · 2.83 KB
/
create_table_from_refcursor.sql
File metadata and controls
77 lines (70 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
FUNCTION: create_table_from_refcursor(text, refcursor)
description :
-> inspired by https://stackoverflow.com/questions/50837548/insert-into-fetch-all-from-cant-be-compiled/52889381#52889381
Create a table corresponding to the curso structure (attribute types and names)
parameters :
_table_name text -- the name of the table to create
_ref refcursor -- the name of the refcursor to get data from
returning :
void
*/
CREATE OR REPLACE FUNCTION create_table_from_refcursor(
_schema_name text,
_table_name text,
_ref refcursor)
RETURNS void
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$
DECLARE
_sql text;
_sql_index text;
_sql_val text = '';
_sql_existing_cols text = '';
_sql_new_cols text = '';
_row record;
BEGIN
RAISE INFO 'entering create_table_from_refcursor() for table %',_table_name;
FETCH FIRST FROM _ref INTO _row;
SELECT _sql_val || '
(' ||
STRING_AGG(concat('"',val.key :: text,'" text'), ',') ||
')'
INTO _sql_val
FROM JSON_EACH(TO_JSON(_row)) val;
_sql = '
CREATE TABLE IF NOT EXISTS ' || _schema_name ||'.'|| _table_name || '
' || _sql_val;
-- RAISE INFO 'SQL script for table cration %',_sql;
EXECUTE (_sql);
_sql_index = 'CREATE UNIQUE INDEX IF NOT EXISTS idx_'||replace(_table_name,'.','_')||' ON '||_schema_name||'.'||_table_name||' USING btree ("data_id")
TABLESPACE pg_default;';
EXECUTE (_sql_index);
/* ading new columns */
SELECT _sql_new_cols ||
STRING_AGG(concat('ALTER TABLE ' , _schema_name ,'.', _table_name , ' ADD COLUMN "',val.key :: text,'" text'), ';') ||';'
INTO _sql_new_cols
FROM JSON_EACH(TO_JSON(_row)) val
WHERE val.key NOT IN ( SELECT attname
FROM pg_class JOIN pg_attribute ON pg_attribute.attrelid=pg_class.oid
JOIN pg_namespace ON relnamespace = pg_namespace.oid
WHERE nspname = _schema_name
AND relkind = 'r' AND pg_class.relname = _table_name AND attnum > 0 AND attname = val.key
);
-- Create new attributes or Run a dummy query if nothing new
-- RAISE INFO 'SQL script for new cols %',_sql_new_cols;
EXECUTE (COALESCE(_sql_new_cols,'SELECT true;'));
RAISE INFO 'exiting from create_table_from_refcursor() for table %',_table_name;
RAISE INFO 'create_table_from_refcursor(): SQL statement is: %', COALESCE(_sql_new_cols,'no new column to add');
END;
$BODY$;
COMMENT ON function create_table_from_refcursor(text,text,refcursor) IS 'description :
-> inspired by https://stackoverflow.com/questions/50837548/insert-into-fetch-all-from-cant-be-compiled/52889381#52889381
Create a table corresponding to the curso structure (attribute types and names)
parameters :
_table_name text -- the name of the table to create
_ref refcursor -- the name of the refcursor to get data from
returning :
void';