Commit 693e7d6f authored by Hugo Hörnquist's avatar Hugo Hörnquist

Moved SQL items to where they should be in the file

parent 6d965f9f
......@@ -110,6 +110,8 @@ CREATE TABLE stock_diff (
*
* The expected_amount field should be nullable if null stock entries
* are to exist.
*
* NOTE This table is deprecated. Use diff_help instead
*/
CREATE TABLE stock_diff_temp (
id INTEGER PRIMARY KEY NOT NULL,
......@@ -121,6 +123,14 @@ CREATE TABLE stock_diff_temp (
FOREIGN KEY (product_id) REFERENCES products(id)
);
CREATE TABLE diff_help (
id INTEGER PRIMARY KEY NOT NULL,
product_id INTEGER NOT NULL,
amount INTEGER,
FOREIGN KEY (product_id) REFERENCES products(id)
);
CREATE TABLE drainage (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
start_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
......@@ -212,6 +222,21 @@ WHERE datetime(log.time, 'localtime') BETWEEN
AND datetime('now', 'localtime', '-12 hours', 'start of day', '+36 hours')
ORDER BY log.time;
CREATE VIEW diff_view AS
SELECT d.id AS id,
p.id AS product_id,
p.sale_status AS sale_status,
-- visible border
p.name AS name,
s.active AS expected,
d.amount AS actual,
s.active - d.amount as diff
FROM products p
INNER JOIN stock s ON p.id = s.product_id
INNER JOIN diff_help d ON p.id = d.product_id;
-- WHERE p.sale_status = 0 OR s.active != 0;
-- where p.sale_status in (0, 1);
-- ============================== Triggers ===============================
-- some of these would benefit from being FOR EACH STATEMENT instead of
......@@ -319,6 +344,41 @@ BEGIN
END;
*/
/*
* delete from the view to transfer,
* delete from the underlying table (diff_help) if
* a transfer is not desired.
*/
CREATE TRIGGER after_diff_help_delete_last
AFTER DELETE ON diff_help
FOR EACH ROW
WHEN (SELECT count (1) = 0 FROM diff_help)
BEGIN
INSERT INTO diff_help (product_id)
SELECT id FROM products p;
END;
CREATE TRIGGER diff_view_transfer
INSTEAD OF DELETE ON diff_view
BEGIN
UPDATE stock SET active = OLD.actual
WHERE OLD.actual IS NOT NULL
AND product_id = OLD.product_id;
INSERT INTO stock_diff (product_id, expected, actual)
SELECT OLD.product_id, OLD.expected, OLD.actual
WHERE OLD.actual IS NOT NULL;
DELETE FROM diff_help WHERE id = OLD.id;
END;
CREATE TRIGGER update_view_trigger
INSTEAD OF UPDATE ON diff_view
BEGIN
UPDATE diff_help SET amount = NEW.actual WHERE id = NEW.id;
END;
CREATE TRIGGER before_money_transfer
BEFORE INSERT ON money_transfers
FOR EACH ROW
......@@ -363,73 +423,9 @@ VALUES ("stock_diff_temp_transfer", 1),
("big_buy_transfer", 1),
("acquisitions_temp_transfer", 1);
-- stock_diff_temp gets it's data when the last record is deleted,
-- diff_help gets it's data when the last record is deleted,
-- so insert *any* data and delete it.
INSERT INTO stock_diff_temp (product_id, expected_amount, diff)
SELECT id, 0, 0 FROM products LIMIT 1;
DELETE FROM stock_diff_temp;
------------------ New Stuff -------------------------------
CREATE TABLE diff_help (
id INTEGER PRIMARY KEY NOT NULL,
product_id INTEGER NOT NULL,
amount INTEGER,
FOREIGN KEY (product_id) REFERENCES products(id)
);
CREATE TRIGGER after_diff_help_delete_last
AFTER DELETE ON diff_help
FOR EACH ROW
WHEN (SELECT count (1) = 0 FROM diff_help)
BEGIN
INSERT INTO diff_help (product_id)
SELECT id FROM products p;
END;
-- create initial data
INSERT INTO diff_help (product_id)
SELECT id FROM products LIMIT 1;
DELETE FROM diff_help;
-- create view
CREATE VIEW diff_view AS
SELECT d.id AS id,
p.id AS product_id,
p.sale_status AS sale_status,
-- visible border
p.name AS name,
s.active AS expected,
d.amount AS actual,
s.active - d.amount as diff
FROM products p
INNER JOIN stock s ON p.id = s.product_id
INNER JOIN diff_help d ON p.id = d.product_id;
-- WHERE p.sale_status = 0 OR s.active != 0;
-- where p.sale_status in (0, 1);
/*
* delete from the view to transfer,
* delete from the underlying table (diff_help) if
* a transfer is not desired.
*/
CREATE TRIGGER diff_view_transfer
INSTEAD OF DELETE ON diff_view
BEGIN
UPDATE stock SET active = OLD.actual
WHERE OLD.actual IS NOT NULL
AND product_id = OLD.product_id;
INSERT INTO stock_diff (product_id, expected, actual)
SELECT OLD.product_id, OLD.expected, OLD.actual
WHERE OLD.actual IS NOT NULL;
DELETE FROM diff_help WHERE id = OLD.id;
END;
CREATE TRIGGER update_view_trigger
INSTEAD OF UPDATE ON diff_view
BEGIN
UPDATE diff_help SET amount = NEW.actual WHERE id = NEW.id;
END;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment