Skip to content

Commit

Permalink
Improve reliability of SqlUtil.getNextAutoIncrementId()
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Jan 5, 2023
1 parent 84d9e1d commit bfba60b
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core-util/src/main/java/org/signal/core/util/SqlUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import android.content.ContentValues
import android.text.TextUtils
import androidx.annotation.VisibleForTesting
import androidx.sqlite.db.SupportSQLiteDatabase
import org.signal.core.util.logging.Log
import java.util.LinkedList
import java.util.Locale
import java.util.stream.Collectors

object SqlUtil {
private val TAG = Log.tag(SqlUtil::class.java)

/** The maximum number of arguments (i.e. question marks) allowed in a SQL statement. */
private const val MAX_QUERY_ARGS = 999

Expand Down Expand Up @@ -50,7 +53,15 @@ object SqlUtil {
if (cursor.moveToFirst()) {
val current = cursor.requireLong("seq")
return current + 1
} else if (db.query("SELECT COUNT(*) FROM $table").readToSingleLong(defaultValue = 0) == 0L) {
Log.w(TAG, "No entries exist in $table. Returning 1.")
return 1
} else if (columnExists(db, table, "_id")) {
Log.w(TAG, "There are entries in $table, but we couldn't get the auto-incrementing id? Using the max _id in the table.")
val current = db.query("SELECT MAX(_id) FROM $table").readToSingleLong(defaultValue = 0)
return current + 1
} else {
Log.w(TAG, "No autoincrement _id, non-empty table, no _id column!")
throw IllegalArgumentException("Table must have an auto-incrementing primary key!")
}
}
Expand Down

0 comments on commit bfba60b

Please sign in to comment.