@atscript/db-sqlite 0.1.167 → 0.1.169
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +62 -16
- package/dist/index.d.cts +8 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.mjs +62 -16
- package/package.json +12 -10
package/dist/index.cjs
CHANGED
|
@@ -339,6 +339,8 @@ const sqliteDialect = {
|
|
|
339
339
|
jsonFromText: (quotedCol) => `CASE WHEN json_valid(${quotedCol}) THEN ${quotedCol} ELSE json_quote(${quotedCol}) END`,
|
|
340
340
|
castDouble: (expr) => `CAST(${expr} AS REAL)`,
|
|
341
341
|
nullsPlacementSyntax: true,
|
|
342
|
+
conditionalAggregate: "filter",
|
|
343
|
+
countDistinctColumns: (columns) => `COUNT(DISTINCT CASE WHEN ${columns.map((c) => `${c} IS NOT NULL`).join(" AND ")} THEN json_array(${columns.join(", ")}) END)`,
|
|
342
344
|
createViewPrefix: "CREATE VIEW IF NOT EXISTS"
|
|
343
345
|
};
|
|
344
346
|
/**
|
|
@@ -738,27 +740,47 @@ const KEYWORD = /^(?:AND|OR|NOT|NEAR)$/i;
|
|
|
738
740
|
* - a `"quoted phrase"` stays a phrase (words in order);
|
|
739
741
|
* - a bare word stays a word, and a trailing `*` keeps its prefix meaning
|
|
740
742
|
* (`quok*` finds `quokka`);
|
|
741
|
-
* - words are AND-ed, as before
|
|
743
|
+
* - words are AND-ed, as before — OR-ed when `match` is `"any"`
|
|
744
|
+
* (`@db.index.fulltext.match`, since 0.1.168);
|
|
742
745
|
* - everything else — `AND` / `OR` / `NOT`, `-`, `+`, `^`, `NEAR(…)`,
|
|
743
746
|
* `column:`, parentheses — is plain text (the tokenizer drops punctuation).
|
|
744
747
|
*
|
|
745
748
|
* Quoted strings never contain a `"`: an inner `"` is doubled (FTS5 escape).
|
|
746
749
|
*/
|
|
747
|
-
function quoteFtsTerm(text) {
|
|
750
|
+
function quoteFtsTerm(text, match) {
|
|
748
751
|
const parts = [];
|
|
749
|
-
for (const
|
|
750
|
-
const phrase =
|
|
752
|
+
for (const found of text.matchAll(/"([^"]*)"|(\S+)/g)) {
|
|
753
|
+
const phrase = found[1];
|
|
751
754
|
if (phrase !== void 0) {
|
|
752
755
|
if (phrase.trim()) parts.push(`"${phrase.replace(/"/g, "\"\"")}"`);
|
|
753
756
|
continue;
|
|
754
757
|
}
|
|
755
|
-
const word =
|
|
758
|
+
const word = found[2];
|
|
756
759
|
const core = word.replace(/\*+$/, "");
|
|
757
760
|
if (!core) continue;
|
|
758
761
|
const quoted = BAREWORD.test(core) && !KEYWORD.test(core) ? core : `"${core.replace(/"/g, "\"\"")}"`;
|
|
759
762
|
parts.push(core.length < word.length ? `${quoted}*` : quoted);
|
|
760
763
|
}
|
|
761
|
-
return parts.length > 0 ? parts.join(" ") : "\"\"";
|
|
764
|
+
return parts.length > 0 ? parts.join(match === "any" ? " OR " : " ") : "\"\"";
|
|
765
|
+
}
|
|
766
|
+
/**
|
|
767
|
+
* The FTS5 `tokenize` option of a fulltext index (since 0.1.168): `english`
|
|
768
|
+
* (`@db.index.fulltext.language`) adds the Porter stemmer; every other
|
|
769
|
+
* language — FTS5 ships no other stemmer — and no language keep FTS5's
|
|
770
|
+
* default `unicode61` (`undefined`: no `tokenize` clause).
|
|
771
|
+
*/
|
|
772
|
+
function ftsTokenizer(index) {
|
|
773
|
+
return index.language === "english" ? "porter unicode61" : void 0;
|
|
774
|
+
}
|
|
775
|
+
/** Whether an FTS5 table's `CREATE` statement has the tokenizer the index's language asks for. */
|
|
776
|
+
function ftsTokenizerMatch(createSql, index) {
|
|
777
|
+
return /tokenize\s*=\s*'([^']*)'/i.exec(createSql)?.[1] === ftsTokenizer(index);
|
|
778
|
+
}
|
|
779
|
+
/** The sync warning for a language FTS5 cannot stem, `undefined` when it can (or none is set). */
|
|
780
|
+
function ftsLanguageWarning(index) {
|
|
781
|
+
const { language } = index;
|
|
782
|
+
if (language === void 0 || language === "english" || language === "none") return void 0;
|
|
783
|
+
return `[sqlite] fulltext index "${index.name}": FTS5 has no ${language} stemmer — words are indexed unstemmed (unicode61)`;
|
|
762
784
|
}
|
|
763
785
|
//#endregion
|
|
764
786
|
//#region src/sqlite-adapter.ts
|
|
@@ -798,6 +820,18 @@ var SqliteAdapter = class SqliteAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
798
820
|
supportsAggregateExpressions() {
|
|
799
821
|
return true;
|
|
800
822
|
}
|
|
823
|
+
/** Conditional aggregates (`$where` on an aggregate `$select` entry). */
|
|
824
|
+
supportsConditionalAggregates() {
|
|
825
|
+
return true;
|
|
826
|
+
}
|
|
827
|
+
/** `countDistinct` over several fields. */
|
|
828
|
+
supportsDistinctFieldLists() {
|
|
829
|
+
return true;
|
|
830
|
+
}
|
|
831
|
+
/** Two-level aggregates (`$from`): the inner level is a derived table. */
|
|
832
|
+
supportsNestedAggregates() {
|
|
833
|
+
return true;
|
|
834
|
+
}
|
|
801
835
|
/** `$nulls` / `@db.sort.nulls` NULL placement — `NULLS FIRST` / `NULLS LAST`. */
|
|
802
836
|
supportsNullsPlacement() {
|
|
803
837
|
return true;
|
|
@@ -1505,6 +1539,8 @@ var SqliteAdapter = class SqliteAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1505
1539
|
async renameTable(oldName) {
|
|
1506
1540
|
return this._stmt(() => {
|
|
1507
1541
|
const newName = this.resolveTableName();
|
|
1542
|
+
const exists = (name) => this.driver.get(`SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?`, [name]) !== null;
|
|
1543
|
+
if (!exists(oldName) && exists(newName)) return;
|
|
1508
1544
|
const ddl = `ALTER TABLE "${esc(oldName)}" RENAME TO "${esc(newName)}"`;
|
|
1509
1545
|
this._log(ddl);
|
|
1510
1546
|
this.driver.exec(ddl);
|
|
@@ -1620,7 +1656,7 @@ var SqliteAdapter = class SqliteAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1620
1656
|
const eqs = plan.exact.exactEqualities.map((eq) => `t.${eq}`);
|
|
1621
1657
|
head = `FROM "${esc(this.resolveTableName())}" AS t LEFT JOIN m ON m.__atscript_r = t.rowid WHERE (m.__atscript_r IS NOT NULL OR ${eqs.join(" OR ")})` + (where.sql === "1=1" ? "" : ` AND (${where.sql})`);
|
|
1622
1658
|
params = [
|
|
1623
|
-
quoteFtsTerm(text),
|
|
1659
|
+
quoteFtsTerm(text, plan.index.match),
|
|
1624
1660
|
...eqs.map(() => plan.exact.n),
|
|
1625
1661
|
...where.params
|
|
1626
1662
|
];
|
|
@@ -1735,7 +1771,7 @@ var SqliteAdapter = class SqliteAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1735
1771
|
if (textFields.length > 0) {
|
|
1736
1772
|
const quoted = `"${esc(this._ftsTableName(index.name))}"`;
|
|
1737
1773
|
parts.push(`rowid IN (SELECT rowid FROM ${quoted} WHERE ${quoted} MATCH ?)`);
|
|
1738
|
-
params.push(quoteFtsTerm(text));
|
|
1774
|
+
params.push(quoteFtsTerm(text, index.match));
|
|
1739
1775
|
}
|
|
1740
1776
|
for (const eq of equalities) {
|
|
1741
1777
|
parts.push(eq);
|
|
@@ -1776,7 +1812,7 @@ var SqliteAdapter = class SqliteAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1776
1812
|
let fromWhere = `FROM "${esc(tableName)}" AS t`;
|
|
1777
1813
|
fromWhere += ` JOIN "${esc(ftsTable)}" AS fts ON t.rowid = fts.rowid`;
|
|
1778
1814
|
fromWhere += ` WHERE fts."${esc(ftsTable)}" MATCH ?`;
|
|
1779
|
-
const params = [quoteFtsTerm(text)];
|
|
1815
|
+
const params = [quoteFtsTerm(text, ftsIndex.match)];
|
|
1780
1816
|
if (where.sql !== "1=1") {
|
|
1781
1817
|
fromWhere += ` AND (${where.sql})`;
|
|
1782
1818
|
params.push(...where.params);
|
|
@@ -1793,19 +1829,23 @@ var SqliteAdapter = class SqliteAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1793
1829
|
const { changes, apply } = (0, _atscript_db.createChangeRecorder)(dryRun);
|
|
1794
1830
|
const ftIndexes = this._getFulltextIndexes().filter((idx) => (0, _atscript_db.splitFulltextFields)(idx).text.length > 0);
|
|
1795
1831
|
const desiredFtsTables = new Set(ftIndexes.map((idx) => this._ftsTableName(idx.name)));
|
|
1796
|
-
|
|
1797
|
-
|
|
1832
|
+
if (!dryRun) for (const index of ftIndexes) {
|
|
1833
|
+
const warning = ftsLanguageWarning(index);
|
|
1834
|
+
if (warning) this.logger.warn(warning);
|
|
1835
|
+
}
|
|
1836
|
+
const existingFts = this._shadowTableSql(tableName, "fts");
|
|
1837
|
+
for (const name of existingFts.keys()) if (!desiredFtsTables.has(name)) await apply({
|
|
1798
1838
|
name,
|
|
1799
1839
|
action: "drop"
|
|
1800
1840
|
}, name, () => this._dropFtsTable(name));
|
|
1801
|
-
const existingSet = new Set(existingFts);
|
|
1802
1841
|
for (const index of ftIndexes) {
|
|
1803
1842
|
const ftsTable = this._ftsTableName(index.name);
|
|
1804
|
-
|
|
1843
|
+
const liveSql = existingFts.get(ftsTable);
|
|
1844
|
+
if (liveSql === void 0) await apply({
|
|
1805
1845
|
name: ftsTable,
|
|
1806
1846
|
action: "add"
|
|
1807
1847
|
}, ftsTable, () => this._createFtsTable(tableName, ftsTable, index));
|
|
1808
|
-
else if (!this._ftsColumnsMatch(ftsTable, index)) await apply({
|
|
1848
|
+
else if (!this._ftsColumnsMatch(ftsTable, index) || !ftsTokenizerMatch(liveSql, index)) await apply({
|
|
1809
1849
|
name: ftsTable,
|
|
1810
1850
|
action: "rebuild"
|
|
1811
1851
|
}, ftsTable, () => {
|
|
@@ -1825,7 +1865,9 @@ var SqliteAdapter = class SqliteAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1825
1865
|
_createFtsTable(tableName, ftsTable, index) {
|
|
1826
1866
|
const fields = (0, _atscript_db.splitFulltextFields)(index).text;
|
|
1827
1867
|
const fieldList = fields.map((f) => `"${esc(f.name)}"`).join(", ");
|
|
1828
|
-
const
|
|
1868
|
+
const tokenizer = ftsTokenizer(index);
|
|
1869
|
+
const tokenize = tokenizer ? `, tokenize='${tokenizer}'` : "";
|
|
1870
|
+
const createSql = `CREATE VIRTUAL TABLE IF NOT EXISTS "${esc(ftsTable)}" USING fts5(${fieldList}, content='${tableName.replace(/'/g, "''")}', content_rowid='rowid'${tokenize})`;
|
|
1829
1871
|
this._log(createSql);
|
|
1830
1872
|
this.driver.exec(createSql);
|
|
1831
1873
|
const newFields = fields.map((f) => `new."${esc(f.name)}"`).join(", ");
|
|
@@ -1857,7 +1899,11 @@ var SqliteAdapter = class SqliteAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
1857
1899
|
}
|
|
1858
1900
|
/** Lists FTS5/vec0 shadow virtual tables for a content table. */
|
|
1859
1901
|
_listShadowTables(tableName, kind) {
|
|
1860
|
-
return this.
|
|
1902
|
+
return [...this._shadowTableSql(tableName, kind).keys()];
|
|
1903
|
+
}
|
|
1904
|
+
/** {@link _listShadowTables} with each virtual table's `CREATE` statement. */
|
|
1905
|
+
_shadowTableSql(tableName, kind) {
|
|
1906
|
+
return new Map(this.driver.all(`SELECT name, sql FROM sqlite_master WHERE type='table' AND name LIKE ?`, [`${tableName}__${kind}__%`]).filter((r) => r.sql.startsWith("CREATE VIRTUAL TABLE")).map((r) => [r.name, r.sql]));
|
|
1861
1907
|
}
|
|
1862
1908
|
/** Drops all FTS virtual tables and triggers for a content table. */
|
|
1863
1909
|
_dropAllFtsTables(tableName) {
|
package/dist/index.d.cts
CHANGED
|
@@ -297,6 +297,12 @@ declare class SqliteAdapter extends BaseDbAdapter {
|
|
|
297
297
|
aggregateFns(): ReadonlySet<AggregateFn>;
|
|
298
298
|
/** Arithmetic in an aggregate `$select` (`{ $expr }`, `{ $fn, $expr }`). */
|
|
299
299
|
supportsAggregateExpressions(): boolean;
|
|
300
|
+
/** Conditional aggregates (`$where` on an aggregate `$select` entry). */
|
|
301
|
+
supportsConditionalAggregates(): boolean;
|
|
302
|
+
/** `countDistinct` over several fields. */
|
|
303
|
+
supportsDistinctFieldLists(): boolean;
|
|
304
|
+
/** Two-level aggregates (`$from`): the inner level is a derived table. */
|
|
305
|
+
supportsNestedAggregates(): boolean;
|
|
300
306
|
/** `$nulls` / `@db.sort.nulls` NULL placement — `NULLS FIRST` / `NULLS LAST`. */
|
|
301
307
|
supportsNullsPlacement(): boolean;
|
|
302
308
|
/**
|
|
@@ -550,6 +556,8 @@ declare class SqliteAdapter extends BaseDbAdapter {
|
|
|
550
556
|
private _dropFtsTable;
|
|
551
557
|
/** Lists FTS5/vec0 shadow virtual tables for a content table. */
|
|
552
558
|
private _listShadowTables;
|
|
559
|
+
/** {@link _listShadowTables} with each virtual table's `CREATE` statement. */
|
|
560
|
+
private _shadowTableSql;
|
|
553
561
|
/** Drops all FTS virtual tables and triggers for a content table. */
|
|
554
562
|
private _dropAllFtsTables;
|
|
555
563
|
private static readonly _RESIDUAL_OVERFETCH;
|
package/dist/index.d.mts
CHANGED
|
@@ -297,6 +297,12 @@ declare class SqliteAdapter extends BaseDbAdapter {
|
|
|
297
297
|
aggregateFns(): ReadonlySet<AggregateFn>;
|
|
298
298
|
/** Arithmetic in an aggregate `$select` (`{ $expr }`, `{ $fn, $expr }`). */
|
|
299
299
|
supportsAggregateExpressions(): boolean;
|
|
300
|
+
/** Conditional aggregates (`$where` on an aggregate `$select` entry). */
|
|
301
|
+
supportsConditionalAggregates(): boolean;
|
|
302
|
+
/** `countDistinct` over several fields. */
|
|
303
|
+
supportsDistinctFieldLists(): boolean;
|
|
304
|
+
/** Two-level aggregates (`$from`): the inner level is a derived table. */
|
|
305
|
+
supportsNestedAggregates(): boolean;
|
|
300
306
|
/** `$nulls` / `@db.sort.nulls` NULL placement — `NULLS FIRST` / `NULLS LAST`. */
|
|
301
307
|
supportsNullsPlacement(): boolean;
|
|
302
308
|
/**
|
|
@@ -550,6 +556,8 @@ declare class SqliteAdapter extends BaseDbAdapter {
|
|
|
550
556
|
private _dropFtsTable;
|
|
551
557
|
/** Lists FTS5/vec0 shadow virtual tables for a content table. */
|
|
552
558
|
private _listShadowTables;
|
|
559
|
+
/** {@link _listShadowTables} with each virtual table's `CREATE` statement. */
|
|
560
|
+
private _shadowTableSql;
|
|
553
561
|
/** Drops all FTS virtual tables and triggers for a content table. */
|
|
554
562
|
private _dropAllFtsTables;
|
|
555
563
|
private static readonly _RESIDUAL_OVERFETCH;
|
package/dist/index.mjs
CHANGED
|
@@ -338,6 +338,8 @@ const sqliteDialect = {
|
|
|
338
338
|
jsonFromText: (quotedCol) => `CASE WHEN json_valid(${quotedCol}) THEN ${quotedCol} ELSE json_quote(${quotedCol}) END`,
|
|
339
339
|
castDouble: (expr) => `CAST(${expr} AS REAL)`,
|
|
340
340
|
nullsPlacementSyntax: true,
|
|
341
|
+
conditionalAggregate: "filter",
|
|
342
|
+
countDistinctColumns: (columns) => `COUNT(DISTINCT CASE WHEN ${columns.map((c) => `${c} IS NOT NULL`).join(" AND ")} THEN json_array(${columns.join(", ")}) END)`,
|
|
341
343
|
createViewPrefix: "CREATE VIEW IF NOT EXISTS"
|
|
342
344
|
};
|
|
343
345
|
/**
|
|
@@ -737,27 +739,47 @@ const KEYWORD = /^(?:AND|OR|NOT|NEAR)$/i;
|
|
|
737
739
|
* - a `"quoted phrase"` stays a phrase (words in order);
|
|
738
740
|
* - a bare word stays a word, and a trailing `*` keeps its prefix meaning
|
|
739
741
|
* (`quok*` finds `quokka`);
|
|
740
|
-
* - words are AND-ed, as before
|
|
742
|
+
* - words are AND-ed, as before — OR-ed when `match` is `"any"`
|
|
743
|
+
* (`@db.index.fulltext.match`, since 0.1.168);
|
|
741
744
|
* - everything else — `AND` / `OR` / `NOT`, `-`, `+`, `^`, `NEAR(…)`,
|
|
742
745
|
* `column:`, parentheses — is plain text (the tokenizer drops punctuation).
|
|
743
746
|
*
|
|
744
747
|
* Quoted strings never contain a `"`: an inner `"` is doubled (FTS5 escape).
|
|
745
748
|
*/
|
|
746
|
-
function quoteFtsTerm(text) {
|
|
749
|
+
function quoteFtsTerm(text, match) {
|
|
747
750
|
const parts = [];
|
|
748
|
-
for (const
|
|
749
|
-
const phrase =
|
|
751
|
+
for (const found of text.matchAll(/"([^"]*)"|(\S+)/g)) {
|
|
752
|
+
const phrase = found[1];
|
|
750
753
|
if (phrase !== void 0) {
|
|
751
754
|
if (phrase.trim()) parts.push(`"${phrase.replace(/"/g, "\"\"")}"`);
|
|
752
755
|
continue;
|
|
753
756
|
}
|
|
754
|
-
const word =
|
|
757
|
+
const word = found[2];
|
|
755
758
|
const core = word.replace(/\*+$/, "");
|
|
756
759
|
if (!core) continue;
|
|
757
760
|
const quoted = BAREWORD.test(core) && !KEYWORD.test(core) ? core : `"${core.replace(/"/g, "\"\"")}"`;
|
|
758
761
|
parts.push(core.length < word.length ? `${quoted}*` : quoted);
|
|
759
762
|
}
|
|
760
|
-
return parts.length > 0 ? parts.join(" ") : "\"\"";
|
|
763
|
+
return parts.length > 0 ? parts.join(match === "any" ? " OR " : " ") : "\"\"";
|
|
764
|
+
}
|
|
765
|
+
/**
|
|
766
|
+
* The FTS5 `tokenize` option of a fulltext index (since 0.1.168): `english`
|
|
767
|
+
* (`@db.index.fulltext.language`) adds the Porter stemmer; every other
|
|
768
|
+
* language — FTS5 ships no other stemmer — and no language keep FTS5's
|
|
769
|
+
* default `unicode61` (`undefined`: no `tokenize` clause).
|
|
770
|
+
*/
|
|
771
|
+
function ftsTokenizer(index) {
|
|
772
|
+
return index.language === "english" ? "porter unicode61" : void 0;
|
|
773
|
+
}
|
|
774
|
+
/** Whether an FTS5 table's `CREATE` statement has the tokenizer the index's language asks for. */
|
|
775
|
+
function ftsTokenizerMatch(createSql, index) {
|
|
776
|
+
return /tokenize\s*=\s*'([^']*)'/i.exec(createSql)?.[1] === ftsTokenizer(index);
|
|
777
|
+
}
|
|
778
|
+
/** The sync warning for a language FTS5 cannot stem, `undefined` when it can (or none is set). */
|
|
779
|
+
function ftsLanguageWarning(index) {
|
|
780
|
+
const { language } = index;
|
|
781
|
+
if (language === void 0 || language === "english" || language === "none") return void 0;
|
|
782
|
+
return `[sqlite] fulltext index "${index.name}": FTS5 has no ${language} stemmer — words are indexed unstemmed (unicode61)`;
|
|
761
783
|
}
|
|
762
784
|
//#endregion
|
|
763
785
|
//#region src/sqlite-adapter.ts
|
|
@@ -797,6 +819,18 @@ var SqliteAdapter = class SqliteAdapter extends BaseDbAdapter {
|
|
|
797
819
|
supportsAggregateExpressions() {
|
|
798
820
|
return true;
|
|
799
821
|
}
|
|
822
|
+
/** Conditional aggregates (`$where` on an aggregate `$select` entry). */
|
|
823
|
+
supportsConditionalAggregates() {
|
|
824
|
+
return true;
|
|
825
|
+
}
|
|
826
|
+
/** `countDistinct` over several fields. */
|
|
827
|
+
supportsDistinctFieldLists() {
|
|
828
|
+
return true;
|
|
829
|
+
}
|
|
830
|
+
/** Two-level aggregates (`$from`): the inner level is a derived table. */
|
|
831
|
+
supportsNestedAggregates() {
|
|
832
|
+
return true;
|
|
833
|
+
}
|
|
800
834
|
/** `$nulls` / `@db.sort.nulls` NULL placement — `NULLS FIRST` / `NULLS LAST`. */
|
|
801
835
|
supportsNullsPlacement() {
|
|
802
836
|
return true;
|
|
@@ -1504,6 +1538,8 @@ var SqliteAdapter = class SqliteAdapter extends BaseDbAdapter {
|
|
|
1504
1538
|
async renameTable(oldName) {
|
|
1505
1539
|
return this._stmt(() => {
|
|
1506
1540
|
const newName = this.resolveTableName();
|
|
1541
|
+
const exists = (name) => this.driver.get(`SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?`, [name]) !== null;
|
|
1542
|
+
if (!exists(oldName) && exists(newName)) return;
|
|
1507
1543
|
const ddl = `ALTER TABLE "${esc(oldName)}" RENAME TO "${esc(newName)}"`;
|
|
1508
1544
|
this._log(ddl);
|
|
1509
1545
|
this.driver.exec(ddl);
|
|
@@ -1619,7 +1655,7 @@ var SqliteAdapter = class SqliteAdapter extends BaseDbAdapter {
|
|
|
1619
1655
|
const eqs = plan.exact.exactEqualities.map((eq) => `t.${eq}`);
|
|
1620
1656
|
head = `FROM "${esc(this.resolveTableName())}" AS t LEFT JOIN m ON m.__atscript_r = t.rowid WHERE (m.__atscript_r IS NOT NULL OR ${eqs.join(" OR ")})` + (where.sql === "1=1" ? "" : ` AND (${where.sql})`);
|
|
1621
1657
|
params = [
|
|
1622
|
-
quoteFtsTerm(text),
|
|
1658
|
+
quoteFtsTerm(text, plan.index.match),
|
|
1623
1659
|
...eqs.map(() => plan.exact.n),
|
|
1624
1660
|
...where.params
|
|
1625
1661
|
];
|
|
@@ -1734,7 +1770,7 @@ var SqliteAdapter = class SqliteAdapter extends BaseDbAdapter {
|
|
|
1734
1770
|
if (textFields.length > 0) {
|
|
1735
1771
|
const quoted = `"${esc(this._ftsTableName(index.name))}"`;
|
|
1736
1772
|
parts.push(`rowid IN (SELECT rowid FROM ${quoted} WHERE ${quoted} MATCH ?)`);
|
|
1737
|
-
params.push(quoteFtsTerm(text));
|
|
1773
|
+
params.push(quoteFtsTerm(text, index.match));
|
|
1738
1774
|
}
|
|
1739
1775
|
for (const eq of equalities) {
|
|
1740
1776
|
parts.push(eq);
|
|
@@ -1775,7 +1811,7 @@ var SqliteAdapter = class SqliteAdapter extends BaseDbAdapter {
|
|
|
1775
1811
|
let fromWhere = `FROM "${esc(tableName)}" AS t`;
|
|
1776
1812
|
fromWhere += ` JOIN "${esc(ftsTable)}" AS fts ON t.rowid = fts.rowid`;
|
|
1777
1813
|
fromWhere += ` WHERE fts."${esc(ftsTable)}" MATCH ?`;
|
|
1778
|
-
const params = [quoteFtsTerm(text)];
|
|
1814
|
+
const params = [quoteFtsTerm(text, ftsIndex.match)];
|
|
1779
1815
|
if (where.sql !== "1=1") {
|
|
1780
1816
|
fromWhere += ` AND (${where.sql})`;
|
|
1781
1817
|
params.push(...where.params);
|
|
@@ -1792,19 +1828,23 @@ var SqliteAdapter = class SqliteAdapter extends BaseDbAdapter {
|
|
|
1792
1828
|
const { changes, apply } = createChangeRecorder(dryRun);
|
|
1793
1829
|
const ftIndexes = this._getFulltextIndexes().filter((idx) => splitFulltextFields(idx).text.length > 0);
|
|
1794
1830
|
const desiredFtsTables = new Set(ftIndexes.map((idx) => this._ftsTableName(idx.name)));
|
|
1795
|
-
|
|
1796
|
-
|
|
1831
|
+
if (!dryRun) for (const index of ftIndexes) {
|
|
1832
|
+
const warning = ftsLanguageWarning(index);
|
|
1833
|
+
if (warning) this.logger.warn(warning);
|
|
1834
|
+
}
|
|
1835
|
+
const existingFts = this._shadowTableSql(tableName, "fts");
|
|
1836
|
+
for (const name of existingFts.keys()) if (!desiredFtsTables.has(name)) await apply({
|
|
1797
1837
|
name,
|
|
1798
1838
|
action: "drop"
|
|
1799
1839
|
}, name, () => this._dropFtsTable(name));
|
|
1800
|
-
const existingSet = new Set(existingFts);
|
|
1801
1840
|
for (const index of ftIndexes) {
|
|
1802
1841
|
const ftsTable = this._ftsTableName(index.name);
|
|
1803
|
-
|
|
1842
|
+
const liveSql = existingFts.get(ftsTable);
|
|
1843
|
+
if (liveSql === void 0) await apply({
|
|
1804
1844
|
name: ftsTable,
|
|
1805
1845
|
action: "add"
|
|
1806
1846
|
}, ftsTable, () => this._createFtsTable(tableName, ftsTable, index));
|
|
1807
|
-
else if (!this._ftsColumnsMatch(ftsTable, index)) await apply({
|
|
1847
|
+
else if (!this._ftsColumnsMatch(ftsTable, index) || !ftsTokenizerMatch(liveSql, index)) await apply({
|
|
1808
1848
|
name: ftsTable,
|
|
1809
1849
|
action: "rebuild"
|
|
1810
1850
|
}, ftsTable, () => {
|
|
@@ -1824,7 +1864,9 @@ var SqliteAdapter = class SqliteAdapter extends BaseDbAdapter {
|
|
|
1824
1864
|
_createFtsTable(tableName, ftsTable, index) {
|
|
1825
1865
|
const fields = splitFulltextFields(index).text;
|
|
1826
1866
|
const fieldList = fields.map((f) => `"${esc(f.name)}"`).join(", ");
|
|
1827
|
-
const
|
|
1867
|
+
const tokenizer = ftsTokenizer(index);
|
|
1868
|
+
const tokenize = tokenizer ? `, tokenize='${tokenizer}'` : "";
|
|
1869
|
+
const createSql = `CREATE VIRTUAL TABLE IF NOT EXISTS "${esc(ftsTable)}" USING fts5(${fieldList}, content='${tableName.replace(/'/g, "''")}', content_rowid='rowid'${tokenize})`;
|
|
1828
1870
|
this._log(createSql);
|
|
1829
1871
|
this.driver.exec(createSql);
|
|
1830
1872
|
const newFields = fields.map((f) => `new."${esc(f.name)}"`).join(", ");
|
|
@@ -1856,7 +1898,11 @@ var SqliteAdapter = class SqliteAdapter extends BaseDbAdapter {
|
|
|
1856
1898
|
}
|
|
1857
1899
|
/** Lists FTS5/vec0 shadow virtual tables for a content table. */
|
|
1858
1900
|
_listShadowTables(tableName, kind) {
|
|
1859
|
-
return this.
|
|
1901
|
+
return [...this._shadowTableSql(tableName, kind).keys()];
|
|
1902
|
+
}
|
|
1903
|
+
/** {@link _listShadowTables} with each virtual table's `CREATE` statement. */
|
|
1904
|
+
_shadowTableSql(tableName, kind) {
|
|
1905
|
+
return new Map(this.driver.all(`SELECT name, sql FROM sqlite_master WHERE type='table' AND name LIKE ?`, [`${tableName}__${kind}__%`]).filter((r) => r.sql.startsWith("CREATE VIRTUAL TABLE")).map((r) => [r.name, r.sql]));
|
|
1860
1906
|
}
|
|
1861
1907
|
/** Drops all FTS virtual tables and triggers for a content table. */
|
|
1862
1908
|
_dropAllFtsTables(tableName) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/db-sqlite",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.169",
|
|
4
4
|
"description": "SQLite adapter for @atscript/db with swappable driver support.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"atscript",
|
|
@@ -35,22 +35,24 @@
|
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public"
|
|
37
37
|
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@atscript/db-sql-tools": "^0.1.169"
|
|
40
|
+
},
|
|
38
41
|
"devDependencies": {
|
|
39
|
-
"@atscript/core": "^0.2.
|
|
40
|
-
"@atscript/typescript": "^0.2.
|
|
42
|
+
"@atscript/core": "^0.2.4",
|
|
43
|
+
"@atscript/typescript": "^0.2.4",
|
|
41
44
|
"@types/better-sqlite3": "^7.6.13",
|
|
42
|
-
"@uniqu/core": "^0.
|
|
45
|
+
"@uniqu/core": "^0.2.0",
|
|
43
46
|
"better-sqlite3": "^12.6.2",
|
|
44
47
|
"sqlite-vec": "^0.1.9",
|
|
45
|
-
"unplugin-atscript": "^0.2.
|
|
48
|
+
"unplugin-atscript": "^0.2.4"
|
|
46
49
|
},
|
|
47
50
|
"peerDependencies": {
|
|
48
|
-
"@atscript/core": "^0.2.
|
|
49
|
-
"@atscript/typescript": "^0.2.
|
|
50
|
-
"@uniqu/core": "^0.
|
|
51
|
+
"@atscript/core": "^0.2.4",
|
|
52
|
+
"@atscript/typescript": "^0.2.4",
|
|
53
|
+
"@uniqu/core": "^0.2.0",
|
|
51
54
|
"better-sqlite3": ">=11.0.0",
|
|
52
|
-
"@atscript/db": "^0.1.
|
|
53
|
-
"@atscript/db-sql-tools": "^0.1.167"
|
|
55
|
+
"@atscript/db": "^0.1.169"
|
|
54
56
|
},
|
|
55
57
|
"peerDependenciesMeta": {
|
|
56
58
|
"better-sqlite3": {
|