Use androidx transaction in databases
Replace the redundant SQLiteDatabase.execute() extension with the AndroidX SQLiteDatabase.transaction() extension, which does the same thing.
This commit is contained in:
parent
4d92df7896
commit
355b3885e3
3 changed files with 6 additions and 27 deletions
|
|
@ -4,6 +4,7 @@ import android.content.ContentValues
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.database.sqlite.SQLiteDatabase
|
import android.database.sqlite.SQLiteDatabase
|
||||||
import android.database.sqlite.SQLiteOpenHelper
|
import android.database.sqlite.SQLiteOpenHelper
|
||||||
|
import androidx.core.database.sqlite.transaction
|
||||||
import org.oxycblt.auxio.logD
|
import org.oxycblt.auxio.logD
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -33,7 +34,7 @@ class BlacklistDatabase(context: Context) : SQLiteOpenHelper(context, DB_NAME, n
|
||||||
fun writePaths(paths: List<String>) {
|
fun writePaths(paths: List<String>) {
|
||||||
assertBackgroundThread()
|
assertBackgroundThread()
|
||||||
|
|
||||||
writableDatabase.execute {
|
writableDatabase.transaction {
|
||||||
delete(TABLE_NAME, null, null)
|
delete(TABLE_NAME, null, null)
|
||||||
|
|
||||||
logD("Deleted paths db")
|
logD("Deleted paths db")
|
||||||
|
|
|
||||||
|
|
@ -6,29 +6,6 @@ import android.os.Looper
|
||||||
import org.oxycblt.auxio.logE
|
import org.oxycblt.auxio.logE
|
||||||
import java.lang.Exception
|
import java.lang.Exception
|
||||||
|
|
||||||
/**
|
|
||||||
* Shortcut for running a series of [commands] on an [SQLiteDatabase].
|
|
||||||
* @return true if the transaction was successful, false if not.
|
|
||||||
*/
|
|
||||||
fun SQLiteDatabase.execute(commands: SQLiteDatabase.() -> Unit): Boolean {
|
|
||||||
beginTransaction()
|
|
||||||
|
|
||||||
val success = try {
|
|
||||||
commands()
|
|
||||||
setTransactionSuccessful()
|
|
||||||
true
|
|
||||||
} catch (e: Exception) {
|
|
||||||
logE("An error occurred when trying to execute commands.")
|
|
||||||
logE(e.stackTraceToString())
|
|
||||||
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
endTransaction()
|
|
||||||
|
|
||||||
return success
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shortcut for querying all items in a database and running [block] with the cursor returned.
|
* Shortcut for querying all items in a database and running [block] with the cursor returned.
|
||||||
* Will not run if the cursor is null.
|
* Will not run if the cursor is null.
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import android.content.Context
|
||||||
import android.database.sqlite.SQLiteDatabase
|
import android.database.sqlite.SQLiteDatabase
|
||||||
import android.database.sqlite.SQLiteOpenHelper
|
import android.database.sqlite.SQLiteOpenHelper
|
||||||
import androidx.core.database.getStringOrNull
|
import androidx.core.database.getStringOrNull
|
||||||
|
import androidx.core.database.sqlite.transaction
|
||||||
import org.oxycblt.auxio.logD
|
import org.oxycblt.auxio.logD
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -85,7 +86,7 @@ class PlaybackStateDatabase(context: Context) :
|
||||||
fun writeState(state: PlaybackState) {
|
fun writeState(state: PlaybackState) {
|
||||||
assertBackgroundThread()
|
assertBackgroundThread()
|
||||||
|
|
||||||
writableDatabase.execute {
|
writableDatabase.transaction {
|
||||||
delete(TABLE_NAME_STATE, null, null)
|
delete(TABLE_NAME_STATE, null, null)
|
||||||
|
|
||||||
this@PlaybackStateDatabase.logD("Wiped state db.")
|
this@PlaybackStateDatabase.logD("Wiped state db.")
|
||||||
|
|
@ -159,7 +160,7 @@ class PlaybackStateDatabase(context: Context) :
|
||||||
|
|
||||||
val database = writableDatabase
|
val database = writableDatabase
|
||||||
|
|
||||||
database.execute {
|
database.transaction {
|
||||||
delete(TABLE_NAME_QUEUE, null, null)
|
delete(TABLE_NAME_QUEUE, null, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -171,7 +172,7 @@ class PlaybackStateDatabase(context: Context) :
|
||||||
while (position < queueItems.size) {
|
while (position < queueItems.size) {
|
||||||
var i = position
|
var i = position
|
||||||
|
|
||||||
database.execute {
|
database.transaction {
|
||||||
while (i < queueItems.size) {
|
while (i < queueItems.size) {
|
||||||
val item = queueItems[i]
|
val item = queueItems[i]
|
||||||
i++
|
i++
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue