5 releases

0.2.3 Mar 26, 2024
0.2.2 Feb 28, 2024
0.2.1 Feb 6, 2024
0.2.0 Jan 30, 2024
0.1.0 Jan 30, 2024

#704 in Database interfaces

Download history 5/week @ 2024-01-29 16/week @ 2024-02-05 74/week @ 2024-02-19 170/week @ 2024-02-26 20/week @ 2024-03-04 10/week @ 2024-03-11 121/week @ 2024-03-25 21/week @ 2024-04-01

154 downloads per month

MIT/Apache

18KB
297 lines

PostgreSQL named arguments

This library allows one to use named arguments in PostgreSQL queries. This library is especially aimed at supporting rust-postgres. A macro is provided to rewrite queries with named arguments, into a query and its positional arguments.

Dependencies

The macro expands to usage of postgres-types, so make sure to have it in your dependencies:

[dependencies]
postgres-types = ...
pg_named_args = ...

Query Argument Syntax

The macro uses struct syntax for the named arguments. The struct name Args is required to support rustfmt and rust-analyzer. As can be seen from the example below, shorthand field initialization is also allowed for named arguments.

let location = "netherlands";
let period = Period {
    start: 2020,
    end: 2030,
};

let (query, args) = query_args!(
    r"
    SELECT location, time, report
    FROM weather_reports
    WHERE location = $location
        AND time BETWEEN $start AND $end
    ORDER BY location, time DESC
    ",
    Args {
        location,
        start: period.start,
        end: period.end,
    }
);
let rows = client.query(query, args).await?;

Insert Syntax

For INSERT's a special syntax is supported, which helps to avoid mismatches between the list of column names and the values:

let location = "sweden";
let time = "monday";
let report = "sunny";

let (query, args) = query_args!(
    r"
    INSERT INTO weather_reports
        ( $[location, time, report] )
    VALUES
        ( $[..] )
    ",
    Args {
        location,
        time,
        report
    }
);
client.execute(query, args).await?;

IDE Support

First, the syntax used by this macro is compatible with rustfmt. Run rustfmt as you would normally and it will format the macro.

Second, the macro is implemented in a way that is rust-analyzer "friendly". This means that rust-analyzer knows which arguments are required and can complete them. Use the code action "Fill struct fields" or ask rust-analyzer to complete a field name.

Goals

  • Increase usability of executing PostgreSQL queries from Rust.

  • Reduce the risk of mismatching query arguments.

  • Support for rustfmt to help with formatting.

  • Support for rust-analyzer completion and some code actions.

Contributing

We welcome community contributions to this project.

Please read our Contributor Terms before you make any contributions.

Any contribution intentionally submitted for inclusion, shall comply with the Rust standard licensing model (MIT OR Apache 2.0) and therefore be dual licensed as described below, without any additional terms or conditions:

License

This contribution is dual licensed under EITHER OF

at your option.

Dependencies

~315–770KB
~18K SLoC