-
I want to build a query interface for my app based on a query syntax that's essentially a subset of SQL. At the same time, I want to be able to parse such a query so I can subsequently refine or otherwise manipulate it in a structured way. That is to say, I want to be able to refine or alter the query without string manipulation of the query string itself. To me, this suggests I want something that can take a query string and return a I know there's a Is this possible with GRDB? Cheers. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Hi @mallman! There is no built-in conversion from SQLRequest to QueryInterfaceRequest, because GRDB does not contain any SQL parser at all! As far as I know, SQLite does not expose any parsing function. And shipping a home-made parser is 1. a difficult task, 2. begging for troubles. It is begging for troubles because the slightest difference between GRDB parsing vs. SQLite parsing would be... a GRDB bug that users would be entitled to report. I don't want to ship code that generates bugs as time passes and SQLite evolves. It is not sustainable to live at the rhythm of iOS/macOS/SQLite releases. And did I mention the huge number of SQLite versions supported by the library, considering its minimum requirements? That's nearly as many slightly different SQL grammars. The only reasonable decision was to avoid SQL parsing like the plague. Instead, I made sure that raw SQL is as well supported as possible (full SQL queries, and SQL snippets). And since not everyone speaks SQL, I designed QueryInterfaceRequest as an SQL builder (not a parser!), based on a limited subset of the SQL syntax (see Query Interface Organization). Building QueryInterfaceRequest was hard, but it was still much easier than parsing SQL 😉. And I control the supported features. I know there are libraries that look like they parse SQL. For example, take Jetpack Room: @Dao
interface UserDao {
// SQLite is unable to understand ":userIds" as a
// placeholder for an array of values.
// Conclusion: Jetpack Room MUST parse this
// string in a way or another.
@Query("SELECT * FROM user WHERE uid IN (:userIds)")
fun loadAllByIds(userIds: IntArray): List<User>
} Now does it mean that Jetpack Room understand the SQL and is able to modify the WHERE clause or alter the SELECT clause? I'm not even sure. After all, GRDB has a similar feature: extension User {
static func filter(ids: [Int]) -> SQLRequest<User> {
"SELECT * FROM user WHERE uid IN \(ids)"
}
} If even Google won't do it... |
Beta Was this translation helpful? Give feedback.
Hi @mallman!
There is no built-in conversion from SQLRequest to QueryInterfaceRequest, because GRDB does not contain any SQL parser at all!
As far as I know, SQLite does not expose any parsing function. And shipping a home-made parser is 1. a difficult task, 2. begging for troubles.
It is begging for troubles because the slightest difference between GRDB parsing vs. SQLite parsing would be... a GRDB bug that users would be entitled to report. I don't want to ship code that generates bugs as time passes and SQLite evolves. It is not sustainable to live at the rhythm of iOS/macOS/SQLite releases. And did I mention the huge number of SQLite versions supported by the library, considering its …