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§
Functions§
- from_
row_ with_ schema - Decode
rowwithT’sHasSchemaschema overSqlRow::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 oneSqlError::InvalidRow. - from_
rows - Decode every row, stopping at the first failure.
- get_
or_ default - Column
idxdecoded asT, orT::default()when the row has no such column (what#[from_row(default)]generates). Other errors are returned as they are. - query_
as querydecoding every row asT:query_as::<Todo, _, _>(sql, params![..])where the return type fixes the environment, elsequery_as::<Todo, SqlClientEnv, _>(..)(SqlClientEnv).- query_
one_ as querydecoding the first row asT;SqlError::RowNotFoundwhen there is none.- query_
opt_ as querydecoding the first row asT, orNonewhen there is none.