diff --git a/src/app/services/sqlite.ts b/src/app/services/sqlite.ts index 647f126..755f1e5 100644 --- a/src/app/services/sqlite.ts +++ b/src/app/services/sqlite.ts @@ -1,14 +1,34 @@ import { inject, Injectable } from '@angular/core'; import { WebSqlite } from 'angular-web-sqlite'; +import { BatchOp } from '../types/sqlite.type'; @Injectable({ providedIn: 'root', }) export class Sqlite { - private readonly webSqlite = inject(WebSqlite) + private readonly webSqlite = inject(WebSqlite); initializeDatabase(dbName: string) { return this.webSqlite.init(dbName); } + async executeQuery(sql: string, params: any[] = []) { + const result = await this.webSqlite.executeSql(sql, params); + return result; + } + + async batchSqlOperations(ops: BatchOp[]) { + const result = await this.webSqlite.batchSql(ops); + return result; + } + + async dropAllTables() { + const tables: { rows: { name: string }[] } = await this.executeQuery( + "SELECT * FROM sqlite_master WHERE type='table';" + ); + const dropQueries: BatchOp[] = tables.rows + .filter((r) => !r.name.startsWith('sqlite_')) + .map((table) => [`DROP TABLE ${table.name};`, []]); + return this.batchSqlOperations(dropQueries); + } } diff --git a/src/app/types/sqlite.type.ts b/src/app/types/sqlite.type.ts new file mode 100644 index 0000000..d46a63b --- /dev/null +++ b/src/app/types/sqlite.type.ts @@ -0,0 +1,2 @@ +export type QueryResult = { rows: T[]}; +export type BatchOp = [string, any[]]; \ No newline at end of file