this post was submitted on 27 Dec 2023
547 points (96.9% liked)

Programmer Humor

19503 readers
652 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 29 points 10 months ago (4 children)

if you don't believe that adding more structure to the absolute maniacal catastrophe that is sql is a good thing then i'm going to start to have doubts about your authenticity as a human being

[–] [email protected] 25 points 10 months ago

If you think this is more structured than traditional SQL, I really disagree. Is this a select * query, it's ambiguous. Also what table is being queried here there's no from or other table identifier.

[–] [email protected] 2 points 10 months ago* (last edited 10 months ago) (5 children)

Me trying to remember on whose output data having, count, sum, etc. work

Once you know functions you would have no reason to go back.
I propose we make SQL into this:

const MAX_AMOUNT = 42, MIN_BATCHES = 2

database
    .from(table)
    .where(
        (amount) => amount < MAX_AMOUNT,
        table.field3
    )
    .select(table.field1, table.field3)
    .group_by(table.field1)
    .having(
        (id) => count(id) >MIN_BATCHES
        table.field0
    )

(Sorry for any glaring mistakes, I'm too lazy right now to know what I'm doing)

..and I bet I just reinvented the wheel, maybe some JavaScript ORM?

[–] [email protected] 5 points 10 months ago* (last edited 10 months ago) (2 children)

Because you never learned SQL properly, from the sound of it.

Also, ORMs produce trash queries and are never expressive enough.

[–] [email protected] 3 points 10 months ago

ORMs produce good queries if you know what you do. Which requires proper knowledge of SQL, unfortunately.

[–] [email protected] 3 points 10 months ago* (last edited 10 months ago)

Because you never learned SQL properly, from the sound of it.

You might be right, though, to be fair, I also keep forgetting syntax of stuff when I don't use it very often (read SQL (._.`))

Also, ORMa produce trash queries and are never expressive enough.

I meant to say that I would like the raw SQL syntax to be more similar to other programming languages to avoid needing to switch between thinking about different flows of logic

[–] [email protected] 3 points 10 months ago (3 children)

Well, if you lose the OOPism of those dots, we can talk.

Anyway, I'm really against the "having" tag. You need another keyword so that you can apply your filter after the group by?

[–] [email protected] 2 points 10 months ago

Well, if you lose the OOPism of those dots, we can talk.

That's a good point, I didn't even think about it, maybe a more functional style would make more sense?

[–] [email protected] 1 points 10 months ago (1 children)

Boy then are you going to hate QUALIFY

[–] [email protected] 2 points 10 months ago

Yes, I do. It's a lot of effort and hidden functionality to try to paper over the fact that the statements do not compose.

[–] [email protected] 0 points 10 months ago (1 children)

having is less annoying way of not doing needless/bug-prone repetition. if you select someCalculatedValue(someInput) as lol you can add having lol > 42 in mysql, whereas without (ie in pgsql) you’d need to do where someCalculatedValue(someInput) > 42, and make sure changes to that call stay in sync despite how far apart they are in a complex sql statement.

[–] [email protected] 1 points 10 months ago

Postgres has the having clause. If it didn't, that wouldn't work, as you can't use aggregates in a where. If you have to make do without having, for some reason, you can use a subquery, something like select * from (select someCalculatedValue(someInput) as lol) as stuff where lol > 42, which is very verbose, but doesn't cause the sync problem.

Also, I don't think they were saying the capability having gives is bad, but that a new query language should be designed such that you get that capability without it.

[–] [email protected] 2 points 10 months ago (1 children)
[–] [email protected] 1 points 10 months ago* (last edited 10 months ago)

Thanks for the suggestion! It looks interesting, not quite what I expected looking at that file*, but that may very well be better

Edit: other examples seem a bit more similar to mine, cool!

[–] [email protected] 2 points 10 months ago

most languages have some first or third party lib that implements a query builder

[–] [email protected] 1 points 10 months ago

No. The arrow function in where eliminates any possibility of using indexes. And how do you propose to deal with logical expressions without resorting to shit like .orWhereNot() and callback hell? And, most importantly, what about joins?

[–] [email protected] 0 points 10 months ago (1 children)

Huh? Sql is one of the most powerful, action packed (as in you can move lots of shit with few commands) languages out there.

It's transferable and ubiquitous.

[–] [email protected] 1 points 10 months ago (1 children)

powerful isn't the same as well-structured

it was written to be a language that anybody could read or write as well as english, which just like every other time that's been tried, results in a language that's exactly as anal about grammar as C or Python except now it's impossible to remember what that structure is because adding anything to the language to make that easier is forbidden

when you write a language where its designers were so keen for it to remain human readable that they made deleting all rows in a table the default action, i don't think "well structured" can be used to describe it

[–] [email protected] 1 points 10 months ago

Disagree, the difference between "week structured" and needing to know the rules of the verbs is pretty big, to me.

[–] [email protected] -3 points 10 months ago* (last edited 10 months ago) (2 children)

but sql doesn't need to be structured that's what abstraction layers and models are for

[–] [email protected] 15 points 10 months ago

SQL is literally structured query language

[–] [email protected] 11 points 10 months ago

SQL is incredibly structured. It's also a very good language, and developers need to stop piling on junk on top of it and producing terrible queries. Learn the damn language. It's not that hard