1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server.healthconnect.storage.utils; 18 19 import android.util.Pair; 20 21 import java.util.ArrayList; 22 import java.util.List; 23 24 /** @hide */ 25 public final class OrderByClause { 26 List<Pair<String, Boolean>> mOrderList; 27 28 /** 29 * Adds Order By condition for the read query. 30 * 31 * @param columnName the column name on which sorting to be done 32 * @param isAscending to specify the sorting order 33 */ addOrderByClause(String columnName, boolean isAscending)34 public OrderByClause addOrderByClause(String columnName, boolean isAscending) { 35 if (mOrderList == null) { 36 mOrderList = new ArrayList<>(); 37 } 38 mOrderList.add(new Pair<>(columnName, isAscending)); 39 return this; 40 } 41 42 /** 43 * Returns the Order By clause for the read query 44 * 45 * @return ordery by clause containing all the order by column conditions in order 46 */ getOrderBy()47 public String getOrderBy() { 48 if (mOrderList == null) { 49 return ""; 50 } 51 final StringBuilder builder = new StringBuilder(" ORDER BY "); 52 String prefix = ""; 53 for (Pair<String, Boolean> column : mOrderList) { 54 builder.append(prefix); 55 prefix = " , "; 56 builder.append(column.first); 57 if (!column.second) { 58 builder.append(" DESC "); 59 } 60 } 61 return builder.toString(); 62 } 63 } 64