#postgresql #sql #async #proc-macro #column-name

tokio-postgres-extractor

High-performance extractors for tokio_postgres

1 unstable release

0.7.0 Apr 29, 2023

#2285 in Database interfaces

Download history 7/week @ 2024-07-19 39/week @ 2024-07-26 26/week @ 2024-08-02 48/week @ 2024-08-09 21/week @ 2024-08-16 129/week @ 2024-08-23 17/week @ 2024-08-30 17/week @ 2024-09-06 6/week @ 2024-09-13 22/week @ 2024-09-20 32/week @ 2024-09-27 49/week @ 2024-10-04 14/week @ 2024-10-11 16/week @ 2024-10-18 27/week @ 2024-10-25 32/week @ 2024-11-01

90 downloads per month

MIT/Apache

35KB
506 lines

tokio-postgres-extractor

crates.io docs.rs

This crate contains traits and proc macros for creating high-performance extractors for Rust types from tokio-postgres Rows.

License

This project is licensed under either of

  • Apache License, Version 2.0
  • MIT License

at your option.


lib.rs:

High-performance extractors for [tokio_postgres].

This crate contains traits and proc macros for creating high-performance extractors for Rust types from [Row]s.

Examples

Extracting a single row

#[derive(Columns, Extract)]
struct User {
    id: i32,
    name: String,
}

async fn get_user(client: &Client, id: i32) -> Result<User, Error> {
    client
        .query_one("select * from user where id = $1", &[&id])
        .await
        .map(|r| r.extract_once())
}

Extracting a stream of rows

#[derive(Columns, Extract)]
struct User {
    id: i32,
    name: String,
}

async fn get_users(client: &Client) -> Result<Vec<User>, Error> {
    client
        .query_raw("select * from user", None::<i32>)
        .await?
        .extract()
        .try_collect()
        .await
}

Generic types

Generic types are fully supported.

#[derive(Columns, Extract)]
struct User<'a, T: ?Sized> {
    id: i32,
    name: &'a T,
}

fn assert_is_extract<'a, T: Extract<'a>>() { }

assert_is_extract::<User<str>>();

Custom column names

You can specify column names that are different from field names. See the documentation of the [Columns][macro@Columns] proc macro.

Design

A naive mapping function such as

User {
    id: row.get("id"),
    name: row.get("name"),
}

has O(N^2) runtime where N is the number of columns. Each invocation of row.get must walk all columns and compare their name to the name of the requested column. This crate solves this by

  1. Constructing an efficient data structure at compile time that reduces lookup time to O(N).

    This data structure is similar to a perfect hash function but more efficient.

  2. Memorizing the mapping from fields to columns whenever possible.

Dependencies

~7–16MB
~218K SLoC