1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 */ 6 7 package org.mozilla.gecko.db; 8 9 import org.mozilla.gecko.db.BrowserContract.Bookmarks; 10 import org.mozilla.gecko.db.BrowserContract.History; 11 12 import android.database.sqlite.SQLiteDatabase; 13 import android.net.Uri; 14 15 // Holds metadata info about urls. Supports some helper functions for getting back a HashMap of key value data. 16 public class URLMetadataTable extends BaseTable { 17 private static final String LOGTAG = "GeckoURLMetadataTable"; 18 19 private static final String TABLE = "metadata"; // Name of the table in the db 20 private static final int TABLE_ID_NUMBER = BrowserProvider.METADATA; 21 22 // Uri for querying this table 23 public static final Uri CONTENT_URI = Uri.withAppendedPath(BrowserContract.AUTHORITY_URI, "metadata"); 24 25 // Columns in the table 26 public static final String ID_COLUMN = "id"; 27 public static final String URL_COLUMN = "url"; 28 public static final String TILE_IMAGE_URL_COLUMN = "tileImage"; 29 public static final String TILE_COLOR_COLUMN = "tileColor"; 30 public static final String TOUCH_ICON_COLUMN = "touchIcon"; 31 URLMetadataTable()32 URLMetadataTable() { } 33 34 @Override getTable()35 protected String getTable() { 36 return TABLE; 37 } 38 39 @Override onCreate(SQLiteDatabase db)40 public void onCreate(SQLiteDatabase db) { 41 String create = "CREATE TABLE " + TABLE + " (" + 42 ID_COLUMN + " INTEGER PRIMARY KEY, " + 43 URL_COLUMN + " TEXT NON NULL UNIQUE, " + 44 TILE_IMAGE_URL_COLUMN + " STRING, " + 45 TILE_COLOR_COLUMN + " STRING, " + 46 TOUCH_ICON_COLUMN + " STRING);"; 47 db.execSQL(create); 48 } 49 upgradeDatabaseFrom26To27(SQLiteDatabase db)50 private void upgradeDatabaseFrom26To27(SQLiteDatabase db) { 51 db.execSQL("ALTER TABLE " + TABLE + 52 " ADD COLUMN " + TOUCH_ICON_COLUMN + " STRING"); 53 } 54 55 @Override onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)56 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 57 // This table was added in v21 of the db. Force its creation if we're coming from an earlier version 58 if (newVersion >= 21 && oldVersion < 21) { 59 onCreate(db); 60 return; 61 } 62 63 // Removed the redundant metadata_url_idx index in version 26 64 if (newVersion >= 26 && oldVersion < 26) { 65 db.execSQL("DROP INDEX IF EXISTS metadata_url_idx"); 66 } 67 if (newVersion >= 27 && oldVersion < 27) { 68 upgradeDatabaseFrom26To27(db); 69 } 70 } 71 72 @Override getContentProviderInfo()73 public Table.ContentProviderInfo[] getContentProviderInfo() { 74 return new Table.ContentProviderInfo[] { 75 new Table.ContentProviderInfo(TABLE_ID_NUMBER, TABLE) 76 }; 77 } 78 deleteUnused(final SQLiteDatabase db)79 public int deleteUnused(final SQLiteDatabase db) { 80 final String selection = URL_COLUMN + " NOT IN " + 81 "(SELECT " + History.URL + 82 " FROM " + History.TABLE_NAME + 83 " WHERE " + History.IS_DELETED + " = 0" + 84 " UNION " + 85 " SELECT " + Bookmarks.URL + 86 " FROM " + Bookmarks.TABLE_NAME + 87 " WHERE " + Bookmarks.IS_DELETED + " = 0 " + 88 " AND " + Bookmarks.URL + " IS NOT NULL)"; 89 90 return db.delete(getTable(), selection, null); 91 } 92 } 93