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 15// Object to facilitate generation of SELECT statement using 16// generateSqlWithInternalLayout. 17// 18// Fields: 19// @columns: a string array list of the columns to be selected from the table. 20// required by the internal_layout function. 21// @sourceTable: the table in the FROM clause, source of the data. 22// @whereClause: the WHERE clause to filter data from the source table. 23// @orderByClause: the ORDER BY clause for the query data. 24interface GenerateSqlArgs { 25 columns: string[]; 26 sourceTable: string; 27 ts: string; 28 dur: string; 29 whereClause?: string; 30 orderByClause?: string; 31} 32 33// Function to generate a SELECT statement utilizing the internal_layout 34// SQL function as a depth field. 35export function generateSqlWithInternalLayout( 36 sqlArgs: GenerateSqlArgs, 37): string { 38 let sql = 39 `SELECT ` + 40 sqlArgs.columns.toString() + 41 `, internal_layout(${sqlArgs.ts}, ${sqlArgs.dur}) OVER (ORDER BY ${sqlArgs.ts}` + 42 ' ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS depth' + 43 ' FROM ' + 44 sqlArgs.sourceTable; 45 if (sqlArgs.whereClause !== undefined) { 46 sql += ' WHERE ' + sqlArgs.whereClause; 47 } 48 if (sqlArgs.orderByClause !== undefined) { 49 sql += ' ORDER BY ' + sqlArgs.orderByClause; 50 } 51 return sql; 52} 53