-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemantic_layer.sql
150 lines (136 loc) · 5.79 KB
/
semantic_layer.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
-- semantic_layer.sql
-- Enhanced Semantic Layer for Memory Graph
-- Generated on 2024-11-29
-- Enable Foreign Key Constraints
PRAGMA foreign_keys = ON;
-- ===========================
-- Relation Types Table
-- ===========================
CREATE TABLE IF NOT EXISTS relation_types (
id INTEGER PRIMARY KEY AUTOINCREMENT,
relation_name TEXT NOT NULL UNIQUE,
semantic_category TEXT NOT NULL,
description TEXT,
inverse_relation INTEGER REFERENCES relation_types(id),
transitive BOOLEAN DEFAULT FALSE,
symmetric BOOLEAN DEFAULT FALSE,
directional BOOLEAN DEFAULT TRUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Insert Known Relation Types
INSERT OR IGNORE INTO relation_types (relation_name, semantic_category, description, transitive, symmetric, directional) VALUES
('implements', 'inheritance', 'Indicates interface implementation', TRUE, FALSE, TRUE),
('extends', 'inheritance', 'Indicates class inheritance', TRUE, FALSE, TRUE),
('uses', 'dependency', 'Indicates usage dependency', FALSE, FALSE, TRUE),
('provides', 'composition', 'Indicates provided functionality', FALSE, FALSE, TRUE),
('configures', 'configuration', 'Indicates configuration relationship', FALSE, FALSE, TRUE),
('complementsWith', 'integration', 'Indicates complementary functionality', TRUE, TRUE, FALSE);
-- ===========================
-- Entity Type Hierarchy Table
-- ===========================
CREATE TABLE IF NOT EXISTS entity_type_hierarchy (
id INTEGER PRIMARY KEY AUTOINCREMENT,
parent_type TEXT NOT NULL,
child_type TEXT NOT NULL,
hierarchy_level INTEGER NOT NULL,
UNIQUE(parent_type, child_type),
CHECK (hierarchy_level > 0)
);
-- Insert Entity Type Hierarchy
INSERT OR IGNORE INTO entity_type_hierarchy (parent_type, child_type, hierarchy_level) VALUES
('BaseClass', 'Class', 1),
('Interface', 'CoreConcept', 1),
('Implementation', 'CorePattern', 2),
('Pattern', 'CorePattern', 1);
-- ===========================
-- Valid Type Relations Table
-- ===========================
CREATE TABLE IF NOT EXISTS valid_type_relations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
from_type TEXT NOT NULL,
relation_type INTEGER NOT NULL, -- References relation_types(id)
to_type TEXT NOT NULL,
UNIQUE(from_type, relation_type, to_type),
FOREIGN KEY (relation_type) REFERENCES relation_types(id)
);
-- Insert Valid Type Relations
INSERT OR IGNORE INTO valid_type_relations (from_type, relation_type, to_type) VALUES
('Implementation', (SELECT id FROM relation_types WHERE relation_name = 'implements'), 'Interface'),
('Class', (SELECT id FROM relation_types WHERE relation_name = 'extends'), 'BaseClass'),
('Implementation', (SELECT id FROM relation_types WHERE relation_name = 'uses'), 'Protocol'),
('Class', (SELECT id FROM relation_types WHERE relation_name = 'provides'), 'Feature'),
('Implementation', (SELECT id FROM relation_types WHERE relation_name = 'configures'), 'Configuration');
-- ===========================
-- Entity Attributes Table
-- ===========================
CREATE TABLE IF NOT EXISTS entity_attributes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
entity_id INTEGER NOT NULL,
attribute_key TEXT NOT NULL,
attribute_value TEXT,
FOREIGN KEY (entity_id) REFERENCES entities(id),
UNIQUE(entity_id, attribute_key)
);
-- ===========================
-- Audit Tables for Change Tracking
-- ===========================
CREATE TABLE IF NOT EXISTS entity_audit (
id INTEGER PRIMARY KEY AUTOINCREMENT,
entity_id INTEGER NOT NULL,
action TEXT NOT NULL, -- 'INSERT', 'UPDATE', 'DELETE'
changed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
changed_by TEXT,
FOREIGN KEY (entity_id) REFERENCES entities(id)
);
CREATE TABLE IF NOT EXISTS relations_audit (
id INTEGER PRIMARY KEY AUTOINCREMENT,
relation_id INTEGER NOT NULL,
action TEXT NOT NULL, -- 'INSERT', 'UPDATE', 'DELETE'
changed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
changed_by TEXT,
FOREIGN KEY (relation_id) REFERENCES relations(id)
);
-- ===========================
-- Triggers for Automatic Audit Logging
-- ===========================
-- Trigger for Entities Table
CREATE TRIGGER IF NOT EXISTS trg_entities_audit
AFTER INSERT OR UPDATE OR DELETE ON entities
FOR EACH ROW
BEGIN
INSERT INTO entity_audit (entity_id, action, changed_by)
VALUES (
COALESCE(NEW.id, OLD.id),
CASE
WHEN (NEW.id IS NOT NULL AND OLD.id IS NULL) THEN 'INSERT'
WHEN (NEW.id IS NOT NULL AND OLD.id IS NOT NULL) THEN 'UPDATE'
WHEN (NEW.id IS NULL AND OLD.id IS NOT NULL) THEN 'DELETE'
END,
'system' -- Replace with dynamic user identifier if available
);
END;
-- Trigger for Relations Table
CREATE TRIGGER IF NOT EXISTS trg_relations_audit
AFTER INSERT OR UPDATE OR DELETE ON relations
FOR EACH ROW
BEGIN
INSERT INTO relations_audit (relation_id, action, changed_by)
VALUES (
COALESCE(NEW.id, OLD.id),
CASE
WHEN (NEW.id IS NOT NULL AND OLD.id IS NULL) THEN 'INSERT'
WHEN (NEW.id IS NOT NULL AND OLD.id IS NOT NULL) THEN 'UPDATE'
WHEN (NEW.id IS NULL AND OLD.id IS NOT NULL) THEN 'DELETE'
END,
'system' -- Replace with dynamic user identifier if available
);
END;
-- ===========================
-- Indexes for Semantic Tables
-- ===========================
CREATE INDEX IF NOT EXISTS idx_relation_types_category ON relation_types(semantic_category);
CREATE INDEX IF NOT EXISTS idx_entity_type_hierarchy_parent ON entity_type_hierarchy(parent_type);
CREATE INDEX IF NOT EXISTS idx_entity_type_hierarchy_child ON entity_type_hierarchy(child_type);
CREATE INDEX IF NOT EXISTS idx_valid_type_relations_from_type ON valid_type_relations(from_type);
CREATE INDEX IF NOT EXISTS idx_valid_type_relations_to_type ON valid_type_relations(to_type);