News for hackers. Stuff that matters.
Re:Rust-Query by Diggsey on Saturday November 30, 2024 @10:49AM (#42282172)
SQL, while not great, does have some advantages that are hard to beat:

- Everyone knows at least some basic SQL, even non-technical users have often encountered it in some form.

- The documentation for eg. PostgreSQL is for SQL, so if you write queries in anything else you have to mentally translate back and forth, and so you need to know SQL anyway.

- Any external tools you use to interact with the database will use SQL.

- Changing the queries doesn't require an expensive compilation step - `sqlx` gets the best of both worlds in this respect, in that it is able to type-check the parameters and rely on the database itself to validate the query, so you don't end up with a ton of type-system shenanigans that increase compilation times.

Maybe for a brand new database, a better query language could win out, but having used sqlx I can't imagine going back to a query-builder style interface for existing SQL databases.

Re:Rust-Query by unshavedyak on Saturday November 30, 2024 @11:44AM (#42282484)
I am toying with spreadsheet backends these days and it's had me wondering - do spreadsheet query languages offer value that SQL is missing? Beyond "people are used to it" of course.

One thing i am wanting beyond SQL though is a more Object oriented language. Ie i want something SQL-like (or more specifically PRQL, which i find very nice), but without the being so tailored towards a flat data structure. I want to use it on JSON/etc.

Re:Rust-Query by wslh on Saturday November 30, 2024 @12:05PM (#42282609)
BTW, do you suggest a LINQ kind of integration in Rust? I just found the Microsoft has a Rust for C#/.NET developers resource [1][2] which is more about integration I think. It is a little weird that there is not Rust.NET yet. .NET is not a garbage collection only platform.

[1] https://microsoft.github.io/rust-for-dotnet-devs/latest/linq... [2] https://microsoft.github.io/rust-for-dotnet-devs/latest/

Re:Rust-Query by estebank on Saturday November 30, 2024 @02:37PM (#42283526)
I'm impressed by this individual's effort to bring rust to the clr: https://github.com/FractalFir/rustc_codegen_clr

It is already way more complete given it's age than I would have expected.

I agree that the clr is a more natural target than, let's say, the JVM (at least until Project Valhalla comes out).

Re:Rust-Query by jeltz on Saturday November 30, 2024 @11:34AM (#42282413)
Yeah, I am using sqlx and it is amazing. The only thing it cannot do is help you in the case when you actually need a query builder, e.g. when you need support for queries with dynamic filters and sort orders. But other than that it is much nicer to work with than every other query library I have used.
Re:Rust-Query by Diggsey on Saturday November 30, 2024 @12:44PM (#42282896)
FWIW, you very rarely need to make the query itself dynamic. After all, SQL alone is powerful enough to do conditionals.

For example:

    SELECT * FROM example
    WHERE ($1 IS NULL OR field1 = $1)
    AND ($2 IS NULL OR field2 = $2)
    ...

When you supply the parameters to this prepared statement, the constant conditions should be optimized away by the query planner if you're using a decent database.
Re:Rust-Query by ris on Saturday November 30, 2024 @03:53PM (#42283967)
Those tricks cover a very small subset of dynamic queries, and if you don't give your developers a proper tool to do dynamic queries in, your code will start growing these funny string-formatting-based query-mangling functions that have god-knows-what escaping problems behind them.
Re:Rust-Query by Diggsey on Saturday November 30, 2024 @04:25PM (#42284138)
That has not been my experience. SQL is a fully fledged programming language so there's very little that you can't express this way.

What are you doing that requires dynamically building a query?

Re:Rust-Query by kelnos on Saturday November 30, 2024 @05:49PM (#42284620)
> That has not been my experience

Well, it has been mine. That trick you suggested works for the simple binary "either include this WHERE clause filter or don't" -- which, agreed, is something that comes up often enough -- but it's only in the simplest of CRUD apps where that's all I've needed.

A quick example: I have a REST endpoint that updates a resource. The JSON fields that map to the DB columns can be missing (leave the column value unchanged), null (set the column value to NULL), or have a value (set the new provided value). The list of assignments in the UPDATE needs to be dynamic; sometimes there might be only one column being updated, sometimes two, three, four five...

Re:Rust-Query by jon_richards on Sunday December 01, 2024 @06:06AM (#42287718)
Isn’t the standard just to set the unchanged column to itself?
Re:Rust-Query by Diggsey on Monday December 02, 2024 @10:19AM (#42297038)
In this case you just set the column value to the existing value...
Re:Rust-Query by LinXitoW on Saturday November 30, 2024 @05:50PM (#42284629)
It's a fully fledged, horrible language. Anything beyond basic queries is unreadable, ESPECIALLY when it's done in plain strings in another language. There's not even a way to not have to repeat values EVERY SINGLE TIME (think variables or constants in every other language).

Oh, but what about <feature>? Well, is that SQL, or a frankensteined version of SQL, aka a "dialect"?

SQL is the JavaScript of databases, and we'll be better for it once we admit this.

Re:Rust-Query by weiznich on Monday December 02, 2024 @11:33AM (#42297771)
As this claim comes up quite often I have a set of examples I typically ask these people to provide. So far I've got so response, maybe you can point out how you would implement one or more of the following queries using `sqlx::query!`?

* A batch insert query for mysql

* A conditional where clause for postgresql that allows the user to dynamically specify the column + filter operation + values over e.g. a rest end point. Each column can appear zero, one or multiple times with different or even the operations.

* A IN expression with a list of values provided by a rust `Vec<_>` (so dynamically sized) for sqlite.

Re:Rust-Query by 63stack on Sunday December 01, 2024 @05:19AM (#42287517)
Can you show an example where you have 4 possible fields to filter on, and then one more where the sorting field is dynamic? (Not the value, but the column)
Re:Rust-Query by pimeys on Sunday December 01, 2024 @05:42AM (#42287615)
You can add sea-orm on top of sqlx for a query builder. It can also introspect your SQL database and generate entity types.

I built my own back in the days when I worked for Prisma. None of these crates existed. It's not that complex if you limit the builder to queries you need in your application.

But, to be honest, jOOQ or LINQ would be awesome to get for Rust.