3 ms·
> You can't "statically syntax check" a dynamically built query. In a language with any sort of decent static type system, you most assuredly can. There should
by vertex-four 12y ago
> You can't "statically syntax check" a dynamically built query.
In a language with any sort of decent static type system, you most assuredly can. There should be no way to build and use an invalid query, the same as you can use the type system to enforce any other constraints.
> It's one thing to use a query builder for those rare complicated dynamic queries, but an entirely different thing to lock down and hide the connection and have your hands at the mercy of an abstraction layer for your every query.
Almost any SQL I've written has needed to be composable, for the same reasons that any other code should be composable. I need to be able to get <X> complex set of data from the database, given an identifier which could be an ID, or an URL slug, or any number of other things. I may also need to get that either for one row or for many.
It's nice to be able to split that up into the bit of the query that selects the data, and the bit of the query that chooses which row(s) to select it from, without writing out the SQL twice or developing "stringly-typed" code. It's a standard across most of the software development world that when dealing with source code (which SQL is), you almost always work with ASTs.
The frameworks I use for this do allow me complete access to the database, including all the things you've mentioned, and the ability to extend my queries with arbitrary syntax.
- mantrax5 12y ago> "There should be no way to build and use an invalid query" Well the SQL server won't actually run an invalid query, so what's your point? I'm not afraid of invalid queries. I'm rather afraid of valid queries like these: "DELETE FROM Table" Because depending on how abstract your ORM is, it's far easier to run something you really don't want to run on your SQL server. But at least it'll be valid, hey!
- mercurial 12y ago> Well the SQL server won't actually run an invalid query, so what's your point? To avoid runtime errors, I suppose. As for "DELETE FROM table", I would say it's not any likelier to happen with an ORM than without. I haven't experienced issues of this sort, at least. The problem is more to build efficient, complex SELECT queries without getting a bad surprise performance-wise once in production.