update DB to 20180416

This adds name_sort to our playlist code
This commit is contained in:
Adrian Ulrich 2018-04-16 16:17:29 +02:00
parent 5367d74692
commit cb96508d97
5 changed files with 75 additions and 17 deletions

View File

@ -360,6 +360,7 @@ public class MediaLibrary {
ContentValues v = new ContentValues();
v.put(MediaLibrary.PlaylistColumns._ID, hash63(name));
v.put(MediaLibrary.PlaylistColumns.NAME, name);
v.put(MediaLibrary.PlaylistColumns.NAME_SORT, keyFor(name));
long id = getBackend(context).insert(MediaLibrary.TABLE_PLAYLISTS, null, v);
if (id != -1)
@ -747,6 +748,10 @@ public class MediaLibrary {
* The name of this playlist
*/
String NAME = "name";
/**
* Sortable column for name
*/
String NAME_SORT = "name_sort";
}
// Song <-> Playlist mapping

View File

@ -35,7 +35,7 @@ public class MediaLibraryBackend extends SQLiteOpenHelper {
/**
* The database version we are using
*/
private static final int DATABASE_VERSION = 20180305;
private static final int DATABASE_VERSION = 20180416;
/**
* on-disk file to store the database
*/

View File

@ -0,0 +1,52 @@
/*
* Copyright (C) 2018 Adrian Ulrich <adrian@blinkenlights.ch>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package ch.blinkenlights.android.medialibrary;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;
import android.util.Log;
public class MediaMigrations {
/**
* Migrate to 20180416
* That is: populate NAME_SORT in the new source database
*
* @param dbh the database to work on
* @param fromDb the name of the source database
* @param toDb the name of the target database
**/
static void migrate_to_20180416(SQLiteDatabase dbh, String fromDb, String toDb) {
Cursor cursor = dbh.query(fromDb, new String[]{MediaLibrary.PlaylistColumns._ID, MediaLibrary.PlaylistColumns.NAME}, null, null, null, null, null);
while (cursor.moveToNext()) {
long id = cursor.getLong(0);
String name = cursor.getString(1);
String key = MediaLibrary.keyFor(name);
Log.v("VanillaMusic", "migrate_to_20180416 -> id="+id+", name="+name+" -> key = "+key);
ContentValues v = new ContentValues();
v.put(MediaLibrary.PlaylistColumns._ID, id);
v.put(MediaLibrary.PlaylistColumns.NAME, name);
v.put(MediaLibrary.PlaylistColumns.NAME_SORT, key);
dbh.insert(toDb, null, v);
}
cursor.close();
}
}

View File

@ -105,7 +105,8 @@ public class MediaSchema {
*/
private static final String DATABASE_CREATE_PLAYLISTS = "CREATE TABLE "+ MediaLibrary.TABLE_PLAYLISTS +" ("
+ MediaLibrary.PlaylistColumns._ID +" INTEGER PRIMARY KEY, "
+ MediaLibrary.PlaylistColumns.NAME +" TEXT NOT NULL "
+ MediaLibrary.PlaylistColumns.NAME +" TEXT NOT NULL, "
+ MediaLibrary.PlaylistColumns.NAME_SORT +" TEXT NOT NULL "
+ ");";
/**
@ -331,6 +332,14 @@ public class MediaSchema {
dbh.execSQL("ALTER TABLE "+MediaLibrary.TABLE_SONGS+" ADD COLUMN "+MediaLibrary.SongColumns.FLAGS+" INTEGER NOT NULL DEFAULT 0 ");
}
if (oldVersion < 20180416) {
// This adds NAME_SORT, so we need to pre-populate all keys.
dbh.execSQL("ALTER TABLE "+MediaLibrary.TABLE_PLAYLISTS+" RENAME TO _migrate");
dbh.execSQL(MediaSchema.DATABASE_CREATE_PLAYLISTS);
MediaMigrations.migrate_to_20180416(dbh, "_migrate", MediaLibrary.TABLE_PLAYLISTS);
dbh.execSQL("DROP TABLE _migrate");
}
}
}

View File

@ -35,6 +35,7 @@ import android.provider.MediaStore;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -222,7 +223,7 @@ public class MediaAdapter
case MediaUtils.TYPE_PLAYLIST:
mSource = MediaLibrary.TABLE_PLAYLISTS;
mFields = new String[] { MediaLibrary.PlaylistColumns.NAME };
mFieldKeys = null;
mFieldKeys = new String[] { MediaLibrary.PlaylistColumns.NAME_SORT };
mSortEntries = new int[] { R.string.title, R.string.date_added };
mAdapterSortValues = new String[] { MediaLibrary.PlaylistColumns.NAME+" %1$s", MediaLibrary.PlaylistColumns._ID+" %1$s" };
mExpandable = true;
@ -318,19 +319,10 @@ public class MediaAdapter
// include the constraint (aka: search string) if any
if (constraint != null && constraint.length() != 0) {
String[] needles;
String[] keySource;
if (mFieldKeys != null) {
String colKey = MediaLibrary.keyFor(constraint);
String spaceColKey = DatabaseUtils.getCollationKey(" ");
needles = colKey.split(spaceColKey);
keySource = mFieldKeys;
} else {
// only used for playlists, maybe we should just update the schema ?
needles = SPACE_SPLIT.split(constraint);
keySource = mFields;
}
String[] needles = colKey.split(spaceColKey);
String[] keySource = mFieldKeys;
int size = needles.length;
selectionArgs = new String[size];