CREATE OR REPLACE FUNCTION food600.importsyncset(
	mserver character varying,
	mport character varying,
	mdatabaza character varying,
	mmeno character varying,
	mheslo character varying)
    RETURNS character varying
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
AS $BODY$
DECLARE 
ret	CHARACTER VARYING DEFAULT '' ;
pokracuj SMALLINT DEFAULT 1;
BEGIN
--	RAISE NOTICE 'mdatabaza:%',mdatabaza;
--	RAISE NOTICE 'mserver:%',mserver;
--	RAISE NOTICE 'mport:%',mport;
--	RAISE NOTICE 'mmeno:%',mmeno;
--	RAISE NOTICE 'mheslo:%',mheslo;
	BEGIN
		PERFORM dblink_connect('connstrx','hostaddr='||btrim(mserver)||' port='||btrim(mport)||' dbname='||btrim(mdatabaza)||' user='||btrim(mmeno)||' password='||btrim(mheslo));
	EXCEPTION
		WHEN duplicate_object THEN --code error 42710
			ret:='this connections exists';
		WHEN others	THEN
			ret:='nepripojene:'||sqlstate;
			pokracuj:=0;
	END;
	IF pokracuj>0 THEN
		TRUNCATE food600.sync_set RESTART IDENTITY;
		INSERT INTO food600.sync_set (idriadok, ip, port, databaza, meno, heslo, id_master, hotel) SELECT idriadok, ip, port, databaza, meno, heslo, id_master, hotel FROM dblink('connstrx','SELECT idriadok, ip, port, databaza, meno, heslo, id_master, hotel FROM food600.sync_set ORDER BY idriadok') as remzmeny(idriadok bigint, ip character varying, port character varying, databaza character varying, meno character varying, heslo character varying, id_master bigint, hotel character varying) ;
		ret:='ok';
		PERFORM dblink_disconnect('connstrx');
	END IF;
--	RAISE NOTICE 'ret2:%',ret;
RETURN ret;
END
$BODY$;
