#831 Migrate from SQLiteOpenHelper to Room

Add indices and foreign keys
This commit is contained in:
Stefan Niedermann 2020-10-07 10:19:03 +02:00
parent d2537d11ca
commit 8eed93b90d
5 changed files with 98 additions and 32 deletions

View file

@ -28,7 +28,7 @@ public interface CategoryDao {
* *
* @param accountId The user accountId * @param accountId The user accountId
*/ */
@Query("DELETE FROM Category WHERE accountId = :accountId AND id NOT IN (SELECT category_id FROM Note)") @Query("DELETE FROM Category WHERE accountId = :accountId AND id NOT IN (SELECT categoryId FROM Note)")
void removeEmptyCategory(long accountId); void removeEmptyCategory(long accountId);
@Query("SELECT id FROM Category WHERE accountId = :accountId AND title = :title") @Query("SELECT id FROM Category WHERE accountId = :accountId AND title = :title")

View file

@ -1,11 +1,27 @@
package it.niedermann.owncloud.notes.persistence.entity; package it.niedermann.owncloud.notes.persistence.entity;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Index;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
import it.niedermann.owncloud.notes.shared.model.CategorySortingMethod; import it.niedermann.owncloud.notes.shared.model.CategorySortingMethod;
@Entity @Entity(
foreignKeys = {
@ForeignKey(
entity = LocalAccount.class,
parentColumns = "accountId",
childColumns = "id",
onDelete = ForeignKey.CASCADE
)
},
indices = {
@Index(value = "accountId"),
@Index(value = "title"),
@Index(value = "categorySortingMethod")
}
)
public class Category { public class Category {
@PrimaryKey @PrimaryKey
private long id; private long id;
@ -45,6 +61,3 @@ public class Category {
this.categorySortingMethod = categorySortingMethod; this.categorySortingMethod = categorySortingMethod;
} }
} }
// "FOREIGN KEY(" + key_category + ") REFERENCES " + table_category + "(" + key_category_id + "), " +
// "FOREIGN KEY(" + key_account_id + ") REFERENCES " + table_accounts + "(" + key_id + "))");
// DatabaseIndexUtil.createIndex(db, table_notes, key_remote_id, key_account_id, key_status, key_favorite, key_category, key_modified);

View file

@ -4,6 +4,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.Ignore; import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
import org.json.JSONArray; import org.json.JSONArray;
@ -20,7 +21,15 @@ import it.niedermann.owncloud.notes.shared.model.ApiVersion;
import it.niedermann.owncloud.notes.shared.model.Capabilities; import it.niedermann.owncloud.notes.shared.model.Capabilities;
import it.niedermann.owncloud.notes.shared.util.ColorUtil; import it.niedermann.owncloud.notes.shared.util.ColorUtil;
@Entity() @Entity(
indices = {
@Index(value = "url"),
@Index(value = "userName"),
@Index(value = "accountName"),
@Index(value = "eTag"),
@Index(value = "modified")
}
)
public class LocalAccount { public class LocalAccount {
@PrimaryKey @PrimaryKey
public Long id; public Long id;
@ -167,5 +176,3 @@ public class LocalAccount {
} }
} }
} }
// DatabaseIndexUtil.createIndex(db, table_accounts, key_url, key_username, key_account_name, key_etag, key_modified);

View file

@ -1,9 +1,10 @@
package it.niedermann.owncloud.notes.persistence.entity; package it.niedermann.owncloud.notes.persistence.entity;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.room.Embedded;
import androidx.room.Entity; import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Ignore; import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey; import androidx.room.PrimaryKey;
import java.io.Serializable; import java.io.Serializable;
@ -12,12 +13,36 @@ import java.util.Calendar;
import it.niedermann.owncloud.notes.shared.model.DBStatus; import it.niedermann.owncloud.notes.shared.model.DBStatus;
import it.niedermann.owncloud.notes.shared.model.Item; import it.niedermann.owncloud.notes.shared.model.Item;
@Entity() @Entity(
foreignKeys = {
@ForeignKey(
entity = LocalAccount.class,
parentColumns = "accountId",
childColumns = "id",
onDelete = ForeignKey.CASCADE
),
@ForeignKey(
entity = Category.class,
parentColumns = "categoryId",
childColumns = "id",
onDelete = ForeignKey.SET_DEFAULT
)
},
indices = {
@Index(value = "remoteId"),
@Index(value = "accountId"),
@Index(value = "categoryId"),
@Index(value = "status"),
@Index(value = "favorite"),
@Index(value = "modified")
}
)
public class Note implements Serializable, Item { public class Note implements Serializable, Item {
@PrimaryKey @PrimaryKey
private Long id; private Long id;
private Long remoteId; private Long remoteId;
private Long accountId; private Long accountId;
private Long categoryId;
private DBStatus status = DBStatus.VOID; private DBStatus status = DBStatus.VOID;
private String title; private String title;
private Calendar modified; private Calendar modified;
@ -26,26 +51,24 @@ public class Note implements Serializable, Item {
private String eTag; private String eTag;
private String excerpt; private String excerpt;
private Integer scrollY; private Integer scrollY;
@Embedded(prefix = "category_")
private Category category;
public Note() { public Note() {
super(); super();
} }
public Note(long remoteId, Calendar modified, String title, String content, Boolean favorite, String category, String eTag) { @Ignore
public Note(long remoteId, Calendar modified, String title, String content, Boolean favorite, String eTag) {
this.remoteId = remoteId; this.remoteId = remoteId;
this.title = title; this.title = title;
this.modified = modified; this.modified = modified;
this.content = content; this.content = content;
this.favorite = favorite; this.favorite = favorite;
this.eTag = eTag; this.eTag = eTag;
this.category = new Category();
this.category.setTitle(category);
} }
public Note(long id, long remoteId, Calendar modified, String title, String content, boolean favorite, String category, String etag, DBStatus status, long accountId, String excerpt, Integer scrollY) { @Ignore
this(remoteId, modified, title, content, favorite, category, etag); public Note(long id, long remoteId, Calendar modified, String title, String content, boolean favorite, String etag, DBStatus status, long accountId, String excerpt, Integer scrollY) {
this(remoteId, modified, title, content, favorite, etag);
this.id = id; this.id = id;
this.status = status; this.status = status;
this.accountId = accountId; this.accountId = accountId;
@ -77,6 +100,14 @@ public class Note implements Serializable, Item {
this.accountId = accountId; this.accountId = accountId;
} }
public Long getCategoryId() {
return categoryId;
}
public void setCategoryId(Long categoryId) {
this.categoryId = categoryId;
}
public DBStatus getStatus() { public DBStatus getStatus() {
return status; return status;
} }
@ -141,14 +172,6 @@ public class Note implements Serializable, Item {
this.scrollY = scrollY; this.scrollY = scrollY;
} }
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@Ignore @Ignore
@Override @Override
public boolean isSection() { public boolean isSection() {
@ -162,16 +185,13 @@ public class Note implements Serializable, Item {
"id=" + id + "id=" + id +
", remoteId=" + remoteId + ", remoteId=" + remoteId +
", accountId=" + accountId + ", accountId=" + accountId +
", categoryId=" + categoryId +
", status=" + status + ", status=" + status +
", title='" + title + '\'' + ", title='" + title + '\'' +
", modified=" + modified + ", modified=" + modified +
", favorite=" + favorite + ", favorite=" + favorite +
", eTag='" + eTag + '\'' + ", eTag='" + eTag + '\'' +
", scrollY=" + scrollY + ", scrollY=" + scrollY +
", category=" + category +
'}'; '}';
} }
} }
// "FOREIGN KEY(" + key_category + ") REFERENCES " + table_category + "(" + key_category_id + "), " +
// "FOREIGN KEY(" + key_account_id + ") REFERENCES " + table_accounts + "(" + key_id + "))");
// DatabaseIndexUtil.createIndex(db, table_notes, key_remote_id, key_account_id, key_status, key_favorite, key_category, key_modified);

View file

@ -0,0 +1,26 @@
package it.niedermann.owncloud.notes.persistence.entity;
import androidx.room.Embedded;
public class NoteWithCategory {
@Embedded
private Note note;
@Embedded
private Category category;
public Note getNote() {
return note;
}
public void setNote(Note note) {
this.note = note;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}