add a new column in category table to store the sorting method, and modify CategorySortingMethod enum class

This commit is contained in:
Peter S 2020-05-22 23:07:30 +08:00
parent d40800c666
commit a6580bcdbc
4 changed files with 66 additions and 14 deletions

View file

@ -10,6 +10,8 @@ import it.niedermann.owncloud.notes.persistence.NotesDatabase;
import it.niedermann.owncloud.notes.util.NoteUtil;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import androidx.test.core.app.ApplicationProvider;
@ -543,6 +545,16 @@ public class NotesDatabaseTest {
}
}
// @Test
// public void test_14_upgrade() {
// SQLiteDatabase sqlite_db = db.getReadableDatabase();
// Cursor cursor = sqlite_db.rawQuery("SELECT * FROM " + AbstractNotesDatabase.table_category, null);
// cursor.moveToNext();
// int sorting_method = cursor.getInt(3);
// Log.i("TEST_14_UPGRADE", "sorting method index: " + sorting_method);
// assertEquals(0, sorting_method);
// }
public static String getCurDate() {
TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());

View file

@ -32,7 +32,7 @@ import it.niedermann.owncloud.notes.util.NoteUtil;
abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
private static final String TAG = AbstractNotesDatabase.class.getSimpleName();
private static final int database_version = 14;
private static final int database_version = 15;
@NonNull
private final Context context;
@ -65,6 +65,8 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
protected static final String key_category_title = "CATEGORY_TITLE";
protected static final String key_category_account_id = "CATEGORY_ACCOUNT_ID";
protected static final String key_category_sorting_method = "CATEGORY_SORTING_METHOD";
protected AbstractNotesDatabase(@NonNull Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory) {
super(context, name, factory, database_version);
this.context = context;
@ -128,6 +130,7 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
key_category_id + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
key_category_account_id + " INTEGER, " +
key_category_title + " TEXT, " +
key_category_sorting_method + " INTEGER DEFAULT 0, " +
" UNIQUE( " + key_category_account_id + " , " + key_category_title + "))");
createCategoryIndexes(db);
}
@ -356,6 +359,10 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
createCategoryIndexes(db);
createNotesIndexes(db);
}
if (oldVersion < 15) {
// add a new column to store the sorting method for a category note list
db.execSQL("ALTER TABLE " + table_category + " ADD COLUMN " + key_category_sorting_method + " INTEGER DEFAULT 0");
}
}
@Override

View file

@ -1,17 +1,17 @@
package it.niedermann.owncloud.notes.util;
public enum CategorySortingMethod {
SORT_LEXICOGRAPHICAL_ASC(0),
SORT_MODIFIED_DESC(1);
SORT_MODIFIED_DESC("MODIFIED DESC"),
SORT_LEXICOGRAPHICAL_ASC("TITLE ASC");
private int smid; // sorting method id
private String sorder; // sorting method OrderBy for SQL
/***
* Constructor
* @param smid given sorting method id
* @param orderby given sorting method OrderBy
*/
CategorySortingMethod(int smid) {
this.smid = smid;
CategorySortingMethod(String orderby) {
this.sorder = orderby;
}
/***
@ -19,6 +19,23 @@ public enum CategorySortingMethod {
* @return the sorting method id for the enum item
*/
public int getCSMID() {
return this.smid;
return this.ordinal();
}
/***
* Retrieve the sorting method order for SQL
* @return the sorting method order for the enum item
*/
public String getSorder() {
return this.sorder;
}
/***
* Retrieve the corresponding enum value with given the index (ordinal)
* @param index the index / ordinal of the corresponding enum value stored in DB
* @return the corresponding enum item with the index (ordinal)
*/
public static CategorySortingMethod getCSM(int index) {
return CategorySortingMethod.values()[index];
}
}

View file

@ -8,11 +8,27 @@ import static org.junit.Assert.*;
public class CategorySortingMethodTest {
@Test
public void getCSMID() {
CategorySortingMethod csm0 = CategorySortingMethod.SORT_LEXICOGRAPHICAL_ASC;
@Test
public void getCSMID() {
CategorySortingMethod csm0 = CategorySortingMethod.SORT_MODIFIED_DESC;
assertEquals(0, csm0.getCSMID());
CategorySortingMethod csm1 = CategorySortingMethod.SORT_MODIFIED_DESC;
CategorySortingMethod csm1 = CategorySortingMethod.SORT_LEXICOGRAPHICAL_ASC;
assertEquals(1, csm1.getCSMID());
}
}
}
@Test
public void getSOrder() {
CategorySortingMethod csm0 = CategorySortingMethod.SORT_MODIFIED_DESC;
assertEquals("MODIFIED DESC", csm0.getSorder());
CategorySortingMethod csm1 = CategorySortingMethod.SORT_LEXICOGRAPHICAL_ASC;
assertEquals("TITLE ASC", csm1.getSorder());
}
@Test
public void getCSM() {
CategorySortingMethod csm0 = CategorySortingMethod.SORT_MODIFIED_DESC;
assertEquals(csm0, CategorySortingMethod.getCSM(0));
CategorySortingMethod csm1 = CategorySortingMethod.SORT_LEXICOGRAPHICAL_ASC;
assertEquals(csm1, CategorySortingMethod.getCSM(1));
}
}