-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsicob_dblink_connect.sql
More file actions
28 lines (26 loc) · 866 Bytes
/
Copy pathsicob_dblink_connect.sql
File metadata and controls
28 lines (26 loc) · 866 Bytes
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
SET CLIENT_ENCODING TO 'utf8';
CREATE OR REPLACE FUNCTION public.sicob_dblink_connect(conn text)
RETURNS character varying
LANGUAGE plpgsql
AS $function$
DECLARE
db text;
username text;
connectstring text;
sql text;
BEGIN
SELECT current_database(),CURRENT_USER INTO db,username;
connectstring := QUOTE_LITERAL('password=Abt2018* dbname=' || db || ' user=' || username);
IF COALESCE(conn,'') = '' THEN
SELECT array_to_string(ARRAY(SELECT chr((97 + round(random() * 25)) :: integer)
FROM generate_series(1,15)), '') into conn;
END IF;
sql := 'SELECT dblink_connect(' || QUOTE_LITERAL(conn) || ',' || connectstring || ');';
RAISE DEBUG 'Running %', sql;
EXECUTE sql;
RETURN conn;
EXCEPTION WHEN others THEN
RAISE EXCEPTION 'sicob_dblink_connect: %, %', SQLERRM, SQLSTATE;
END;
$function$