Docs.rs
  • postgres-from-row-0.5.2
    • postgres-from-row 0.5.2
    • Permalink
    • Docs.rs crate page
    • Links
    • Homepage
    • Repository
    • crates.io
    • Source
    • Owners
    • remkop22
    • Dependencies
      • postgres-from-row-derive =0.5.2 normal
      • tokio-postgres ^0.7.8 normal
    • Versions
    • 100% of the crate is documented
  • Platform
    • i686-pc-windows-msvc
    • i686-unknown-linux-gnu
    • x86_64-apple-darwin
    • x86_64-pc-windows-msvc
    • x86_64-unknown-linux-gnu
  • Feature flags
  • docs.rs
    • About docs.rs
    • Badges
    • Builds
    • Metadata
    • Shorthand URLs
    • Download
    • Rustdoc JSON
    • Build queue
    • Privacy policy
  • Rust
    • Rust website
    • The Book
    • Standard Library API Reference
    • Rust by Example
    • The Cargo Guide
    • Clippy Documentation

Crate postgres_from_row

postgres_from_row0.5.2

  • All Items

Sections

  • postgres-from-row
    • Examples

Crate Items

  • Re-exports
  • Traits
  • Derive Macros

Crates

  • postgres_from_row

Crate postgres_from_row

Source
Expand description

§postgres-from-row

Derive FromRow to generate a mapping between a struct and postgres rows.

This crate is compatible with both postgres and tokio-postgres.

[dependencies]
postgres_from_row = "0.5.2"

§Examples

use postgres_from_row::FromRow;

#[derive(FromRow)]
struct Todo {
    todo_id: i32,
    text: String
    author_id: i32,
}

let row = client.query_one("SELECT todo_id, text, author_id FROM todos", &[]).unwrap();

// Pass a row with the correct columns.
let todo = Todo::from_row(&row);

let row = client.query_one("SELECT foo FROM bar", &[]).unwrap();

// Use `try_from_row` if the operation could fail.
let todo = Todo::try_from_row(&row);
assert!(todo.is_err());

Each field need’s to implement postgres::types::FromSql, as this will be used to convert a single column to the specified type. If you want to override this behavior and delegate it to a nested structure that also implements FromRow, use #[from_row(flatten)]:

use postgres_from_row::FromRow;

#[derive(FromRow)]
struct Todo {
    todo_id: i32,
    text: String,
    #[from_row(flatten)]
    author: User
}

#[derive(FromRow)]
struct User {
    user_id: i32,
    username: String
}

let row = client.query_one("SELECT todo_id, text, user_id, username FROM todos t, users u WHERE t.author_id = u.user_id", &[]).unwrap();
let todo = Todo::from_row(&row);

If a the struct contains a field with a name that differs from the name of the sql column, you can use the #[from_row(rename = "..")] attribute.

When a field in your struct has a type T that doesn’t implement FromSql or FromRow but it does impement T: From<C> or T: TryFrom<c>, and C does implment FromSql or FromRow you can use #[from_row(from = "C")] or #[from_row(try_from = "C")]. This will use type C to extract it from the row and then finally converts it into T.


struct Todo {
    // If the postgres column is named `todo_id`.
    #[from_row(rename = "todo_id")]
    id: i32,
    // If the postgres column is `VARCHAR`, it will be decoded to `String`,
    // using `FromSql` and then converted to `Vec<u8>` using `std::convert::From`.
    #[from_row(from = "String")]
    todo: Vec<u8>
}

Re-exports§

pub use tokio_postgres;

Traits§

FromRow
A trait that allows mapping rows from either postgres or tokio-postgres, to other types.

Derive Macros§

FromRow
Calls the fallible entry point and writes any errors to the tokenstream.

Results

Settings
Help
    trait
    postgres_from_row::FromRow
    A trait that allows mapping rows from either postgres or …
    derive macro
    postgres_from_row::FromRow
    Calls the fallible entry point and writes any errors to …
    trait method
    postgres_from_row::FromRow::from_row
    Performce the conversion
    trait method
    postgres_from_row::FromRow::try_from_row
    Try’s to perform the conversion.
    extern crate
    postgres_from_row
    postgres-from-row
No results :(
Try on DuckDuckGo?

Or try looking in one of these:
  • The Rust Reference for technical details about the language.
  • Rust By Example for expository code examples.
  • The Rust Book for introductions to language features and the language itself.
  • Docs.rs for documentation of crates released on crates.io.
    trait method
    postgres_from_row::FromRow::from_row
    &Row -> FromRow
    Performce the conversion
    trait method
    postgres_from_row::FromRow::try_from_row
    &Row -> Result<FromRow, Error>
    Try’s to perform the conversion.