#831 Migrate from SQLiteOpenHelper to Room

Fix some build bugs
This commit is contained in:
Stefan Niedermann 2020-10-05 15:55:06 +02:00
parent 889b6e44c3
commit ccdf700698
7 changed files with 85 additions and 58 deletions

View file

@ -2,7 +2,7 @@
"formatVersion": 1, "formatVersion": 1,
"database": { "database": {
"version": 18, "version": 18,
"identityHash": "2b759c5539fc95fe87684d51707cd440", "identityHash": "6d6111b4da8084c8f73df0eb63e39070",
"entities": [ "entities": [
{ {
"tableName": "LocalAccountEntity", "tableName": "LocalAccountEntity",
@ -80,7 +80,7 @@
}, },
{ {
"tableName": "NoteEntity", "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": [ "fields": [
{ {
"fieldPath": "id", "fieldPath": "id",
@ -147,6 +147,44 @@
"columnName": "scrollY", "columnName": "scrollY",
"affinity": "INTEGER", "affinity": "INTEGER",
"notNull": true "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": { "primaryKey": {
@ -162,7 +200,7 @@
"views": [], "views": [],
"setupQueries": [ "setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", "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')"
] ]
} }
} }

View file

@ -1,12 +1,8 @@
package it.niedermann.owncloud.notes.persistence; package it.niedermann.owncloud.notes.persistence;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.pm.ShortcutInfo;
import android.content.pm.ShortcutManager; import android.content.pm.ShortcutManager;
import android.graphics.drawable.Icon;
import android.os.Build; import android.os.Build;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -14,21 +10,20 @@ import androidx.annotation.WorkerThread;
import androidx.room.Database; import androidx.room.Database;
import androidx.room.Room; import androidx.room.Room;
import androidx.room.RoomDatabase; import androidx.room.RoomDatabase;
import androidx.room.TypeConverters;
import androidx.room.migration.Migration; import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase; import androidx.sqlite.db.SupportSQLiteDatabase;
import com.nextcloud.android.sso.model.SingleSignOnAccount; import com.nextcloud.android.sso.model.SingleSignOnAccount;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import it.niedermann.owncloud.notes.R; 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.CategoryDao;
import it.niedermann.owncloud.notes.persistence.dao.LocalAccountDao; import it.niedermann.owncloud.notes.persistence.dao.LocalAccountDao;
import it.niedermann.owncloud.notes.persistence.dao.NoteDao; import it.niedermann.owncloud.notes.persistence.dao.NoteDao;
import it.niedermann.owncloud.notes.persistence.entity.CategoryEntity; 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.LocalAccountEntity;
import it.niedermann.owncloud.notes.persistence.entity.NoteEntity; import it.niedermann.owncloud.notes.persistence.entity.NoteEntity;
import it.niedermann.owncloud.notes.shared.model.Capabilities; 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.model.DBStatus;
import it.niedermann.owncloud.notes.shared.util.ColorUtil; 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.shared.util.NoteUtil.generateNoteExcerpt;
import static it.niedermann.owncloud.notes.widget.notelist.NoteListWidget.updateNoteListWidgets; import static it.niedermann.owncloud.notes.widget.notelist.NoteListWidget.updateNoteListWidgets;
import static it.niedermann.owncloud.notes.widget.singlenote.SingleNoteWidget.updateSingleNoteWidgets; 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( @Database(
entities = { entities = {
LocalAccountEntity.class, LocalAccountEntity.class,
NoteEntity.class NoteEntity.class,
CategoryEntity.class
}, version = 18 }, version = 18
) )
@TypeConverters({Converters.class})
public abstract class NotesRoomDatabase extends RoomDatabase { public abstract class NotesRoomDatabase extends RoomDatabase {
private static final String TAG = NotesRoomDatabase.class.getSimpleName(); private static final String TAG = NotesRoomDatabase.class.getSimpleName();
@ -238,7 +234,7 @@ public abstract class NotesRoomDatabase extends RoomDatabase {
entity.setFavorite(note.isFavorite()); entity.setFavorite(note.isFavorite());
// FIXME // FIXME
// entity.setCategory(getOrCreateCategoryIdByTitle(accountId, note.getCategory())); // entity.setCategory(getOrCreateCategoryIdByTitle(accountId, note.getCategory()));
entity.seteTag(note.getEtag()); entity.setETag(note.getEtag());
return getNoteDao().addNote(entity); return getNoteDao().addNote(entity);
} }

View file

@ -1,19 +1,11 @@
package it.niedermann.owncloud.notes.persistence.dao; 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.annotation.NonNull;
import androidx.room.Dao; import androidx.room.Dao;
import androidx.room.Insert; import androidx.room.Insert;
import androidx.room.Query; 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.persistence.entity.CategoryEntity;
import it.niedermann.owncloud.notes.shared.model.DBStatus;
@Dao @Dao
public interface CategoryDao { public interface CategoryDao {
@ -27,12 +19,12 @@ public interface CategoryDao {
* *
* @param accountId The user accountId * @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); void removeEmptyCategory(long accountId);
@Insert @Insert
Long addCategory(@NonNull CategoryEntity entity); 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); Long getCategoryIdByTitle(long accountId, @NonNull String title);
} }

View file

@ -1,14 +1,5 @@
package it.niedermann.owncloud.notes.persistence.dao; 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.Dao;
import androidx.room.Insert; import androidx.room.Insert;
import androidx.room.Query; import androidx.room.Query;
@ -16,8 +7,6 @@ import androidx.room.Query;
import java.util.List; import java.util.List;
import it.niedermann.owncloud.notes.persistence.entity.LocalAccountEntity; 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 @Dao
public interface LocalAccountDao { public interface LocalAccountDao {

View file

@ -1,17 +1,9 @@
package it.niedermann.owncloud.notes.persistence.dao; 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.Dao;
import androidx.room.Insert; import androidx.room.Insert;
import androidx.room.Query; import androidx.room.Query;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -22,19 +14,19 @@ import it.niedermann.owncloud.notes.shared.model.DBStatus;
@Dao @Dao
public interface NoteDao { 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 @Insert
long addNote(NoteEntity noteEntity); 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); void updateStatus(long id, DBStatus status);
/** /**
@ -43,7 +35,7 @@ public interface NoteDao {
* @param accountId get the remoteIds from all notes of this account * @param accountId get the remoteIds from all notes of this account
* @return {@link Set<String>} remoteIds from all notes * @return {@link Set<String>} 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<String> getRemoteIds(long accountId); Set<String> getRemoteIds(long accountId);

View file

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

View file

@ -1,15 +1,17 @@
package it.niedermann.owncloud.notes.persistence.entity; package it.niedermann.owncloud.notes.persistence.entity;
import androidx.room.Embedded;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
import androidx.room.Relation; import androidx.room.Relation;
import androidx.room.TypeConverters;
import java.util.Calendar; import java.util.Calendar;
import it.niedermann.owncloud.notes.shared.model.DBNote; import it.niedermann.owncloud.notes.shared.model.DBNote;
import it.niedermann.owncloud.notes.shared.model.DBStatus; import it.niedermann.owncloud.notes.shared.model.DBStatus;
@Entity @Entity()
public class NoteEntity { public class NoteEntity {
@PrimaryKey @PrimaryKey
private long id; private long id;
@ -23,7 +25,6 @@ public class NoteEntity {
private String eTag; private String eTag;
private String excerpt; private String excerpt;
private int scrollY; private int scrollY;
@Relation(parentColumn = "id", entityColumn = "id")
private CategoryEntity category; private CategoryEntity category;
public long getId() { public long getId() {
@ -90,11 +91,11 @@ public class NoteEntity {
this.favorite = favorite; this.favorite = favorite;
} }
public String geteTag() { public String getETag() {
return eTag; return eTag;
} }
public void seteTag(String eTag) { public void setETag(String eTag) {
this.eTag = eTag; this.eTag = eTag;
} }
@ -137,7 +138,7 @@ public class NoteEntity {
entity.getContent(), entity.getContent(),
entity.getFavorite(), entity.getFavorite(),
entity.getCategory().getTitle(), entity.getCategory().getTitle(),
entity.geteTag(), entity.getETag(),
entity.getStatus(), entity.getStatus(),
entity.getAccountId(), entity.getAccountId(),
entity.getExcerpt(), entity.getExcerpt(),