Created
May 25, 2015 13:06
-
-
Save SammyVimes/6074434a2b424b3d09e2 to your computer and use it in GitHub Desktop.
DatabaseHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.danilov.mangareaderplus.core.database; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
/** | |
* Created by Semyon Danilov on 04.07.2014. | |
*/ | |
public class DatabaseHelper { | |
private String path; | |
private SQLiteDatabase database; | |
private int version; | |
private DatabaseUpgradeHandler handler; | |
private boolean withSpecificTable = false; | |
private String tableName; | |
public DatabaseHelper(final String path, final int version, final DatabaseUpgradeHandler handler) { | |
this.path = path; | |
this.version = version; | |
this.handler = handler; | |
} | |
public DatabaseHelper(final String path, final int version, final DatabaseUpgradeHandler handler, final boolean withSpecificTable, final String tableName) { | |
this(path, version, handler); | |
this.withSpecificTable = withSpecificTable; | |
this.tableName = tableName; | |
} | |
public synchronized SQLiteDatabase openWritable() throws DatabaseAccessException { | |
return withSpecificTable ? internalOpenWritableWithSpecificTable() : internalOpenWritable(); | |
} | |
private SQLiteDatabase internalOpenWritableWithSpecificTable() throws DatabaseAccessException { | |
if (database != null) { | |
if (!database.isOpen()) { | |
database = null; | |
} else if (!database.isReadOnly()) { | |
return database; | |
} | |
} | |
if (database != null) { | |
throw new DatabaseAccessException("Can't open writable"); | |
} | |
tryCreateDatabase(); | |
database = SQLiteDatabase.openOrCreateDatabase(path, null); | |
int v = database.getVersion(); | |
boolean shouldUpgrade = false; | |
if (v != version || !tableExists(tableName, database)) { | |
shouldUpgrade = true; | |
} | |
if (shouldUpgrade) { | |
database.beginTransaction(); | |
try { | |
if (handler != null) { | |
handler.onUpgrade(database, v); | |
} | |
database.setVersion(version); | |
database.setTransactionSuccessful(); | |
} finally { | |
database.endTransaction(); | |
} | |
} | |
return database; | |
} | |
private SQLiteDatabase internalOpenWritable() throws DatabaseAccessException { | |
if (database != null) { | |
if (!database.isOpen()) { | |
database = null; | |
} else if (!database.isReadOnly()) { | |
return database; | |
} | |
} | |
if (database != null) { | |
throw new DatabaseAccessException("Can't open writable"); | |
} | |
tryCreateDatabase(); | |
database = SQLiteDatabase.openOrCreateDatabase(path, null); | |
int v = database.getVersion(); | |
boolean shouldUpgrade = false; | |
if (v != version) { | |
shouldUpgrade = true; | |
} | |
if (shouldUpgrade) { | |
database.beginTransaction(); | |
try { | |
if (handler != null) { | |
handler.onUpgrade(database, v); | |
} | |
database.setVersion(version); | |
database.setTransactionSuccessful(); | |
} finally { | |
database.endTransaction(); | |
} | |
} | |
return database; | |
} | |
public synchronized SQLiteDatabase openReadable() throws DatabaseAccessException { | |
return withSpecificTable ? internalOpenReadableWithSpecificTable() : internalOpenReadable(); | |
} | |
private SQLiteDatabase internalOpenReadable() throws DatabaseAccessException { | |
if (database != null) { | |
if (!database.isOpen()) { | |
database = null; | |
} else { | |
return database; | |
} | |
} | |
tryCreateDatabase(); | |
database = SQLiteDatabase.openOrCreateDatabase(path, null); | |
int v = database.getVersion(); | |
boolean shouldUpgrade = false; | |
if (v != version) { | |
shouldUpgrade = true; | |
} | |
if (shouldUpgrade) { | |
database.beginTransaction(); | |
try { | |
if (handler != null) { | |
handler.onUpgrade(database, v); | |
} | |
database.setVersion(version); | |
database.setTransactionSuccessful(); | |
} finally { | |
database.endTransaction(); | |
} | |
} | |
return database; | |
} | |
private SQLiteDatabase internalOpenReadableWithSpecificTable() throws DatabaseAccessException { | |
if (database != null) { | |
if (!database.isOpen()) { | |
database = null; | |
} else { | |
return database; | |
} | |
} | |
tryCreateDatabase(); | |
database = SQLiteDatabase.openOrCreateDatabase(path, null); | |
int v = database.getVersion(); | |
boolean shouldUpgrade = false; | |
if (v != version || !tableExists(tableName, database)) { | |
shouldUpgrade = true; | |
} | |
if (shouldUpgrade) { | |
database.beginTransaction(); | |
try { | |
if (handler != null) { | |
handler.onUpgrade(database, v); | |
} | |
database.setVersion(version); | |
database.setTransactionSuccessful(); | |
} finally { | |
database.endTransaction(); | |
} | |
} | |
return database; | |
} | |
public void tryCreateDatabase() throws DatabaseAccessException { | |
//TODO: implementation | |
// File f = new File(path); | |
// if (!f.exists()) { | |
// File parent = new File(f.getParent() + File.separator); | |
// if (!parent.exists()) { | |
// boolean result = parent.mkdirs(); | |
// if (!result) { | |
// throw new DatabaseAccessException("Can't create database folder"); | |
// } | |
// } | |
// try { | |
// boolean result = f.createNewFile(); | |
// if (!result) { | |
// throw new DatabaseAccessException("Can't create database file"); | |
// } | |
// } catch (IOException e) { | |
// throw new DatabaseAccessException("Can't create database: " + e.getMessage()); | |
// } | |
// } | |
} | |
public static interface DatabaseUpgradeHandler { | |
void onUpgrade(final SQLiteDatabase database, final int currentVersion); | |
} | |
private boolean tableExists(final String tableName, final SQLiteDatabase database) { | |
Cursor cursor = database.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '" + tableName + "'", null); | |
if (cursor != null) { | |
if (cursor.getCount() > 0) { | |
cursor.close(); | |
return true; | |
} | |
cursor.close(); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment