1// Copyright (C) 2023 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import {SortDirection} from '../common/state'; 16 17interface OrderClause { 18 fieldName: string; 19 direction?: SortDirection; 20} 21 22// Interface for defining constraints which can be passed to a SQL query. 23export interface SQLConstraints { 24 filters?: string[]; 25 orderBy?: OrderClause[]; 26 limit?: number; 27} 28 29// Formatting given constraints into a string which can be injected into 30// SQL query. 31export function constraintsToQueryFragment(c: SQLConstraints): string { 32 const result: string[] = []; 33 if (c.filters && c.filters.length > 0) { 34 result.push(`WHERE ${c.filters.join(' and ')}`); 35 } 36 if (c.orderBy && c.orderBy.length > 0) { 37 const orderBys = c.orderBy.map((clause) => { 38 const direction = clause.direction ? ` ${clause.direction}` : ''; 39 return `${clause.fieldName}${direction}`; 40 }); 41 result.push(`ORDER BY ${orderBys.join(', ')}`); 42 } 43 if (c.limit) { 44 result.push(`LIMIT ${c.limit}`); 45 } 46 return result.join('\n'); 47} 48 49// Trace Processor returns number | null for NUM_NULL, while most of the UI 50// code uses number | undefined. This functions provides a short-hand 51// conversion. 52// TODO(altimin): Support NUM_UNDEFINED as a first-class citizen. 53export function fromNumNull(n: number|null): number|undefined { 54 if (n === null) { 55 return undefined; 56 } 57 return n; 58} 59