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// @layoutParams: a config of the timestamp (ts) and duration (dur) fields 21// required by the internal_layout function. 22// @sourceTable: the table in the FROM clause, source of the data. 23// @whereClause: the WHERE clause to filter data from the source table. 24// @orderByClause: the ORDER BY clause for the query data. 25interface GenerateSqlArgs { 26 columns: string[]; 27 layoutParams: {ts: string, dur: string}; 28 sourceTable: 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(sqlArgs: GenerateSqlArgs): 36 string { 37 let sql = `SELECT ` + sqlArgs.columns.toString() + ', internal_layout(' + 38 sqlArgs.layoutParams.ts + ',' + sqlArgs.layoutParams.dur + 39 ') OVER (ORDER BY ' + sqlArgs.layoutParams.ts + 40 ' ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS depth' + 41 ' FROM ' + sqlArgs.sourceTable; 42 if (sqlArgs.whereClause !== undefined) { 43 sql += ' WHERE ' + sqlArgs.whereClause; 44 } 45 if (sqlArgs.orderByClause !== undefined) { 46 sql += ' ORDER BY ' + sqlArgs.orderByClause; 47 } 48 sql += ';'; 49 return sql; 50} 51