2023-02-21 13:23:07 -06:00
|
|
|
import { FilterOperators } from '../enums/FilterOperators.js';
|
2023-02-12 15:52:47 -06:00
|
|
|
|
|
|
|
|
export class QueryFilter {
|
|
|
|
|
public fieldId: number;
|
|
|
|
|
public operator: FilterOperators;
|
|
|
|
|
public value: string | number | Date | null;
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
fieldId: number,
|
|
|
|
|
operator: FilterOperators,
|
|
|
|
|
value: string | number | Date | null
|
|
|
|
|
) {
|
|
|
|
|
if (
|
|
|
|
|
value === null &&
|
|
|
|
|
operator !== FilterOperators.IsNull &&
|
|
|
|
|
operator !== FilterOperators.NotNull
|
|
|
|
|
) {
|
|
|
|
|
throw new Error('Value cannot be null for this operator');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.fieldId = fieldId;
|
|
|
|
|
this.operator = operator;
|
|
|
|
|
this.value = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public toString(): string {
|
|
|
|
|
if (this.value == null) {
|
|
|
|
|
return `${this.fieldId} ${this.operator}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.value instanceof Date) {
|
|
|
|
|
return `${this.fieldId} ${
|
|
|
|
|
this.operator
|
|
|
|
|
} datetime'${this.value.toISOString()}'`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (typeof this.value === 'string') {
|
|
|
|
|
return `${this.fieldId} ${this.operator} '${this.value}'`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return `${this.fieldId} ${this.operator} ${this.value}`;
|
|
|
|
|
}
|
|
|
|
|
}
|