3 releases (breaking)

0.3.0 Mar 18, 2024
0.2.0 Dec 7, 2023
0.1.0 Dec 7, 2023

#2755 in Database interfaces

Download history 1/week @ 2024-02-16 73/week @ 2024-02-23 23/week @ 2024-03-01 1/week @ 2024-03-08 125/week @ 2024-03-15 17/week @ 2024-03-22 22/week @ 2024-03-29 1/week @ 2024-04-05

112 downloads per month

MIT license

21KB
94 lines

PRQLX

Combining the query language of PRQL with the macro powers of SQLX.

PRQL is an amazing DSL for sql, but it doesn't have native support in sqlx. This crate bridges the gap by compiling prql in the rust macro itself before it's sent to sqlx (and your database). This means you can use the prql syntax with:

  • All supported sqlx database
  • Nice compiler errors
  • Fully typed rust

Usage

cargo add prqlx
cargo add sqlx # requires sqlx to be installed by you so you can configure it correctly
use prqlx::{query, query_as};

async fn test_query() {
    // get your sqlx pool in whatever way
    let pool = get_database_pool().await;

    // simply use it like a regular sqlx query, except you now use PRQL!
    let val = query!(
        "
        from users
        select { id, name }
        "
    )
    .fetch_all(&pool)
    .await
    .unwrap();

    // Same thing with query_as!
    // Bindings also just work:tm:
    let val2 = query_as!(
        User,
        "
        from users
        select { id, name }
        filter id == $1
        ",
        123
    )
    .fetch_one(&pool)
    .await
    .unwrap();

    println!("{:?}", val2);
}

Caveats

PRQL doesn't natively support ? for bindings. You will have to use s"?" for that. Rather use ${num} for bindings.

Todos

Not all macros have been implemented. The only ones are query and query_as. If you would like another to be added, please file an issue (or make a PR!).

Testing/Contributing

Before running cargo test, the database must first be setup. To do so, run cargo run --example prepare to prepare the sqlite database. After that, you can develop as usual.

Dependencies

~13–25MB
~353K SLoC