1 /*
2  * Copyright (C) 2017 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.tv.common.util.sql;
18 
19 import android.database.DatabaseUtils;
20 import android.support.annotation.Nullable;
21 import java.util.Arrays;
22 
23 /** Convenience class for SQL operations. */
24 public class SqlParams {
25     private String mTables;
26     private @Nullable String mSelection;
27     private @Nullable String[] mSelectionArgs;
28 
SqlParams(String tables, @Nullable String selection, @Nullable String... selectionArgs)29     public SqlParams(String tables, @Nullable String selection, @Nullable String... selectionArgs) {
30         setTables(tables);
31         setWhere(selection, selectionArgs);
32     }
33 
getTables()34     public String getTables() {
35         return mTables;
36     }
37 
getSelection()38     public @Nullable String getSelection() {
39         return mSelection;
40     }
41 
getSelectionArgs()42     public @Nullable String[] getSelectionArgs() {
43         return mSelectionArgs;
44     }
45 
setTables(String tables)46     public void setTables(String tables) {
47         mTables = tables;
48     }
49 
setWhere(String selection, String... selectionArgs)50     public void setWhere(String selection, String... selectionArgs) {
51         mSelection = selection;
52         mSelectionArgs = selectionArgs;
53     }
54 
appendWhere(String selection, String... selectionArgs)55     public void appendWhere(String selection, String... selectionArgs) {
56         mSelection = DatabaseUtils.concatenateWhere(mSelection, selection);
57         if (selectionArgs != null) {
58             mSelectionArgs = DatabaseUtils.appendSelectionArgs(mSelectionArgs, selectionArgs);
59         }
60     }
61 
appendWhereEquals(String name, String value)62     public void appendWhereEquals(String name, String value) {
63         appendWhere(name + "=?", value);
64     }
65 
66     @Override
toString()67     public String toString() {
68         return "tables  "
69                 + getTables()
70                 + " where "
71                 + getSelection()
72                 + " with "
73                 + Arrays.toString(getSelectionArgs());
74     }
75 }
76