Skip to content

Commit 1491463

Browse files
committed
Added basic tree-view schema browser with support for tables, views and triggers. Fixed major bug in editor parsing that proper execution of CREATE TRIGGER blocks. Updated example schemas with triggers and audit tables. New stress test script in examples directory.
1 parent 4b3bc51 commit 1491463

12 files changed

Lines changed: 844 additions & 134 deletions

File tree

examples/inventory/schema.sql

Lines changed: 78 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,85 @@
1-
-- Home Inventory Schema
2-
-- SQLite/CE Example
1+
-- Home Inventory Schema
2+
-- SQLite/CE Example
3+
4+
CREATE TABLE rooms (
5+
id INTEGER PRIMARY KEY,
6+
name TEXT NOT NULL,
7+
floor INTEGER
8+
);
9+
10+
CREATE TABLE categories (
11+
id INTEGER PRIMARY KEY,
12+
name TEXT NOT NULL
13+
);
14+
15+
CREATE TABLE items (
16+
id INTEGER PRIMARY KEY,
17+
name TEXT NOT NULL,
18+
category_id INTEGER,
19+
room_id INTEGER,
20+
brand TEXT,
21+
model TEXT,
22+
serial_number TEXT,
23+
purchase_date TEXT,
24+
purchase_price REAL,
25+
current_value REAL,
26+
warranty_expires TEXT,
27+
notes TEXT
28+
);
29+
30+
CREATE TABLE photos (
31+
id INTEGER PRIMARY KEY,
32+
item_id INTEGER NOT NULL,
33+
filename TEXT NOT NULL,
34+
description TEXT
35+
);
36+
37+
CREATE INDEX idx_items_room ON items(room_id);
38+
CREATE INDEX idx_items_category ON items(category_id);
39+
CREATE INDEX idx_photos_item ON photos(item_id);
40+
41+
-- Views
42+
CREATE VIEW v_items AS
43+
SELECT i.id, i.name, c.name AS category, r.name AS room,
44+
i.brand, i.model, i.purchase_price, i.current_value
45+
FROM items i
46+
LEFT JOIN categories c ON i.category_id = c.id
47+
LEFT JOIN rooms r ON i.room_id = r.id;
48+
49+
CREATE VIEW v_room_totals AS
50+
SELECT r.name AS room, COUNT(i.id) AS item_count,
51+
SUM(i.current_value) AS total_value
52+
FROM rooms r
53+
LEFT JOIN items i ON r.id = i.room_id
54+
GROUP BY r.id;
55+
56+
CREATE VIEW v_warranty_expiring AS
57+
SELECT name, brand, model, warranty_expires
58+
FROM items
59+
WHERE warranty_expires IS NOT NULL
60+
AND warranty_expires <= date('now', '+6 months');
361

4-
CREATE TABLE rooms (
5-
id INTEGER PRIMARY KEY,
6-
name TEXT NOT NULL,
7-
floor INTEGER
8-
);
9-
10-
CREATE TABLE categories (
11-
id INTEGER PRIMARY KEY,
12-
name TEXT NOT NULL
13-
);
1462

15-
CREATE TABLE items (
63+
-- Audit log for tracking changes
64+
CREATE TABLE audit_log (
1665
id INTEGER PRIMARY KEY,
17-
name TEXT NOT NULL,
18-
category_id INTEGER,
19-
room_id INTEGER,
20-
brand TEXT,
21-
model TEXT,
22-
serial_number TEXT,
23-
purchase_date TEXT,
24-
purchase_price REAL,
25-
current_value REAL,
26-
warranty_expires TEXT,
27-
notes TEXT
66+
table_name TEXT,
67+
action TEXT,
68+
item_id INTEGER
2869
);
2970

30-
CREATE TABLE photos (
31-
id INTEGER PRIMARY KEY,
32-
item_id INTEGER NOT NULL,
33-
filename TEXT NOT NULL,
34-
description TEXT
35-
);
36-
37-
CREATE INDEX idx_items_room ON items(room_id);
38-
CREATE INDEX idx_items_category ON items(category_id);
39-
CREATE INDEX idx_photos_item ON photos(item_id);
40-
41-
-- Views
42-
CREATE VIEW v_items AS
43-
SELECT i.id, i.name, c.name AS category, r.name AS room,
44-
i.brand, i.model, i.purchase_price, i.current_value
45-
FROM items i
46-
LEFT JOIN categories c ON i.category_id = c.id
47-
LEFT JOIN rooms r ON i.room_id = r.id;
71+
-- Triggers
72+
CREATE TRIGGER items_insert AFTER INSERT ON items FOR EACH ROW
73+
BEGIN
74+
INSERT INTO audit_log (table_name, action, item_id) VALUES ('items', 'INSERT', NEW.id);
75+
END;
4876

49-
CREATE VIEW v_room_totals AS
50-
SELECT r.name AS room, COUNT(i.id) AS item_count,
51-
SUM(i.current_value) AS total_value
52-
FROM rooms r
53-
LEFT JOIN items i ON r.id = i.room_id
54-
GROUP BY r.id;
77+
CREATE TRIGGER items_update AFTER UPDATE ON items FOR EACH ROW
78+
BEGIN
79+
INSERT INTO audit_log (table_name, action, item_id) VALUES ('items', 'UPDATE', NEW.id);
80+
END;
5581

56-
CREATE VIEW v_warranty_expiring AS
57-
SELECT name, brand, model, warranty_expires
58-
FROM items
59-
WHERE warranty_expires IS NOT NULL
60-
AND warranty_expires <= date('now', '+6 months');
82+
CREATE TRIGGER items_delete AFTER DELETE ON items FOR EACH ROW
83+
BEGIN
84+
INSERT INTO audit_log (table_name, action, item_id) VALUES ('items', 'DELETE', OLD.id);
85+
END;

examples/library/schema.sql

Lines changed: 66 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,73 @@
1-
-- Home Library Catalog Schema
2-
-- SQLite/CE Example
1+
-- Home Library Catalog Schema
2+
-- SQLite/CE Example
3+
4+
CREATE TABLE authors (
5+
id INTEGER PRIMARY KEY,
6+
name TEXT NOT NULL,
7+
birth_year INTEGER
8+
);
9+
10+
CREATE TABLE books (
11+
id INTEGER PRIMARY KEY,
12+
title TEXT NOT NULL,
13+
author_id INTEGER,
14+
isbn TEXT,
15+
year_published INTEGER,
16+
genre TEXT,
17+
format TEXT,
18+
location TEXT,
19+
rating INTEGER,
20+
date_added TEXT,
21+
notes TEXT
22+
);
23+
24+
CREATE TABLE loans (
25+
id INTEGER PRIMARY KEY,
26+
book_id INTEGER NOT NULL,
27+
borrower TEXT NOT NULL,
28+
date_out TEXT NOT NULL,
29+
date_due TEXT,
30+
date_returned TEXT
31+
);
32+
33+
CREATE INDEX idx_books_author ON books(author_id);
34+
CREATE INDEX idx_books_genre ON books(genre);
35+
CREATE INDEX idx_loans_book ON loans(book_id);
36+
37+
-- Views
38+
CREATE VIEW v_books AS
39+
SELECT b.id, b.title, a.name AS author, b.year_published,
40+
b.genre, b.format, b.location, b.rating
41+
FROM books b
42+
LEFT JOIN authors a ON b.author_id = a.id;
43+
44+
CREATE VIEW v_loans_active AS
45+
SELECT l.id, b.title, l.borrower, l.date_out, l.date_due
46+
FROM loans l
47+
JOIN books b ON l.book_id = b.id
48+
WHERE l.date_returned IS NULL;
349

4-
CREATE TABLE authors (
5-
id INTEGER PRIMARY KEY,
6-
name TEXT NOT NULL,
7-
birth_year INTEGER
8-
);
9-
10-
CREATE TABLE books (
11-
id INTEGER PRIMARY KEY,
12-
title TEXT NOT NULL,
13-
author_id INTEGER,
14-
isbn TEXT,
15-
year_published INTEGER,
16-
genre TEXT,
17-
format TEXT,
18-
location TEXT,
19-
rating INTEGER,
20-
date_added TEXT,
21-
notes TEXT
22-
);
2350

24-
CREATE TABLE loans (
51+
-- Audit log for tracking changes
52+
CREATE TABLE audit_log (
2553
id INTEGER PRIMARY KEY,
26-
book_id INTEGER NOT NULL,
27-
borrower TEXT NOT NULL,
28-
date_out TEXT NOT NULL,
29-
date_due TEXT,
30-
date_returned TEXT
54+
table_name TEXT,
55+
action TEXT,
56+
record_id INTEGER
3157
);
3258

33-
CREATE INDEX idx_books_author ON books(author_id);
34-
CREATE INDEX idx_books_genre ON books(genre);
35-
CREATE INDEX idx_loans_book ON loans(book_id);
59+
-- Triggers for books
60+
CREATE TRIGGER books_insert AFTER INSERT ON books FOR EACH ROW
61+
BEGIN
62+
INSERT INTO audit_log (table_name, action, record_id) VALUES ('books', 'INSERT', NEW.id);
63+
END;
3664

37-
-- Views
38-
CREATE VIEW v_books AS
39-
SELECT b.id, b.title, a.name AS author, b.year_published,
40-
b.genre, b.format, b.location, b.rating
41-
FROM books b
42-
LEFT JOIN authors a ON b.author_id = a.id;
65+
CREATE TRIGGER books_update AFTER UPDATE ON books FOR EACH ROW
66+
BEGIN
67+
INSERT INTO audit_log (table_name, action, record_id) VALUES ('books', 'UPDATE', NEW.id);
68+
END;
4369

44-
CREATE VIEW v_loans_active AS
45-
SELECT l.id, b.title, l.borrower, l.date_out, l.date_due
46-
FROM loans l
47-
JOIN books b ON l.book_id = b.id
48-
WHERE l.date_returned IS NULL;
70+
CREATE TRIGGER books_delete AFTER DELETE ON books FOR EACH ROW
71+
BEGIN
72+
INSERT INTO audit_log (table_name, action, record_id) VALUES ('books', 'DELETE', OLD.id);
73+
END;

0 commit comments

Comments
 (0)