Search code examples
scalalift

store ID after deletion with java scala in lift


I have a table containing id,name,description and unique random ID and I need to store the unique id(not primary key) after a user deletes a record. This is because the deleted unique id shouldn't be assigned to a new record later. Below is my code for generating the unique ID, how can I store them in an array only after deletion, also how I can check new id against the stored id so that it won't get assigned again.

def uniqueRandomKey(chars: String, length: Int, uniqueFunc: String=>Boolean) : String =
{
val newKey = (1 to length).map(
x =>
{
 val index = Random.nextInt(chars.length)
 chars(index)
}
).mkString("")

if (uniqueFunc(newKey))
newKey
else
uniqueRandomKey(chars, length, uniqueFunc)
}
def isUnique(s:String):Boolean = true
val chars = ('A' to 'Z')
val key = uniqueRandomKey(chars.mkString(""), 4, isUnique)

Solution

  • Can you change the schema? If so, add a DELETED column and never actually delete rows.

    Then you can either lookup the unique id for a new record to check it is not the same as a deleted one, or use a unique constraint on the column to get an error when you try to use it.

    You probably don't want them in just an in-memory array anyway as presumably the need to check deleted ids aren't reused persists over (say) server restarts.

    If you can't change the table definition but you can add another table, create a DELETED_ITEMS table, copy the deleted records into that before you delete them, then lookup ids of proposed new recordsin this table to check they've not been used before