import { InanoSQLTable , InanoSQLPlugin , InanoSQLInstance , SQLiteAbstractFns } from "@nano-sql/core/lib/interfaces" ; import { nanoSQLMemoryIndex } from "@nano-sql/core/lib/adapters/memoryIndex" ; import { SQLiteAbstract } from "@nano-sql/core/lib/adapters/webSQL" ; const NSSQLite = require ( "nativescript-sqlite" ) ; export class NativeSQLite extends nanoSQLMemoryIndex { plugin : InanoSQLPlugin = { name : "NativeScript SQLite Adapter" , version : 2.03 } ; nSQL : InanoSQLInstance ; public _id : string ; public _db : any ; public _ai : { [ table ] : number ; } ; public _sqlite : SQLiteAbstractFns ; public _tableConfigs : { [ tableName ] : InanoSQLTable ; } ; public _filename : string ; constructor ( fileName ? ) { super ( false , true ) ; this . _ai = { } ; this . _query = this . _query . bind ( this ) ; this . _filename = fileName || "" ; this . _tableConfigs = { } ; this . _sqlite = SQLiteAbstract ( this . _query , 500 ) ; } connect ( id , complete , error ) { this . _id = id ; new NSSQLite ( this . _filename && this . _filename . length ? this . _filename : id , ( err , db ) => { if ( err ) { error ( err ) ; return ; } this . _db = db ; this . _sqlite . createAI ( ( ) => { this . _db . resultType ( NSSQLite . RESULTSASOBJECT ) ; complete ( ) ; } , error ) ; } ) ; } createTable ( tableName , tableData , complete , error ) { this . _tableConfigs [ tableName ] = tableData ; this . _sqlite . createTable ( tableName , tableData , this . _ai , complete , error ) ; } _query ( allowWrite , sql , args , onRow , complete , error ) { if ( allowWrite ) { this . _db . execSQL ( sql , args , ( err ) => { if ( err ) { error ( err ) ; return ; } complete ( ) ; } ) ; } else { let count = 0 ; this . _db . each ( sql , args , ( err , row ) => { if ( err ) { error ( err ) ; return ; } onRow ( row , count ) ; count ++ ; } , ( err ) => { if ( err ) { error ( err ) ; return ; } complete ( ) ; } ) ; } } dropTable ( table , complete , error ) { this . _sqlite . dropTable ( table , complete , error ) ; } disconnect ( complete , error ) { complete ( ) ; } write ( table , pk , row , complete , error ) { this . _sqlite . write ( this . _tableConfigs [ table ] . pkType , this . _tableConfigs [ table ] . pkCol , table , pk , row , this . _tableConfigs [ table ] . ai , this . _ai , complete , error ) ; } read ( table , pk , complete , error ) { this . _sqlite . read ( table , pk , complete , error ) ; } delete ( table , pk , complete , error ) { this . _sqlite . remove ( table , pk , complete , error ) ; } readMulti ( table , type , offsetOrLow , limitOrHigh , reverse , onRow , complete , error ) { this . _sqlite . readMulti ( table , type , offsetOrLow , limitOrHigh , reverse , onRow , complete , error ) ; } getTableIndex ( table , complete , error ) { this . _sqlite . getIndex ( table , complete , error ) ; } getTableIndexLength ( table , complete , error ) { this . _sqlite . getNumberOfRecords ( table , complete , error ) ; } }