• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     private final List<Pair<String, Boolean>> mOrderList = new ArrayList<>();
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         mOrderList.add(new Pair<>(columnName, isAscending));
36         return this;
37     }
38 
39     /**
40      * Returns the Order By clause for the read query
41      *
42      * @return ordery by clause containing all the order by column conditions in order
43      */
getOrderBy()44     public String getOrderBy() {
45         if (mOrderList.isEmpty()) {
46             return "";
47         }
48         final StringBuilder builder = new StringBuilder(" ORDER BY ");
49         String prefix = "";
50         for (Pair<String, Boolean> column : mOrderList) {
51             builder.append(prefix);
52             prefix = " , ";
53             builder.append(column.first);
54             if (!column.second) {
55                 builder.append(" DESC ");
56             }
57         }
58         return builder.toString();
59     }
60 }
61