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