Selaa lähdekoodia

[db] add triggers and change value inserts

Maxime Alves LIRMM@home 4 vuotta sitten
vanhempi
commit
46d80b2c43
2 muutettua tiedostoa jossa 47 lisäystä ja 18 poistoa
  1. 34
    5
      db/pyheatpump.sql
  2. 13
    13
      db/test_variable_values.sql

+ 34
- 5
db/pyheatpump.sql Näytä tiedosto

@@ -10,7 +10,7 @@ CREATE TABLE IF NOT EXISTS variable (
10 10
 	type CHAR(1) NOT NULL,
11 11
 	address INT NOT NULL,
12 12
 	unit VARCHAR(5) NULL,
13
-	last_update DATE NULL,
13
+	last_update INT NULL,
14 14
 
15 15
 
16 16
 	FOREIGN KEY (type) REFERENCES var_type(slabel)
@@ -21,7 +21,7 @@ CREATE TABLE IF NOT EXISTS variable (
21 21
 CREATE TABLE IF NOT EXISTS var_value (
22 22
 	type CHAR(1) NOT NULL,
23 23
 	address INT NOT NULL,
24
-	time DATE DEFAULT CURRENT_TIMESTAMP,
24
+	time INT DEFAULT CURRENT_TIMESTAMP,
25 25
 	value INT NOT NULL,
26 26
 
27 27
 	FOREIGN KEY (type) REFERENCES variable(type)
@@ -32,8 +32,37 @@ CREATE TABLE IF NOT EXISTS var_value (
32 32
 );
33 33
 
34 34
 INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
35
-	'A', 'Analog', 'float', 1, 500);
35
+	'A', 'Analog', 'float', 1, (1 + 500));
36 36
 INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
37
-	'I', 'Integer', 'int', 5002, 6252);
37
+	'I', 'Integer', 'int', 5002, (5002 + 500));
38 38
 INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
39
-	'D', 'Digital', 'boolean', 1, 1000);
39
+	'D', 'Digital', 'boolean', 1, (1 + 1000));
40
+
41
+CREATE TRIGGER variable_insert BEFORE INSERT ON var_value
42
+FOR EACH ROW
43
+WHEN
44
+	NOT EXISTS (
45
+		SELECT MAX(time) FROM var_value
46
+		WHERE
47
+			NEW.type = type
48
+			AND
49
+			NEW.address = address
50
+			AND
51
+			NEW.value = value
52
+	)
53
+BEGIN
54
+	INSERT INTO var_value
55
+	(type, address, value)
56
+	VALUES (NEW.type, NEW.address, NEW.value);
57
+END;
58
+
59
+CREATE TRIGGER variable_last_update AFTER INSERT ON var_value
60
+FOR EACH ROW
61
+BEGIN
62
+	UPDATE variable
63
+	SET last_update = NEW.time
64
+	WHERE
65
+		type = NEW.type
66
+		AND
67
+		address = NEW.address;
68
+END;

+ 13
- 13
db/test_variable_values.sql Näytä tiedosto

@@ -1,13 +1,13 @@
1
-INSERT INTO var_value (type, address, value) VALUES
2
-('A', 10, 42),
3
-('A', 11, 24),
4
-('A', 10, 20),
5
-('A', 10, 0),
6
-('I', 5010, 42),
7
-('I', 5011, 24),
8
-('I', 5010, 20),
9
-('I', 5010, 0),
10
-('D', 10, 1),
11
-('D', 11, 1),
12
-('D', 10, 1),
13
-('D', 10, 0);
1
+INSERT INTO var_value (type, address, value, time) VALUES
2
+('A', 10, 42, strftime('%s', 'now')),
3
+('A', 11, 24, strftime('%s', datetime('now', '-1 hours'))),
4
+('A', 10, 20, strftime('%s', datetime('now', '-1 days'))),
5
+('A', 10, 0, strftime('%s', datetime('now', '-2 days'))),
6
+('I', 5010, 42, strftime('%s',  datetime('now'))),
7
+('I', 5011, 24, strftime('%s',  datetime('now'))),
8
+('I', 5010, 20, strftime('%s',  datetime('now', '-1 hours'))),
9
+('I', 5010, 0, strftime('%s',  datetime('now', '-2 days'))),
10
+('D', 10, 1, strftime('%s',  datetime('now'))),
11
+('D', 11, 1, strftime('%s',  datetime('now'))),
12
+('D', 10, 1, strftime('%s',  datetime('now', '-1 days'))),
13
+('D', 10, 0, strftime('%s',  datetime('now', '-2 days')));

Loading…
Peruuta
Tallenna