1 /* 2 * Copyright (C) 2011 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 19 import java.util.ArrayList; 20 import java.util.Locale; 21 import java.util.regex.Pattern; 22 23 /** 24 * Describes how to configure a database. 25 * <p> 26 * The purpose of this object is to keep track of all of the little 27 * configuration settings that are applied to a database after it 28 * is opened so that they can be applied to all connections in the 29 * connection pool uniformly. 30 * </p><p> 31 * Each connection maintains its own copy of this object so it can 32 * keep track of which settings have already been applied. 33 * </p> 34 * 35 * @hide 36 */ 37 public final class SQLiteDatabaseConfiguration { 38 // The pattern we use to strip email addresses from database paths 39 // when constructing a label to use in log messages. 40 private static final Pattern EMAIL_IN_DB_PATTERN = 41 Pattern.compile("[\\w\\.\\-]+@[\\w\\.\\-]+"); 42 43 /** 44 * Special path used by in-memory databases. 45 */ 46 public static final String MEMORY_DB_PATH = ":memory:"; 47 48 /** 49 * The database path. 50 */ 51 public final String path; 52 53 /** 54 * The label to use to describe the database when it appears in logs. 55 * This is derived from the path but is stripped to remove PII. 56 */ 57 public final String label; 58 59 /** 60 * The flags used to open the database. 61 */ 62 public int openFlags; 63 64 /** 65 * The maximum size of the prepared statement cache for each database connection. 66 * Must be non-negative. 67 * 68 * Default is 25. 69 */ 70 public int maxSqlCacheSize; 71 72 /** 73 * The database locale. 74 * 75 * Default is the value returned by {@link Locale#getDefault()}. 76 */ 77 public Locale locale; 78 79 /** 80 * True if foreign key constraints are enabled. 81 * 82 * Default is false. 83 */ 84 public boolean foreignKeyConstraintsEnabled; 85 86 /** 87 * The custom functions to register. 88 */ 89 public final ArrayList<SQLiteCustomFunction> customFunctions = 90 new ArrayList<SQLiteCustomFunction>(); 91 92 /** 93 * Creates a database configuration with the required parameters for opening a 94 * database and default values for all other parameters. 95 * 96 * @param path The database path. 97 * @param openFlags Open flags for the database, such as {@link SQLiteDatabase#OPEN_READWRITE}. 98 */ 99 public SQLiteDatabaseConfiguration(String path, int openFlags) { 100 if (path == null) { 101 throw new IllegalArgumentException("path must not be null."); 102 } 103 104 this.path = path; 105 label = stripPathForLogs(path); 106 this.openFlags = openFlags; 107 108 // Set default values for optional parameters. 109 maxSqlCacheSize = 25; 110 locale = Locale.getDefault(); 111 } 112 113 /** 114 * Creates a database configuration as a copy of another configuration. 115 * 116 * @param other The other configuration. 117 */ 118 public SQLiteDatabaseConfiguration(SQLiteDatabaseConfiguration other) { 119 if (other == null) { 120 throw new IllegalArgumentException("other must not be null."); 121 } 122 123 this.path = other.path; 124 this.label = other.label; 125 updateParametersFrom(other); 126 } 127 128 /** 129 * Updates the non-immutable parameters of this configuration object 130 * from the other configuration object. 131 * 132 * @param other The object from which to copy the parameters. 133 */ 134 public void updateParametersFrom(SQLiteDatabaseConfiguration other) { 135 if (other == null) { 136 throw new IllegalArgumentException("other must not be null."); 137 } 138 if (!path.equals(other.path)) { 139 throw new IllegalArgumentException("other configuration must refer to " 140 + "the same database."); 141 } 142 143 openFlags = other.openFlags; 144 maxSqlCacheSize = other.maxSqlCacheSize; 145 locale = other.locale; 146 foreignKeyConstraintsEnabled = other.foreignKeyConstraintsEnabled; 147 customFunctions.clear(); 148 customFunctions.addAll(other.customFunctions); 149 } 150 151 /** 152 * Returns true if the database is in-memory. 153 * @return True if the database is in-memory. 154 */ 155 public boolean isInMemoryDb() { 156 return path.equalsIgnoreCase(MEMORY_DB_PATH); 157 } 158 159 private static String stripPathForLogs(String path) { 160 if (path.indexOf('@') == -1) { 161 return path; 162 } 163 return EMAIL_IN_DB_PATTERN.matcher(path).replaceAll("XX@YY"); 164 } 165 } 166