1 /* 2 * Copyright (C) 2010 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.dumprendertree2; 18 19 import android.content.ContentValues; 20 import android.content.Context; 21 import android.database.Cursor; 22 import android.database.SQLException; 23 import android.database.sqlite.SQLiteDatabase; 24 import android.database.sqlite.SQLiteOpenHelper; 25 26 import java.util.HashSet; 27 import java.util.Set; 28 29 /** 30 * A basic class that wraps database accesses inside itself and provides functionality to 31 * store and retrieve AbstractResults. 32 */ 33 public class SummarizerDBHelper { 34 private static final String KEY_ID = "id"; 35 private static final String KEY_PATH = "path"; 36 private static final String KEY_BYTES = "bytes"; 37 38 private static final String DATABASE_NAME = "SummarizerDB"; 39 private static final int DATABASE_VERSION = 1; 40 41 static final String EXPECTED_FAILURES_TABLE = "expectedFailures"; 42 static final String UNEXPECTED_FAILURES_TABLE = "unexpectedFailures"; 43 static final String EXPECTED_PASSES_TABLE = "expextedPasses"; 44 static final String UNEXPECTED_PASSES_TABLE = "unexpextedPasses"; 45 private static final Set<String> TABLES_NAMES = new HashSet<String>(); 46 { 47 TABLES_NAMES.add(EXPECTED_FAILURES_TABLE); 48 TABLES_NAMES.add(EXPECTED_PASSES_TABLE); 49 TABLES_NAMES.add(UNEXPECTED_FAILURES_TABLE); 50 TABLES_NAMES.add(UNEXPECTED_PASSES_TABLE); 51 } 52 53 private static final void createTables(SQLiteDatabase db) { 54 String cmd; 55 for (String tableName : TABLES_NAMES) { 56 cmd = "create table " + tableName + " (" 57 + KEY_ID + " integer primary key autoincrement, " 58 + KEY_PATH + " text not null, " 59 + KEY_BYTES + " blob not null);"; 60 db.execSQL(cmd); 61 } 62 } 63 64 private static final void dropTables(SQLiteDatabase db) { 65 for (String tableName : TABLES_NAMES) { 66 db.execSQL("DROP TABLE IF EXISTS " + tableName); 67 } 68 } 69 70 private static class DatabaseHelper extends SQLiteOpenHelper { 71 DatabaseHelper(Context context) { 72 super(context, DATABASE_NAME, null, DATABASE_VERSION); 73 } 74 75 @Override 76 public void onCreate(SQLiteDatabase db) { 77 dropTables(db); 78 createTables(db); 79 } 80 81 @Override 82 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 83 /** NOOP for now, because we will never upgrade the db */ 84 } 85 86 public void reset(SQLiteDatabase db) { 87 dropTables(db); 88 createTables(db); 89 } 90 } 91 92 private DatabaseHelper mDbHelper; 93 private SQLiteDatabase mDb; 94 95 private final Context mContext; 96 97 public SummarizerDBHelper(Context ctx) { 98 mContext = ctx; 99 mDbHelper = new DatabaseHelper(mContext); 100 } 101 102 public void reset() { 103 mDbHelper.reset(this.mDb); 104 } 105 106 public void open() throws SQLException { 107 mDb = mDbHelper.getWritableDatabase(); 108 } 109 110 public void close() { 111 mDbHelper.close(); 112 } 113 114 public void insertAbstractResult(AbstractResult result, String table) { 115 ContentValues cv = new ContentValues(); 116 cv.put(KEY_PATH, result.getRelativePath()); 117 cv.put(KEY_BYTES, result.getBytes()); 118 mDb.insert(table, null, cv); 119 } 120 121 public Cursor getAbstractResults(String table) throws SQLException { 122 return mDb.query(false, table, new String[] {KEY_BYTES}, null, null, null, null, 123 KEY_PATH + " ASC", null); 124 } 125 126 public static AbstractResult getAbstractResult(Cursor cursor) { 127 return AbstractResult.create(cursor.getBlob(cursor.getColumnIndex(KEY_BYTES))); 128 } 129 }