MCPcopy Create free account
hub / github.com/damms005/devdb-vscode / SqliteEngine

Class SqliteEngine

src/database-engines/sqlite-engine.ts:7–599  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5import { buildWhereClause } from '../services/sql';
6
7export class SqliteEngine implements DatabaseEngine {
8 private db: Database | null = null;
9 public dbPath: string;
10 private inMemory: boolean;
11
12 constructor(dbPath: string = ':memory:') {
13 this.dbPath = dbPath;
14 this.inMemory = dbPath === ':memory:';
15 }
16
17 getType(): KnexClient {
18 return 'sqlite';
19 }
20
21 getFilename(): string {
22 return this.dbPath;
23 }
24
25 getConnection(): CustomSqliteEngine | null {
26 if (this.db) {
27 return this.db as any as CustomSqliteEngine;
28 }
29
30 try {
31 this.db = new Database(this.dbPath, (err) => {
32 if (err) {
33 throw new Error(String(err));
34 }
35 });
36
37 return this.db as any as CustomSqliteEngine;
38 } catch (err) {
39 reportError(`SQLite connection error: ${err}`);
40 return null;
41 }
42
43 }
44
45 async isOkay(): Promise<boolean> {
46 try {
47 const db = this.getConnection();
48
49 if (!db) {
50 return false
51 }
52
53 if (this.inMemory) {
54 return true
55 }
56
57 const stats = await stat(this.dbPath);
58 const fileSizeGB = Math.round((stats.size / (1024 * 1024 * 1024) + Number.EPSILON) * 100) / 100; // Convert to GB and round to 2 decimal places
59 const size = 5;
60
61 if (fileSizeGB > size) {
62 reportError(`Warning: SQLite database file size too big for database integrity check: ${fileSizeGB}GB. Maximum size is ${size}GB. Hence, database integrity not checked. File: ${this.dbPath}`);
63 return true;
64 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected