The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 android.database.sqlite; |
| 18 | |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Mathew Inwood | 41b3194 | 2018-08-10 16:00:53 +0100 | [diff] [blame] | 21 | import android.annotation.UnsupportedAppUsage; |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 22 | import android.content.ContentValues; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | import android.database.Cursor; |
| 24 | import android.database.DatabaseUtils; |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 25 | import android.os.Build; |
Jeff Brown | a7771df | 2012-05-07 20:06:46 -0700 | [diff] [blame] | 26 | import android.os.CancellationSignal; |
| 27 | import android.os.OperationCanceledException; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 28 | import android.provider.BaseColumns; |
| 29 | import android.text.TextUtils; |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 30 | import android.util.ArrayMap; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 31 | import android.util.Log; |
| 32 | |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 33 | import com.android.internal.util.ArrayUtils; |
| 34 | |
Jeff Sharkey | 42122bf | 2018-07-26 09:39:18 -0600 | [diff] [blame] | 35 | import libcore.util.EmptyArray; |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 36 | |
| 37 | import java.util.Arrays; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | import java.util.Iterator; |
Jeff Sharkey | 7049e65 | 2018-09-13 17:05:07 -0600 | [diff] [blame] | 39 | import java.util.List; |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 40 | import java.util.Locale; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 41 | import java.util.Map; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 42 | import java.util.Map.Entry; |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 43 | import java.util.Objects; |
Daniel Lehmann | 50b1f8d | 2011-06-01 15:24:23 -0700 | [diff] [blame] | 44 | import java.util.Set; |
Jeff Sharkey | 82d783c | 2019-03-29 15:46:35 -0600 | [diff] [blame] | 45 | import java.util.regex.Matcher; |
Owen Lin | ab18d1f | 2009-05-06 16:45:59 -0700 | [diff] [blame] | 46 | import java.util.regex.Pattern; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
Joshua Baxter | 3639e2f | 2018-03-26 14:55:14 -0700 | [diff] [blame] | 49 | * This is a convenience class that helps build SQL queries to be sent to |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 50 | * {@link SQLiteDatabase} objects. |
| 51 | */ |
Jeff Sharkey | b89df9e | 2018-07-26 14:36:59 -0600 | [diff] [blame] | 52 | public class SQLiteQueryBuilder { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | private static final String TAG = "SQLiteQueryBuilder"; |
Jeff Sharkey | 82d783c | 2019-03-29 15:46:35 -0600 | [diff] [blame] | 54 | |
Jeff Sharkey | 82d783c | 2019-03-29 15:46:35 -0600 | [diff] [blame] | 55 | private static final Pattern sAggregationPattern = Pattern.compile( |
Jeff Sharkey | 0ab6feb | 2019-04-10 10:27:15 -0600 | [diff] [blame] | 56 | "(?i)(AVG|COUNT|MAX|MIN|SUM|TOTAL|GROUP_CONCAT)\\((.+)\\)"); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 57 | |
| 58 | private Map<String, String> mProjectionMap = null; |
Jeff Sharkey | 7049e65 | 2018-09-13 17:05:07 -0600 | [diff] [blame] | 59 | private List<Pattern> mProjectionGreylist = null; |
| 60 | |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 61 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 62 | private String mTables = ""; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 63 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Brad Fitzpatrick | ae6cdd1 | 2010-03-14 11:38:06 -0700 | [diff] [blame] | 64 | private StringBuilder mWhereClause = null; // lazily created |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 65 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 66 | private boolean mDistinct; |
| 67 | private SQLiteDatabase.CursorFactory mFactory; |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 68 | |
| 69 | private static final int STRICT_PARENTHESES = 1 << 0; |
| 70 | private static final int STRICT_COLUMNS = 1 << 1; |
| 71 | private static final int STRICT_GRAMMAR = 1 << 2; |
| 72 | |
| 73 | private int mStrictFlags; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 74 | |
| 75 | public SQLiteQueryBuilder() { |
| 76 | mDistinct = false; |
| 77 | mFactory = null; |
| 78 | } |
| 79 | |
| 80 | /** |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 81 | * Mark the query as {@code DISTINCT}. |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 82 | * |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 83 | * @param distinct if true the query is {@code DISTINCT}, otherwise it isn't |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 84 | */ |
| 85 | public void setDistinct(boolean distinct) { |
| 86 | mDistinct = distinct; |
| 87 | } |
| 88 | |
| 89 | /** |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 90 | * Get if the query is marked as {@code DISTINCT}, as last configured by |
Jeff Sharkey | 0ec586b | 2019-02-14 15:29:16 -0700 | [diff] [blame] | 91 | * {@link #setDistinct(boolean)}. |
| 92 | */ |
Jeff Sharkey | aa28dff | 2019-03-25 09:28:34 -0600 | [diff] [blame] | 93 | public boolean isDistinct() { |
Jeff Sharkey | 0ec586b | 2019-02-14 15:29:16 -0700 | [diff] [blame] | 94 | return mDistinct; |
| 95 | } |
| 96 | |
| 97 | /** |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 98 | * Returns the list of tables being queried |
| 99 | * |
| 100 | * @return the list of tables being queried |
| 101 | */ |
Jeff Sharkey | a30e5c3 | 2019-02-28 12:02:10 -0700 | [diff] [blame] | 102 | public @Nullable String getTables() { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 103 | return mTables; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Sets the list of tables to query. Multiple tables can be specified to perform a join. |
| 108 | * For example: |
| 109 | * setTables("foo, bar") |
| 110 | * setTables("foo LEFT OUTER JOIN bar ON (foo.id = bar.foo_id)") |
| 111 | * |
| 112 | * @param inTables the list of tables to query on |
| 113 | */ |
Jeff Sharkey | a30e5c3 | 2019-02-28 12:02:10 -0700 | [diff] [blame] | 114 | public void setTables(@Nullable String inTables) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | mTables = inTables; |
| 116 | } |
| 117 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 118 | /** |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 119 | * Append a chunk to the {@code WHERE} clause of the query. All chunks appended are surrounded |
| 120 | * by parenthesis and {@code AND}ed with the selection passed to {@link #query}. The final |
| 121 | * {@code WHERE} clause looks like: |
| 122 | * <p> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 123 | * WHERE (<append chunk 1><append chunk2>) AND (<query() selection parameter>) |
| 124 | * |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 125 | * @param inWhere the chunk of text to append to the {@code WHERE} clause. |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 126 | */ |
Jeff Sharkey | b89df9e | 2018-07-26 14:36:59 -0600 | [diff] [blame] | 127 | public void appendWhere(@NonNull CharSequence inWhere) { |
Brad Fitzpatrick | ae6cdd1 | 2010-03-14 11:38:06 -0700 | [diff] [blame] | 128 | if (mWhereClause == null) { |
| 129 | mWhereClause = new StringBuilder(inWhere.length() + 16); |
| 130 | } |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 131 | mWhereClause.append(inWhere); |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 135 | * Append a chunk to the {@code WHERE} clause of the query. All chunks appended are surrounded |
Jeff Sharkey | 91be926 | 2018-07-19 09:30:16 -0600 | [diff] [blame] | 136 | * by parenthesis and ANDed with the selection passed to {@link #query}. The final |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 137 | * {@code WHERE} clause looks like: |
| 138 | * <p> |
Jeff Sharkey | 91be926 | 2018-07-19 09:30:16 -0600 | [diff] [blame] | 139 | * WHERE (<append chunk 1><append chunk2>) AND (<query() selection parameter>) |
Jeff Sharkey | 99cc118 | 2018-07-16 14:34:21 -0600 | [diff] [blame] | 140 | * |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 141 | * @param inWhere the chunk of text to append to the {@code WHERE} clause. it will be escaped |
Jeff Sharkey | 91be926 | 2018-07-19 09:30:16 -0600 | [diff] [blame] | 142 | * to avoid SQL injection attacks |
Jeff Sharkey | 99cc118 | 2018-07-16 14:34:21 -0600 | [diff] [blame] | 143 | */ |
Jeff Sharkey | b89df9e | 2018-07-26 14:36:59 -0600 | [diff] [blame] | 144 | public void appendWhereEscapeString(@NonNull String inWhere) { |
Jeff Sharkey | 99cc118 | 2018-07-16 14:34:21 -0600 | [diff] [blame] | 145 | if (mWhereClause == null) { |
| 146 | mWhereClause = new StringBuilder(inWhere.length() + 16); |
| 147 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 148 | DatabaseUtils.appendEscapedSQLString(mWhereClause, inWhere); |
| 149 | } |
| 150 | |
| 151 | /** |
Jeff Sharkey | b89df9e | 2018-07-26 14:36:59 -0600 | [diff] [blame] | 152 | * Add a standalone chunk to the {@code WHERE} clause of this query. |
| 153 | * <p> |
| 154 | * This method differs from {@link #appendWhere(CharSequence)} in that it |
| 155 | * automatically appends {@code AND} to any existing {@code WHERE} clause |
| 156 | * already under construction before appending the given standalone |
| 157 | * expression wrapped in parentheses. |
| 158 | * |
| 159 | * @param inWhere the standalone expression to append to the {@code WHERE} |
| 160 | * clause. It will be wrapped in parentheses when it's appended. |
| 161 | */ |
| 162 | public void appendWhereStandalone(@NonNull CharSequence inWhere) { |
| 163 | if (mWhereClause == null) { |
| 164 | mWhereClause = new StringBuilder(inWhere.length() + 16); |
| 165 | } |
| 166 | if (mWhereClause.length() > 0) { |
| 167 | mWhereClause.append(" AND "); |
| 168 | } |
| 169 | mWhereClause.append('(').append(inWhere).append(')'); |
| 170 | } |
| 171 | |
| 172 | /** |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 173 | * Sets the projection map for the query. The projection map maps |
| 174 | * from column names that the caller passes into query to database |
| 175 | * column names. This is useful for renaming columns as well as |
| 176 | * disambiguating column names when doing joins. For example you |
| 177 | * could map "name" to "people.name". If a projection map is set |
| 178 | * it must contain all column names the user may request, even if |
| 179 | * the key and value are the same. |
| 180 | * |
| 181 | * @param columnMap maps from the user column names to the database column names |
| 182 | */ |
Jeff Sharkey | a30e5c3 | 2019-02-28 12:02:10 -0700 | [diff] [blame] | 183 | public void setProjectionMap(@Nullable Map<String, String> columnMap) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 184 | mProjectionMap = columnMap; |
| 185 | } |
| 186 | |
| 187 | /** |
Jeff Sharkey | 0ec586b | 2019-02-14 15:29:16 -0700 | [diff] [blame] | 188 | * Gets the projection map for the query, as last configured by |
| 189 | * {@link #setProjectionMap(Map)}. |
| 190 | */ |
Jeff Sharkey | a30e5c3 | 2019-02-28 12:02:10 -0700 | [diff] [blame] | 191 | public @Nullable Map<String, String> getProjectionMap() { |
Jeff Sharkey | 0ec586b | 2019-02-14 15:29:16 -0700 | [diff] [blame] | 192 | return mProjectionMap; |
| 193 | } |
| 194 | |
| 195 | /** |
Jeff Sharkey | 7049e65 | 2018-09-13 17:05:07 -0600 | [diff] [blame] | 196 | * Sets a projection greylist of columns that will be allowed through, even |
| 197 | * when {@link #setStrict(boolean)} is enabled. This provides a way for |
| 198 | * abusive custom columns like {@code COUNT(*)} to continue working. |
| 199 | * |
| 200 | * @hide |
| 201 | */ |
Jeff Sharkey | a30e5c3 | 2019-02-28 12:02:10 -0700 | [diff] [blame] | 202 | public void setProjectionGreylist(@Nullable List<Pattern> projectionGreylist) { |
Jeff Sharkey | 7049e65 | 2018-09-13 17:05:07 -0600 | [diff] [blame] | 203 | mProjectionGreylist = projectionGreylist; |
| 204 | } |
| 205 | |
| 206 | /** |
Jeff Sharkey | 0ec586b | 2019-02-14 15:29:16 -0700 | [diff] [blame] | 207 | * Gets the projection greylist for the query, as last configured by |
| 208 | * {@link #setProjectionGreylist(List)}. |
| 209 | * |
| 210 | * @hide |
| 211 | */ |
Jeff Sharkey | a30e5c3 | 2019-02-28 12:02:10 -0700 | [diff] [blame] | 212 | public @Nullable List<Pattern> getProjectionGreylist() { |
Jeff Sharkey | 0ec586b | 2019-02-14 15:29:16 -0700 | [diff] [blame] | 213 | return mProjectionGreylist; |
| 214 | } |
| 215 | |
Jeff Sharkey | 82d783c | 2019-03-29 15:46:35 -0600 | [diff] [blame] | 216 | /** {@hide} */ |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 217 | @Deprecated |
Jeff Sharkey | 82d783c | 2019-03-29 15:46:35 -0600 | [diff] [blame] | 218 | public void setProjectionAggregationAllowed(boolean projectionAggregationAllowed) { |
Jeff Sharkey | 82d783c | 2019-03-29 15:46:35 -0600 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /** {@hide} */ |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 222 | @Deprecated |
Jeff Sharkey | 82d783c | 2019-03-29 15:46:35 -0600 | [diff] [blame] | 223 | public boolean isProjectionAggregationAllowed() { |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 224 | return true; |
Jeff Sharkey | 82d783c | 2019-03-29 15:46:35 -0600 | [diff] [blame] | 225 | } |
| 226 | |
Jeff Sharkey | 0ec586b | 2019-02-14 15:29:16 -0700 | [diff] [blame] | 227 | /** |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 228 | * Sets the cursor factory to be used for the query. You can use |
| 229 | * one factory for all queries on a database but it is normally |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 230 | * easier to specify the factory when doing this query. |
| 231 | * |
| 232 | * @param factory the factory to use. |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 233 | */ |
Jeff Sharkey | a30e5c3 | 2019-02-28 12:02:10 -0700 | [diff] [blame] | 234 | public void setCursorFactory(@Nullable SQLiteDatabase.CursorFactory factory) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 235 | mFactory = factory; |
| 236 | } |
| 237 | |
| 238 | /** |
Jeff Sharkey | aa28dff | 2019-03-25 09:28:34 -0600 | [diff] [blame] | 239 | * Gets the cursor factory to be used for the query, as last configured by |
Jeff Sharkey | 0ec586b | 2019-02-14 15:29:16 -0700 | [diff] [blame] | 240 | * {@link #setCursorFactory(android.database.sqlite.SQLiteDatabase.CursorFactory)}. |
| 241 | */ |
Jeff Sharkey | a30e5c3 | 2019-02-28 12:02:10 -0700 | [diff] [blame] | 242 | public @Nullable SQLiteDatabase.CursorFactory getCursorFactory() { |
Jeff Sharkey | 0ec586b | 2019-02-14 15:29:16 -0700 | [diff] [blame] | 243 | return mFactory; |
| 244 | } |
| 245 | |
| 246 | /** |
Daniel Lehmann | 50b1f8d | 2011-06-01 15:24:23 -0700 | [diff] [blame] | 247 | * When set, the selection is verified against malicious arguments. |
| 248 | * When using this class to create a statement using |
| 249 | * {@link #buildQueryString(boolean, String, String[], String, String, String, String, String)}, |
| 250 | * non-numeric limits will raise an exception. If a projection map is specified, fields |
| 251 | * not in that map will be ignored. |
| 252 | * If this class is used to execute the statement directly using |
| 253 | * {@link #query(SQLiteDatabase, String[], String, String[], String, String, String)} |
| 254 | * or |
| 255 | * {@link #query(SQLiteDatabase, String[], String, String[], String, String, String, String)}, |
| 256 | * additionally also parenthesis escaping selection are caught. |
| 257 | * |
| 258 | * To summarize: To get maximum protection against malicious third party apps (for example |
| 259 | * content provider consumers), make sure to do the following: |
| 260 | * <ul> |
| 261 | * <li>Set this value to true</li> |
| 262 | * <li>Use a projection map</li> |
| 263 | * <li>Use one of the query overloads instead of getting the statement as a sql string</li> |
| 264 | * </ul> |
| 265 | * By default, this value is false. |
| 266 | */ |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 267 | public void setStrict(boolean strict) { |
| 268 | if (strict) { |
| 269 | mStrictFlags |= STRICT_PARENTHESES; |
| 270 | } else { |
| 271 | mStrictFlags &= ~STRICT_PARENTHESES; |
| 272 | } |
Dmitri Plotnikov | 40eb4aa | 2010-04-14 16:09:46 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /** |
Jeff Sharkey | 0ec586b | 2019-02-14 15:29:16 -0700 | [diff] [blame] | 276 | * Get if the query is marked as strict, as last configured by |
| 277 | * {@link #setStrict(boolean)}. |
| 278 | */ |
Jeff Sharkey | aa28dff | 2019-03-25 09:28:34 -0600 | [diff] [blame] | 279 | public boolean isStrict() { |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 280 | return (mStrictFlags & STRICT_PARENTHESES) != 0; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * When enabled, verify that all projections and {@link ContentValues} only |
| 285 | * contain valid columns as defined by {@link #setProjectionMap(Map)}. |
| 286 | * <p> |
| 287 | * This enforcement applies to {@link #insert}, {@link #query}, and |
| 288 | * {@link #update} operations. Any enforcement failures will throw an |
| 289 | * {@link IllegalArgumentException}. |
| 290 | */ |
| 291 | public void setStrictColumns(boolean strictColumns) { |
| 292 | if (strictColumns) { |
| 293 | mStrictFlags |= STRICT_COLUMNS; |
| 294 | } else { |
| 295 | mStrictFlags &= ~STRICT_COLUMNS; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Get if the query is marked as strict, as last configured by |
| 301 | * {@link #setStrictColumns(boolean)}. |
| 302 | */ |
| 303 | public boolean isStrictColumns() { |
| 304 | return (mStrictFlags & STRICT_COLUMNS) != 0; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * When enabled, verify that all untrusted SQL conforms to a restricted SQL |
| 309 | * grammar. Here are the restrictions applied: |
| 310 | * <ul> |
| 311 | * <li>In {@code WHERE} and {@code HAVING} clauses: subqueries, raising, and |
| 312 | * windowing terms are rejected. |
| 313 | * <li>In {@code GROUP BY} clauses: only valid columns are allowed. |
| 314 | * <li>In {@code ORDER BY} clauses: only valid columns, collation, and |
| 315 | * ordering terms are allowed. |
| 316 | * <li>In {@code LIMIT} clauses: only numerical values and offset terms are |
| 317 | * allowed. |
| 318 | * </ul> |
| 319 | * All column references must be valid as defined by |
| 320 | * {@link #setProjectionMap(Map)}. |
| 321 | * <p> |
| 322 | * This enforcement applies to {@link #query}, {@link #update} and |
| 323 | * {@link #delete} operations. This enforcement does not apply to trusted |
| 324 | * inputs, such as those provided by {@link #appendWhere}. Any enforcement |
| 325 | * failures will throw an {@link IllegalArgumentException}. |
| 326 | */ |
| 327 | public void setStrictGrammar(boolean strictGrammar) { |
| 328 | if (strictGrammar) { |
| 329 | mStrictFlags |= STRICT_GRAMMAR; |
| 330 | } else { |
| 331 | mStrictFlags &= ~STRICT_GRAMMAR; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Get if the query is marked as strict, as last configured by |
| 337 | * {@link #setStrictGrammar(boolean)}. |
| 338 | */ |
| 339 | public boolean isStrictGrammar() { |
| 340 | return (mStrictFlags & STRICT_GRAMMAR) != 0; |
Jeff Sharkey | 0ec586b | 2019-02-14 15:29:16 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /** |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 344 | * Build an SQL query string from the given clauses. |
| 345 | * |
| 346 | * @param distinct true if you want each row to be unique, false otherwise. |
| 347 | * @param tables The table names to compile the query against. |
| 348 | * @param columns A list of which columns to return. Passing null will |
| 349 | * return all columns, which is discouraged to prevent reading |
| 350 | * data from storage that isn't going to be used. |
| 351 | * @param where A filter declaring which rows to return, formatted as an SQL |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 352 | * {@code WHERE} clause (excluding the {@code WHERE} itself). Passing {@code null} will |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 353 | * return all rows for the given URL. |
| 354 | * @param groupBy A filter declaring how to group rows, formatted as an SQL |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 355 | * {@code GROUP BY} clause (excluding the {@code GROUP BY} itself). Passing {@code null} |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 356 | * will cause the rows to not be grouped. |
| 357 | * @param having A filter declare which row groups to include in the cursor, |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 358 | * if row grouping is being used, formatted as an SQL {@code HAVING} |
| 359 | * clause (excluding the {@code HAVING} itself). Passing null will cause |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 360 | * all row groups to be included, and is required when row |
| 361 | * grouping is not being used. |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 362 | * @param orderBy How to order the rows, formatted as an SQL {@code ORDER BY} clause |
| 363 | * (excluding the {@code ORDER BY} itself). Passing null will use the |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 364 | * default sort order, which may be unordered. |
| 365 | * @param limit Limits the number of rows returned by the query, |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 366 | * formatted as {@code LIMIT} clause. Passing null denotes no {@code LIMIT} clause. |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 367 | * @return the SQL query string |
| 368 | */ |
| 369 | public static String buildQueryString( |
| 370 | boolean distinct, String tables, String[] columns, String where, |
| 371 | String groupBy, String having, String orderBy, String limit) { |
| 372 | if (TextUtils.isEmpty(groupBy) && !TextUtils.isEmpty(having)) { |
| 373 | throw new IllegalArgumentException( |
| 374 | "HAVING clauses are only permitted when using a groupBy clause"); |
| 375 | } |
| 376 | |
| 377 | StringBuilder query = new StringBuilder(120); |
| 378 | |
| 379 | query.append("SELECT "); |
| 380 | if (distinct) { |
| 381 | query.append("DISTINCT "); |
| 382 | } |
| 383 | if (columns != null && columns.length != 0) { |
| 384 | appendColumns(query, columns); |
| 385 | } else { |
| 386 | query.append("* "); |
| 387 | } |
| 388 | query.append("FROM "); |
| 389 | query.append(tables); |
| 390 | appendClause(query, " WHERE ", where); |
| 391 | appendClause(query, " GROUP BY ", groupBy); |
| 392 | appendClause(query, " HAVING ", having); |
| 393 | appendClause(query, " ORDER BY ", orderBy); |
Owen Lin | ab18d1f | 2009-05-06 16:45:59 -0700 | [diff] [blame] | 394 | appendClause(query, " LIMIT ", limit); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 395 | |
| 396 | return query.toString(); |
| 397 | } |
| 398 | |
| 399 | private static void appendClause(StringBuilder s, String name, String clause) { |
| 400 | if (!TextUtils.isEmpty(clause)) { |
| 401 | s.append(name); |
| 402 | s.append(clause); |
| 403 | } |
| 404 | } |
| 405 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 406 | /** |
| 407 | * Add the names that are non-null in columns to s, separating |
| 408 | * them with commas. |
| 409 | */ |
| 410 | public static void appendColumns(StringBuilder s, String[] columns) { |
| 411 | int n = columns.length; |
| 412 | |
| 413 | for (int i = 0; i < n; i++) { |
| 414 | String column = columns[i]; |
| 415 | |
| 416 | if (column != null) { |
| 417 | if (i > 0) { |
| 418 | s.append(", "); |
| 419 | } |
| 420 | s.append(column); |
| 421 | } |
| 422 | } |
| 423 | s.append(' '); |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Perform a query by combining all current settings and the |
| 428 | * information passed into this method. |
| 429 | * |
| 430 | * @param db the database to query on |
Jeff Sharkey | 91be926 | 2018-07-19 09:30:16 -0600 | [diff] [blame] | 431 | * @param projectionIn A list of which columns to return. Passing |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 432 | * null will return all columns, which is discouraged to prevent |
| 433 | * reading data from storage that isn't going to be used. |
| 434 | * @param selection A filter declaring which rows to return, |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 435 | * formatted as an SQL {@code WHERE} clause (excluding the {@code WHERE} |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 436 | * itself). Passing null will return all rows for the given URL. |
| 437 | * @param selectionArgs You may include ?s in selection, which |
| 438 | * will be replaced by the values from selectionArgs, in order |
| 439 | * that they appear in the selection. The values will be bound |
| 440 | * as Strings. |
| 441 | * @param groupBy A filter declaring how to group rows, formatted |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 442 | * as an SQL {@code GROUP BY} clause (excluding the {@code GROUP BY} |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 443 | * itself). Passing null will cause the rows to not be grouped. |
| 444 | * @param having A filter declare which row groups to include in |
| 445 | * the cursor, if row grouping is being used, formatted as an |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 446 | * SQL {@code HAVING} clause (excluding the {@code HAVING} itself). Passing |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 447 | * null will cause all row groups to be included, and is |
| 448 | * required when row grouping is not being used. |
| 449 | * @param sortOrder How to order the rows, formatted as an SQL |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 450 | * {@code ORDER BY} clause (excluding the {@code ORDER BY} itself). Passing null |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 451 | * will use the default sort order, which may be unordered. |
| 452 | * @return a cursor over the result set |
| 453 | * @see android.content.ContentResolver#query(android.net.Uri, String[], |
| 454 | * String, String[], String) |
| 455 | */ |
Jeff Sharkey | 91be926 | 2018-07-19 09:30:16 -0600 | [diff] [blame] | 456 | public Cursor query(SQLiteDatabase db, String[] projectionIn, |
| 457 | String selection, String[] selectionArgs, String groupBy, |
| 458 | String having, String sortOrder) { |
| 459 | return query(db, projectionIn, selection, selectionArgs, groupBy, having, sortOrder, |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 460 | null /* limit */, null /* cancellationSignal */); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Perform a query by combining all current settings and the |
| 465 | * information passed into this method. |
| 466 | * |
| 467 | * @param db the database to query on |
Jeff Sharkey | 91be926 | 2018-07-19 09:30:16 -0600 | [diff] [blame] | 468 | * @param projectionIn A list of which columns to return. Passing |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 469 | * null will return all columns, which is discouraged to prevent |
| 470 | * reading data from storage that isn't going to be used. |
| 471 | * @param selection A filter declaring which rows to return, |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 472 | * formatted as an SQL {@code WHERE} clause (excluding the {@code WHERE} |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 473 | * itself). Passing null will return all rows for the given URL. |
| 474 | * @param selectionArgs You may include ?s in selection, which |
| 475 | * will be replaced by the values from selectionArgs, in order |
| 476 | * that they appear in the selection. The values will be bound |
| 477 | * as Strings. |
| 478 | * @param groupBy A filter declaring how to group rows, formatted |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 479 | * as an SQL {@code GROUP BY} clause (excluding the {@code GROUP BY} |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 480 | * itself). Passing null will cause the rows to not be grouped. |
| 481 | * @param having A filter declare which row groups to include in |
| 482 | * the cursor, if row grouping is being used, formatted as an |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 483 | * SQL {@code HAVING} clause (excluding the {@code HAVING} itself). Passing |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 484 | * null will cause all row groups to be included, and is |
| 485 | * required when row grouping is not being used. |
| 486 | * @param sortOrder How to order the rows, formatted as an SQL |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 487 | * {@code ORDER BY} clause (excluding the {@code ORDER BY} itself). Passing null |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 488 | * will use the default sort order, which may be unordered. |
| 489 | * @param limit Limits the number of rows returned by the query, |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 490 | * formatted as {@code LIMIT} clause. Passing null denotes no {@code LIMIT} clause. |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 491 | * @return a cursor over the result set |
| 492 | * @see android.content.ContentResolver#query(android.net.Uri, String[], |
| 493 | * String, String[], String) |
| 494 | */ |
Jeff Sharkey | 91be926 | 2018-07-19 09:30:16 -0600 | [diff] [blame] | 495 | public Cursor query(SQLiteDatabase db, String[] projectionIn, |
| 496 | String selection, String[] selectionArgs, String groupBy, |
| 497 | String having, String sortOrder, String limit) { |
| 498 | return query(db, projectionIn, selection, selectionArgs, |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 499 | groupBy, having, sortOrder, limit, null); |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Perform a query by combining all current settings and the |
| 504 | * information passed into this method. |
| 505 | * |
| 506 | * @param db the database to query on |
Jeff Sharkey | 91be926 | 2018-07-19 09:30:16 -0600 | [diff] [blame] | 507 | * @param projectionIn A list of which columns to return. Passing |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 508 | * null will return all columns, which is discouraged to prevent |
| 509 | * reading data from storage that isn't going to be used. |
| 510 | * @param selection A filter declaring which rows to return, |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 511 | * formatted as an SQL {@code WHERE} clause (excluding the {@code WHERE} |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 512 | * itself). Passing null will return all rows for the given URL. |
| 513 | * @param selectionArgs You may include ?s in selection, which |
| 514 | * will be replaced by the values from selectionArgs, in order |
| 515 | * that they appear in the selection. The values will be bound |
| 516 | * as Strings. |
| 517 | * @param groupBy A filter declaring how to group rows, formatted |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 518 | * as an SQL {@code GROUP BY} clause (excluding the {@code GROUP BY} |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 519 | * itself). Passing null will cause the rows to not be grouped. |
| 520 | * @param having A filter declare which row groups to include in |
| 521 | * the cursor, if row grouping is being used, formatted as an |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 522 | * SQL {@code HAVING} clause (excluding the {@code HAVING} itself). Passing |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 523 | * null will cause all row groups to be included, and is |
| 524 | * required when row grouping is not being used. |
| 525 | * @param sortOrder How to order the rows, formatted as an SQL |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 526 | * {@code ORDER BY} clause (excluding the {@code ORDER BY} itself). Passing null |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 527 | * will use the default sort order, which may be unordered. |
| 528 | * @param limit Limits the number of rows returned by the query, |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 529 | * formatted as {@code LIMIT} clause. Passing null denotes no {@code LIMIT} clause. |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 530 | * @param cancellationSignal A signal to cancel the operation in progress, or null if none. |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 531 | * If the operation is canceled, then {@link OperationCanceledException} will be thrown |
| 532 | * when the query is executed. |
| 533 | * @return a cursor over the result set |
| 534 | * @see android.content.ContentResolver#query(android.net.Uri, String[], |
| 535 | * String, String[], String) |
| 536 | */ |
Jeff Sharkey | 91be926 | 2018-07-19 09:30:16 -0600 | [diff] [blame] | 537 | public Cursor query(SQLiteDatabase db, String[] projectionIn, |
| 538 | String selection, String[] selectionArgs, String groupBy, |
| 539 | String having, String sortOrder, String limit, CancellationSignal cancellationSignal) { |
| 540 | if (mTables == null) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 541 | return null; |
| 542 | } |
| 543 | |
Jeff Sharkey | be8e0d0 | 2018-07-25 14:01:59 -0600 | [diff] [blame] | 544 | final String sql; |
| 545 | final String unwrappedSql = buildQuery( |
| 546 | projectionIn, selection, groupBy, having, |
| 547 | sortOrder, limit); |
| 548 | |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 549 | if (isStrictColumns()) { |
| 550 | enforceStrictColumns(projectionIn); |
| 551 | } |
| 552 | if (isStrictGrammar()) { |
| 553 | enforceStrictGrammar(selection, groupBy, having, sortOrder, limit); |
| 554 | } |
| 555 | if (isStrict()) { |
Daniel Lehmann | 50b1f8d | 2011-06-01 15:24:23 -0700 | [diff] [blame] | 556 | // Validate the user-supplied selection to detect syntactic anomalies |
| 557 | // in the selection string that could indicate a SQL injection attempt. |
| 558 | // The idea is to ensure that the selection clause is a valid SQL expression |
| 559 | // by compiling it twice: once wrapped in parentheses and once as |
| 560 | // originally specified. An attacker cannot create an expression that |
| 561 | // would escape the SQL expression while maintaining balanced parentheses |
| 562 | // in both the wrapped and original forms. |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 563 | |
Jeff Sharkey | be8e0d0 | 2018-07-25 14:01:59 -0600 | [diff] [blame] | 564 | // NOTE: The ordering of the below operations is important; we must |
| 565 | // execute the wrapped query to ensure the untrusted clause has been |
| 566 | // fully isolated. |
| 567 | |
| 568 | // Validate the unwrapped query |
| 569 | db.validateSql(unwrappedSql, cancellationSignal); // will throw if query is invalid |
| 570 | |
| 571 | // Execute wrapped query for extra protection |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 572 | final String wrappedSql = buildQuery(projectionIn, wrap(selection), groupBy, |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 573 | wrap(having), sortOrder, limit); |
Jeff Sharkey | be8e0d0 | 2018-07-25 14:01:59 -0600 | [diff] [blame] | 574 | sql = wrappedSql; |
| 575 | } else { |
| 576 | // Execute unwrapped query |
| 577 | sql = unwrappedSql; |
| 578 | } |
Jeff Sharkey | 6adc98c | 2018-07-12 19:47:49 -0600 | [diff] [blame] | 579 | |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 580 | final String[] sqlArgs = selectionArgs; |
Jeff Sharkey | 91be926 | 2018-07-19 09:30:16 -0600 | [diff] [blame] | 581 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 582 | if (Build.IS_DEBUGGABLE) { |
| 583 | Log.d(TAG, sql + " with args " + Arrays.toString(sqlArgs)); |
| 584 | } else { |
| 585 | Log.d(TAG, sql); |
| 586 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 587 | } |
| 588 | return db.rawQueryWithFactory( |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 589 | mFactory, sql, sqlArgs, |
Jeff Brown | 75ea64f | 2012-01-25 19:37:13 -0800 | [diff] [blame] | 590 | SQLiteDatabase.findEditTable(mTables), |
Jeff Brown | 4c1241d | 2012-02-02 17:05:00 -0800 | [diff] [blame] | 591 | cancellationSignal); // will throw if query is invalid |
Daniel Lehmann | 50b1f8d | 2011-06-01 15:24:23 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /** |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 595 | * Perform an insert by combining all current settings and the |
| 596 | * information passed into this method. |
| 597 | * |
| 598 | * @param db the database to insert on |
| 599 | * @return the row ID of the newly inserted row, or -1 if an error occurred |
| 600 | */ |
| 601 | public long insert(@NonNull SQLiteDatabase db, @NonNull ContentValues values) { |
| 602 | Objects.requireNonNull(mTables, "No tables defined"); |
| 603 | Objects.requireNonNull(db, "No database defined"); |
| 604 | Objects.requireNonNull(values, "No values defined"); |
| 605 | |
| 606 | if (isStrictColumns()) { |
| 607 | enforceStrictColumns(values); |
| 608 | } |
| 609 | |
| 610 | final String sql = buildInsert(values); |
| 611 | |
| 612 | final ArrayMap<String, Object> rawValues = values.getValues(); |
| 613 | final int valuesLength = rawValues.size(); |
| 614 | final Object[] sqlArgs = new Object[valuesLength]; |
| 615 | for (int i = 0; i < sqlArgs.length; i++) { |
| 616 | sqlArgs[i] = rawValues.valueAt(i); |
| 617 | } |
| 618 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 619 | if (Build.IS_DEBUGGABLE) { |
| 620 | Log.d(TAG, sql + " with args " + Arrays.toString(sqlArgs)); |
| 621 | } else { |
| 622 | Log.d(TAG, sql); |
| 623 | } |
| 624 | } |
| 625 | return db.executeSql(sql, sqlArgs); |
| 626 | } |
| 627 | |
| 628 | /** |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 629 | * Perform an update by combining all current settings and the |
| 630 | * information passed into this method. |
| 631 | * |
| 632 | * @param db the database to update on |
| 633 | * @param selection A filter declaring which rows to return, |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 634 | * formatted as an SQL {@code WHERE} clause (excluding the {@code WHERE} |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 635 | * itself). Passing null will return all rows for the given URL. |
| 636 | * @param selectionArgs You may include ?s in selection, which |
| 637 | * will be replaced by the values from selectionArgs, in order |
| 638 | * that they appear in the selection. The values will be bound |
| 639 | * as Strings. |
| 640 | * @return the number of rows updated |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 641 | */ |
| 642 | public int update(@NonNull SQLiteDatabase db, @NonNull ContentValues values, |
| 643 | @Nullable String selection, @Nullable String[] selectionArgs) { |
| 644 | Objects.requireNonNull(mTables, "No tables defined"); |
| 645 | Objects.requireNonNull(db, "No database defined"); |
| 646 | Objects.requireNonNull(values, "No values defined"); |
| 647 | |
| 648 | final String sql; |
| 649 | final String unwrappedSql = buildUpdate(values, selection); |
| 650 | |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 651 | if (isStrictColumns()) { |
| 652 | enforceStrictColumns(values); |
| 653 | } |
| 654 | if (isStrictGrammar()) { |
| 655 | enforceStrictGrammar(selection, null, null, null, null); |
| 656 | } |
| 657 | if (isStrict()) { |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 658 | // Validate the user-supplied selection to detect syntactic anomalies |
| 659 | // in the selection string that could indicate a SQL injection attempt. |
| 660 | // The idea is to ensure that the selection clause is a valid SQL expression |
| 661 | // by compiling it twice: once wrapped in parentheses and once as |
| 662 | // originally specified. An attacker cannot create an expression that |
| 663 | // would escape the SQL expression while maintaining balanced parentheses |
| 664 | // in both the wrapped and original forms. |
| 665 | |
| 666 | // NOTE: The ordering of the below operations is important; we must |
| 667 | // execute the wrapped query to ensure the untrusted clause has been |
| 668 | // fully isolated. |
| 669 | |
| 670 | // Validate the unwrapped query |
| 671 | db.validateSql(unwrappedSql, null); // will throw if query is invalid |
| 672 | |
| 673 | // Execute wrapped query for extra protection |
| 674 | final String wrappedSql = buildUpdate(values, wrap(selection)); |
| 675 | sql = wrappedSql; |
| 676 | } else { |
| 677 | // Execute unwrapped query |
| 678 | sql = unwrappedSql; |
| 679 | } |
| 680 | |
Jeff Sharkey | 42122bf | 2018-07-26 09:39:18 -0600 | [diff] [blame] | 681 | if (selectionArgs == null) { |
| 682 | selectionArgs = EmptyArray.STRING; |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 683 | } |
Jeff Sharkey | 42122bf | 2018-07-26 09:39:18 -0600 | [diff] [blame] | 684 | final ArrayMap<String, Object> rawValues = values.getValues(); |
| 685 | final int valuesLength = rawValues.size(); |
| 686 | final Object[] sqlArgs = new Object[valuesLength + selectionArgs.length]; |
| 687 | for (int i = 0; i < sqlArgs.length; i++) { |
| 688 | if (i < valuesLength) { |
| 689 | sqlArgs[i] = rawValues.valueAt(i); |
| 690 | } else { |
| 691 | sqlArgs[i] = selectionArgs[i - valuesLength]; |
| 692 | } |
| 693 | } |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 694 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 695 | if (Build.IS_DEBUGGABLE) { |
| 696 | Log.d(TAG, sql + " with args " + Arrays.toString(sqlArgs)); |
| 697 | } else { |
| 698 | Log.d(TAG, sql); |
| 699 | } |
| 700 | } |
| 701 | return db.executeSql(sql, sqlArgs); |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * Perform a delete by combining all current settings and the |
| 706 | * information passed into this method. |
| 707 | * |
| 708 | * @param db the database to delete on |
| 709 | * @param selection A filter declaring which rows to return, |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 710 | * formatted as an SQL {@code WHERE} clause (excluding the {@code WHERE} |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 711 | * itself). Passing null will return all rows for the given URL. |
| 712 | * @param selectionArgs You may include ?s in selection, which |
| 713 | * will be replaced by the values from selectionArgs, in order |
| 714 | * that they appear in the selection. The values will be bound |
| 715 | * as Strings. |
| 716 | * @return the number of rows deleted |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 717 | */ |
| 718 | public int delete(@NonNull SQLiteDatabase db, @Nullable String selection, |
| 719 | @Nullable String[] selectionArgs) { |
| 720 | Objects.requireNonNull(mTables, "No tables defined"); |
| 721 | Objects.requireNonNull(db, "No database defined"); |
| 722 | |
| 723 | final String sql; |
| 724 | final String unwrappedSql = buildDelete(selection); |
| 725 | |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 726 | if (isStrictGrammar()) { |
| 727 | enforceStrictGrammar(selection, null, null, null, null); |
| 728 | } |
| 729 | if (isStrict()) { |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 730 | // Validate the user-supplied selection to detect syntactic anomalies |
| 731 | // in the selection string that could indicate a SQL injection attempt. |
| 732 | // The idea is to ensure that the selection clause is a valid SQL expression |
| 733 | // by compiling it twice: once wrapped in parentheses and once as |
| 734 | // originally specified. An attacker cannot create an expression that |
| 735 | // would escape the SQL expression while maintaining balanced parentheses |
| 736 | // in both the wrapped and original forms. |
| 737 | |
| 738 | // NOTE: The ordering of the below operations is important; we must |
| 739 | // execute the wrapped query to ensure the untrusted clause has been |
| 740 | // fully isolated. |
| 741 | |
| 742 | // Validate the unwrapped query |
| 743 | db.validateSql(unwrappedSql, null); // will throw if query is invalid |
| 744 | |
| 745 | // Execute wrapped query for extra protection |
| 746 | final String wrappedSql = buildDelete(wrap(selection)); |
| 747 | sql = wrappedSql; |
| 748 | } else { |
| 749 | // Execute unwrapped query |
| 750 | sql = unwrappedSql; |
| 751 | } |
| 752 | |
| 753 | final String[] sqlArgs = selectionArgs; |
| 754 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 755 | if (Build.IS_DEBUGGABLE) { |
| 756 | Log.d(TAG, sql + " with args " + Arrays.toString(sqlArgs)); |
| 757 | } else { |
| 758 | Log.d(TAG, sql); |
| 759 | } |
| 760 | } |
| 761 | return db.executeSql(sql, sqlArgs); |
| 762 | } |
| 763 | |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 764 | private void enforceStrictColumns(@Nullable String[] projection) { |
| 765 | Objects.requireNonNull(mProjectionMap, "No projection map defined"); |
| 766 | |
| 767 | computeProjection(projection); |
| 768 | } |
| 769 | |
| 770 | private void enforceStrictColumns(@NonNull ContentValues values) { |
| 771 | Objects.requireNonNull(mProjectionMap, "No projection map defined"); |
| 772 | |
| 773 | final ArrayMap<String, Object> rawValues = values.getValues(); |
| 774 | for (int i = 0; i < rawValues.size(); i++) { |
| 775 | final String column = rawValues.keyAt(i); |
| 776 | if (!mProjectionMap.containsKey(column)) { |
| 777 | throw new IllegalArgumentException("Invalid column " + column); |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | private void enforceStrictGrammar(@Nullable String selection, @Nullable String groupBy, |
| 783 | @Nullable String having, @Nullable String sortOrder, @Nullable String limit) { |
| 784 | SQLiteTokenizer.tokenize(selection, SQLiteTokenizer.OPTION_NONE, |
Jeff Sharkey | 1949102 | 2019-12-18 17:55:39 -0700 | [diff] [blame] | 785 | this::enforceStrictToken); |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 786 | SQLiteTokenizer.tokenize(groupBy, SQLiteTokenizer.OPTION_NONE, |
Jeff Sharkey | 1949102 | 2019-12-18 17:55:39 -0700 | [diff] [blame] | 787 | this::enforceStrictToken); |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 788 | SQLiteTokenizer.tokenize(having, SQLiteTokenizer.OPTION_NONE, |
Jeff Sharkey | 1949102 | 2019-12-18 17:55:39 -0700 | [diff] [blame] | 789 | this::enforceStrictToken); |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 790 | SQLiteTokenizer.tokenize(sortOrder, SQLiteTokenizer.OPTION_NONE, |
Jeff Sharkey | 1949102 | 2019-12-18 17:55:39 -0700 | [diff] [blame] | 791 | this::enforceStrictToken); |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 792 | SQLiteTokenizer.tokenize(limit, SQLiteTokenizer.OPTION_NONE, |
Jeff Sharkey | 1949102 | 2019-12-18 17:55:39 -0700 | [diff] [blame] | 793 | this::enforceStrictToken); |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 794 | } |
| 795 | |
Jeff Sharkey | 1949102 | 2019-12-18 17:55:39 -0700 | [diff] [blame] | 796 | private void enforceStrictToken(@NonNull String token) { |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 797 | if (isTableOrColumn(token)) return; |
| 798 | if (SQLiteTokenizer.isFunction(token)) return; |
| 799 | if (SQLiteTokenizer.isType(token)) return; |
| 800 | |
Jeff Sharkey | 1949102 | 2019-12-18 17:55:39 -0700 | [diff] [blame] | 801 | // Carefully block any tokens that are attempting to jump across query |
| 802 | // clauses or create subqueries, since they could leak data that should |
| 803 | // have been filtered by the trusted where clause |
| 804 | boolean isAllowedKeyword = SQLiteTokenizer.isKeyword(token); |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 805 | switch (token.toUpperCase(Locale.US)) { |
Jeff Sharkey | 1949102 | 2019-12-18 17:55:39 -0700 | [diff] [blame] | 806 | case "SELECT": |
| 807 | case "FROM": |
| 808 | case "WHERE": |
| 809 | case "GROUP": |
| 810 | case "HAVING": |
| 811 | case "WINDOW": |
| 812 | case "VALUES": |
| 813 | case "ORDER": |
| 814 | case "LIMIT": |
| 815 | isAllowedKeyword = false; |
| 816 | break; |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 817 | } |
Jeff Sharkey | 1949102 | 2019-12-18 17:55:39 -0700 | [diff] [blame] | 818 | if (!isAllowedKeyword) { |
| 819 | throw new IllegalArgumentException("Invalid token " + token); |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 820 | } |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 821 | } |
| 822 | |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 823 | /** |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 824 | * Construct a {@code SELECT} statement suitable for use in a group of |
| 825 | * {@code SELECT} statements that will be joined through {@code UNION} operators |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 826 | * in buildUnionQuery. |
| 827 | * |
| 828 | * @param projectionIn A list of which columns to return. Passing |
| 829 | * null will return all columns, which is discouraged to |
| 830 | * prevent reading data from storage that isn't going to be |
| 831 | * used. |
| 832 | * @param selection A filter declaring which rows to return, |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 833 | * formatted as an SQL {@code WHERE} clause (excluding the {@code WHERE} |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 834 | * itself). Passing null will return all rows for the given |
| 835 | * URL. |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 836 | * @param groupBy A filter declaring how to group rows, formatted |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 837 | * as an SQL {@code GROUP BY} clause (excluding the {@code GROUP BY} itself). |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 838 | * Passing null will cause the rows to not be grouped. |
| 839 | * @param having A filter declare which row groups to include in |
| 840 | * the cursor, if row grouping is being used, formatted as an |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 841 | * SQL {@code HAVING} clause (excluding the {@code HAVING} itself). Passing |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 842 | * null will cause all row groups to be included, and is |
| 843 | * required when row grouping is not being used. |
| 844 | * @param sortOrder How to order the rows, formatted as an SQL |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 845 | * {@code ORDER BY} clause (excluding the {@code ORDER BY} itself). Passing null |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 846 | * will use the default sort order, which may be unordered. |
| 847 | * @param limit Limits the number of rows returned by the query, |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 848 | * formatted as {@code LIMIT} clause. Passing null denotes no {@code LIMIT} clause. |
| 849 | * @return the resulting SQL {@code SELECT} statement |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 850 | */ |
| 851 | public String buildQuery( |
Jonas Schwertfeger | 8402903 | 2010-11-12 11:42:28 +0100 | [diff] [blame] | 852 | String[] projectionIn, String selection, String groupBy, |
| 853 | String having, String sortOrder, String limit) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 854 | String[] projection = computeProjection(projectionIn); |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 855 | String where = computeWhere(selection); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 856 | |
| 857 | return buildQueryString( |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 858 | mDistinct, mTables, projection, where, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 859 | groupBy, having, sortOrder, limit); |
| 860 | } |
| 861 | |
| 862 | /** |
Jonas Schwertfeger | 8402903 | 2010-11-12 11:42:28 +0100 | [diff] [blame] | 863 | * @deprecated This method's signature is misleading since no SQL parameter |
| 864 | * substitution is carried out. The selection arguments parameter does not get |
| 865 | * used at all. To avoid confusion, call |
| 866 | * {@link #buildQuery(String[], String, String, String, String, String)} instead. |
| 867 | */ |
| 868 | @Deprecated |
| 869 | public String buildQuery( |
| 870 | String[] projectionIn, String selection, String[] selectionArgs, |
| 871 | String groupBy, String having, String sortOrder, String limit) { |
| 872 | return buildQuery(projectionIn, selection, groupBy, having, sortOrder, limit); |
| 873 | } |
| 874 | |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 875 | /** {@hide} */ |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 876 | public String buildInsert(ContentValues values) { |
| 877 | if (values == null || values.isEmpty()) { |
| 878 | throw new IllegalArgumentException("Empty values"); |
| 879 | } |
| 880 | |
| 881 | StringBuilder sql = new StringBuilder(120); |
| 882 | sql.append("INSERT INTO "); |
| 883 | sql.append(SQLiteDatabase.findEditTable(mTables)); |
| 884 | sql.append(" ("); |
| 885 | |
| 886 | final ArrayMap<String, Object> rawValues = values.getValues(); |
| 887 | for (int i = 0; i < rawValues.size(); i++) { |
| 888 | if (i > 0) { |
| 889 | sql.append(','); |
| 890 | } |
| 891 | sql.append(rawValues.keyAt(i)); |
| 892 | } |
| 893 | sql.append(") VALUES ("); |
| 894 | for (int i = 0; i < rawValues.size(); i++) { |
| 895 | if (i > 0) { |
| 896 | sql.append(','); |
| 897 | } |
| 898 | sql.append('?'); |
| 899 | } |
| 900 | sql.append(")"); |
| 901 | return sql.toString(); |
| 902 | } |
| 903 | |
| 904 | /** {@hide} */ |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 905 | public String buildUpdate(ContentValues values, String selection) { |
| 906 | if (values == null || values.isEmpty()) { |
| 907 | throw new IllegalArgumentException("Empty values"); |
| 908 | } |
| 909 | |
| 910 | StringBuilder sql = new StringBuilder(120); |
| 911 | sql.append("UPDATE "); |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 912 | sql.append(SQLiteDatabase.findEditTable(mTables)); |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 913 | sql.append(" SET "); |
| 914 | |
| 915 | final ArrayMap<String, Object> rawValues = values.getValues(); |
| 916 | for (int i = 0; i < rawValues.size(); i++) { |
| 917 | if (i > 0) { |
| 918 | sql.append(','); |
| 919 | } |
| 920 | sql.append(rawValues.keyAt(i)); |
| 921 | sql.append("=?"); |
| 922 | } |
| 923 | |
| 924 | final String where = computeWhere(selection); |
| 925 | appendClause(sql, " WHERE ", where); |
| 926 | return sql.toString(); |
| 927 | } |
| 928 | |
| 929 | /** {@hide} */ |
| 930 | public String buildDelete(String selection) { |
| 931 | StringBuilder sql = new StringBuilder(120); |
| 932 | sql.append("DELETE FROM "); |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 933 | sql.append(SQLiteDatabase.findEditTable(mTables)); |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 934 | |
| 935 | final String where = computeWhere(selection); |
| 936 | appendClause(sql, " WHERE ", where); |
| 937 | return sql.toString(); |
| 938 | } |
| 939 | |
Jonas Schwertfeger | 8402903 | 2010-11-12 11:42:28 +0100 | [diff] [blame] | 940 | /** |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 941 | * Construct a {@code SELECT} statement suitable for use in a group of |
| 942 | * {@code SELECT} statements that will be joined through {@code UNION} operators |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 943 | * in buildUnionQuery. |
| 944 | * |
| 945 | * @param typeDiscriminatorColumn the name of the result column |
| 946 | * whose cells will contain the name of the table from which |
| 947 | * each row was drawn. |
| 948 | * @param unionColumns the names of the columns to appear in the |
| 949 | * result. This may include columns that do not appear in the |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 950 | * table this {@code SELECT} is querying (i.e. mTables), but that do |
| 951 | * appear in one of the other tables in the {@code UNION} query that we |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 952 | * are constructing. |
| 953 | * @param columnsPresentInTable a Set of the names of the columns |
| 954 | * that appear in this table (i.e. in the table whose name is |
| 955 | * mTables). Since columns in unionColumns include columns that |
| 956 | * appear only in other tables, we use this array to distinguish |
| 957 | * which ones actually are present. Other columns will have |
| 958 | * NULL values for results from this subquery. |
| 959 | * @param computedColumnsOffset all columns in unionColumns before |
| 960 | * this index are included under the assumption that they're |
| 961 | * computed and therefore won't appear in columnsPresentInTable, |
| 962 | * e.g. "date * 1000 as normalized_date" |
| 963 | * @param typeDiscriminatorValue the value used for the |
| 964 | * type-discriminator column in this subquery |
| 965 | * @param selection A filter declaring which rows to return, |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 966 | * formatted as an SQL {@code WHERE} clause (excluding the {@code WHERE} |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 967 | * itself). Passing null will return all rows for the given |
| 968 | * URL. |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 969 | * @param groupBy A filter declaring how to group rows, formatted |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 970 | * as an SQL {@code GROUP BY} clause (excluding the {@code GROUP BY} itself). |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 971 | * Passing null will cause the rows to not be grouped. |
| 972 | * @param having A filter declare which row groups to include in |
| 973 | * the cursor, if row grouping is being used, formatted as an |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 974 | * SQL {@code HAVING} clause (excluding the {@code HAVING} itself). Passing |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 975 | * null will cause all row groups to be included, and is |
| 976 | * required when row grouping is not being used. |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 977 | * @return the resulting SQL {@code SELECT} statement |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 978 | */ |
| 979 | public String buildUnionSubQuery( |
| 980 | String typeDiscriminatorColumn, |
| 981 | String[] unionColumns, |
| 982 | Set<String> columnsPresentInTable, |
| 983 | int computedColumnsOffset, |
| 984 | String typeDiscriminatorValue, |
| 985 | String selection, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 986 | String groupBy, |
| 987 | String having) { |
| 988 | int unionColumnsCount = unionColumns.length; |
| 989 | String[] projectionIn = new String[unionColumnsCount]; |
| 990 | |
| 991 | for (int i = 0; i < unionColumnsCount; i++) { |
| 992 | String unionColumn = unionColumns[i]; |
| 993 | |
| 994 | if (unionColumn.equals(typeDiscriminatorColumn)) { |
| 995 | projectionIn[i] = "'" + typeDiscriminatorValue + "' AS " |
| 996 | + typeDiscriminatorColumn; |
| 997 | } else if (i <= computedColumnsOffset |
| 998 | || columnsPresentInTable.contains(unionColumn)) { |
| 999 | projectionIn[i] = unionColumn; |
| 1000 | } else { |
| 1001 | projectionIn[i] = "NULL AS " + unionColumn; |
| 1002 | } |
| 1003 | } |
| 1004 | return buildQuery( |
Jonas Schwertfeger | 8402903 | 2010-11-12 11:42:28 +0100 | [diff] [blame] | 1005 | projectionIn, selection, groupBy, having, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1006 | null /* sortOrder */, |
| 1007 | null /* limit */); |
| 1008 | } |
| 1009 | |
| 1010 | /** |
Jonas Schwertfeger | 8402903 | 2010-11-12 11:42:28 +0100 | [diff] [blame] | 1011 | * @deprecated This method's signature is misleading since no SQL parameter |
| 1012 | * substitution is carried out. The selection arguments parameter does not get |
| 1013 | * used at all. To avoid confusion, call |
Jean-Baptiste Queru | f4072fc | 2010-11-17 16:47:59 -0800 | [diff] [blame] | 1014 | * {@link #buildUnionSubQuery} |
Jonas Schwertfeger | 8402903 | 2010-11-12 11:42:28 +0100 | [diff] [blame] | 1015 | * instead. |
| 1016 | */ |
| 1017 | @Deprecated |
| 1018 | public String buildUnionSubQuery( |
| 1019 | String typeDiscriminatorColumn, |
| 1020 | String[] unionColumns, |
| 1021 | Set<String> columnsPresentInTable, |
| 1022 | int computedColumnsOffset, |
| 1023 | String typeDiscriminatorValue, |
| 1024 | String selection, |
| 1025 | String[] selectionArgs, |
| 1026 | String groupBy, |
| 1027 | String having) { |
| 1028 | return buildUnionSubQuery( |
| 1029 | typeDiscriminatorColumn, unionColumns, columnsPresentInTable, |
| 1030 | computedColumnsOffset, typeDiscriminatorValue, selection, |
| 1031 | groupBy, having); |
| 1032 | } |
| 1033 | |
| 1034 | /** |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 1035 | * Given a set of subqueries, all of which are {@code SELECT} statements, |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1036 | * construct a query that returns the union of what those |
| 1037 | * subqueries return. |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 1038 | * @param subQueries an array of SQL {@code SELECT} statements, all of |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1039 | * which must have the same columns as the same positions in |
| 1040 | * their results |
| 1041 | * @param sortOrder How to order the rows, formatted as an SQL |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 1042 | * {@code ORDER BY} clause (excluding the {@code ORDER BY} itself). Passing |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1043 | * null will use the default sort order, which may be unordered. |
| 1044 | * @param limit The limit clause, which applies to the entire union result set |
| 1045 | * |
Jeff Sharkey | b91eaa5 | 2019-02-19 11:09:13 -0700 | [diff] [blame] | 1046 | * @return the resulting SQL {@code SELECT} statement |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1047 | */ |
| 1048 | public String buildUnionQuery(String[] subQueries, String sortOrder, String limit) { |
| 1049 | StringBuilder query = new StringBuilder(128); |
| 1050 | int subQueryCount = subQueries.length; |
| 1051 | String unionOperator = mDistinct ? " UNION " : " UNION ALL "; |
| 1052 | |
| 1053 | for (int i = 0; i < subQueryCount; i++) { |
| 1054 | if (i > 0) { |
| 1055 | query.append(unionOperator); |
| 1056 | } |
| 1057 | query.append(subQueries[i]); |
| 1058 | } |
| 1059 | appendClause(query, " ORDER BY ", sortOrder); |
| 1060 | appendClause(query, " LIMIT ", limit); |
| 1061 | return query.toString(); |
| 1062 | } |
| 1063 | |
Jeff Sharkey | 82d783c | 2019-03-29 15:46:35 -0600 | [diff] [blame] | 1064 | private static @NonNull String maybeWithOperator(@Nullable String operator, |
| 1065 | @NonNull String column) { |
| 1066 | if (operator != null) { |
| 1067 | return operator + "(" + column + ")"; |
| 1068 | } else { |
| 1069 | return column; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | /** {@hide} */ |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 1074 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 1075 | public @Nullable String[] computeProjection(@Nullable String[] projectionIn) { |
| 1076 | if (!ArrayUtils.isEmpty(projectionIn)) { |
| 1077 | String[] projectionOut = new String[projectionIn.length]; |
| 1078 | for (int i = 0; i < projectionIn.length; i++) { |
| 1079 | projectionOut[i] = computeSingleProjectionOrThrow(projectionIn[i]); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1080 | } |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 1081 | return projectionOut; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1082 | } else if (mProjectionMap != null) { |
| 1083 | // Return all columns in projection map. |
| 1084 | Set<Entry<String, String>> entrySet = mProjectionMap.entrySet(); |
| 1085 | String[] projection = new String[entrySet.size()]; |
| 1086 | Iterator<Entry<String, String>> entryIter = entrySet.iterator(); |
| 1087 | int i = 0; |
| 1088 | |
| 1089 | while (entryIter.hasNext()) { |
| 1090 | Entry<String, String> entry = entryIter.next(); |
| 1091 | |
| 1092 | // Don't include the _count column when people ask for no projection. |
| 1093 | if (entry.getKey().equals(BaseColumns._COUNT)) { |
| 1094 | continue; |
| 1095 | } |
| 1096 | projection[i++] = entry.getValue(); |
| 1097 | } |
| 1098 | return projection; |
| 1099 | } |
| 1100 | return null; |
| 1101 | } |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 1102 | |
Jeff Sharkey | ae6a466 | 2019-07-16 16:50:42 -0600 | [diff] [blame] | 1103 | private @NonNull String computeSingleProjectionOrThrow(@NonNull String userColumn) { |
| 1104 | final String column = computeSingleProjection(userColumn); |
| 1105 | if (column != null) { |
| 1106 | return column; |
| 1107 | } else { |
| 1108 | throw new IllegalArgumentException("Invalid column " + userColumn); |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | private @Nullable String computeSingleProjection(@NonNull String userColumn) { |
| 1113 | // When no mapping provided, anything goes |
| 1114 | if (mProjectionMap == null) { |
| 1115 | return userColumn; |
| 1116 | } |
| 1117 | |
| 1118 | String operator = null; |
| 1119 | String column = mProjectionMap.get(userColumn); |
| 1120 | |
| 1121 | // When no direct match found, look for aggregation |
| 1122 | if (column == null) { |
| 1123 | final Matcher matcher = sAggregationPattern.matcher(userColumn); |
| 1124 | if (matcher.matches()) { |
| 1125 | operator = matcher.group(1); |
| 1126 | userColumn = matcher.group(2); |
| 1127 | column = mProjectionMap.get(userColumn); |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | if (column != null) { |
| 1132 | return maybeWithOperator(operator, column); |
| 1133 | } |
| 1134 | |
| 1135 | if (mStrictFlags == 0 && |
| 1136 | (userColumn.contains(" AS ") || userColumn.contains(" as "))) { |
| 1137 | /* A column alias already exist */ |
| 1138 | return maybeWithOperator(operator, userColumn); |
| 1139 | } |
| 1140 | |
| 1141 | // If greylist is configured, we might be willing to let |
| 1142 | // this custom column bypass our strict checks. |
| 1143 | if (mProjectionGreylist != null) { |
| 1144 | boolean match = false; |
| 1145 | for (Pattern p : mProjectionGreylist) { |
| 1146 | if (p.matcher(userColumn).matches()) { |
| 1147 | match = true; |
| 1148 | break; |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | if (match) { |
| 1153 | Log.w(TAG, "Allowing abusive custom column: " + userColumn); |
| 1154 | return maybeWithOperator(operator, userColumn); |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | return null; |
| 1159 | } |
| 1160 | |
| 1161 | private boolean isTableOrColumn(String token) { |
| 1162 | if (mTables.equals(token)) return true; |
| 1163 | return computeSingleProjection(token) != null; |
| 1164 | } |
| 1165 | |
Jeff Sharkey | 82d783c | 2019-03-29 15:46:35 -0600 | [diff] [blame] | 1166 | /** {@hide} */ |
| 1167 | public @Nullable String computeWhere(@Nullable String selection) { |
Jeff Sharkey | b13ea30 | 2018-07-25 14:52:14 -0600 | [diff] [blame] | 1168 | final boolean hasInternal = !TextUtils.isEmpty(mWhereClause); |
| 1169 | final boolean hasExternal = !TextUtils.isEmpty(selection); |
| 1170 | |
| 1171 | if (hasInternal || hasExternal) { |
| 1172 | final StringBuilder where = new StringBuilder(); |
| 1173 | if (hasInternal) { |
| 1174 | where.append('(').append(mWhereClause).append(')'); |
| 1175 | } |
| 1176 | if (hasInternal && hasExternal) { |
| 1177 | where.append(" AND "); |
| 1178 | } |
| 1179 | if (hasExternal) { |
| 1180 | where.append('(').append(selection).append(')'); |
| 1181 | } |
| 1182 | return where.toString(); |
| 1183 | } else { |
| 1184 | return null; |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | /** |
| 1189 | * Wrap given argument in parenthesis, unless it's {@code null} or |
| 1190 | * {@code ()}, in which case return it verbatim. |
| 1191 | */ |
| 1192 | private @Nullable String wrap(@Nullable String arg) { |
| 1193 | if (TextUtils.isEmpty(arg)) { |
| 1194 | return arg; |
| 1195 | } else { |
| 1196 | return "(" + arg + ")"; |
| 1197 | } |
| 1198 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1199 | } |