stephencelis / SQLite.swift

A type-safe, Swift-language layer over SQLite3.
MIT License
9.64k stars 1.56k forks source link

Filter is not working when Inserting Encodable Values #1017

Open Firas-Shrourou opened 4 years ago

Firas-Shrourou commented 4 years ago

Inserting using [String: String] is working fine like this:

func insertSomeValues(tableName: String, contentValues: [String: String]) {
        do {
            let table = Table(tableName)
            let rowid = try database.run(table.insert(contentValues))
            print ("inserted id: \(rowid)")
        } catch {
            print("Insertion Into Table Failed: \(error)")
        }
    }

But when filtering the table using .filter(Key == "Some_Key") it will not work

You need to use it like this to work

.filter(Key == "\"SomeKey\"")

What is the problem here ?

Firas-Shrourou commented 4 years ago

Seems that we need to make a change in the code

        func encode<T>(_ value: T, forKey key: Key) throws where T: Swift.Encodable {
            if let data = value as? Data {
                self.encoder.setters.append(Expression(key.stringValue) <- data)
            } else if let string = value as? String {
                self.encoder.setters.append(Expression(key.stringValue) <- string)
            } else {
                let encoded = try JSONEncoder().encode(value)
                let string = String(data: encoded, encoding: .utf8)
                self.encoder.setters.append(Expression(key.stringValue) <- string)
            }
        }
TenMaKo commented 4 years ago

Yes. This is a quickfix I proposed. It fixes the issue about strings being inserted as _"yourstring" instead of _yourstring.

Firas-Shrourou commented 4 years ago

You need also to clean folder and rebuild.

TenMaKo commented 4 years ago

You need also to clean folder and rebuild.

Yes, as it won't correct already inserted data.

Firas-Shrourou commented 4 years ago

is there any plan to fix this inside the distributed package/ pod in the future?

TenMaKo commented 4 years ago

is there any plan to fix this inside the distributed package/ pod in the future?

This repo doesn't seem to be maintained anymore and I'm in no way connected to the maintainer. In other opened issues related to Codable, you will find workarounds to be able to use this protocol but it's not a proper implementation so I have no plan about creating a PR or fork. Sorry :/

nathanfallet commented 3 years ago

@TenMaKo We would be happy to review your PR if you open one (and mark it as fixing this issue)