CREATE TRIGGER zzz_last_request_trigger BEFORE INSERT ON requests FOR EACH ROW
EXECUTE PROCEDURE final_request_handler();
----------------------------------------------------------------------------------
---- Dummy Services ---
----------------------------------------------------------------------------------
+------------------------------------------------------------------------------
+--- Dummy Services ---
+------------------------------------------------------------------------------
INSERT INTO sources VALUES ('nothing');
LANGUAGE SQL EXTERNAL SECURITY DEFINER;
REVOKE ALL ON FUNCTION can_show(varchar, varchar) FROM public;
----------------------------------------------------------------------------------
---- Coke Bank Services ---
----------------------------------------------------------------------------------
+------------------------------------------------------------------------------
+--- Coke Bank Services ---
+------------------------------------------------------------------------------
INSERT INTO sources VALUES ('cokebank');
LANGUAGE SQL EXTERNAL SECURITY DEFINER;
REVOKE ALL ON FUNCTION can_show(varchar, varchar) FROM public;
----------------------------------------------------------------------------------
---- Coke Machine Services ---
----------------------------------------------------------------------------------
+------------------------------------------------------------------------------
+--- Coke Machine Services ---
+------------------------------------------------------------------------------
INSERT INTO sources VALUES ('coke');
INSERT INTO items VALUES ('coke', 'update_coke', NULL, NULL, '<slot number> <new name> <new price> <stock count>', NULL);
+CREATE TABLE coke_requests (
+ request_id integer,
+ request_slot integer,
+ request_handled boolean NOT NULL DEFAULT false,
+ request_time timestamp DEFAULT now()
+);
+
CREATE OR REPLACE FUNCTION update_slot_info(integer, varchar(20), integer, integer) RETURNS void AS
'
BEGIN
' LANGUAGE plpgsql EXTERNAL SECURITY DEFINER;
REVOKE ALL ON FUNCTION update_slot_info(integer, varchar, integer, integer) FROM public;
-CREATE OR REPLACE FUNCTION dispense_slot(integer) RETURNS void AS
+CREATE OR REPLACE FUNCTION dispense_slot(integer, integer) RETURNS void AS
'
elog(NOTICE, q(And out pops a drink!));
+ INSERT INTO coke_requests VALUES ($1, $2);
+ NOTIFY coke_requests;
' LANGUAGE plperlu EXTERNAL SECURITY DEFINER;
-REVOKE ALL ON FUNCTION dispense_slot(integer) FROM public;
-
-CREATE OR REPLACE FUNCTION update_slot_status() RETURNS void AS
- '
- ' LANGUAGE plperlu EXTERNAL SECURITY DEFINER;
-REVOKE ALL ON FUNCTION update_slot_status() FROM public;
+REVOKE ALL ON FUNCTION dispense_slot(integer, integer) FROM public;
CREATE OR REPLACE FUNCTION coke_slot_trigger() RETURNS "trigger" AS
'
IF NEW.handled AND EXISTS(SELECT item_name FROM items WHERE source_name = ''coke'' AND item_name = NEW.item_name) THEN
-- Drop a drink and update stock
SELECT INTO slotnum items.item_data[1] FROM items WHERE item_name = NEW.item_name and source_name = ''coke'';
- PERFORM dispense_slot(slotnum);
- PERFORM update_slot_status();
+ PERFORM dispense_slot(NEW.request_id, slotnum);
END IF;
RETURN NULL;
END;
LANGUAGE SQL EXTERNAL SECURITY DEFINER;
REVOKE ALL ON FUNCTION can_show(varchar, varchar) FROM public;
----------------------------------------------------------------------------------
---- Vending Machine Services ---
----------------------------------------------------------------------------------
+------------------------------------------------------------------------------
+--- Vending Machine Services ---
+------------------------------------------------------------------------------
INSERT INTO sources VALUES ('vend');
INSERT INTO items VALUES ('vend', 'cheese and onion', 125, array['21'], NULL, 0);
INSERT INTO items VALUES ('vend', 'update_vend', NULL, NULL, '<slot number> <new name> <new price> <new count>', NULL);
+CREATE TABLE vend_requests (
+ request_id integer,
+ request_slot varchar(2),
+ request_handled boolean NOT NULL DEFAULT false,
+ request_time timestamp DEFAULT now()
+);
+
CREATE OR REPLACE FUNCTION valid_vend_slot(varchar(2)) RETURNS boolean AS
- '
- BEGIN
- RETURN $1 ~ ''^[0-9]{2}$'';
- END;
- ' LANGUAGE plpgsql EXTERNAL SECURITY DEFINER;
+ 'SELECT $1 ~ ''^[0-9]{2}$''' LANGUAGE SQL EXTERNAL SECURITY DEFINER;
REVOKE ALL ON FUNCTION valid_vend_slot(varchar(2)) FROM public;
CREATE OR REPLACE FUNCTION update_vend_slot_info(varchar(2), varchar(20), integer, integer) RETURNS void AS
' LANGUAGE plpgsql EXTERNAL SECURITY DEFINER;
REVOKE ALL ON FUNCTION update_vend_slot_info(varchar(2), varchar, integer, integer) FROM public;
-CREATE OR REPLACE FUNCTION vend_slot(varchar(2)) RETURNS void AS
+CREATE OR REPLACE FUNCTION vend_slot(integer, varchar(2)) RETURNS void AS
'
elog(NOTICE, q(And out pops a snack!));
+ INSERT INTO vend_requests VALUES ($1, $2);
+ NOTIFY vend_requests;
' LANGUAGE plperlu EXTERNAL SECURITY DEFINER;
-REVOKE ALL ON FUNCTION vend_slot(varchar(2)) FROM public;
+REVOKE ALL ON FUNCTION vend_slot(integer, varchar(2)) FROM public;
CREATE OR REPLACE FUNCTION vend_slot_trigger() RETURNS "trigger" AS
'
-- deducted money et al.
--
-- So if we get this far, we don''t need to care about much.
- -- Dropping the drink & checking stock occurs in the AFTER-INSERT trigger.
+ -- Dropping the snack occurs in the AFTER-INSERT trigger.
-- Mark the request as having been dealt with
NEW.handled = true;
IF NEW.handled AND EXISTS(SELECT item_name FROM items WHERE source_name = ''vend'' AND item_name = NEW.item_name) THEN
-- Drop a snack and update stock
SELECT INTO slotnum items.item_data[1] FROM items WHERE item_name = NEW.item_name and source_name = ''vend'';
- PERFORM vend_slot(slotnum);
+ PERFORM vend_slot(NEW.request_id, slotnum);
END IF;
RETURN NULL;
END;
LANGUAGE SQL EXTERNAL SECURITY DEFINER;
REVOKE ALL ON FUNCTION can_show(varchar, varchar) FROM public;
----------------------------------------------------------------------------------
---- UCC Door Services ---
----------------------------------------------------------------------------------
+------------------------------------------------------------------------------
+--- UCC Door Services ---
+------------------------------------------------------------------------------
CREATE TABLE doorcontrollers (
user_name varchar(16) UNIQUE references users(user_name),
DROP FUNCTION can_show_pre_door(varchar, varchar);
ALTER FUNCTION can_show(varchar(16), varchar(20)) RENAME TO can_show_pre_door;
CREATE FUNCTION can_show(varchar(16), varchar(20)) RETURNS boolean AS
- 'SELECT can_show_pre_door($1, $2) AND NOT ($2 = ''opendoor'' AND NOT in_door($1))'
- LANGUAGE SQL EXTERNAL SECURITY DEFINER;
+ 'SELECT can_show_pre_door($1, $2) AND NOT ($2 = ''opendoor'' AND (NOT in_door($1)))' LANGUAGE SQL EXTERNAL SECURITY DEFINER;
REVOKE ALL ON FUNCTION can_show(varchar, varchar) FROM public;