197 lines
5.4 KiB
SQL
197 lines
5.4 KiB
SQL
-- Enable foreign key support
|
||
PRAGMA foreign_keys = ON;
|
||
PRAGMA journal_mode = WAL;
|
||
PRAGMA cache_size = -2000;
|
||
PRAGMA mmap_size = 30000000000;
|
||
|
||
|
||
-- Words table
|
||
-- TODO restore a separate words table?
|
||
CREATE TABLE expressions(
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
spelling TEXT NOT NULL,
|
||
language_id INTEGER NOT NULL,
|
||
ipa TEXT,
|
||
frequency INTEGER,
|
||
type TEXT NOT NULL,
|
||
subtype TEXT,
|
||
FOREIGN KEY (language_id) REFERENCES languages(id)
|
||
);
|
||
CREATE INDEX idx_words_spelling ON expressions(spelling);
|
||
CREATE INDEX idx_words_type ON expressions(type);
|
||
CREATE INDEX idx_words_subtype ON expressions(subtype);
|
||
CREATE INDEX idx_words_language_id ON expressions(language_id);
|
||
|
||
CREATE TABLE expression_words(
|
||
expression_id INTEGER NOT NULL,
|
||
word_id INTEGER NOT NULL,
|
||
PRIMARY KEY (expression_id, word_id),
|
||
FOREIGN KEY (word_id) REFERENCES expressions(id),
|
||
FOREIGN KEY (expression_id) REFERENCES expressions(id)
|
||
);
|
||
|
||
|
||
-- Languages table
|
||
CREATE TABLE languages (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
name TEXT NOT NULL
|
||
);
|
||
|
||
-- Parts of Speech table
|
||
CREATE TABLE parts_of_speech (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
name TEXT NOT NULL
|
||
);
|
||
|
||
-- Categories table (for noun and verb categories)
|
||
CREATE TABLE categories (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
name TEXT NOT NULL,
|
||
part_of_speech_id INTEGER NOT NULL,
|
||
FOREIGN KEY (part_of_speech_id) REFERENCES parts_of_speech(id)
|
||
);
|
||
CREATE INDEX idx_categories_name ON categories(name);
|
||
CREATE INDEX idx_categories_part_of_speech_id ON categories(part_of_speech_id);
|
||
|
||
-- Word Categories junction table
|
||
CREATE TABLE word_categories (
|
||
word_id INTEGER NOT NULL,
|
||
category_id INTEGER NOT NULL,
|
||
PRIMARY KEY (word_id, category_id),
|
||
FOREIGN KEY (word_id) REFERENCES words(id),
|
||
FOREIGN KEY (category_id) REFERENCES categories(id)
|
||
);
|
||
CREATE INDEX idx_word_categories_category_id ON word_categories(category_id);
|
||
|
||
-- Example data insertion
|
||
INSERT INTO languages (name) VALUES ('en-us');
|
||
INSERT INTO languages (name) VALUES ('th');
|
||
INSERT INTO languages (name) VALUES ('zh-cn');
|
||
INSERT INTO languages (name) VALUES ('zh-hk');
|
||
INSERT INTO languages (name) VALUES ('ja-jp');
|
||
|
||
INSERT INTO parts_of_speech (name) VALUES ('noun'), ('verb'), ('adjective'), ('adverb'), ('pronoun'), ('adposition'), ('conjunction'), ('unknown');
|
||
|
||
INSERT INTO categories (name, part_of_speech_id) VALUES
|
||
('countable', 1),
|
||
('uncountable', 1),
|
||
('animate', 1),
|
||
('inanimate', 1),
|
||
('spatial', 1),
|
||
('temporal', 1),
|
||
('abstract', 1),
|
||
('noun', 1),
|
||
-- verbs
|
||
('transitive', 2),
|
||
('intransitive', 2),
|
||
('action', 2),
|
||
('mental', 2),
|
||
('auxiliar', 2),
|
||
('verb', 2),
|
||
-- adjectives
|
||
('adjective', 3),
|
||
-- adverbs
|
||
('adverb', 4),
|
||
-- pronouns
|
||
('nominative', 5),
|
||
('accusative', 5),
|
||
('genitive', 5),
|
||
('interrogative', 5),
|
||
-- not really a pronoun but whatever
|
||
('determiner', 5),
|
||
-- adpositions
|
||
('preposition', 6),
|
||
('postposition', 6),
|
||
('circumposition', 6),
|
||
-- conjunctions
|
||
('conjunction', 7),
|
||
-- ?
|
||
('unknown', 8);
|
||
|
||
-- -- Example word insertion
|
||
-- INSERT INTO words (spelling, ipa, language_id) VALUES ('book', 'bʊk', 1);
|
||
|
||
-- -- Categorize 'book' as a countable, inanimate noun
|
||
-- INSERT INTO word_categories (word_id, category_id)
|
||
-- SELECT
|
||
-- (SELECT id FROM words WHERE spelling = 'book'),
|
||
-- id
|
||
-- FROM categories
|
||
-- WHERE name IN ('countable', 'inanimate');
|
||
|
||
-- -- Example verb insertion
|
||
-- INSERT INTO words (spelling, ipa, language_id) VALUES ('think','θɪŋk', 1);
|
||
|
||
-- -- Categorize 'think' as an intransitive, mental verb
|
||
-- INSERT INTO word_categories (word_id, category_id)
|
||
-- SELECT
|
||
-- (SELECT id FROM words WHERE spelling = 'think'),
|
||
-- id
|
||
-- FROM categories
|
||
-- WHERE name IN ('intransitive', 'mental');
|
||
|
||
|
||
-- Progress
|
||
CREATE TABLE users(
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
name TEXT NOT NULL,
|
||
creds TEXT NOT NULL
|
||
);
|
||
|
||
-- Lessons
|
||
CREATE TABLE lessons(
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
text TEXT NOT NULL
|
||
);
|
||
CREATE TABLE cards(
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
text TEXT NOT NULL,
|
||
note TEXT
|
||
);
|
||
CREATE TABLE cards_expressions(
|
||
expression_id INTEGER NOT NULL,
|
||
card_id INTEGER NOT NULL,
|
||
PRIMARY KEY (card_id, expression_id),
|
||
FOREIGN KEY (card_id) REFERENCES cards(id),
|
||
FOREIGN KEY (expression_id) REFERENCES expressions(id)
|
||
);
|
||
CREATE TABLE cards_lessons(
|
||
lesson_id INTEGER,
|
||
card_id INTEGER NOT NULL,
|
||
PRIMARY KEY (card_id, lesson_id),
|
||
FOREIGN KEY (card_id) REFERENCES cards(id),
|
||
FOREIGN KEY (lesson_id) REFERENCES lessons(id)
|
||
);
|
||
|
||
CREATE TABLE attempts(
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
user_id INTEGER NOT NULL,
|
||
timestamp INTEGER NOT NULL,
|
||
card_id INTEGER NOT NULL,
|
||
good INTEGER NOT NULL, -- 0 or 1
|
||
FOREIGN KEY (user_id) REFERENCES users(id)
|
||
FOREIGN KEY (card_id) REFERENCES cards(id)
|
||
);
|
||
|
||
-- Index to query attempts on a specific card
|
||
CREATE INDEX idx_attempts_card ON attempts(card_id);
|
||
|
||
-- Index to query attempts for a specific user
|
||
CREATE INDEX idx_attempts_user ON attempts(user_id);
|
||
|
||
-- (Optional) Index to query attempts by user and resource (useful if you often query by both)
|
||
CREATE INDEX idx_attempts_user_resource ON attempts(user_id, card_id);
|
||
CREATE INDEX idx_cards_resources
|
||
ON cards_expressions(expression_id, card_id);
|
||
|
||
-- CREATE TRIGGER IF NOT EXISTS populate_cards_resources
|
||
-- AFTER INSERT ON cards
|
||
-- FOR EACH ROW
|
||
-- BEGIN
|
||
-- -- Insert matching words into cards_resources
|
||
-- INSERT INTO cards_expressions(card_id, expression_id)
|
||
-- SELECT NEW.id, w.id
|
||
-- FROM expressions w
|
||
-- WHERE NEW.text LIKE '%' || w.spelling || '%';
|
||
-- END;
|