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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x |
import {
SQLiteArray,
SQLiteListArgs,
SQLiteParams,
SQLiteType,
SQLiteValue,
SQLiteBlob,
SQLiteDouble,
SQLiteInteger,
SQLiteNull,
SQLiteText
} from './SQLiteTypes';
import {SQLiteParamValueConverter} from './SQLiteParamValueConverter';
/**
* @since v0.2.0
*/
export class SQLiteParamAdapter {
public constructor() {}
/**
* Attempt to adapt an arbitrary data type to a supported SQLite Type
*
* There is a set of default adaptions for common types including
* null, strings and numbers which are all passthrough types. There is also
* default adaptions for booleans, Dates and blobs, where booleans are
* convered to an integer of 1 or 0, Dates are converted to an ISO string,
* and blobs are adapted to a special JSON format to represent bytearrays.
*
* If required, any of the _adapt* methods could be overwritten to change
* the behaviour however it would be recommended to keep the defaults.
*
* If you are importing custom types, override the `_adapt` method to handle
* them.
*
* @param v
* @returns
*/
private async $adapt(v: unknown): Promise<SQLiteType> {
let out: SQLiteType;
Iif (v === null) {
out = this._adaptNull(<null>v);
}
else Iif (typeof v === 'number') {
out = this._adaptNumber(v);
}
else Iif (typeof v === 'string') {
out = this._adaptString(v);
}
else Iif (typeof v === 'boolean') {
out = this._adaptBoolean(v);
}
else if (v instanceof Date) {
out = this._adaptDate(v);
}
else Eif (v instanceof Blob) {
out = await this._adaptBlob(v);
}
else if (v instanceof ArrayBuffer) {
out = await this._adaptArrayBuffer(v);
}
else if ((v instanceof Int8Array) || (v instanceof Uint8Array)) {
out = await this._adaptInt8TypedArray(v);
}
else {
out = await this._adapt(v);
}
return out;
}
protected async _adaptInt8TypedArray(v: Int8Array | Uint8Array): Promise<SQLiteBlob> {
return await SQLiteParamValueConverter.int8OrUint8ToSQLiteBlob(v);
}
protected async _adaptArrayBuffer(v: ArrayBuffer): Promise<SQLiteBlob> {
return await SQLiteParamValueConverter.arrayBufferToSQLiteBlob(v);
}
protected async _adaptBlob(v: Blob): Promise<SQLiteBlob> {
return await SQLiteParamValueConverter.blobToSQLiteBlob(v);
}
protected _adaptDate(v: Date): SQLiteText {
if (v.toString() === 'Invalid Date') {
throw new Error('Invalid Date');
}
return SQLiteParamValueConverter.dateToText(v);
}
protected _adaptBoolean(v: boolean): SQLiteInteger {
return SQLiteParamValueConverter.booleanToInteger(v);
}
protected _adaptString(v: string): SQLiteText {
return v;
}
protected _adaptNumber(v: number): SQLiteDouble | SQLiteInteger {
return v;
}
protected _adaptNull(v: null): SQLiteNull {
return null;
}
protected async _adapt(v: unknown): Promise<SQLiteType> {
throw new Error('Hit an unknown type. This is an unsupported operation. To support custom types, extend SQLiteParamAdapter and implement the _adapt method.');
}
public async processArray(input: unknown[][]): Promise<SQLiteListArgs> {
// any cause we need to match the array structure...
let out: SQLiteListArgs = [];
for (let i: number = 0; i < input.length; i++){
let row: unknown[] = input[i];
let outRow: SQLiteArray = [];
for (let j: number = 0; j < row.length; j++) {
let v: unknown = row[j];
outRow.push(await this.$adapt(v));
}
out.push(outRow);
}
return out;
}
public async processKWargs(input: Record<string, unknown>): Promise<SQLiteParams> {
let out: SQLiteParams = {};
for (let i in input) {
let inValue: unknown = input[i];
Iif (inValue === undefined) {
// skip undefined values, don't add it to the param object.
continue;
}
let outValue: SQLiteValue;
Iif (inValue instanceof Array) {
Iif (inValue.length === 0) {
// skip empty arrays, don't add it to param object
continue;
}
outValue = [];
for (let j: number = 0; j < inValue.length; j++) {
outValue.push(await this.$adapt(inValue[j]));
}
}
else {
outValue = await this.$adapt(input[i]);
}
out[i] = outValue;
}
return out;
}
}
|