В качестве одного из моих первых кодов plpgsql я написал приведенный ниже код, который, на мой взгляд, часто повторяется. Как я могу избавиться от дублирования?
CREATE TABLE devices (
device SERIAL PRIMARY KEY NOT NULL,
fingerprint VARCHAR UNIQUE NOT NULL
);
CREATE TABLE guests (
guest SERIAL PRIMARY KEY NOT NULL,
uuid CHAR(36) NOT NULL
);
CREATE TABLE vms (
vm SERIAL PRIMARY KEY NOT NULL,
guest SERIAL REFERENCES guests(guest),
device SERIAL REFERENCES devices(device),
name VARCHAR NOT NULL
);
CREATE OR REPLACE FUNCTION got_device_guest(fp TEXT, vmuuid CHAR(36), vmname TEXT) RETURNS VOID AS $BODY$
DECLARE
deviceid INT;
guestid INT;
BEGIN
IF NOT EXISTS(SELECT * FROM devices WHERE devices.fingerprint = fp) THEN
INSERT INTO devices(fingerprint) VALUES (fp);
END IF;
SELECT device INTO deviceid FROM devices WHERE devices.fingerprint = fp;
IF NOT (EXISTS(SELECT * FROM guests WHERE guests.uuid = vmuuid)) THEN
INSERT INTO guests(uuid) VALUES (vmuuid);
END IF;
SELECT guest INTO guestid FROM guests WHERE guests.uuid = vmuuid;
IF NOT (EXISTS(SELECT * FROM vms WHERE vms.guest = guestid AND vms.device=deviceid)) THEN
INSERT INTO vms(guest, device, name) VALUES (guestid, deviceid, vmname);
END IF;
END;
$BODY$ LANGUAGE plpgsql;