#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
*/
@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);
@Query("SELECT id FROM Category WHERE accountId = :accountId AND title = :title")

View file

@ -1,11 +1,27 @@
package it.niedermann.owncloud.notes.persistence.entity;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Index;
import androidx.room.PrimaryKey;
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 {
@PrimaryKey
private long id;
@ -44,7 +60,4 @@ public class Category {
public void setCategorySortingMethod(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.room.Entity;
import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey;
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.util.ColorUtil;
@Entity()
@Entity(
indices = {
@Index(value = "url"),
@Index(value = "userName"),
@Index(value = "accountName"),
@Index(value = "eTag"),
@Index(value = "modified")
}
)
public class LocalAccount {
@PrimaryKey
public Long id;
@ -166,6 +175,4 @@ public class LocalAccount {
this.preferredApiVersion = null;
}
}
}
// 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;
import androidx.annotation.NonNull;
import androidx.room.Embedded;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey;
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.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 {
@PrimaryKey
private Long id;
private Long remoteId;
private Long accountId;
private Long categoryId;
private DBStatus status = DBStatus.VOID;
private String title;
private Calendar modified;
@ -26,26 +51,24 @@ public class Note implements Serializable, Item {
private String eTag;
private String excerpt;
private Integer scrollY;
@Embedded(prefix = "category_")
private Category category;
public Note() {
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.title = title;
this.modified = modified;
this.content = content;
this.favorite = favorite;
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) {
this(remoteId, modified, title, content, favorite, category, etag);
@Ignore
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.status = status;
this.accountId = accountId;
@ -77,6 +100,14 @@ public class Note implements Serializable, Item {
this.accountId = accountId;
}
public Long getCategoryId() {
return categoryId;
}
public void setCategoryId(Long categoryId) {
this.categoryId = categoryId;
}
public DBStatus getStatus() {
return status;
}
@ -141,14 +172,6 @@ public class Note implements Serializable, Item {
this.scrollY = scrollY;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@Ignore
@Override
public boolean isSection() {
@ -162,16 +185,13 @@ public class Note implements Serializable, Item {
"id=" + id +
", remoteId=" + remoteId +
", accountId=" + accountId +
", categoryId=" + categoryId +
", status=" + status +
", title='" + title + '\'' +
", modified=" + modified +
", favorite=" + favorite +
", eTag='" + eTag + '\'' +
", 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;
}
}