[db] add triggers and change value inserts
This commit is contained in:
parent
6aa3cc37ae
commit
46d80b2c43
2 changed files with 47 additions and 18 deletions
|
|
@ -10,7 +10,7 @@ CREATE TABLE IF NOT EXISTS variable (
|
|||
type CHAR(1) NOT NULL,
|
||||
address INT NOT NULL,
|
||||
unit VARCHAR(5) NULL,
|
||||
last_update DATE NULL,
|
||||
last_update INT NULL,
|
||||
|
||||
|
||||
FOREIGN KEY (type) REFERENCES var_type(slabel)
|
||||
|
|
@ -21,7 +21,7 @@ CREATE TABLE IF NOT EXISTS variable (
|
|||
CREATE TABLE IF NOT EXISTS var_value (
|
||||
type CHAR(1) NOT NULL,
|
||||
address INT NOT NULL,
|
||||
time DATE DEFAULT CURRENT_TIMESTAMP,
|
||||
time INT DEFAULT CURRENT_TIMESTAMP,
|
||||
value INT NOT NULL,
|
||||
|
||||
FOREIGN KEY (type) REFERENCES variable(type)
|
||||
|
|
@ -32,8 +32,37 @@ CREATE TABLE IF NOT EXISTS var_value (
|
|||
);
|
||||
|
||||
INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
|
||||
'A', 'Analog', 'float', 1, 500);
|
||||
'A', 'Analog', 'float', 1, (1 + 500));
|
||||
INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
|
||||
'I', 'Integer', 'int', 5002, 6252);
|
||||
'I', 'Integer', 'int', 5002, (5002 + 500));
|
||||
INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
|
||||
'D', 'Digital', 'boolean', 1, 1000);
|
||||
'D', 'Digital', 'boolean', 1, (1 + 1000));
|
||||
|
||||
CREATE TRIGGER variable_insert BEFORE INSERT ON var_value
|
||||
FOR EACH ROW
|
||||
WHEN
|
||||
NOT EXISTS (
|
||||
SELECT MAX(time) FROM var_value
|
||||
WHERE
|
||||
NEW.type = type
|
||||
AND
|
||||
NEW.address = address
|
||||
AND
|
||||
NEW.value = value
|
||||
)
|
||||
BEGIN
|
||||
INSERT INTO var_value
|
||||
(type, address, value)
|
||||
VALUES (NEW.type, NEW.address, NEW.value);
|
||||
END;
|
||||
|
||||
CREATE TRIGGER variable_last_update AFTER INSERT ON var_value
|
||||
FOR EACH ROW
|
||||
BEGIN
|
||||
UPDATE variable
|
||||
SET last_update = NEW.time
|
||||
WHERE
|
||||
type = NEW.type
|
||||
AND
|
||||
address = NEW.address;
|
||||
END;
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
INSERT INTO var_value (type, address, value) VALUES
|
||||
('A', 10, 42),
|
||||
('A', 11, 24),
|
||||
('A', 10, 20),
|
||||
('A', 10, 0),
|
||||
('I', 5010, 42),
|
||||
('I', 5011, 24),
|
||||
('I', 5010, 20),
|
||||
('I', 5010, 0),
|
||||
('D', 10, 1),
|
||||
('D', 11, 1),
|
||||
('D', 10, 1),
|
||||
('D', 10, 0);
|
||||
INSERT INTO var_value (type, address, value, time) VALUES
|
||||
('A', 10, 42, strftime('%s', 'now')),
|
||||
('A', 11, 24, strftime('%s', datetime('now', '-1 hours'))),
|
||||
('A', 10, 20, strftime('%s', datetime('now', '-1 days'))),
|
||||
('A', 10, 0, strftime('%s', datetime('now', '-2 days'))),
|
||||
('I', 5010, 42, strftime('%s', datetime('now'))),
|
||||
('I', 5011, 24, strftime('%s', datetime('now'))),
|
||||
('I', 5010, 20, strftime('%s', datetime('now', '-1 hours'))),
|
||||
('I', 5010, 0, strftime('%s', datetime('now', '-2 days'))),
|
||||
('D', 10, 1, strftime('%s', datetime('now'))),
|
||||
('D', 11, 1, strftime('%s', datetime('now'))),
|
||||
('D', 10, 1, strftime('%s', datetime('now', '-1 days'))),
|
||||
('D', 10, 0, strftime('%s', datetime('now', '-2 days')));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue