Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d434c60

Browse filesBrowse files
committed
fix(lib/database): support better-bibtex v6.7.128+
close #236
1 parent 3576461 commit d434c60
Copy full SHA for d434c60

14 files changed

+345-121Lines changed: 345 additions & 121 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎app/obsidian/src/services/zotero-db/auto-refresh/service.ts‎

Copy file name to clipboardExpand all lines: app/obsidian/src/services/zotero-db/auto-refresh/service.ts
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ export default class DatabaseWatcher extends Service {
6161
this.settings.zoteroDbPath,
6262
this.onDatabaseUpdate("main"),
6363
);
64-
if (await this.api.checkDbStatus("bbt")) {
64+
const loadStatus = await this.api.getLoadStatus();
65+
if (loadStatus.bbt) {
6566
this.#watcher.bbt = watch(
66-
this.settings.betterBibTexDbPath,
67+
loadStatus.bbtVersion === "v0"
68+
? this.settings.bbtSearchDbPath
69+
: this.settings.bbtMainDbPath,
6770
this.onDatabaseUpdate("bbt"),
6871
);
6972
}
Collapse file

‎app/obsidian/src/services/zotero-db/connector/service.ts‎

Copy file name to clipboardExpand all lines: app/obsidian/src/services/zotero-db/connector/service.ts
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ export default class Database extends Service {
206206
async #openDbConn() {
207207
const [paths, opts] = this.settings.dbConnParams;
208208

209-
const [mainOpened, bbtOpened] = await this.api.openDb(paths, opts);
210-
if (!bbtOpened) {
209+
const { main, bbtMain, bbtSearch } = await this.api.openDb(paths, opts);
210+
if (!bbtMain || bbtSearch === false) {
211211
log.debug("Failed to open Better BibTeX database, skipping...");
212212
}
213-
if (!mainOpened) {
213+
if (!main) {
214214
throw new Error("Failed to init ZoteroDB");
215215
}
216216
}
Collapse file

‎app/obsidian/src/setting-tab/connect/Database.tsx‎

Copy file name to clipboardExpand all lines: app/obsidian/src/setting-tab/connect/Database.tsx
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,6 @@ function useApplyDataDir(updateStatus: () => void) {
9090
function useDatabasePath(target: "main" | "bbt") {
9191
const service = useContext(SettingTabCtx).settings;
9292
const value =
93-
target === "main" ? service.zoteroDbPath : service.betterBibTexDbPath;
93+
target === "main" ? service.zoteroDbPath : service.bbtMainDbPath;
9494
return value;
9595
}
Collapse file

‎app/obsidian/src/setting-tab/connect/useDatabaseStatus.tsx‎

Copy file name to clipboardExpand all lines: app/obsidian/src/setting-tab/connect/useDatabaseStatus.tsx
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { SettingTabCtx, useRefreshAsync } from "../common";
44
export function useDatabaseStatus(target: "zotero" | "bbt") {
55
const { database } = useContext(SettingTabCtx);
66
const [promise, refresh] = useRefreshAsync(
7-
() => database.api.checkDbStatus(target),
7+
() =>
8+
database.api
9+
.getLoadStatus()
10+
.then((s) => (target === "zotero" ? s.main : s.bbt)),
811
[target],
912
);
1013

Collapse file

‎app/obsidian/src/settings/base.ts‎

Copy file name to clipboardExpand all lines: app/obsidian/src/settings/base.ts
+10-2Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,25 @@ export class SettingsService extends _SettingsService<Settings> {
5454
return join(this.current?.zoteroDataDir, "zotero.sqlite");
5555
}
5656

57-
@calc get betterBibTexDbPath(): string {
57+
@calc get bbtSearchDbPath(): string {
5858
return join(this.current?.zoteroDataDir, "better-bibtex-search.sqlite");
5959
}
6060

61+
@calc get bbtMainDbPath(): string {
62+
return join(this.current?.zoteroDataDir, "better-bibtex.sqlite");
63+
}
64+
6165
@calc get zoteroCacheDirPath(): string {
6266
return join(this.current?.zoteroDataDir, "cache");
6367
}
6468

6569
@calc get dbConnParams(): [paths: DatabasePaths, opts: DatabaseOptions] {
6670
return [
67-
{ zotero: this.zoteroDbPath, bbt: this.betterBibTexDbPath },
71+
{
72+
zotero: this.zoteroDbPath,
73+
bbtSearch: this.bbtSearchDbPath,
74+
bbtMain: this.bbtMainDbPath,
75+
},
6876
{ nativeBinding: this.nativeBinding },
6977
];
7078
}
Collapse file

‎lib/database/src/api.ts‎

Copy file name to clipboardExpand all lines: lib/database/src/api.ts
+20-10Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,37 @@ export type QueryOption = DocumentSearchOptions<false>;
1111
export type { SimpleDocumentSearchResultSetUnit } from "flexsearch";
1212

1313
export interface DatabaseOptions {
14+
/** The path to the SQLite nodejs native binding binary. */
1415
nativeBinding: string;
1516
}
1617

1718
export interface DatabasePaths {
1819
zotero: string;
19-
bbt: string;
20+
/** better-bibtex-search.sqlite */
21+
bbtSearch: string;
22+
/** better-bibtex.sqlite */
23+
bbtMain: string;
24+
}
25+
export interface LoadStatus extends BBTLoadStatus {
26+
/** zotero.sqlite load status */
27+
main: boolean;
28+
}
29+
export interface BBTLoadStatus {
30+
/** better-bibtex.sqlite load status */
31+
bbtMain: boolean;
32+
/**
33+
* better-bibtex-search.sqlite load status
34+
* null => version after migration, no need to load
35+
*/
36+
bbtSearch: boolean | null;
2037
}
2138

2239
export interface DbWorkerAPI {
2340
setLoglevel(level: LogLevel): void;
24-
/**
25-
* open new database connection or refresh existing if no param passed in
26-
* @returns return true if successful
27-
*/
28-
openDb(
29-
paths: DatabasePaths,
30-
opts: DatabaseOptions,
31-
): [mainDbResult: boolean, bbtDbResult: boolean];
41+
openDb(paths: DatabasePaths, opts: DatabaseOptions): LoadStatus;
3242

3343
isUpToDate(): boolean | null;
34-
checkDbStatus(name: "zotero" | "bbt"): boolean;
44+
getLoadStatus(): { main: boolean; bbt: boolean; bbtVersion: "v0" | "v1" };
3545

3646
/* start index for library, need to be called before query and after openDb */
3747
initIndex(libraryID: number): void;
Collapse file

‎lib/database/src/index.ts‎

Copy file name to clipboardExpand all lines: lib/database/src/index.ts
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ export { AnnotByParent } from "./sql/annotations/by-parent.js";
33
export { NoteByKeys } from "./sql/notes/by-keys.js";
44
export { NoteByParent } from "./sql/notes/by-parent.js";
55
export { Attachements } from "./sql/attachments.js";
6-
export { BibtexGetCitekey } from "./sql/bibtex/get-citekey.js";
7-
export { BibtexGetId } from "./sql/bibtex/get-id.js";
6+
export {
7+
BibtexGetCitekeyV0,
8+
BibtexGetCitekeyV1,
9+
} from "./sql/bibtex/get-citekey.js";
10+
export { BibtexGetIdV0, BibtexGetIdV1 } from "./sql/bibtex/get-id.js";
11+
export { BBT_MAIN_DB_NAME, BBT_SEARCH_DB_NAME } from "./sql/bibtex/base.js";
812
export { CreatorsFull } from "./sql/creator/full.js";
913
export { Creators } from "./sql/creator/part.js";
1014
export { ItemFieldsFull } from "./sql/item-fields/full.js";
Collapse file
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// can't be alias, because it's hard-coded in the database schema
2+
export const BBT_MAIN_DB_NAME = "betterbibtex";
3+
export const BBT_SEARCH_DB_NAME = "bbts";
Collapse file

‎lib/database/src/sql/bibtex/get-citekey.ts‎

Copy file name to clipboardExpand all lines: lib/database/src/sql/bibtex/get-citekey.ts
+48-13Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
import type { Transaction } from "@aidenlx/better-sqlite3";
22
import type { IDLibID } from "../../utils/index.js";
33
import { PreparedBase } from "../../utils/index.js";
4-
5-
const query = `--sql
6-
SELECT
7-
citekey
8-
FROM
9-
citekeys
10-
WHERE
11-
itemID = $itemID
12-
AND (libraryID IS NULL OR libraryID = $libId)
13-
`;
4+
import { BBT_MAIN_DB_NAME, BBT_SEARCH_DB_NAME } from "./base.js";
145

156
interface InputSql {
167
itemID: number;
@@ -26,7 +17,51 @@ interface OutputSql {
2617
}
2718
type Output = Record<number, string>;
2819

29-
export class BibtexGetCitekey extends PreparedBase<
20+
const sqlMain = `--sql
21+
SELECT
22+
citationkey as citekey
23+
FROM
24+
${BBT_MAIN_DB_NAME}.citationkey
25+
WHERE
26+
itemID = $itemID
27+
AND (libraryID IS NULL OR libraryID = $libId)
28+
`;
29+
30+
const sqlSearch = `--sql
31+
SELECT
32+
citekey
33+
FROM
34+
${BBT_SEARCH_DB_NAME}.citekeys
35+
WHERE
36+
itemID = $itemID
37+
AND (libraryID IS NULL OR libraryID = $libId)
38+
`;
39+
40+
export class BibtexGetCitekeyV1 extends PreparedBase<
41+
InputSql,
42+
OutputSql,
43+
Output
44+
> {
45+
trxFunc = (items: IDLibID[]) =>
46+
items.reduce((rec, [itemID, libId]) => {
47+
const result = this.get({ itemID, libId });
48+
if (result) {
49+
rec[itemID] = result.citekey;
50+
}
51+
return rec;
52+
}, {} as Output);
53+
trx: Transaction = this.database.transaction(this.trxFunc);
54+
55+
sql(): string {
56+
return sqlMain;
57+
}
58+
59+
query(input: Input): Output {
60+
return (this.trx as this["trxFunc"])(input.items);
61+
}
62+
}
63+
64+
export class BibtexGetCitekeyV0 extends PreparedBase<
3065
InputSql,
3166
OutputSql,
3267
Output
@@ -42,10 +77,10 @@ export class BibtexGetCitekey extends PreparedBase<
4277
trx: Transaction = this.database.transaction(this.trxFunc);
4378

4479
sql(): string {
45-
return query;
80+
return sqlSearch;
4681
}
4782

4883
query(input: Input): Output {
49-
return (this.trx as BibtexGetCitekey["trxFunc"])(input.items);
84+
return (this.trx as this["trxFunc"])(input.items);
5085
}
5186
}
Collapse file

‎lib/database/src/sql/bibtex/get-id.ts‎

Copy file name to clipboardExpand all lines: lib/database/src/sql/bibtex/get-id.ts
+33-5Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import type { Transaction } from "@aidenlx/better-sqlite3";
22
import { PreparedBase } from "../../utils/index.js";
3+
import { BBT_MAIN_DB_NAME, BBT_SEARCH_DB_NAME } from "./base.js";
34

4-
const query = `--sql
5+
const sqlMain = `--sql
56
SELECT
67
itemID
78
FROM
8-
citekeys
9+
${BBT_MAIN_DB_NAME}.citationkey
10+
WHERE
11+
citationkey = $citekey
12+
`;
13+
14+
const sqlSearch = `--sql
15+
SELECT
16+
itemID
17+
FROM
18+
${BBT_SEARCH_DB_NAME}.citekeys
919
WHERE
1020
citekey = $citekey
1121
`;
@@ -23,7 +33,25 @@ interface OutputSql {
2333
}
2434
type Output = Record<string, number>;
2535

26-
export class BibtexGetId extends PreparedBase<InputSql, OutputSql, Output> {
36+
export class BibtexGetIdV1 extends PreparedBase<InputSql, OutputSql, Output> {
37+
trxFunc = (citekeys: string[]) =>
38+
citekeys.reduce((rec, citekey) => {
39+
const result = this.get({ citekey });
40+
rec[citekey] = result?.itemID ?? -1;
41+
return rec;
42+
}, {} as Output);
43+
trx: Transaction = this.database.transaction(this.trxFunc);
44+
45+
sql(): string {
46+
return sqlMain;
47+
}
48+
49+
query(input: Input): Output {
50+
return (this.trx as this["trxFunc"])(input.citekeys);
51+
}
52+
}
53+
54+
export class BibtexGetIdV0 extends PreparedBase<InputSql, OutputSql, Output> {
2755
trxFunc = (citekeys: string[]) =>
2856
citekeys.reduce((rec, citekey) => {
2957
const result = this.get({ citekey });
@@ -33,10 +61,10 @@ export class BibtexGetId extends PreparedBase<InputSql, OutputSql, Output> {
3361
trx: Transaction = this.database.transaction(this.trxFunc);
3462

3563
sql(): string {
36-
return query;
64+
return sqlSearch;
3765
}
3866

3967
query(input: Input): Output {
40-
return (this.trx as BibtexGetId["trxFunc"])(input.citekeys);
68+
return (this.trx as this["trxFunc"])(input.citekeys);
4169
}
4270
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.