That said, concatenating raw strings to form SQL that has no compile time type-checking is tedious and error prone. Thankfully, my "expert" knowledge of SQL does not blind me to the benefits of what a well-written ORM can do for productivity. (E.g. results caching, compile-time validation, auto-completion in the IDE, etc)
I know SQL and actually often enjoy using it. I guess this is a common trait for Java developers.
Still I often find a ORM to be a good tool on many projects.
The only substitute I'd accept is a quasiquoter that checked my SQL syntax for me at compile time.
Some of the additional query languages might be able to do it (they could certainly do chunks of what I said), but they'd still be pretty klunky about it since they bodge it on the side, and still don't compose anywhere near as well as they could or should.
Mind you, I'm not sure this library can do it either; SQL is also quite difficult to wrap around because of its structural deficiencies. Trying to hack away foundational issues at higher levels is always messy, error-prone, and still filled with the quirks that shine through.
Something like:
CREATE VIEW permitted_X AS
SELECT X.*
FROM X, user
WHERE X.user_id = user.user_id
AND (X.permission OR user.is_super);
You second example doesn't have enough info for me to see if it can be accomplished by a VIEW.Other abstraction mechanisms are user-defined functions.
[1] https://www.postgresql.org/docs/current/static/sql-createvie...
Some of these things are fixed up by the product-specific languages, but only some of these things.
Here's another example; using your product-specific language, can you create a table for me with a variable number and types of columns based on the parameters passed in? I don't know them all, but I bet it's hard in most or all of them. No credit if your product-specific language lets you bash a string together and then somehow execute it; I'm calling for everything to be done via first-class mechanisms. (Also, I'm not asking for whether this is a good idea; it is obviously a tricky thing of dubious use. But that should be a software engineering determination, not a language restriction.)
PREPARE stmt1 FROM 'SELECT X.* FROM ? X, user WHERE X.user_id = user.user_id AND (X.permission OR user.is_super)';
EXECUTE stmt1 USING 'yourtable';
It's also not enough to know SQL. You also need to know how your ORM maps objects to the database. E.g. do you get the same object if you execute a query twice? A surprising number of people don't know, and assume that ORM objects behave like normal objects and that is not always the case.
Personally, I prefer to write SQL in my models as it just _makes sense_ to me, but I understand why some don't.
If you start creating custom views and queries then you need to update them whenever there's a change to your objects. Also getting views into source control is something that you'll have to manage on your own.
An ORM is just less work. Although you'll often need to drop into real SQL for performance reasons or for complex work.
All the good ORMs allow this.
ORM does the reverse, it tries to map objects into relational data, the problem is that those are two incompatible way of storing data, you can do this for some objects but it doesn't apply for all cases.
Since relational algebra was proposed by Cobb, it took over all databases because it was proven that it's most efficient way to store data we know.
ORM might be useful for some cases, but it will generally limit you with what you can do with data.
IMO I think what's really missing is to have have another query language that is integrated with your programming language (so you don't have to write it as a string, and can benefit from language features such as auto completion, type checking etc).
I kind of wish QUEL[1] would win instead of SQL, since that language seems to be easier to be integrated.
There are also attempts such as LINQ[2] which appears to do good (I did not use it myself, since I don't program in .NET), but it looks great, I wish it became a standard and got integrated in other languages.