1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.database.sqlite;
import android.compat.annotation.UnsupportedAppUsage;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Pair;
import java.util.ArrayList;
import java.util.Locale;
import java.util.function.BinaryOperator;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;
/**
* Describes how to configure a database.
* <p>
* The purpose of this object is to keep track of all of the little
* configuration settings that are applied to a database after it
* is opened so that they can be applied to all connections in the
* connection pool uniformly.
* </p><p>
* Each connection maintains its own copy of this object so it can
* keep track of which settings have already been applied.
* </p>
*
* @hide
*/
public final class SQLiteDatabaseConfiguration {
// The pattern we use to strip email addresses from database paths
// when constructing a label to use in log messages.
private static final Pattern EMAIL_IN_DB_PATTERN =
Pattern.compile("[\\w\\.\\-]+@[\\w\\.\\-]+");
/**
* Special path used by in-memory databases.
*/
public static final String MEMORY_DB_PATH = ":memory:";
/**
* The database path.
*/
public final String path;
/**
* The label to use to describe the database when it appears in logs.
* This is derived from the path but is stripped to remove PII.
*/
public final String label;
/**
* The flags used to open the database.
*/
public int openFlags;
/**
* The maximum size of the prepared statement cache for each database connection.
* Must be non-negative.
*
* Default is 25.
*/
@UnsupportedAppUsage
public int maxSqlCacheSize;
/**
* The database locale.
*
* Default is the value returned by {@link Locale#getDefault()}.
*/
public Locale locale;
/**
* True if foreign key constraints are enabled.
*
* Default is false.
*/
public boolean foreignKeyConstraintsEnabled;
/**
* The custom scalar functions to register.
*/
public final ArrayMap<String, UnaryOperator<String>> customScalarFunctions
= new ArrayMap<>();
/**
* The custom aggregate functions to register.
*/
public final ArrayMap<String, BinaryOperator<String>> customAggregateFunctions
= new ArrayMap<>();
/**
* The statements to execute to initialize each connection.
*/
public final ArrayList<Pair<String, Object[]>> perConnectionSql = new ArrayList<>();
/**
* The size in bytes of each lookaside slot
*
* <p>If negative, the default lookaside configuration will be used
*/
public int lookasideSlotSize = -1;
/**
* The total number of lookaside memory slots per database connection
*
* <p>If negative, the default lookaside configuration will be used
*/
public int lookasideSlotCount = -1;
/**
* The number of milliseconds that SQLite connection is allowed to be idle before it
* is closed and removed from the pool.
* <p>By default, idle connections are not closed
*/
public long idleConnectionTimeoutMs = Long.MAX_VALUE;
/**
* Journal mode to use when {@link SQLiteDatabase#ENABLE_WRITE_AHEAD_LOGGING} is not set.
* <p>Default is returned by {@link SQLiteGlobal#getDefaultJournalMode()}
*/
public @SQLiteDatabase.JournalMode String journalMode;
/**
* Synchronous mode to use.
* <p>Default is returned by {@link SQLiteGlobal#getDefaultSyncMode()}
* or {@link SQLiteGlobal#getWALSyncMode()} depending on journal mode
*/
public @SQLiteDatabase.SyncMode String syncMode;
public boolean shouldTruncateWalFile;
/**
* Creates a database configuration with the required parameters for opening a
* database and default values for all other parameters.
*
* @param path The database path.
* @param openFlags Open flags for the database, such as {@link SQLiteDatabase#OPEN_READWRITE}.
*/
public SQLiteDatabaseConfiguration(String path, int openFlags) {
if (path == null) {
throw new IllegalArgumentException("path must not be null.");
}
this.path = path;
label = stripPathForLogs(path);
this.openFlags = openFlags;
// Set default values for optional parameters.
maxSqlCacheSize = 25;
locale = Locale.getDefault();
}
/**
* Creates a database configuration as a copy of another configuration.
*
* @param other The other configuration.
*/
public SQLiteDatabaseConfiguration(SQLiteDatabaseConfiguration other) {
if (other == null) {
throw new IllegalArgumentException("other must not be null.");
}
this.path = other.path;
this.label = other.label;
updateParametersFrom(other);
}
/**
* Updates the non-immutable parameters of this configuration object
* from the other configuration object.
*
* @param other The object from which to copy the parameters.
*/
public void updateParametersFrom(SQLiteDatabaseConfiguration other) {
if (other == null) {
throw new IllegalArgumentException("other must not be null.");
}
if (!path.equals(other.path)) {
throw new IllegalArgumentException("other configuration must refer to "
+ "the same database.");
}
openFlags = other.openFlags;
maxSqlCacheSize = other.maxSqlCacheSize;
locale = other.locale;
foreignKeyConstraintsEnabled = other.foreignKeyConstraintsEnabled;
customScalarFunctions.clear();
customScalarFunctions.putAll(other.customScalarFunctions);
customAggregateFunctions.clear();
customAggregateFunctions.putAll(other.customAggregateFunctions);
perConnectionSql.clear();
perConnectionSql.addAll(other.perConnectionSql);
lookasideSlotSize = other.lookasideSlotSize;
lookasideSlotCount = other.lookasideSlotCount;
idleConnectionTimeoutMs = other.idleConnectionTimeoutMs;
journalMode = other.journalMode;
syncMode = other.syncMode;
}
/**
* Returns true if the database is in-memory.
* @return True if the database is in-memory.
*/
public boolean isInMemoryDb() {
return path.equalsIgnoreCase(MEMORY_DB_PATH);
}
public boolean isReadOnlyDatabase() {
return (openFlags & SQLiteDatabase.OPEN_READONLY) != 0;
}
boolean isLegacyCompatibilityWalEnabled() {
return journalMode == null && syncMode == null
&& (openFlags & SQLiteDatabase.ENABLE_LEGACY_COMPATIBILITY_WAL) != 0;
}
private static String stripPathForLogs(String path) {
if (path.indexOf('@') == -1) {
return path;
}
return EMAIL_IN_DB_PATTERN.matcher(path).replaceAll("XX@YY");
}
boolean isLookasideConfigSet() {
return lookasideSlotCount >= 0 && lookasideSlotSize >= 0;
}
/**
* Resolves the journal mode that should be used when opening a connection to the database.
*
* Note: assumes openFlags have already been set.
*
* @return Resolved journal mode that should be used for this database connection or an empty
* string if no journal mode should be set.
*/
public @SQLiteDatabase.JournalMode String resolveJournalMode() {
if (isReadOnlyDatabase()) {
// No need to specify a journal mode when only reading.
return "";
}
if (isInMemoryDb()) {
if (journalMode != null
&& journalMode.equalsIgnoreCase(SQLiteDatabase.JOURNAL_MODE_OFF)) {
return SQLiteDatabase.JOURNAL_MODE_OFF;
}
return SQLiteDatabase.JOURNAL_MODE_MEMORY;
}
shouldTruncateWalFile = false;
if (isWalEnabledInternal()) {
shouldTruncateWalFile = true;
return SQLiteDatabase.JOURNAL_MODE_WAL;
} else {
// WAL is not explicitly set so use requested journal mode or platform default
return this.journalMode != null ? this.journalMode
: SQLiteGlobal.getDefaultJournalMode();
}
}
/**
* Resolves the sync mode that should be used when opening a connection to the database.
*
* Note: assumes openFlags have already been set.
* @return Resolved journal mode that should be used for this database connection or null
* if no journal mode should be set.
*/
public @SQLiteDatabase.SyncMode String resolveSyncMode() {
if (isReadOnlyDatabase()) {
// No sync mode will be used since database will be only used for reading.
return "";
}
if (isInMemoryDb()) {
// No sync mode will be used since database will be in volatile memory
return "";
}
if (!TextUtils.isEmpty(syncMode)) {
return syncMode;
}
if (isWalEnabledInternal()) {
if (isLegacyCompatibilityWalEnabled()) {
return SQLiteCompatibilityWalFlags.getWALSyncMode();
} else {
return SQLiteGlobal.getWALSyncMode();
}
} else {
return SQLiteGlobal.getDefaultSyncMode();
}
}
private boolean isWalEnabledInternal() {
final boolean walEnabled = (openFlags & SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING) != 0;
// Use compatibility WAL unless an app explicitly set journal/synchronous mode
// or DISABLE_COMPATIBILITY_WAL flag is set
final boolean isCompatibilityWalEnabled = isLegacyCompatibilityWalEnabled();
return walEnabled || isCompatibilityWalEnabled
|| (journalMode != null
&& journalMode.equalsIgnoreCase(SQLiteDatabase.JOURNAL_MODE_WAL));
}
}
|