| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.database.sqlite; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.text.TextUtils; |
| 21 |
import android.util.ArrayMap; |
| 22 |
import android.util.Pair; |
| 23 |
import java.util.ArrayList; |
| 24 |
import java.util.Locale; |
| 25 |
import java.util.function.BinaryOperator; |
| 26 |
import java.util.function.UnaryOperator; |
| 27 |
import java.util.regex.Pattern; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public final class SQLiteDatabaseConfiguration { |
| 44 |
|
| 45 |
|
| 46 |
private static final Pattern EMAIL_IN_DB_PATTERN = |
| 47 |
Pattern.compile("[\\w\\.\\-]+@[\\w\\.\\-]+"); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public static final String MEMORY_DB_PATH = ":memory:"; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public final String path; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public final String label; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public int openFlags; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
@UnsupportedAppUsage |
| 77 |
public int maxSqlCacheSize; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public Locale locale; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public boolean foreignKeyConstraintsEnabled; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public final ArrayMap<String, UnaryOperator<String>> customScalarFunctions |
| 97 |
= new ArrayMap<>(); |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public final ArrayMap<String, BinaryOperator<String>> customAggregateFunctions |
| 103 |
= new ArrayMap<>(); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public final ArrayList<Pair<String, Object[]>> perConnectionSql = new ArrayList<>(); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public int lookasideSlotSize = -1; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public int lookasideSlotCount = -1; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public long idleConnectionTimeoutMs = Long.MAX_VALUE; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public @SQLiteDatabase.JournalMode String journalMode; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public @SQLiteDatabase.SyncMode String syncMode; |
| 143 |
|
| 144 |
public boolean shouldTruncateWalFile; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public SQLiteDatabaseConfiguration(String path, int openFlags) { |
| 154 |
if (path == null) { |
| 155 |
throw new IllegalArgumentException("path must not be null."); |
| 156 |
} |
| 157 |
|
| 158 |
this.path = path; |
| 159 |
label = stripPathForLogs(path); |
| 160 |
this.openFlags = openFlags; |
| 161 |
|
| 162 |
|
| 163 |
maxSqlCacheSize = 25; |
| 164 |
locale = Locale.getDefault(); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public SQLiteDatabaseConfiguration(SQLiteDatabaseConfiguration other) { |
| 173 |
if (other == null) { |
| 174 |
throw new IllegalArgumentException("other must not be null."); |
| 175 |
} |
| 176 |
|
| 177 |
this.path = other.path; |
| 178 |
this.label = other.label; |
| 179 |
updateParametersFrom(other); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public void updateParametersFrom(SQLiteDatabaseConfiguration other) { |
| 189 |
if (other == null) { |
| 190 |
throw new IllegalArgumentException("other must not be null."); |
| 191 |
} |
| 192 |
if (!path.equals(other.path)) { |
| 193 |
throw new IllegalArgumentException("other configuration must refer to " |
| 194 |
+ "the same database."); |
| 195 |
} |
| 196 |
|
| 197 |
openFlags = other.openFlags; |
| 198 |
maxSqlCacheSize = other.maxSqlCacheSize; |
| 199 |
locale = other.locale; |
| 200 |
foreignKeyConstraintsEnabled = other.foreignKeyConstraintsEnabled; |
| 201 |
customScalarFunctions.clear(); |
| 202 |
customScalarFunctions.putAll(other.customScalarFunctions); |
| 203 |
customAggregateFunctions.clear(); |
| 204 |
customAggregateFunctions.putAll(other.customAggregateFunctions); |
| 205 |
perConnectionSql.clear(); |
| 206 |
perConnectionSql.addAll(other.perConnectionSql); |
| 207 |
lookasideSlotSize = other.lookasideSlotSize; |
| 208 |
lookasideSlotCount = other.lookasideSlotCount; |
| 209 |
idleConnectionTimeoutMs = other.idleConnectionTimeoutMs; |
| 210 |
journalMode = other.journalMode; |
| 211 |
syncMode = other.syncMode; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public boolean isInMemoryDb() { |
| 219 |
return path.equalsIgnoreCase(MEMORY_DB_PATH); |
| 220 |
} |
| 221 |
|
| 222 |
public boolean isReadOnlyDatabase() { |
| 223 |
return (openFlags & SQLiteDatabase.OPEN_READONLY) != 0; |
| 224 |
} |
| 225 |
|
| 226 |
boolean isLegacyCompatibilityWalEnabled() { |
| 227 |
return journalMode == null && syncMode == null |
| 228 |
&& (openFlags & SQLiteDatabase.ENABLE_LEGACY_COMPATIBILITY_WAL) != 0; |
| 229 |
} |
| 230 |
|
| 231 |
private static String stripPathForLogs(String path) { |
| 232 |
if (path.indexOf('@') == -1) { |
| 233 |
return path; |
| 234 |
} |
| 235 |
return EMAIL_IN_DB_PATTERN.matcher(path).replaceAll("XX@YY"); |
| 236 |
} |
| 237 |
|
| 238 |
boolean isLookasideConfigSet() { |
| 239 |
return lookasideSlotCount >= 0 && lookasideSlotSize >= 0; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
public @SQLiteDatabase.JournalMode String resolveJournalMode() { |
| 251 |
if (isReadOnlyDatabase()) { |
| 252 |
|
| 253 |
return ""; |
| 254 |
} |
| 255 |
|
| 256 |
if (isInMemoryDb()) { |
| 257 |
if (journalMode != null |
| 258 |
&& journalMode.equalsIgnoreCase(SQLiteDatabase.JOURNAL_MODE_OFF)) { |
| 259 |
return SQLiteDatabase.JOURNAL_MODE_OFF; |
| 260 |
} |
| 261 |
return SQLiteDatabase.JOURNAL_MODE_MEMORY; |
| 262 |
} |
| 263 |
|
| 264 |
shouldTruncateWalFile = false; |
| 265 |
|
| 266 |
if (isWalEnabledInternal()) { |
| 267 |
shouldTruncateWalFile = true; |
| 268 |
return SQLiteDatabase.JOURNAL_MODE_WAL; |
| 269 |
} else { |
| 270 |
|
| 271 |
return this.journalMode != null ? this.journalMode |
| 272 |
: SQLiteGlobal.getDefaultJournalMode(); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public @SQLiteDatabase.SyncMode String resolveSyncMode() { |
| 284 |
if (isReadOnlyDatabase()) { |
| 285 |
|
| 286 |
return ""; |
| 287 |
} |
| 288 |
|
| 289 |
if (isInMemoryDb()) { |
| 290 |
|
| 291 |
return ""; |
| 292 |
} |
| 293 |
|
| 294 |
if (!TextUtils.isEmpty(syncMode)) { |
| 295 |
return syncMode; |
| 296 |
} |
| 297 |
|
| 298 |
if (isWalEnabledInternal()) { |
| 299 |
if (isLegacyCompatibilityWalEnabled()) { |
| 300 |
return SQLiteCompatibilityWalFlags.getWALSyncMode(); |
| 301 |
} else { |
| 302 |
return SQLiteGlobal.getWALSyncMode(); |
| 303 |
} |
| 304 |
} else { |
| 305 |
return SQLiteGlobal.getDefaultSyncMode(); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
private boolean isWalEnabledInternal() { |
| 310 |
final boolean walEnabled = (openFlags & SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING) != 0; |
| 311 |
|
| 312 |
|
| 313 |
final boolean isCompatibilityWalEnabled = isLegacyCompatibilityWalEnabled(); |
| 314 |
return walEnabled || isCompatibilityWalEnabled |
| 315 |
|| (journalMode != null |
| 316 |
&& journalMode.equalsIgnoreCase(SQLiteDatabase.JOURNAL_MODE_WAL)); |
| 317 |
} |
| 318 |
} |