Trait ockam::ToSqlxType
source · pub trait ToSqlxType {
// Required method
fn to_sql(&self) -> SqlxType;
}
Expand description
This trait can be implemented by any type that can be converted to a database type
Typically an Identifier
(to a Text
), a TimestampInSeconds
(to an Integer
) etc…
This allows a value to be used as a bind parameters in a sqlx query for example:
use std::str::FromStr; use sqlx::query_as; use ockam_node::database::{SqlxType, ToSqlxType};
// newtype for a UNIX-like timestamp struct TimestampInSeconds(u64);
// this implementation maps the TimestampInSecond type to one of the types that Sqlx // can serialize for sqlite impl ToSqlxType for TimestampInSeconds { fn to_sql(&self) -> SqlxType { self.0.to_sql() } }
let timestamp = TimestampInSeconds(10000000); let query = query_as(“SELECT identifier, change_history FROM identity WHERE created_at >= $1”).bind(timestamp.as_sql());