Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlite needs "_" char for LIKE queries to be escaped #221

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading