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