In schema.sql renamed table _temp to __DO_NOT_MANIPULATE_trigger_memory
This way, you have been warned: Once you fiddle with this internal database logic, you are on your own.
This commit is contained in:
parent
3773c3f123
commit
6d31efee39
48
schema.sql
48
schema.sql
@ -50,7 +50,7 @@ CREATE TABLE Transfer (
|
|||||||
);
|
);
|
||||||
|
|
||||||
-- For internal purposes: Memory of rebalance triggers
|
-- For internal purposes: Memory of rebalance triggers
|
||||||
CREATE TABLE _temp (d, c, m);
|
CREATE TABLE __DO_NOT_MANIPULATE__trigger_memory (d, c, m);
|
||||||
|
|
||||||
-- Only for use of HTTP interface
|
-- Only for use of HTTP interface
|
||||||
CREATE TABLE web_auth ( user_id primary key, password, grade not null, username, email );
|
CREATE TABLE web_auth ( user_id primary key, password, grade not null, username, email );
|
||||||
@ -73,7 +73,7 @@ BEGIN
|
|||||||
HAVING count(d.billId) == 0
|
HAVING count(d.billId) == 0
|
||||||
;
|
;
|
||||||
|
|
||||||
INSERT INTO _temp
|
INSERT INTO __DO_NOT_MANIPULATE__trigger_memory
|
||||||
SELECT remainingDebt, remainingCredit, min(remainingDebt,remainingCredit)
|
SELECT remainingDebt, remainingCredit, min(remainingDebt,remainingCredit)
|
||||||
FROM (SELECT
|
FROM (SELECT
|
||||||
(SELECT value - paid FROM Debit WHERE billId=NEW.billId) AS remainingDebt,
|
(SELECT value - paid FROM Debit WHERE billId=NEW.billId) AS remainingDebt,
|
||||||
@ -83,33 +83,33 @@ BEGIN
|
|||||||
|
|
||||||
UPDATE Debit
|
UPDATE Debit
|
||||||
SET paid = paid + CASE
|
SET paid = paid + CASE
|
||||||
WHEN (SELECT d FROM _temp) <= 0
|
WHEN (SELECT d FROM __DO_NOT_MANIPULATE__trigger_memory) <= 0
|
||||||
THEN RAISE(FAIL, "Debt settled")
|
THEN RAISE(FAIL, "Debt settled")
|
||||||
ELSE
|
ELSE
|
||||||
(SELECT m FROM _temp)
|
(SELECT m FROM __DO_NOT_MANIPULATE__trigger_memory)
|
||||||
END
|
END
|
||||||
WHERE billId=NEW.billId;
|
WHERE billId=NEW.billId;
|
||||||
|
|
||||||
UPDATE Credit
|
UPDATE Credit
|
||||||
SET spent = spent + CASE
|
SET spent = spent + CASE
|
||||||
WHEN (SELECT c FROM _temp) <= 0
|
WHEN (SELECT c FROM __DO_NOT_MANIPULATE__trigger_memory) <= 0
|
||||||
THEN RAISE(FAIL, "Credit spent")
|
THEN RAISE(FAIL, "Credit spent")
|
||||||
ELSE IFNULL(
|
ELSE IFNULL(
|
||||||
(SELECT m FROM _temp),
|
(SELECT m FROM __DO_NOT_MANIPULATE__trigger_memory),
|
||||||
RAISE(FAIL,"Oops, lost _temp record before increasing spent")
|
RAISE(FAIL,"Oops, lost __DO_NOT_MANIPULATE__trigger_memory record before increasing spent")
|
||||||
)
|
)
|
||||||
END
|
END
|
||||||
WHERE credId=NEW.credId;
|
WHERE credId=NEW.credId;
|
||||||
|
|
||||||
UPDATE Transfer
|
UPDATE Transfer
|
||||||
SET amount = (SELECT m FROM _temp)
|
SET amount = (SELECT m FROM __DO_NOT_MANIPULATE__trigger_memory)
|
||||||
WHERE billId=NEW.billId AND credId=NEW.credId
|
WHERE billId=NEW.billId AND credId=NEW.credId
|
||||||
;
|
;
|
||||||
|
|
||||||
UPDATE Credit
|
UPDATE Credit
|
||||||
SET value = value + IFNULL(
|
SET value = value + IFNULL(
|
||||||
(SELECT m FROM _temp),
|
(SELECT m FROM __DO_NOT_MANIPULATE__trigger_memory),
|
||||||
RAISE(FAIL, "Oops, lost _temp record before increasing value")
|
RAISE(FAIL, "Oops, lost __DO_NOT_MANIPULATE__trigger_memory record before increasing value")
|
||||||
)
|
)
|
||||||
WHERE credId = (
|
WHERE credId = (
|
||||||
SELECT targetCredit
|
SELECT targetCredit
|
||||||
@ -117,7 +117,7 @@ BEGIN
|
|||||||
WHERE billId=NEW.billId
|
WHERE billId=NEW.billId
|
||||||
);
|
);
|
||||||
|
|
||||||
DELETE FROM _temp;
|
DELETE FROM __DO_NOT_MANIPULATE__trigger_memory;
|
||||||
|
|
||||||
END;
|
END;
|
||||||
|
|
||||||
@ -125,7 +125,7 @@ CREATE TRIGGER revokeTransfer
|
|||||||
BEFORE DELETE ON Transfer
|
BEFORE DELETE ON Transfer
|
||||||
BEGIN
|
BEGIN
|
||||||
|
|
||||||
INSERT INTO _temp VALUES (null,null,OLD.amount);
|
INSERT INTO __DO_NOT_MANIPULATE__trigger_memory VALUES (null,null,OLD.amount);
|
||||||
|
|
||||||
UPDATE Debit
|
UPDATE Debit
|
||||||
SET paid = paid - OLD.amount
|
SET paid = paid - OLD.amount
|
||||||
@ -144,7 +144,7 @@ BEGIN
|
|||||||
SET spent = spent - OLD.amount
|
SET spent = spent - OLD.amount
|
||||||
WHERE credId = OLD.credId;
|
WHERE credId = OLD.credId;
|
||||||
|
|
||||||
DELETE FROM _temp;
|
DELETE FROM __DO_NOT_MANIPULATE__trigger_memory;
|
||||||
|
|
||||||
END;
|
END;
|
||||||
|
|
||||||
@ -169,7 +169,9 @@ CREATE TRIGGER enforceDebtImmutableOutsideTrigger
|
|||||||
WHEN NOT EXISTS (SELECT * FROM Transfer t WHERE NEW.billId=t.billId AND amount IS NULL)
|
WHEN NOT EXISTS (SELECT * FROM Transfer t WHERE NEW.billId=t.billId AND amount IS NULL)
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT RAISE(FAIL, "paid is set and adjusted automatically according to added Transfer records")
|
SELECT RAISE(FAIL, "paid is set and adjusted automatically according to added Transfer records")
|
||||||
WHERE (NEW.paid + IFNULL((SELECT m FROM _temp WHERE c IS NULL AND d IS NULL),0) ) <> OLD.paid;
|
WHERE (NEW.paid + IFNULL(
|
||||||
|
(SELECT m FROM __DO_NOT_MANIPULATE__trigger_memory WHERE c IS NULL AND d IS NULL), 0
|
||||||
|
) ) <> OLD.paid;
|
||||||
END;
|
END;
|
||||||
|
|
||||||
-- When a transfer is revoked, the targetCredit of the associated bill is reduced. Hence we must
|
-- When a transfer is revoked, the targetCredit of the associated bill is reduced. Hence we must
|
||||||
@ -180,7 +182,7 @@ CREATE TRIGGER rebalanceReducedCredit
|
|||||||
WHEN NEW.value < OLD.spent
|
WHEN NEW.value < OLD.spent
|
||||||
BEGIN
|
BEGIN
|
||||||
|
|
||||||
REPLACE INTO _temp (d, m)
|
REPLACE INTO __DO_NOT_MANIPULATE__trigger_memory (d, m)
|
||||||
SELECT
|
SELECT
|
||||||
'from_' || NEW.credId,
|
'from_' || NEW.credId,
|
||||||
billId
|
billId
|
||||||
@ -195,14 +197,14 @@ BEGIN
|
|||||||
WHERE credId = NEW.credId
|
WHERE credId = NEW.credId
|
||||||
AND billId IN (
|
AND billId IN (
|
||||||
SELECT billId
|
SELECT billId
|
||||||
FROM _temp
|
FROM __DO_NOT_MANIPULATE__trigger_memory
|
||||||
WHERE c = 'from_' || NEW.credId
|
WHERE c = 'from_' || NEW.credId
|
||||||
)
|
)
|
||||||
;
|
;
|
||||||
|
|
||||||
INSERT INTO Transfer (credId, billId)
|
INSERT INTO Transfer (credId, billId)
|
||||||
SELECT NEW.credId, m
|
SELECT NEW.credId, m
|
||||||
FROM _temp
|
FROM __DO_NOT_MANIPULATE__trigger_memory
|
||||||
WHERE d = 'from_' || NEW.credId
|
WHERE d = 'from_' || NEW.credId
|
||||||
AND NEW.value > (
|
AND NEW.value > (
|
||||||
SELECT spent
|
SELECT spent
|
||||||
@ -211,7 +213,7 @@ BEGIN
|
|||||||
)
|
)
|
||||||
;
|
;
|
||||||
|
|
||||||
DELETE FROM _temp WHERE d = 'from_' || NEW.credId;
|
DELETE FROM __DO_NOT_MANIPULATE__trigger_memory WHERE d = 'from_' || NEW.credId;
|
||||||
|
|
||||||
END;
|
END;
|
||||||
|
|
||||||
@ -253,15 +255,17 @@ CREATE TRIGGER enforceSpentImmutableOutsideTrigger
|
|||||||
WHEN NOT EXISTS (SELECT * FROM Transfer t WHERE NEW.credId=t.credId AND amount IS NULL)
|
WHEN NOT EXISTS (SELECT * FROM Transfer t WHERE NEW.credId=t.credId AND amount IS NULL)
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT RAISE(FAIL, "spent is set and adjusted automatically according to added Transfer records")
|
SELECT RAISE(FAIL, "spent is set and adjusted automatically according to added Transfer records")
|
||||||
WHERE (NEW.spent + IFNULL((SELECT m FROM _temp WHERE c IS NULL AND d IS NULL),0) ) <> OLD.spent;
|
WHERE (NEW.spent + IFNULL(
|
||||||
|
(SELECT m FROM __DO_NOT_MANIPULATE__trigger_memory WHERE c IS NULL AND d IS NULL), 0
|
||||||
|
) ) <> OLD.spent;
|
||||||
END;
|
END;
|
||||||
|
|
||||||
CREATE TRIGGER enforceFixedCredit
|
CREATE TRIGGER enforceFixedCredit
|
||||||
BEFORE UPDATE OF account, value ON Credit
|
BEFORE UPDATE OF account, value ON Credit
|
||||||
WHEN NOT EXISTS (SELECT * FROM _temp)
|
WHEN EXISTS (SELECT * FROM Transfer WHERE credId=NEW.credId)
|
||||||
|
AND NOT EXISTS (SELECT * FROM __DO_NOT_MANIPULATE__trigger_memory)
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT RAISE(FAIL, "Credit involved in transactions to revoke at first")
|
SELECT RAISE(FAIL, "Credit involved in transactions to revoke at first");
|
||||||
WHERE EXISTS (SELECT * FROM Transfer WHERE credId=NEW.credId);
|
|
||||||
END;
|
END;
|
||||||
|
|
||||||
CREATE TRIGGER checkIBANatTransfer
|
CREATE TRIGGER checkIBANatTransfer
|
||||||
|
Loading…
Reference in New Issue
Block a user