-
-
Notifications
You must be signed in to change notification settings - Fork 722
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
DatabaseValueConvertible support for Encodable / Decodable (?) #1096
Comments
Hello @freak4pc, Collections, arrays, sets, etc, are not Those are very old decisions that I can't fully remember now, I admit. It may even be time to revisit some of them, or at least check what would break if they were reverted. But if you consider that Codable Record types have options that control the JSON encoding of their columns, you'll see that in the line This alone is a good reason for not letting Is there a specific reason you write an explicit |
Now I remember. At the root was ccgus/fmdb#180. With FMDB, you can write: NSArray *ids = @[@1, @2, @3];
[db executeQuery:@"SELECT * FROM table WHERE id IN (?)" withArgumentsInArray:@[ids]] The result was garbage, because this would not run I learned the hard way that SQLite values that feed Based on this experience, I did not want GRDB users to feel my pain. So this code has never compiled: // Compiler error: [Int] is not DatabaseValueConvertible
let ids = [1, 2, 3]
let players = try Player.fetchAll(db, sql: "SELECT * FROM player WHERE id IN (?)", arguments: [ids]) And I don't want that it starts compiling: Since then, I did a lot to enhance support for collections, though. For example you can run: let ids = [1, 2, 3]
let request: SQLRequest<Player> = "SELECT * FROM player WHERE id IN \(ids)"
let request = Player.filter(ids.contains(Column("id")))
let players = try Player.fetchAll(db, ids: ids) So maybe a layer of convenience could be added to But before we start looking into this, I need to know why. Hence my question:
|
@groue Thanks for the prompt response! I think I bumped into that issue with FMDB, but that's the troubles of a dynamic language. I think it would be "easier" to avoid it in a statically typed language such as Swift. Per your question, of "why":
Those are the cases I have in mind. We start having a few helper methods to encode into BLOB and decode from BLOB but that's a bit more involved (probably best option if GRDB doesn't have something like it). |
I agree.
OK, I get it. That's quite legit. Thanks 👍 In the end, my understanding is that a convenience method or subscript on |
That's what we did for now :) |
Thank you very much @freak4pc for this issue. 👍 To be honest, I think that this will only be handled in GRDB 6, where I'll finally allow errors to be thrown from row initializers (a long overdue) - and I have strong hopes that by then the language will have throwing subscripts. I add flag "needs revisiting", as an issue that contains ideas that should not be forgotten. |
Looking forward! Thanks :) FYI, The language already has throwing subscripts in Swift 5.5 :) at least for getters
|
What did you do?
In some model:
What did you expect to happen?
I assumed that you can assign
Codable
things as if they wereDatabaseValueConvertibles
(at least that's how it seems theoretically in DatabaseValueConvertible+Decodable.swift and DatabaseValueConvertible+Encodable.swift), but this doesn't seem to be the cast, unless I'm missing something.What happened instead?
Environment
GRDB flavor(s): GRDB
GRDB version: 5.12.0
Installation method: CocoaPods
Xcode version: 13.1
Swift version: 5.5
Platform(s) running GRDB: iOS
macOS version running Xcode: Monterey
Demo Project
If needed let me know, seems unneeded in this question context
The text was updated successfully, but these errors were encountered: