|
@@ -1,68 +1,68 @@
|
1
|
1
|
CREATE TABLE IF NOT EXISTS var_type (
|
2
|
|
- slabel CHAR(1) UNIQUE PRIMARY KEY,
|
3
|
|
- label VARCHAR(10) UNIQUE,
|
4
|
|
- type VARCHAR(10) NOT NULL,
|
5
|
|
- start_address INT NOT NULL DEFAULT 0,
|
6
|
|
- end_address INT NOT NULL DEFAULT 250
|
|
2
|
+ slabel CHAR(1) UNIQUE PRIMARY KEY,
|
|
3
|
+ label VARCHAR(10) UNIQUE,
|
|
4
|
+ type VARCHAR(10) NOT NULL,
|
|
5
|
+ start_address INT NOT NULL DEFAULT 0,
|
|
6
|
+ end_address INT NOT NULL DEFAULT 250
|
7
|
7
|
);
|
8
|
8
|
|
9
|
9
|
CREATE TABLE IF NOT EXISTS variable (
|
10
|
|
- type CHAR(1) NOT NULL,
|
11
|
|
- address INT NOT NULL,
|
12
|
|
- unit VARCHAR(5) NULL,
|
13
|
|
- last_update INT NULL,
|
|
10
|
+ type CHAR(1) NOT NULL,
|
|
11
|
+ address INT NOT NULL,
|
|
12
|
+ unit VARCHAR(5) NULL,
|
|
13
|
+ last_update INT NULL,
|
14
|
14
|
|
15
|
15
|
|
16
|
|
- FOREIGN KEY (type) REFERENCES var_type(slabel)
|
17
|
|
- ON DELETE CASCADE,
|
18
|
|
- PRIMARY KEY(type, address)
|
|
16
|
+ FOREIGN KEY (type) REFERENCES var_type(slabel)
|
|
17
|
+ ON DELETE CASCADE,
|
|
18
|
+ PRIMARY KEY(type, address)
|
19
|
19
|
);
|
20
|
20
|
|
21
|
21
|
CREATE TABLE IF NOT EXISTS var_value (
|
22
|
|
- type CHAR(1) NOT NULL,
|
23
|
|
- address INT NOT NULL,
|
24
|
|
- time INT DEFAULT CURRENT_TIMESTAMP,
|
25
|
|
- value INT NOT NULL,
|
|
22
|
+ type CHAR(1) NOT NULL,
|
|
23
|
+ address INT NOT NULL,
|
|
24
|
+ time INT DEFAULT CURRENT_TIMESTAMP,
|
|
25
|
+ value INT NOT NULL,
|
26
|
26
|
|
27
|
|
- FOREIGN KEY (type) REFERENCES variable(type)
|
28
|
|
- ON DELETE CASCADE,
|
29
|
|
- FOREIGN KEY (address) REFERENCES variable(address)
|
30
|
|
- ON DELETE CASCADE,
|
31
|
|
- PRIMARY KEY(type, address, time)
|
|
27
|
+ FOREIGN KEY (type) REFERENCES variable(type)
|
|
28
|
+ ON DELETE CASCADE,
|
|
29
|
+ FOREIGN KEY (address) REFERENCES variable(address)
|
|
30
|
+ ON DELETE CASCADE,
|
|
31
|
+ PRIMARY KEY(type, address, time)
|
32
|
32
|
);
|
33
|
33
|
|
34
|
34
|
INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
|
35
|
|
- 'A', 'Analog', 'float', 1, 5000);
|
|
35
|
+ 'A', 'Analog', 'float', 1, 5000);
|
36
|
36
|
INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
|
37
|
|
- 'I', 'Integer', 'int', 5002, 10002);
|
|
37
|
+ 'I', 'Integer', 'int', 5002, 10002);
|
38
|
38
|
INSERT INTO var_type (slabel, label, type, start_address, end_address) VALUES (
|
39
|
|
- 'D', 'Digital', 'bool', 1, 2048);
|
|
39
|
+ 'D', 'Digital', 'bool', 1, 2048);
|
40
|
40
|
|
41
|
41
|
CREATE TRIGGER variable_insert BEFORE INSERT ON var_value
|
42
|
42
|
FOR EACH ROW
|
43
|
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
|
|
- )
|
|
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
|
53
|
BEGIN
|
54
|
|
- INSERT INTO var_value
|
55
|
|
- (type, address, value)
|
56
|
|
- VALUES (NEW.type, NEW.address, NEW.value);
|
|
54
|
+ INSERT INTO var_value
|
|
55
|
+ (type, address, value)
|
|
56
|
+ VALUES (NEW.type, NEW.address, NEW.value);
|
57
|
57
|
END;
|
58
|
58
|
|
59
|
59
|
CREATE TRIGGER variable_last_update AFTER INSERT ON var_value
|
60
|
60
|
FOR EACH ROW
|
61
|
61
|
BEGIN
|
62
|
|
- UPDATE variable
|
63
|
|
- SET last_update = NEW.time
|
64
|
|
- WHERE
|
65
|
|
- type = NEW.type
|
66
|
|
- AND
|
67
|
|
- address = NEW.address;
|
|
62
|
+ UPDATE variable
|
|
63
|
+ SET last_update = NEW.time
|
|
64
|
+ WHERE
|
|
65
|
+ type = NEW.type
|
|
66
|
+ AND
|
|
67
|
+ address = NEW.address;
|
68
|
68
|
END;
|