Skip to content

Commit

Permalink
add utility methods to get the correct database version on backup cre…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
Kamuno committed Aug 22, 2022
1 parent ec782fb commit af64403
Showing 1 changed file with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
import org.secuso.privacyfriendlybackup.api.util.toBase64
import java.io.StringWriter


/**
* This is a convenience class, that provides utility methods to write databases to json.
* <p>
Expand Down Expand Up @@ -199,7 +200,13 @@ object DatabaseUtil {
}

@JvmStatic
fun getSupportSQLiteOpenHelper(context: Context, databaseName: String, version: Int) : SupportSQLiteOpenHelper {
@JvmOverloads
fun getSupportSQLiteOpenHelper(context: Context, databaseName: String, version: Int = 0) : SupportSQLiteOpenHelper {
var version = version
if(version == 0) {
version = getVersion(context, databaseName)
}

val config = SupportSQLiteOpenHelper.Configuration.builder(context).apply {
name(databaseName)
callback(object : SupportSQLiteOpenHelper.Callback(version) {
Expand All @@ -210,6 +217,16 @@ object DatabaseUtil {

return FrameworkSQLiteOpenHelperFactory().create(config)
}

@JvmStatic
fun getVersion(context: Context, databaseName: String): Int {
val dataBase = SQLiteDatabase.openDatabase(
context.getDatabasePath(databaseName).path,
null,
SQLiteDatabase.OPEN_READONLY
)
return dataBase.version
}
}

fun SupportSQLiteDatabase.toJSON() : String {
Expand Down

0 comments on commit af64403

Please sign in to comment.