All files BulkInsertQuery.ts

12.5% Statements 2/16
0% Branches 0/6
0% Functions 0/6
13.33% Lines 2/15

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  1x         1x                                                                                                                        
 
import {Query} from './Query';
import {SQLiteParams, SQLiteType} from './SQLiteTypes';
 
export type TBulkInsertParams = Array<Array<SQLiteType>>;
 
export abstract class BulkInsertQuery<TParams extends TBulkInsertParams> extends Query<TParams, void> {
    private $escapeColumn(column: string): string {
        let pieces: Array<string> = column.split('.');
        Iif (pieces[0].charAt(0) != '`') {
            pieces[0] = `\`${pieces[0]}\``;
        }
        Iif (pieces[1] && pieces[1].charAt(0) != '`') {
            pieces[1] = `\`${pieces[1]}\``;
        }
 
        return pieces[0] + (pieces[1] ? `.${pieces[1]}` : '');
    }
 
    public getQuery(): string {
        let columns: Array<string> = this._getColumns().slice();
        for (let i = 0; i < columns.length; ++i ){
            columns[i] = this.$escapeColumn(columns[i]);
        }
        return `
            INSERT INTO ${this._getTable()}
            (
                ${columns.join(",")}
            )
            :BulkInsertValue
            ${this._getOnConflict()}
        `;
    }
 
    protected _validateParameterNames() { /* No-op because we never have parameter names to validate */ }
 
    protected override _getNativeMethod(): string {
        return 'bulkInsert';
    }
 
    protected async _getParameters(params: TParams): Promise<SQLiteParams> {
        return params;
    }
 
    /**
     * Value will not be sanitized.
     */
    protected abstract _getTable(): string;
    /**
     * Value will not be sanitized.
     */
    protected abstract _getColumns(): Array<string>;
    /**
     * Override this to provide the ON CONFLICT (column) DO UPDATE clause.
     * Value will not be sanitized.
     * 
     * Return Value Example:
     * 
     *     ON CONFLICT (id) DO UPDATE SET
     *         sourceTarget = excluded.sourceTarget,
     *         sourceVersion = excluded.sourceVersion
     */
    protected _getOnConflict(): string {
        return "";
    }
}