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// @source: the table or select statement in the FROM clause. 22// @whereClause: the WHERE clause to filter data from the source table. 23// @orderByClause: the ORDER BY clause for the query data. 24// TODO(stevegolton): Move these comments inline. 25interface GenerateSqlArgs { 26 columns: string[]; 27 source: string; 28 ts: string; 29 dur: string; 30 whereClause?: string; 31 orderByClause?: string; 32} 33 34// Function to generate a SELECT statement utilizing the internal_layout 35// SQL function as a depth field. 36export function generateSqlWithInternalLayout( 37 sqlArgs: GenerateSqlArgs, 38): string { 39 let sql = 40 `SELECT ` + 41 sqlArgs.columns.toString() + 42 `, internal_layout(${sqlArgs.ts}, ${sqlArgs.dur}) OVER (ORDER BY ${sqlArgs.ts}` + 43 ' ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS depth' + 44 ` FROM (${sqlArgs.source})`; 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