1 /* 2 * Copyright (C) 2013 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 com.android.gallery3d.filtershow.data; 18 19 import android.content.Context; 20 import android.database.sqlite.SQLiteDatabase; 21 import android.database.sqlite.SQLiteOpenHelper; 22 23 public class FilterStackDBHelper extends SQLiteOpenHelper { 24 25 public static final int DATABASE_VERSION = 1; 26 public static final String DATABASE_NAME = "filterstacks.db"; 27 private static final String SQL_CREATE_TABLE = "CREATE TABLE "; 28 29 public static interface FilterStack { 30 /** The row uid */ 31 public static final String _ID = "_id"; 32 /** The table name */ 33 public static final String TABLE = "filterstack"; 34 /** The stack name */ 35 public static final String STACK_ID = "stack_id"; 36 /** A serialized stack of filters. */ 37 public static final String FILTER_STACK= "stack"; 38 } 39 40 private static final String[][] CREATE_FILTER_STACK = { 41 { FilterStack._ID, "INTEGER PRIMARY KEY AUTOINCREMENT" }, 42 { FilterStack.STACK_ID, "TEXT" }, 43 { FilterStack.FILTER_STACK, "BLOB" }, 44 }; 45 FilterStackDBHelper(Context context, String name, int version)46 public FilterStackDBHelper(Context context, String name, int version) { 47 super(context, name, null, version); 48 } 49 FilterStackDBHelper(Context context, String name)50 public FilterStackDBHelper(Context context, String name) { 51 this(context, name, DATABASE_VERSION); 52 } 53 FilterStackDBHelper(Context context)54 public FilterStackDBHelper(Context context) { 55 this(context, DATABASE_NAME); 56 } 57 58 @Override onCreate(SQLiteDatabase db)59 public void onCreate(SQLiteDatabase db) { 60 createTable(db, FilterStack.TABLE, CREATE_FILTER_STACK); 61 } 62 63 @Override onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)64 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 65 dropTable(db, FilterStack.TABLE); 66 onCreate(db); 67 } 68 createTable(SQLiteDatabase db, String table, String[][] columns)69 protected static void createTable(SQLiteDatabase db, String table, String[][] columns) { 70 StringBuilder create = new StringBuilder(SQL_CREATE_TABLE); 71 create.append(table).append('('); 72 boolean first = true; 73 for (String[] column : columns) { 74 if (!first) { 75 create.append(','); 76 } 77 first = false; 78 for (String val : column) { 79 create.append(val).append(' '); 80 } 81 } 82 create.append(')'); 83 db.beginTransaction(); 84 try { 85 db.execSQL(create.toString()); 86 db.setTransactionSuccessful(); 87 } finally { 88 db.endTransaction(); 89 } 90 } 91 dropTable(SQLiteDatabase db, String table)92 protected static void dropTable(SQLiteDatabase db, String table) { 93 db.beginTransaction(); 94 try { 95 db.execSQL("drop table if exists " + table); 96 db.setTransactionSuccessful(); 97 } finally { 98 db.endTransaction(); 99 } 100 } 101 } 102