X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/ecfa844eeff535078ca59f8baa0915052932338c..2a7fd99c212c33a1ec9911f8529fa5afc59a7bb2:/src/services/api/filter-builder.ts diff --git a/src/services/api/filter-builder.ts b/src/services/api/filter-builder.ts index 06a040e3cc..bb97665a8c 100644 --- a/src/services/api/filter-builder.ts +++ b/src/services/api/filter-builder.ts @@ -2,17 +2,19 @@ // // SPDX-License-Identifier: AGPL-3.0 -import * as _ from "lodash"; - -export function joinFilters(filters0?: string, filters1?: string) { - return [filters0, filters1].filter(s => s).join(","); +export function joinFilters(...filters: string[]) { + return filters.filter(s => s).join(","); } export class FilterBuilder { constructor(private filters = "") { } - public addEqual(field: string, value?: string | boolean, resourcePrefix?: string) { - return this.addCondition(field, "=", value, "", "", resourcePrefix ); + public addEqual(field: string, value?: string | string[] | boolean | null, resourcePrefix?: string) { + return this.addCondition(field, "=", value, "", "", resourcePrefix); + } + + public addDistinct(field: string, value?: string | boolean | null, resourcePrefix?: string) { + return this.addCondition(field, "!=", value, "", "", resourcePrefix); } public addLike(field: string, value?: string, resourcePrefix?: string) { @@ -23,6 +25,10 @@ export class FilterBuilder { return this.addCondition(field, "ilike", value, "%", "%", resourcePrefix); } + public addContains(field: string, value?: string, resourcePrefix?: string) { + return this.addCondition(field, "contains", value, "", "", resourcePrefix); + } + public addIsA(field: string, value?: string | string[], resourcePrefix?: string) { return this.addCondition(field, "is_a", value, "", "", resourcePrefix); } @@ -31,25 +37,78 @@ export class FilterBuilder { return this.addCondition(field, "in", value, "", "", resourcePrefix); } + public addNotIn(field: string, value?: string | string[], resourcePrefix?: string) { + return this.addCondition(field, "not in", value, "", "", resourcePrefix); + } + + public addGt(field: string, value?: string, resourcePrefix?: string) { + return this.addCondition(field, ">", value, "", "", resourcePrefix); + } + + public addGte(field: string, value?: string, resourcePrefix?: string) { + return this.addCondition(field, ">=", value, "", "", resourcePrefix); + } + + public addLt(field: string, value?: string, resourcePrefix?: string) { + return this.addCondition(field, "<", value, "", "", resourcePrefix); + } + + public addLte(field: string, value?: string, resourcePrefix?: string) { + return this.addCondition(field, "<=", value, "", "", resourcePrefix); + } + + public addExists(value?: string, resourcePrefix?: string) { + return this.addCondition("properties", "exists", value, "", "", resourcePrefix); + } + public addDoesNotExist(field: string, resourcePrefix?: string) { + return this.addCondition("properties." + field, "exists", false, "", "", resourcePrefix); + } + + public addFullTextSearch(value: string, table?: string) { + const regex = /"[^"]*"/; + const matches: any[] = []; + + let match = value.match(regex); + + while (match) { + value = value.replace(match[0], ""); + matches.push(match[0].replace(/"/g, '')); + match = value.match(regex); + } + + let searchIn = 'any'; + if (table) { + searchIn = table + ".any"; + } + + const terms = value.trim().split(/(\s+)/).concat(matches); + terms.forEach(term => { + if (term !== " ") { + this.addCondition(searchIn, "ilike", term, "%", "%"); + } + }); + return this; + } + public getFilters() { return this.filters; } - private addCondition(field: string, cond: string, value?: string | string[] | boolean, prefix: string = "", postfix: string = "", resourcePrefix?: string) { - if (value) { + private addCondition(field: string, cond: string, value?: string | string[] | boolean | null, prefix: string = "", postfix: string = "", resourcePrefix?: string) { + if (value !== undefined) { if (typeof value === "string") { value = `"${prefix}${value}${postfix}"`; } else if (Array.isArray(value)) { value = `["${value.join(`","`)}"]`; - } else { + } else if (value !== null) { value = value ? "true" : "false"; } const resPrefix = resourcePrefix - ? _.snakeCase(resourcePrefix) + "." + ? resourcePrefix + "." : ""; - this.filters += `${this.filters ? "," : ""}["${resPrefix}${_.snakeCase(field)}","${cond}",${value}]`; + this.filters += `${this.filters ? "," : ""}["${resPrefix}${field}","${cond}",${value}]`; } return this; }