From ccdf700698593bd005cb954fa395bf7149ea86ed Mon Sep 17 00:00:00 2001 From: Stefan Niedermann Date: Mon, 5 Oct 2020 15:55:06 +0200 Subject: [PATCH] #831 Migrate from SQLiteOpenHelper to Room Fix some build bugs --- .../18.json | 44 +++++++++++++++++-- .../notes/persistence/NotesRoomDatabase.java | 16 +++---- .../notes/persistence/dao/CategoryDao.java | 12 +---- .../persistence/dao/LocalAccountDao.java | 11 ----- .../notes/persistence/dao/NoteDao.java | 30 +++++-------- .../notes/persistence/entity/Converters.java | 19 ++++++++ .../notes/persistence/entity/NoteEntity.java | 11 ++--- 7 files changed, 85 insertions(+), 58 deletions(-) create mode 100644 app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/Converters.java diff --git a/app/schemas/it.niedermann.owncloud.notes.persistence.NotesRoomDatabase/18.json b/app/schemas/it.niedermann.owncloud.notes.persistence.NotesRoomDatabase/18.json index e149fcf0..74214109 100644 --- a/app/schemas/it.niedermann.owncloud.notes.persistence.NotesRoomDatabase/18.json +++ b/app/schemas/it.niedermann.owncloud.notes.persistence.NotesRoomDatabase/18.json @@ -2,7 +2,7 @@ "formatVersion": 1, "database": { "version": 18, - "identityHash": "2b759c5539fc95fe87684d51707cd440", + "identityHash": "6d6111b4da8084c8f73df0eb63e39070", "entities": [ { "tableName": "LocalAccountEntity", @@ -80,7 +80,7 @@ }, { "tableName": "NoteEntity", - "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `remoteId` INTEGER NOT NULL, `accountId` INTEGER NOT NULL, `status` TEXT, `title` TEXT, `modified` INTEGER NOT NULL, `content` TEXT, `favorite` INTEGER, `eTag` TEXT, `excerpt` TEXT, `scrollY` INTEGER NOT NULL, PRIMARY KEY(`id`))", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `remoteId` INTEGER NOT NULL, `accountId` INTEGER NOT NULL, `status` TEXT, `title` TEXT, `modified` INTEGER NOT NULL, `content` TEXT, `favorite` INTEGER, `eTag` TEXT, `excerpt` TEXT, `scrollY` INTEGER NOT NULL, `category` TEXT, PRIMARY KEY(`id`))", "fields": [ { "fieldPath": "id", @@ -147,6 +147,44 @@ "columnName": "scrollY", "affinity": "INTEGER", "notNull": true + }, + { + "fieldPath": "category", + "columnName": "category", + "affinity": "TEXT", + "notNull": false + } + ], + "primaryKey": { + "columnNames": [ + "id" + ], + "autoGenerate": false + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "CategoryEntity", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `accountId` INTEGER NOT NULL, `title` TEXT, PRIMARY KEY(`id`))", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "accountId", + "columnName": "accountId", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "title", + "columnName": "title", + "affinity": "TEXT", + "notNull": false } ], "primaryKey": { @@ -162,7 +200,7 @@ "views": [], "setupQueries": [ "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", - "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '2b759c5539fc95fe87684d51707cd440')" + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '6d6111b4da8084c8f73df0eb63e39070')" ] } } \ No newline at end of file diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRoomDatabase.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRoomDatabase.java index 1195bbed..a087fd34 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRoomDatabase.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/NotesRoomDatabase.java @@ -1,12 +1,8 @@ package it.niedermann.owncloud.notes.persistence; import android.content.Context; -import android.content.Intent; -import android.content.pm.ShortcutInfo; import android.content.pm.ShortcutManager; -import android.graphics.drawable.Icon; import android.os.Build; -import android.text.TextUtils; import android.util.Log; import androidx.annotation.NonNull; @@ -14,21 +10,20 @@ import androidx.annotation.WorkerThread; import androidx.room.Database; import androidx.room.Room; import androidx.room.RoomDatabase; +import androidx.room.TypeConverters; import androidx.room.migration.Migration; import androidx.sqlite.db.SupportSQLiteDatabase; import com.nextcloud.android.sso.model.SingleSignOnAccount; -import java.util.ArrayList; import java.util.Collections; -import java.util.List; import it.niedermann.owncloud.notes.R; -import it.niedermann.owncloud.notes.edit.EditNoteActivity; import it.niedermann.owncloud.notes.persistence.dao.CategoryDao; import it.niedermann.owncloud.notes.persistence.dao.LocalAccountDao; import it.niedermann.owncloud.notes.persistence.dao.NoteDao; import it.niedermann.owncloud.notes.persistence.entity.CategoryEntity; +import it.niedermann.owncloud.notes.persistence.entity.Converters; import it.niedermann.owncloud.notes.persistence.entity.LocalAccountEntity; import it.niedermann.owncloud.notes.persistence.entity.NoteEntity; import it.niedermann.owncloud.notes.shared.model.Capabilities; @@ -37,7 +32,6 @@ import it.niedermann.owncloud.notes.shared.model.DBNote; import it.niedermann.owncloud.notes.shared.model.DBStatus; import it.niedermann.owncloud.notes.shared.util.ColorUtil; -import static it.niedermann.owncloud.notes.edit.EditNoteActivity.ACTION_SHORTCUT; import static it.niedermann.owncloud.notes.shared.util.NoteUtil.generateNoteExcerpt; import static it.niedermann.owncloud.notes.widget.notelist.NoteListWidget.updateNoteListWidgets; import static it.niedermann.owncloud.notes.widget.singlenote.SingleNoteWidget.updateSingleNoteWidgets; @@ -45,9 +39,11 @@ import static it.niedermann.owncloud.notes.widget.singlenote.SingleNoteWidget.up @Database( entities = { LocalAccountEntity.class, - NoteEntity.class + NoteEntity.class, + CategoryEntity.class }, version = 18 ) +@TypeConverters({Converters.class}) public abstract class NotesRoomDatabase extends RoomDatabase { private static final String TAG = NotesRoomDatabase.class.getSimpleName(); @@ -238,7 +234,7 @@ public abstract class NotesRoomDatabase extends RoomDatabase { entity.setFavorite(note.isFavorite()); // FIXME // entity.setCategory(getOrCreateCategoryIdByTitle(accountId, note.getCategory())); - entity.seteTag(note.getEtag()); + entity.setETag(note.getEtag()); return getNoteDao().addNote(entity); } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/CategoryDao.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/CategoryDao.java index 3e64c64a..3bbd1e97 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/CategoryDao.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/CategoryDao.java @@ -1,19 +1,11 @@ package it.niedermann.owncloud.notes.persistence.dao; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; -import android.util.Log; - import androidx.annotation.NonNull; import androidx.room.Dao; import androidx.room.Insert; import androidx.room.Query; -import java.util.HashMap; -import java.util.Map; - import it.niedermann.owncloud.notes.persistence.entity.CategoryEntity; -import it.niedermann.owncloud.notes.shared.model.DBStatus; @Dao public interface CategoryDao { @@ -27,12 +19,12 @@ public interface CategoryDao { * * @param accountId The user accountId */ - @Query("DELETE FROM categoryentity WHERE id NOT IN (SELECT noteentity.category FROM noteentity)") + @Query("DELETE FROM CategoryEntity WHERE accountId = :accountId AND id NOT IN (SELECT category FROM NoteEntity)") void removeEmptyCategory(long accountId); @Insert Long addCategory(@NonNull CategoryEntity entity); - @Query("SELECT id FROM categoryentity WHERE accountId = :accountId AND title = :title") + @Query("SELECT id FROM CategoryEntity WHERE accountId = :accountId AND title = :title") Long getCategoryIdByTitle(long accountId, @NonNull String title); } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/LocalAccountDao.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/LocalAccountDao.java index a7c9f348..c53a11f3 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/LocalAccountDao.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/LocalAccountDao.java @@ -1,14 +1,5 @@ package it.niedermann.owncloud.notes.persistence.dao; -import android.content.ContentValues; -import android.database.Cursor; -import android.database.sqlite.SQLiteConstraintException; -import android.database.sqlite.SQLiteDatabase; -import android.graphics.Color; -import android.util.Log; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import androidx.room.Dao; import androidx.room.Insert; import androidx.room.Query; @@ -16,8 +7,6 @@ import androidx.room.Query; import java.util.List; import it.niedermann.owncloud.notes.persistence.entity.LocalAccountEntity; -import it.niedermann.owncloud.notes.shared.model.Capabilities; -import it.niedermann.owncloud.notes.shared.model.LocalAccount; @Dao public interface LocalAccountDao { diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/NoteDao.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/NoteDao.java index 580eadc1..9fcdfd7b 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/NoteDao.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/dao/NoteDao.java @@ -1,17 +1,9 @@ package it.niedermann.owncloud.notes.persistence.dao; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; - -import androidx.annotation.NonNull; -import androidx.annotation.WorkerThread; import androidx.room.Dao; import androidx.room.Insert; import androidx.room.Query; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; @@ -22,19 +14,19 @@ import it.niedermann.owncloud.notes.shared.model.DBStatus; @Dao public interface NoteDao { - @Query("DELETE FROM noteentity WHERE id = :id and status= :forceDBStatus") - void deleteByCardId(long id, DBStatus forceDBStatus); - - @Query("UPDATE noteentity SET scrollY = :scrollY WHERE id = :id") - void updateScrollY(long id, int scrollY); - - @Query("SELECT * FROM noteentity WHERE id = :id AND accountId = :accountId AND status != :") - NoteEntity getNote(long accountId, long id); - @Insert long addNote(NoteEntity noteEntity); - @Query("UPDATE noteentity SET status = :status WHERE id = :id") + @Query("DELETE FROM NoteEntity WHERE id = :id and status = :forceDBStatus") + void deleteByCardId(long id, DBStatus forceDBStatus); + + @Query("UPDATE NoteEntity SET scrollY = :scrollY WHERE id = :id") + void updateScrollY(long id, int scrollY); + + @Query("SELECT * FROM NoteEntity WHERE id = :id AND accountId = :accountId AND status != :accountId") + NoteEntity getNote(long accountId, long id); + + @Query("UPDATE NoteEntity SET status = :status WHERE id = :id") void updateStatus(long id, DBStatus status); /** @@ -43,7 +35,7 @@ public interface NoteDao { * @param accountId get the remoteIds from all notes of this account * @return {@link Set} remoteIds from all notes */ - @Query("SELECT remoteId FROM noteentity WHERE accountId = :accountId AND status != \"LOCAL_DELETED\"") + @Query("SELECT remoteId FROM NoteEntity WHERE accountId = :accountId AND status != \"LOCAL_DELETED\"") Set getRemoteIds(long accountId); diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/Converters.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/Converters.java new file mode 100644 index 00000000..06e027b5 --- /dev/null +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/Converters.java @@ -0,0 +1,19 @@ +package it.niedermann.owncloud.notes.persistence.entity; + +import androidx.room.TypeConverter; + +import it.niedermann.owncloud.notes.shared.model.DBStatus; + +public class Converters { + + @TypeConverter + public static DBStatus fromString(String value) { + return value == null ? null : DBStatus.parse(value); + } + + @TypeConverter + public static String dbStatusToString(DBStatus status) { + return status == null ? null : status.getTitle(); + } + +} diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/NoteEntity.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/NoteEntity.java index 87213d3a..9d277b1a 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/NoteEntity.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/entity/NoteEntity.java @@ -1,15 +1,17 @@ package it.niedermann.owncloud.notes.persistence.entity; +import androidx.room.Embedded; import androidx.room.Entity; import androidx.room.PrimaryKey; import androidx.room.Relation; +import androidx.room.TypeConverters; import java.util.Calendar; import it.niedermann.owncloud.notes.shared.model.DBNote; import it.niedermann.owncloud.notes.shared.model.DBStatus; -@Entity +@Entity() public class NoteEntity { @PrimaryKey private long id; @@ -23,7 +25,6 @@ public class NoteEntity { private String eTag; private String excerpt; private int scrollY; - @Relation(parentColumn = "id", entityColumn = "id") private CategoryEntity category; public long getId() { @@ -90,11 +91,11 @@ public class NoteEntity { this.favorite = favorite; } - public String geteTag() { + public String getETag() { return eTag; } - public void seteTag(String eTag) { + public void setETag(String eTag) { this.eTag = eTag; } @@ -137,7 +138,7 @@ public class NoteEntity { entity.getContent(), entity.getFavorite(), entity.getCategory().getTitle(), - entity.geteTag(), + entity.getETag(), entity.getStatus(), entity.getAccountId(), entity.getExcerpt(),