1/*
2 * Copyright (C) 2007 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
17package android.database.sqlite;
18
19import android.content.Context;
20import android.database.DatabaseErrorHandler;
21import android.database.DefaultDatabaseErrorHandler;
22import android.database.sqlite.SQLiteDatabase.CursorFactory;
23import android.util.Log;
24
25/**
26 * A helper class to manage database creation and version management.
27 *
28 * <p>You create a subclass implementing {@link #onCreate}, {@link #onUpgrade} and
29 * optionally {@link #onOpen}, and this class takes care of opening the database
30 * if it exists, creating it if it does not, and upgrading it as necessary.
31 * Transactions are used to make sure the database is always in a sensible state.
32 *
33 * <p>This class makes it easy for {@link android.content.ContentProvider}
34 * implementations to defer opening and upgrading the database until first use,
35 * to avoid blocking application startup with long-running database upgrades.
36 *
37 * <p>For an example, see the NotePadProvider class in the NotePad sample application,
38 * in the <em>samples/</em> directory of the SDK.</p>
39 *
40 * <p class="note"><strong>Note:</strong> this class assumes
41 * monotonically increasing version numbers for upgrades.</p>
42 */
43public abstract class SQLiteOpenHelper {
44    private static final String TAG = SQLiteOpenHelper.class.getSimpleName();
45
46    private final Context mContext;
47    private final String mName;
48    private final CursorFactory mFactory;
49    private final int mNewVersion;
50
51    private SQLiteDatabase mDatabase = null;
52    private boolean mIsInitializing = false;
53    private final DatabaseErrorHandler mErrorHandler;
54
55    /**
56     * Create a helper object to create, open, and/or manage a database.
57     * This method always returns very quickly.  The database is not actually
58     * created or opened until one of {@link #getWritableDatabase} or
59     * {@link #getReadableDatabase} is called.
60     *
61     * @param context to use to open or create the database
62     * @param name of the database file, or null for an in-memory database
63     * @param factory to use for creating cursor objects, or null for the default
64     * @param version number of the database (starting at 1); if the database is older,
65     *     {@link #onUpgrade} will be used to upgrade the database; if the database is
66     *     newer, {@link #onDowngrade} will be used to downgrade the database
67     */
68    public SQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) {
69        this(context, name, factory, version, new DefaultDatabaseErrorHandler());
70    }
71
72    /**
73     * Create a helper object to create, open, and/or manage a database.
74     * The database is not actually created or opened until one of
75     * {@link #getWritableDatabase} or {@link #getReadableDatabase} is called.
76     *
77     * <p>Accepts input param: a concrete instance of {@link DatabaseErrorHandler} to be
78     * used to handle corruption when sqlite reports database corruption.</p>
79     *
80     * @param context to use to open or create the database
81     * @param name of the database file, or null for an in-memory database
82     * @param factory to use for creating cursor objects, or null for the default
83     * @param version number of the database (starting at 1); if the database is older,
84     *     {@link #onUpgrade} will be used to upgrade the database
85     * @param errorHandler the {@link DatabaseErrorHandler} to be used when sqlite reports database
86     * corruption.
87     */
88    public SQLiteOpenHelper(Context context, String name, CursorFactory factory, int version,
89            DatabaseErrorHandler errorHandler) {
90        if (version < 1) throw new IllegalArgumentException("Version must be >= 1, was " + version);
91        if (errorHandler == null) {
92            throw new IllegalArgumentException("DatabaseErrorHandler param value can't be null.");
93        }
94
95        mContext = context;
96        mName = name;
97        mFactory = factory;
98        mNewVersion = version;
99        mErrorHandler = errorHandler;
100    }
101
102    /**
103     * Return the name of the SQLite database being opened, as given tp
104     * the constructor.
105     */
106    public String getDatabaseName() {
107        return mName;
108    }
109
110    /**
111     * Create and/or open a database that will be used for reading and writing.
112     * The first time this is called, the database will be opened and
113     * {@link #onCreate}, {@link #onUpgrade} and/or {@link #onOpen} will be
114     * called.
115     *
116     * <p>Once opened successfully, the database is cached, so you can
117     * call this method every time you need to write to the database.
118     * (Make sure to call {@link #close} when you no longer need the database.)
119     * Errors such as bad permissions or a full disk may cause this method
120     * to fail, but future attempts may succeed if the problem is fixed.</p>
121     *
122     * <p class="caution">Database upgrade may take a long time, you
123     * should not call this method from the application main thread, including
124     * from {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
125     *
126     * @throws SQLiteException if the database cannot be opened for writing
127     * @return a read/write database object valid until {@link #close} is called
128     */
129    public synchronized SQLiteDatabase getWritableDatabase() {
130        if (mDatabase != null) {
131            if (!mDatabase.isOpen()) {
132                // darn! the user closed the database by calling mDatabase.close()
133                mDatabase = null;
134            } else if (!mDatabase.isReadOnly()) {
135                return mDatabase;  // The database is already open for business
136            }
137        }
138
139        if (mIsInitializing) {
140            throw new IllegalStateException("getWritableDatabase called recursively");
141        }
142
143        // If we have a read-only database open, someone could be using it
144        // (though they shouldn't), which would cause a lock to be held on
145        // the file, and our attempts to open the database read-write would
146        // fail waiting for the file lock.  To prevent that, we acquire the
147        // lock on the read-only database, which shuts out other users.
148
149        boolean success = false;
150        SQLiteDatabase db = null;
151        if (mDatabase != null) mDatabase.lock();
152        try {
153            mIsInitializing = true;
154            if (mName == null) {
155                db = SQLiteDatabase.create(null);
156            } else {
157                db = mContext.openOrCreateDatabase(mName, 0, mFactory, mErrorHandler);
158            }
159
160            int version = db.getVersion();
161            if (version != mNewVersion) {
162                db.beginTransaction();
163                try {
164                    if (version == 0) {
165                        onCreate(db);
166                    } else {
167                        if (version > mNewVersion) {
168                            onDowngrade(db, version, mNewVersion);
169                        } else {
170                            onUpgrade(db, version, mNewVersion);
171                        }
172                    }
173                    db.setVersion(mNewVersion);
174                    db.setTransactionSuccessful();
175                } finally {
176                    db.endTransaction();
177                }
178            }
179
180            onOpen(db);
181            success = true;
182            return db;
183        } finally {
184            mIsInitializing = false;
185            if (success) {
186                if (mDatabase != null) {
187                    try { mDatabase.close(); } catch (Exception e) { }
188                    mDatabase.unlock();
189                }
190                mDatabase = db;
191            } else {
192                if (mDatabase != null) mDatabase.unlock();
193                if (db != null) db.close();
194            }
195        }
196    }
197
198    /**
199     * Create and/or open a database.  This will be the same object returned by
200     * {@link #getWritableDatabase} unless some problem, such as a full disk,
201     * requires the database to be opened read-only.  In that case, a read-only
202     * database object will be returned.  If the problem is fixed, a future call
203     * to {@link #getWritableDatabase} may succeed, in which case the read-only
204     * database object will be closed and the read/write object will be returned
205     * in the future.
206     *
207     * <p class="caution">Like {@link #getWritableDatabase}, this method may
208     * take a long time to return, so you should not call it from the
209     * application main thread, including from
210     * {@link android.content.ContentProvider#onCreate ContentProvider.onCreate()}.
211     *
212     * @throws SQLiteException if the database cannot be opened
213     * @return a database object valid until {@link #getWritableDatabase}
214     *     or {@link #close} is called.
215     */
216    public synchronized SQLiteDatabase getReadableDatabase() {
217        if (mDatabase != null) {
218            if (!mDatabase.isOpen()) {
219                // darn! the user closed the database by calling mDatabase.close()
220                mDatabase = null;
221            } else {
222                return mDatabase;  // The database is already open for business
223            }
224        }
225
226        if (mIsInitializing) {
227            throw new IllegalStateException("getReadableDatabase called recursively");
228        }
229
230        try {
231            return getWritableDatabase();
232        } catch (SQLiteException e) {
233            if (mName == null) throw e;  // Can't open a temp database read-only!
234            Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);
235        }
236
237        SQLiteDatabase db = null;
238        try {
239            mIsInitializing = true;
240            String path = mContext.getDatabasePath(mName).getPath();
241            db = SQLiteDatabase.openDatabase(path, mFactory, SQLiteDatabase.OPEN_READONLY,
242                    mErrorHandler);
243            if (db.getVersion() != mNewVersion) {
244                throw new SQLiteException("Can't upgrade read-only database from version " +
245                        db.getVersion() + " to " + mNewVersion + ": " + path);
246            }
247
248            onOpen(db);
249            Log.w(TAG, "Opened " + mName + " in read-only mode");
250            mDatabase = db;
251            return mDatabase;
252        } finally {
253            mIsInitializing = false;
254            if (db != null && db != mDatabase) db.close();
255        }
256    }
257
258    /**
259     * Close any open database object.
260     */
261    public synchronized void close() {
262        if (mIsInitializing) throw new IllegalStateException("Closed during initialization");
263
264        if (mDatabase != null && mDatabase.isOpen()) {
265            mDatabase.close();
266            mDatabase = null;
267        }
268    }
269
270    /**
271     * Called when the database is created for the first time. This is where the
272     * creation of tables and the initial population of the tables should happen.
273     *
274     * @param db The database.
275     */
276    public abstract void onCreate(SQLiteDatabase db);
277
278    /**
279     * Called when the database needs to be upgraded. The implementation
280     * should use this method to drop tables, add tables, or do anything else it
281     * needs to upgrade to the new schema version.
282     *
283     * <p>The SQLite ALTER TABLE documentation can be found
284     * <a href="http://sqlite.org/lang_altertable.html">here</a>. If you add new columns
285     * you can use ALTER TABLE to insert them into a live table. If you rename or remove columns
286     * you can use ALTER TABLE to rename the old table, then create the new table and then
287     * populate the new table with the contents of the old table.
288     *
289     * @param db The database.
290     * @param oldVersion The old database version.
291     * @param newVersion The new database version.
292     */
293    public abstract void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion);
294
295    /**
296     * Called when the database needs to be downgraded. This is stricly similar to
297     * onUpgrade() method, but is called whenever current version is newer than requested one.
298     * However, this method is not abstract, so it is not mandatory for a customer to
299     * implement it. If not overridden, default implementation will reject downgrade and
300     * throws SQLiteException
301     *
302     * @param db The database.
303     * @param oldVersion The old database version.
304     * @param newVersion The new database version.
305     */
306    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
307        throw new SQLiteException("Can't downgrade database from version " +
308                oldVersion + " to " + newVersion);
309    }
310
311    /**
312     * Called when the database has been opened.  The implementation
313     * should check {@link SQLiteDatabase#isReadOnly} before updating the
314     * database.
315     *
316     * @param db The database.
317     */
318    public void onOpen(SQLiteDatabase db) {}
319}
320