import { Adapter , ConnectionOptions , DatabaseResult } from "./adapter.ts" ; import { SqliteDB } from "../../deps.ts" ; import type { DatabaseDialect } from "../connect.ts" ; /**
 * SQLite database adapter
 */ export class SqliteAdapter extends Adapter { public dialect : DatabaseDialect = "sqlite" ; public get lastInsertedId ( ) : number { return this . client ?. lastInsertRowId || 0 ; } /**
     * Database file location
     */ private fileLocation : string ; /**
     * SQLite library database instance
     */ private client ? : SqliteDB ; constructor ( options ) { super ( ) ; if ( options . database ) { this . fileLocation = options . database ; } else { throw new Error ( "SQLite adapter needs 'database' property!" ) ; } } public connect ( ) { return new Promise ( ( resolve , reject ) => { this . client = new SqliteDB ( this . fileLocation ) ; resolve ( ) ; } ) ; } public disconnect ( ) { return new Promise ( ( resolve , reject ) => { this . client ! . close ( ) ; resolve ( ) ; } ) ; } public query ( query , values ? ) { return new Promise ( ( resolve , reject ) => { let result ; if ( Array . isArray ( values ) && values . length >= 1 ) { result = this . client ! . query ( query , values ) ; } else { result = this . client ! . query ( query ) ; } const records = [ ] ; let columns = [ ] ; try { columns = result . columns ( ) . map ( ( column ) => column . name ) ; for ( const record of result ) { let data = { } ; ( record as any [ ] ) . forEach ( ( value , index ) => { const fieldName = columns [ index ] ; data [ fieldName ] = value ; } ) ; records . push ( data ) ; } resolve ( records ) ; } catch { resolve ( [ ] ) ; } } ) ; } }