Refactor Android database classes

This commit is contained in:
2017-06-20 22:37:35 -04:00
parent 1976160ae8
commit 59745fb90f
32 changed files with 47 additions and 172 deletions

View File

@@ -23,11 +23,11 @@ import android.app.*;
import android.content.*;
import org.isoron.androidbase.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.core.reminders.*;
import org.isoron.uhabits.core.tasks.*;
import org.isoron.uhabits.core.ui.*;
import org.isoron.uhabits.database.*;
import org.isoron.uhabits.utils.*;
import org.isoron.uhabits.widgets.*;
@@ -92,7 +92,7 @@ public class HabitsApplication extends Application
{
DatabaseUtils.initializeDatabase(context);
}
catch (InvalidDatabaseVersionException e)
catch (UnsupportedDatabaseVersionException e)
{
File db = DatabaseUtils.getDatabaseFile(context);
db.renameTo(new File(db.getAbsolutePath() + ".invalid"));

View File

@@ -24,18 +24,21 @@ package org.isoron.uhabits;
import android.content.*;
import android.database.sqlite.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.database.*;
public class HabitsDatabaseOpener extends BaseSQLiteOpenHelper
public class HabitsDatabaseOpener extends SQLiteOpenHelper
{
private final int version;
private MigrationHelper helper;
public HabitsDatabaseOpener(Context context,
String databaseFilename,
int version)
{
super(context, databaseFilename, version);
super(context, databaseFilename, null, version);
this.version = version;
}
@@ -49,6 +52,13 @@ public class HabitsDatabaseOpener extends BaseSQLiteOpenHelper
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if(oldVersion < 8) throw new UnsupportedDatabaseVersionException();
super.onUpgrade(db, oldVersion, newVersion);
helper = new MigrationHelper(new AndroidDatabase(db));
helper.executeMigrations(oldVersion, newVersion);
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
throw new UnsupportedDatabaseVersionException();
}
}

View File

@@ -77,7 +77,7 @@ public class HabitsModule
public ModelFactory getModelFactory()
{
return new SQLModelFactory(
new AndroidSQLiteDatabase(DatabaseUtils.openDatabase()));
new AndroidDatabase(DatabaseUtils.openDatabase()));
}
@Provides

View File

@@ -21,7 +21,7 @@
package org.isoron.uhabits.database;
import org.isoron.uhabits.core.db.*;
import org.isoron.uhabits.core.database.*;
public class AndroidCursor implements Cursor
{

View File

@@ -24,15 +24,15 @@ package org.isoron.uhabits.database;
import android.content.*;
import android.database.sqlite.*;
import org.isoron.uhabits.core.db.*;
import org.isoron.uhabits.core.database.*;
import java.util.*;
public class AndroidSQLiteDatabase implements Database
public class AndroidDatabase implements Database
{
private final SQLiteDatabase db;
public AndroidSQLiteDatabase(SQLiteDatabase db)
public AndroidDatabase(SQLiteDatabase db)
{
this.db = db;
}

View File

@@ -1,67 +0,0 @@
/*
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
package org.isoron.uhabits.database;
import android.content.*;
import android.database.sqlite.*;
import org.isoron.androidbase.*;
import org.isoron.uhabits.core.db.*;
public class BaseSQLiteOpenHelper extends SQLiteOpenHelper
{
private final Context context;
private final int version;
public BaseSQLiteOpenHelper(@AppContext Context context,
String databaseFilename,
int version)
{
super(context, databaseFilename, null, version);
this.context = context;
this.version = version;
}
@Override
public void onCreate(SQLiteDatabase db)
{
MigrationHelper helper =
new MigrationHelper(new AndroidSQLiteDatabase(db));
helper.executeMigrations(-1, version);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
MigrationHelper helper =
new MigrationHelper(new AndroidSQLiteDatabase(db));
helper.executeMigrations(oldVersion, newVersion);
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
throw new UnsupportedDatabaseVersionException();
}
}

View File

@@ -1,30 +0,0 @@
/*
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
package org.isoron.uhabits.database;
public class InconsistentDatabaseException extends RuntimeException
{
public InconsistentDatabaseException(String message)
{
super(message);
}
}

View File

@@ -1,26 +0,0 @@
/*
* Copyright (C) 2017 Álinson Santos Xavier <isoron@gmail.com>
*
* This file is part of Loop Habit Tracker.
*
* Loop Habit Tracker is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* Loop Habit Tracker is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
package org.isoron.uhabits.database;
public class InvalidDatabaseVersionException extends RuntimeException
{
}

View File

@@ -21,7 +21,7 @@ package org.isoron.uhabits.sync;
import android.support.annotation.*;
import org.isoron.uhabits.core.db.*;
import org.isoron.uhabits.core.database.*;
@Table(name = "Events")
public class Event

View File

@@ -26,7 +26,7 @@ import org.isoron.androidbase.*;
import org.isoron.uhabits.BuildConfig;
import org.isoron.uhabits.core.*;
import org.isoron.uhabits.core.commands.*;
import org.isoron.uhabits.core.db.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.preferences.*;
import org.isoron.uhabits.database.*;
import org.isoron.uhabits.utils.*;
@@ -115,7 +115,7 @@ public class SyncManager implements CommandRunner.Listener
this.isListening = false;
repository = new Repository<>(Event.class,
new AndroidSQLiteDatabase(DatabaseUtils.openDatabase()));
new AndroidDatabase(DatabaseUtils.openDatabase()));
pendingConfirmation = new LinkedList<>();
pendingEmit = new LinkedList<>(repository.findAll("order by timestamp"));

View File

@@ -26,7 +26,6 @@ import android.support.annotation.*;
import org.isoron.androidbase.utils.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.utils.*;
import org.isoron.uhabits.database.*;
import java.io.*;
import java.text.*;
@@ -81,17 +80,8 @@ public abstract class DatabaseUtils
@SuppressWarnings("unchecked")
public static void initializeDatabase(Context context)
{
try
{
opener = new HabitsDatabaseOpener(context, getDatabaseFilename(),
BuildConfig.databaseVersion);
}
catch (RuntimeException e)
{
if (e.getMessage().contains("downgrade"))
throw new InvalidDatabaseVersionException();
else throw e;
}
opener = new HabitsDatabaseOpener(context, getDatabaseFilename(),
BuildConfig.databaseVersion);
}
@SuppressWarnings("ResultOfMethodCallIgnored")
@@ -113,7 +103,7 @@ public abstract class DatabaseUtils
@NonNull
public static SQLiteDatabase openDatabase()
{
if(opener == null) throw new IllegalStateException();
if (opener == null) throw new IllegalStateException();
return opener.getWritableDatabase();
}

View File

@@ -17,7 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.db;
package org.isoron.uhabits.core.database;
import java.lang.annotation.*;

View File

@@ -17,7 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.db;
package org.isoron.uhabits.core.database;
public interface Cursor extends AutoCloseable
{

View File

@@ -17,7 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.db;
package org.isoron.uhabits.core.database;
import java.util.*;

View File

@@ -19,7 +19,7 @@
*
*/
package org.isoron.uhabits.core.db;
package org.isoron.uhabits.core.database;
import java.sql.*;

View File

@@ -17,7 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.db;
package org.isoron.uhabits.core.database;
import org.apache.commons.lang3.*;

View File

@@ -17,7 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.db;
package org.isoron.uhabits.core.database;
import android.support.annotation.*;

View File

@@ -17,7 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.db;
package org.isoron.uhabits.core.database;
import android.support.annotation.*;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.isoron.uhabits.core.db;
package org.isoron.uhabits.core.database;
import java.io.BufferedInputStream;
import java.io.IOException;

View File

@@ -17,7 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.db;
package org.isoron.uhabits.core.database;
import java.lang.annotation.*;

View File

@@ -15,11 +15,9 @@
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
*/
package org.isoron.uhabits.database;
package org.isoron.uhabits.core.database;
public class UnsupportedDatabaseVersionException extends RuntimeException
{

View File

@@ -19,7 +19,7 @@
package org.isoron.uhabits.core.models;
import org.isoron.uhabits.core.db.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.models.sqlite.records.*;
/**

View File

@@ -19,7 +19,7 @@
package org.isoron.uhabits.core.models.memory;
import org.isoron.uhabits.core.db.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.models.sqlite.records.*;

View File

@@ -21,7 +21,7 @@
package org.isoron.uhabits.core.models.sqlite;
import org.isoron.uhabits.core.db.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.models.memory.*;
import org.isoron.uhabits.core.models.sqlite.records.*;

View File

@@ -23,7 +23,7 @@ package org.isoron.uhabits.core.models.sqlite;
import android.support.annotation.*;
import org.isoron.uhabits.core.db.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.models.memory.*;
import org.isoron.uhabits.core.models.sqlite.records.*;

View File

@@ -24,7 +24,7 @@ package org.isoron.uhabits.core.models.sqlite;
import android.support.annotation.*;
import android.support.annotation.Nullable;
import org.isoron.uhabits.core.db.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.models.memory.*;
import org.isoron.uhabits.core.models.sqlite.records.*;

View File

@@ -22,7 +22,7 @@
package org.isoron.uhabits.core.models.sqlite.records;
import org.apache.commons.lang3.builder.*;
import org.isoron.uhabits.core.db.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.models.*;
/**

View File

@@ -21,7 +21,7 @@
package org.isoron.uhabits.core.models.sqlite.records;
import org.isoron.uhabits.core.db.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.models.*;
/**

View File

@@ -20,7 +20,7 @@
package org.isoron.uhabits;
import org.isoron.uhabits.core.commands.*;
import org.isoron.uhabits.core.db.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.models.memory.*;
import org.isoron.uhabits.core.tasks.*;

View File

@@ -17,7 +17,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.isoron.uhabits.core.db;
package org.isoron.uhabits.core.database;
import org.apache.commons.lang3.builder.*;
import org.isoron.uhabits.*;

View File

@@ -22,7 +22,7 @@
package org.isoron.uhabits.core.models.sqlite;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.db.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.models.sqlite.records.*;
import org.junit.*;

View File

@@ -22,7 +22,7 @@ package org.isoron.uhabits.core.models.sqlite;
import android.support.annotation.*;
import org.isoron.uhabits.*;
import org.isoron.uhabits.core.db.*;
import org.isoron.uhabits.core.database.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.models.sqlite.records.*;
import org.isoron.uhabits.core.test.*;