Skip to content

Commit

Permalink
sqlite needs "_" char for LIKE queries to be escaped
Browse files Browse the repository at this point in the history
  • Loading branch information
zoff99 committed Apr 23, 2024
1 parent bfcd5e7 commit e9f2497
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ import com.zoffcc.applications.trifa.CustomSemaphore
import com.zoffcc.applications.trifa.EmojiStrAndName
import com.zoffcc.applications.trifa.FriendSettingDetails
import com.zoffcc.applications.trifa.GroupSettingDetails
import com.zoffcc.applications.trifa.GroupStoreFilterFilter
import com.zoffcc.applications.trifa.SqliteEscapeLikeString
import com.zoffcc.applications.trifa.HelperGeneric.PubkeyShort
import com.zoffcc.applications.trifa.HelperGeneric.ngc_video_frame_last_incoming_ts
import com.zoffcc.applications.trifa.HelperGroup
Expand Down Expand Up @@ -1338,7 +1338,7 @@ fun load_messages_for_friend(selectedContactPubkey: String?)
var messages: MutableList<Message>? = null
val filter_active = contactstore.state.messageFilterActive
val filter_value_raw = contactstore.state.messageFilterString
val filter_value = GroupStoreFilterFilter(filter_value_raw)
val filter_value = SqliteEscapeLikeString(filter_value_raw)
if ((filter_active) &&
(!filter_value_raw.isNullOrEmpty()) &&
(filter_value_raw.isNotBlank()) &&
Expand Down Expand Up @@ -1424,7 +1424,7 @@ fun load_groupmessages(selectedGroupId: String?)
var messages: MutableList<GroupMessage>? = null
val filter_active = groupstore.state.groupmessageFilterActive
val filter_value_raw = groupstore.state.groupmessageFilterString
val filter_value = GroupStoreFilterFilter(filter_value_raw)
val filter_value = SqliteEscapeLikeString(filter_value_raw)
if ((filter_active) &&
(!filter_value_raw.isNullOrEmpty()) &&
(filter_value_raw.isNotBlank()) &&
Expand Down
12 changes: 10 additions & 2 deletions src/main/kotlin/com/zoffcc/applications/trifa/GroupStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ interface GroupStore
val state get() = stateFlow.value
}

fun GroupStoreFilterFilter(filterstring_raw: String): String
fun SqliteEscapeLikeString(filterstring_raw: String): String
{
try
{
return filterstring_raw.replace("\\", "\\\\").replace("%", "\\%")
// HINT:
// It is standard SQL that in LIKE expressions:
//
// % matches any sequence of characters, including an empty one. It is equivalent to .* in a regular expression.
// _ matches a single character. It is equivalent to . in a regular expression.
//
return filterstring_raw.replace("\\", "\\\\")
.replace("%", "\\%")
.replace("_", "\\_")
} catch (_: Exception)
{
return filterstring_raw
Expand Down

0 comments on commit e9f2497

Please sign in to comment.