[Feature] Improvements to QueryBuilder

Aperta
#4,141 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 4 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
22/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
postgresql, rust, sql
Ambito
backend, databases

Direzione di ricerca

This is a design-level change to QueryBuilder: reusable bind IDs (Postgres), storing SQL/binds as a Vec until build time so queries can nest and concatenate, and a format_sql! macro that keeps some static checking. Start from the existing QueryBuilder API (including sql vs into_sql) and how binds are numbered today. Done means the three capabilities work without breaking into_sql, with tests for nested queries and repeated binds; the sql method may be dropped.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement
Description

The QueryBuilder is adequate for building a query in order, but it has several drawbacks:

  1. It's not possible to refer to the same "bind" parameter multiple times (this might be a Postgres-only feature).
  2. It's not possible to build queries "out of order". This happens if eg. I want to build a query, then wrap the entire thing as a subquery within some outer query. The code building the subquery has to be refactored to work with a mutable borrow of the same parent query.
  3. All static analysis of the queries are lost.
Prefered solution
  1. There should be two Postgres-specific extension methods. One that pushes a new bind and returns an opaque "ID" for that bind. A second that pushes a reference to a previous bind by "ID".

  2. The QueryBuilder should store everything internally as a Vec<SqlOrBind>, and only convert to a string representation when the query is actually built. This way queries can be concatenated, one query can be used inside another, etc. without messing up the bind parameters. (When they are concatenated, all of the binds in the second query are automatically adjusted to not conflict with any binds from the first query)

  3. The QueryBuilder should support a format! style macro, where the user can annotate format arguments with the type of SQL AST node they correspond to. For example: format_sql!("SELECT * FROM foo WHERE {:expression} LIMIT 2"). This would produce a statically checked query like the query! macros, where during checking, the format argument is replaced by a suitable placeholder (eg. NULL for an expression). These format arguments should themselves be QueryBuilders, so that complex parts of the SQL can be built dynamically while still retaining some static checking.

Is this a breaking change? Why or why not?

The sql method on QueryBuilder cannot easily be supported with these changes (although into_sql is fine).

Lingua principale
Rust
Stelle
17.5k
Fork
1.7k
Merge medio
1g 17h
PR unite (30g)
7

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di transact-rs/sqlx

Tutte le issue di transact-rs/sqlx

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.