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 | 139x 31x 685x 159x 159x 159x 159x 685x 445x 240x 31x 1x 33x 30x 30x 30x 30x 30x 562x 562x 562x 7x 7x 30x | import { AbstractDataType, type DataTypeInput } from './abstract-data-type.ts';
import { RangeType } from './range-type.ts';
const MULTIRANGE_SQL_TYPES: Readonly<Record<string, string>> = {
INT4RANGE: 'INT4MULTIRANGE',
INT8RANGE: 'INT8MULTIRANGE',
NUMRANGE: 'NUMMULTIRANGE',
TSRANGE: 'TSMULTIRANGE',
TSTZRANGE: 'TSTZMULTIRANGE',
DATERANGE: 'DATEMULTIRANGE',
};
/**
* Multirange type (`DataTypes.MULTIRANGE(DataTypes.INTEGER)` → `INT4MULTIRANGE`, PostgreSQL 14+);
* values are arrays of ranges, each a `[lower, upper]` pair like {@link RangeType} values.
*
* @internal
*/
export class MultirangeType extends AbstractDataType {
/** Converts a driver multirange literal into an array of ranges (`[]` for `{}`). */
readonly parse = (value: unknown): unknown => this.#parse(value);
/** Converts an array of ranges into a multirange literal. */
readonly serialize = (value: unknown): unknown => this.#serialize(value);
readonly #range: RangeType;
/**
* @param subtype - Element type: `INTEGER`, `BIGINT`, `DECIMAL`, `TIMESTAMP`, `DATE` or `DATEONLY`.
* @throws Error for any other element type.
*/
constructor(subtype: DataTypeInput) {
const range = new RangeType(subtype);
super('MULTIRANGE', MULTIRANGE_SQL_TYPES[range.sqlType] ?? range.sqlType);
this.#range = range;
Object.freeze(this);
}
#serialize(value: unknown): unknown {
if (!Array.isArray(value)) {
return value;
}
return `{${value.map((range) => String(this.#range.serialize(range))).join(',')}}`;
}
#parse(value: unknown): unknown {
if (typeof value !== 'string' || !value.startsWith('{')) {
return value;
}
return MultirangeType.#split(value.slice(1, -1)).map((text) => this.#range.parse(text));
}
/** Splits at the commas that are outside brackets and quotes. */
static #split(body: string): string[] {
const parts: string[] = [];
let depth = 0;
let quoted = false;
let start = 0;
for (const [index, char] of [...body].entries()) {
quoted = char === '"' ? !quoted : quoted;
depth += quoted ? 0 : '[('.includes(char) ? 1 : '])'.includes(char) ? -1 : 0;
if (char === ',' && depth === 0 && !quoted) {
parts.push(body.slice(start, index));
start = index + 1;
}
}
return body === '' ? [] : [...parts, body.slice(start)];
}
}
|