1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 export function joinFilters(filters0?: string, filters1?: string) {
6 return [filters0, filters1].filter(s => s).join(",");
9 export class FilterBuilder {
10 constructor(private filters = "") { }
12 public addEqual(field: string, value?: string | boolean | null, resourcePrefix?: string) {
13 return this.addCondition(field, "=", value, "", "", resourcePrefix);
16 public addDistinct(field: string, value?: string | boolean | null, resourcePrefix?: string) {
17 return this.addCondition(field, "!=", value, "", "", resourcePrefix);
20 public addLike(field: string, value?: string, resourcePrefix?: string) {
21 return this.addCondition(field, "like", value, "%", "%", resourcePrefix);
24 public addILike(field: string, value?: string, resourcePrefix?: string) {
25 return this.addCondition(field, "ilike", value, "%", "%", resourcePrefix);
28 public addIsA(field: string, value?: string | string[], resourcePrefix?: string) {
29 return this.addCondition(field, "is_a", value, "", "", resourcePrefix);
32 public addIn(field: string, value?: string | string[], resourcePrefix?: string) {
33 return this.addCondition(field, "in", value, "", "", resourcePrefix);
36 public addNotIn(field: string, value?: string | string[], resourcePrefix?: string) {
37 return this.addCondition(field, "not in", value, "", "", resourcePrefix);
40 public addGt(field: string, value?: string, resourcePrefix?: string) {
41 return this.addCondition(field, ">", value, "", "", resourcePrefix);
44 public addGte(field: string, value?: string, resourcePrefix?: string) {
45 return this.addCondition(field, ">=", value, "", "", resourcePrefix);
48 public addLt(field: string, value?: string, resourcePrefix?: string) {
49 return this.addCondition(field, "<", value, "", "", resourcePrefix);
52 public addLte(field: string, value?: string, resourcePrefix?: string) {
53 return this.addCondition(field, "<=", value, "", "", resourcePrefix);
56 public addExists(value?: string, resourcePrefix?: string) {
57 return this.addCondition("properties", "exists", value, "", "", resourcePrefix);
60 public addFullTextSearch(value: string) {
61 const terms = value.trim().split(/(\s+)/);
62 terms.forEach(term => {
64 this.addCondition("any", "ilike", term, "%", "%");
74 private addCondition(field: string, cond: string, value?: string | string[] | boolean | null, prefix: string = "", postfix: string = "", resourcePrefix?: string) {
75 if (value !== undefined) {
76 if (typeof value === "string") {
77 value = `"${prefix}${value}${postfix}"`;
78 } else if (Array.isArray(value)) {
79 value = `["${value.join(`","`)}"]`;
80 } else if (value !== null) {
81 value = value ? "true" : "false";
84 const resPrefix = resourcePrefix
85 ? resourcePrefix + "."
88 this.filters += `${this.filters ? "," : ""}["${resPrefix}${field}","${cond}",${value}]`;