2022-10-21 08:00:43 +00:00
|
|
|
import { AsyncSQLite } from "../../plugos/sqlite/async_sqlite.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
import { SysCallMapping } from "../system.ts";
|
|
|
|
|
2022-10-21 08:00:43 +00:00
|
|
|
export async function ensureFTSTable(
|
|
|
|
db: AsyncSQLite,
|
2022-10-10 12:50:21 +00:00
|
|
|
tableName: string,
|
|
|
|
) {
|
2022-10-21 08:00:43 +00:00
|
|
|
const result = await db.query(
|
2022-10-19 07:52:29 +00:00
|
|
|
`SELECT name FROM sqlite_master WHERE type='table' AND name=?`,
|
2022-10-21 08:00:43 +00:00
|
|
|
tableName,
|
2022-10-19 07:52:29 +00:00
|
|
|
);
|
|
|
|
if (result.length === 0) {
|
2022-10-21 08:00:43 +00:00
|
|
|
await db.execute(
|
2022-10-19 07:52:29 +00:00
|
|
|
`CREATE VIRTUAL TABLE ${tableName} USING fts5(key, value);`,
|
|
|
|
);
|
2022-10-10 12:50:21 +00:00
|
|
|
|
2022-10-19 07:52:29 +00:00
|
|
|
console.log(`Created fts5 table ${tableName}`);
|
|
|
|
}
|
2022-10-10 12:50:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function fullTextSearchSyscalls(
|
2022-10-21 08:00:43 +00:00
|
|
|
db: AsyncSQLite,
|
2022-10-10 12:50:21 +00:00
|
|
|
tableName: string,
|
|
|
|
): SysCallMapping {
|
|
|
|
return {
|
|
|
|
"fulltext.index": async (_ctx, key: string, value: string) => {
|
2022-10-21 08:00:43 +00:00
|
|
|
await db.execute(`DELETE FROM ${tableName} WHERE key = ?`, key);
|
|
|
|
await db.execute(
|
2022-10-10 12:50:21 +00:00
|
|
|
`INSERT INTO ${tableName} (key, value) VALUES (?, ?)`,
|
|
|
|
key,
|
|
|
|
value,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
"fulltext.delete": async (_ctx, key: string) => {
|
2022-10-21 08:00:43 +00:00
|
|
|
await db.execute(`DELETE FROM ${tableName} WHERE key = ?`, key);
|
2022-10-10 12:50:21 +00:00
|
|
|
},
|
|
|
|
"fulltext.search": async (_ctx, phrase: string, limit: number) => {
|
2022-10-19 07:52:29 +00:00
|
|
|
console.log("Got search query", phrase);
|
2022-10-10 12:50:21 +00:00
|
|
|
return (
|
2022-10-21 08:00:43 +00:00
|
|
|
await db.query(
|
2022-10-10 12:50:21 +00:00
|
|
|
`SELECT key, rank FROM ${tableName} WHERE value MATCH ? ORDER BY key, rank LIMIT ?`,
|
|
|
|
phrase,
|
|
|
|
limit,
|
|
|
|
)
|
|
|
|
).map((item) => ({ name: item.key, rank: item.rank }));
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|