Refactor Android database classes

pull/312/head
Alinson S. Xavier 8 years ago
parent 1976160ae8
commit 59745fb90f

@ -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"));

@ -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();
}
}

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

@ -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
{

@ -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;
}

@ -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();
}
}

@ -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);
}
}

@ -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
{
}

@ -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

@ -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"));

@ -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.*;
@ -80,19 +79,10 @@ 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;
}
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public static String saveDatabaseCopy(Context context, File dir)
@ -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();
}

@ -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.*;

@ -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
{

@ -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.*;

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

@ -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.*;

@ -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.*;

@ -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.*;

@ -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;

@ -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.*;

@ -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
{

@ -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.*;
/**

@ -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.*;

@ -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.*;

@ -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.*;

@ -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.*;

@ -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.*;
/**

@ -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.*;
/**

@ -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.*;

@ -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.*;

@ -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.*;

@ -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.*;

Loading…
Cancel
Save