All files SQLiteParamValueConverter.ts

6.89% Statements 2/29
0% Branches 0/5
0% Functions 0/13
7.14% Lines 2/28

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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96        2x   2x                                                                                                                                                                                  
 
import { SQLiteInteger, SQLiteDouble, SQLiteNull, SQLiteText, SQLiteBlob } from './SQLiteTypes';
 
type TInternalTypedArray =  Uint8Array  | Int8Array;
const NativeFileReader = window.FileReader; // Hold a reference to FileReader before plugins like cordova-plugin-file overwrites it.
 
export class SQLiteParamValueConverter {
    public static numberToInteger(value: number): SQLiteInteger {
        return value;
    }
 
    public static numberToDouble(value: number): SQLiteDouble {
        return value;
    }
 
    public static booleanToInteger(value: boolean): SQLiteInteger {
        return value ? 1 : 0;
    }
 
    public static nullOrUndefinedToSQLiteNull(value: null | undefined): SQLiteNull {
        return null;
    }
 
    public static dateToText(value: Date): SQLiteText {
        return value.toISOString();
    }
 
    public static stringToText(value: string): SQLiteText {
        return value;
    }
 
    public static async blobToSQLiteBlob(value: Blob): Promise<SQLiteBlob> {
        return {
            type: 'bytearray',
            value: SQLiteParamValueConverter.$normalizeBufferedArray(new Uint8Array(await SQLiteParamValueConverter.$getArrayBufferFromBlob(value)))
        };
    }
 
    public static async arrayBufferToSQLiteBlob(value: ArrayBuffer): Promise<SQLiteBlob> {
        return {
            type: 'bytearray',
            value: SQLiteParamValueConverter.$normalizeBufferedArray(new Uint8Array(value))
        };
    }
 
    public static async int8OrUint8ToSQLiteBlob(value: Uint8Array | Int8Array): Promise<SQLiteBlob> {
        return {
            type: 'bytearray',
            value: SQLiteParamValueConverter.$normalizeBufferedArray(value)
        };
    }
 
    private static $normalizeBufferedArray(ta: TInternalTypedArray): Array<number> {
        let out: Array<number> = [];
 
        for (let i: number = 0; i < ta.length; i++) {
            out.push(ta[i]);
        }
 
        return out;
    }
 
    private static async $getArrayBufferFromBlob(value: Blob): Promise<ArrayBuffer> {
        let ab: ArrayBuffer;
        if (value.arrayBuffer) {
            ab = await value.arrayBuffer();
        }
        else {
            // If the arrayBuffer method is not available, then
            // we need to read it manually.
            ab = await new Promise<ArrayBuffer>((resolve, reject) => {
                let reader = new NativeFileReader();
 
                // event handlers needs to be manually detached
                // to avoid memory leaks
                // https://developer.mozilla.org/en-US/docs/Web/API/FileReader#events
                const onLoadEnd = () => {
                    reader.removeEventListener('loadend', onLoadEnd);
                    Iif (reader.error) {
                        reject(reader.error);
                        return;
                    }
 
                    resolve(<ArrayBuffer>reader.result);
                };
 
                reader.addEventListener('loadend', onLoadEnd);
 
                reader.readAsArrayBuffer(value);
            });
        }
 
        return ab;
    }
}