diff options
Diffstat (limited to 'db')
-rw-r--r-- | db/db.ts | 22 | ||||
-rw-r--r-- | db/makefile | 2 | ||||
-rw-r--r-- | db/user/init.sql | 5 |
3 files changed, 18 insertions, 11 deletions
@@ -47,7 +47,7 @@ interface DBFindResult { * const results = db.findTerm("なった"); */ export default class DB { - private connection: Database; + private connection: Database = new Database(":memory:", { create: false }); public ready: Promise<void>; private here = path.dirname(path.fromFileUrl(import.meta.url)); private paths = { @@ -59,22 +59,19 @@ export default class DB { find: path.resolve(this.here, 'find.sql'), }, } as const; - private statement: { - attach: Statement; - queryTerm: Statement; - }; + private statement = { + attach: this.connection.prepare("attach database ? as ?"), + queryTerm: this.connection.prepare(""), + termPriority: this.connection.prepare(""), + } satisfies { [name: string]: Statement }; constructor() { - this.connection = new Database(":memory:", { create: false }); - this.statement = { - attach: this.connection.prepare("attach database ? as ?"), - queryTerm: this.connection.prepare(""), - }; this.attach(this.paths.db.dict, 'dict'); this.attach(this.paths.db.user, 'user'); this.ready = new Promise<void>(async resolve => { const statement = await Deno.readTextFile(this.paths.query.find); this.statement.queryTerm = this.connection.prepare(statement); + this.statement.termPriority = this.connection.prepare("insert into sort_overlay (user_id, expression, reading, sort) values (?, ?, ?, ?)"); resolve(); }); } @@ -104,4 +101,9 @@ export default class DB { }); return terms; } + + async termPriority(userID: number, expression: string, reading: string, priority: number) { + await this.ready; + this.statement.termPriority.run(userID, expression, reading, priority); + } }; diff --git a/db/makefile b/db/makefile index 88d4bba..3968ae7 100644 --- a/db/makefile +++ b/db/makefile @@ -32,7 +32,7 @@ dict/dict.sql: $(DEFAULT_DICTS) user/base.sql: user/reset.sql user/init.sql cat $^ > $@ -user/full.sql: user/base.sql user/root.sql +user/full.sql: user/base.sql # user/root.sql cat $^ > $@ %.sql: %.dict.sql $(DICT_TEMPLATE) diff --git a/db/user/init.sql b/db/user/init.sql index 1d0e830..af60fa3 100644 --- a/db/user/init.sql +++ b/db/user/init.sql @@ -4,6 +4,8 @@ create table if not exists user ( unique(username) ); +insert into user (id, username) values (0, 'root'); + create table if not exists sort_overlay ( id integer primary key autoincrement, user_id int not null default 0, @@ -14,3 +16,6 @@ create table if not exists sort_overlay ( unique(user_id, expression, reading) on conflict replace ); +create index sort_overlay_expression on sort_overlay (expression); +create index sort_overlay_reading on sort_overlay (reading); + |