1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as _ from "lodash";
7 export function joinFilters(filters0?: string, filters1?: string) {
8 return [filters0, filters1].filter(s => s).join(",");
11 export class FilterBuilder {
12 constructor(private filters = "") { }
14 public addEqual(field: string, value?: string | boolean | null, resourcePrefix?: string) {
15 return this.addCondition(field, "=", value, "", "", resourcePrefix);
18 public addLike(field: string, value?: string, resourcePrefix?: string) {
19 return this.addCondition(field, "like", value, "%", "%", resourcePrefix);
22 public addILike(field: string, value?: string, resourcePrefix?: string) {
23 return this.addCondition(field, "ilike", value, "%", "%", resourcePrefix);
26 public addIsA(field: string, value?: string | string[], resourcePrefix?: string) {
27 return this.addCondition(field, "is_a", value, "", "", resourcePrefix);
30 public addIn(field: string, value?: string | string[], resourcePrefix?: string) {
31 return this.addCondition(field, "in", value, "", "", resourcePrefix);
34 public addNotIn(field: string, value?: string | string[], resourcePrefix?: string) {
35 return this.addCondition(field, "not in", value, "", "", resourcePrefix);
38 public addGt(field: string, value?: string, resourcePrefix?: string) {
39 return this.addCondition(field, ">", value, "", "", resourcePrefix);
42 public addGte(field: string, value?: string, resourcePrefix?: string) {
43 return this.addCondition(field, ">=", value, "", "", resourcePrefix);
46 public addLt(field: string, value?: string, resourcePrefix?: string) {
47 return this.addCondition(field, "<", value, "", "", resourcePrefix);
50 public addLte(field: string, value?: string, resourcePrefix?: string) {
51 return this.addCondition(field, "<=", value, "", "", resourcePrefix);
54 public addExists(value?: string, resourcePrefix?: string) {
55 return this.addCondition("properties", "exists", value, "", "", resourcePrefix);
58 public addFullTextSearch(value: string) {
59 const terms = value.trim().split(/(\s+)/);
60 terms.forEach(term => {
62 this.addCondition("any", "ilike", term, "%", "%");
72 private addCondition(field: string, cond: string, value?: string | string[] | boolean | null, prefix: string = "", postfix: string = "", resourcePrefix?: string) {
73 if (value !== undefined) {
74 if (typeof value === "string") {
75 value = `"${prefix}${value}${postfix}"`;
76 } else if (Array.isArray(value)) {
77 value = `["${value.join(`","`)}"]`;
78 } else if (value !== null) {
79 value = value ? "true" : "false";
82 const resPrefix = resourcePrefix
83 ? resourcePrefix + "."
86 const fld = field.indexOf('properties.') < 0 ? _.snakeCase(field) : field;
88 this.filters += `${this.filters ? "," : ""}["${resPrefix}${fld}","${cond}",${value}]`;