Skip to main content

Module from_row

Module from_row 

Source
Expand description

Typed rows: FromRow decodes an SqlRow into a struct (#[derive(FromRow)]) or a tuple, and query_as / query_one_as / query_opt_as run a query straight into them.

use id_effect_sql::{FromRow, SqlError, SqlRow};

#[derive(Debug, PartialEq, FromRow)]
struct Todo {
  id: i64,
  #[from_row(rename = "title_text")]
  title: String,
  note: Option<String>,
}

let row = SqlRow::from_pairs([
  ("id", 1i64.into()),
  ("title_text", "docs".into()),
  ("note", id_effect_sql::SqlValue::Null),
]);
assert_eq!(Todo::from_row(&row), Ok(Todo { id: 1, title: "docs".into(), note: None }));
assert_eq!(<(i64, String)>::from_row(&row), Ok((1, "docs".into())));
assert_eq!(
  Todo::from_row(&SqlRow::from_pairs([("id", 1i64)])),
  Err(SqlError::ColumnNotFound { column: "title_text".into() })
);

Traits§

FromRow
A type decoded from one SqlRow.

Functions§

from_row_with_schema
Decode row with T’s HasSchema schema over SqlRow::to_unknown (what #[from_row(schema)] generates): refinements and brands run on database data, and every issue the schema’s accumulating decoder reports comes back in one SqlError::InvalidRow.
from_rows
Decode every row, stopping at the first failure.
get_or_default
Column idx decoded as T, or T::default() when the row has no such column (what #[from_row(default)] generates). Other errors are returned as they are.
query_as
query decoding every row as T: query_as::<Todo, _, _>(sql, params![..]) where the return type fixes the environment, else query_as::<Todo, SqlClientEnv, _>(..) (SqlClientEnv).
query_one_as
query decoding the first row as T; SqlError::RowNotFound when there is none.
query_opt_as
query decoding the first row as T, or None when there is none.