All files SQLite.ts

57.69% Statements 15/26
33.33% Branches 2/6
12.5% Functions 1/8
57.69% Lines 15/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80                                1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x   1x   1x                                                                                    
/*
   Copyright 2022 Total Pave Inc.
 
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
 
import {Database} from './Database';
import {IError} from './IError';
import {SQLiteInteger} from './SQLiteTypes';
 
enum OpenFlags {
    READ_ONLY       = 0x00000001,
    READ_WRITE      = 0x00000002,
    CREATE          = 0x00000004,
    URI             = 0x00000040,
    MEMORY          = 0x00000080,
    NO_MUTEX        = 0x00008000,
    FULL_MUTEX      = 0x00010000,
    SHARED_CACHE    = 0x00020000,
    PRIVATE_CACHE   = 0x00040000,
    NO_FOLLOW       = 0x01000000
};
 
export const SERVICE_NAME: string = "TPSQLite";
 
const DEFAULT_BUSY_TIMEOUT: number = 60000; // 60 seconds
 
export class SQLite {
    private static async $exec<TArgs extends Array<any> = Array<any>, TResponse = any>(method: string, vargs: TArgs): Promise<TResponse> {
        return new Promise<TResponse>((resolve, reject) => {
            cordova.exec((response) => {
                resolve(response);
            }, (error: IError) => {
                reject(error);
            }, SERVICE_NAME, method, vargs);
        });
    }
 
    public static async open(path: string, writeAccess: boolean, busyTimeout: SQLiteInteger = DEFAULT_BUSY_TIMEOUT): Promise<Database> {
        Iif (path.indexOf("file://") !== 0) {
            throw new Error("Database path must start with file://");
        }
 
        let dbHandle: string = (
            await this.$exec<
                [string, number, SQLiteInteger],
                {dbHandle: string}
            >(
                'open',
                [
                    path,
                    writeAccess ? OpenFlags.CREATE | OpenFlags.READ_WRITE : OpenFlags.READ_ONLY,
                    busyTimeout
                ]
            )
        ).dbHandle;
        return new Database(dbHandle);
    }
 
    public static async close(db: Database): Promise<void> {
        await this.$exec<[{dbHandle: string}], void>('close', [ { dbHandle: db.getHandle() } ]);
        db.__close();
    }
 
    // Incomplete API
    // public static async getLogs(): Promise<Array<String>> {
    //     return await this.$exec('getLogs', []);
    // }
};