Tencent / wcdb

WCDB is a cross-platform database framework developed by WeChat.
Other
10.78k stars 1.41k forks source link

How to perform a fuzzy search on JSON fields? #1126

Closed EchoLunar closed 2 months ago

EchoLunar commented 2 months ago

The language of WCDB

Swift

The version of WCDB

v2.1.6

The platform of WCDB

iOS

The installation of WCDB

Package

What's the issue?

struct Home: TableCodable {
    var id: Int = 0
    var name: String = ""
    var room: Room?

    enum CodingKeys: String, CodingTableKey {
        typealias Root = Home
        static let objectRelationalMapping = TableBinding(CodingKeys.self)
        case id
        case name
        case room
    }
}

struct Room: ColumnJSONCodable {
    var id: Int = 0
    var content: String = ""
    var name: String = ""
}

I have created the table "Home", how can I perform a fuzzy search on the content field of Room?

I try to use this code, but it doesn't work.

let keyword = "%\(key)%"
let homes: [Home] = try! database.getObjects(fromTable: "Home", where: Home.Properties.room.like(keyword)) 
Qiuwen-chen commented 2 months ago

ColumnJSONCodable use JSONDecoder to encode your model into a json data. The json data may not use utf8 encoding for strings, while SQLite uses utf8 format for text matching.