stephencelis / SQLite.swift

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

WIP: Demo Codable Enum issue #1124

Open ReDetection opened 2 years ago

ReDetection commented 2 years ago

This is not the MR to merge in the current, but more like an issue with submitted demo test case.

The problem: Codable Enum works fine only if saved via Encodable insert, but breaks if I insert column values directly. Compare:

try db.run(table.insert(value)) //this one will read just fine
// INSERT INTO "codable_camel_case" ("camelCaseString") VALUES ('"one"')
try db.run(table.insert(column <- .one)) //this one produces runtime error upon decode
// INSERT INTO "codable_camel_case" ("camelCaseString") VALUES ('one')

I did implement conformance to Value protocol, as you can see in the code, but it seems fromDatatypeValue() function is never used. Instead, it seems to treat it as a JSON, but the second inserted value is of course no valid JSON

joprice commented 7 months ago

There's also an issue with filtering: When using a String enum as a Value type for a column, it gets wrapped in quotes (its json representation instead of the raw string), but when using it as an Expression, it is not wrapped. This causes well-typed filters to fail to match. Not sure if there's a workaround for that.