pub trait ToSql: Debug {
// Required methods
fn to_sql(
&self,
ty: &Type,
out: &mut BytesMut,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>
where Self: Sized;
fn accepts(ty: &Type) -> bool
where Self: Sized;
fn to_sql_checked(
&self,
ty: &Type,
out: &mut BytesMut,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>;
// Provided method
fn encode_format(&self, _ty: &Type) -> Format { ... }
}
Expand description
A trait for types that can be converted into Postgres values.
§Types
The following implementations are provided by this crate, along with the corresponding Postgres types:
Rust type | Postgres type(s) |
---|---|
bool | BOOL |
i8 | “char” |
i16 | SMALLINT, SMALLSERIAL |
i32 | INT, SERIAL |
u32 | OID |
i64 | BIGINT, BIGSERIAL |
f32 | REAL |
f64 | DOUBLE PRECISION |
&str /String | VARCHAR, CHAR(n), TEXT, CITEXT, NAME |
LTREE, LQUERY, LTXTQUERY | |
&[u8] /Vec<u8> /[u8; N] | BYTEA |
HashMap<String, Option<String>> | HSTORE |
SystemTime | TIMESTAMP, TIMESTAMP WITH TIME ZONE |
IpAddr | INET |
In addition, some implementations are provided for types in third party
crates. These are disabled by default; to opt into one of these
implementations, activate the Cargo feature corresponding to the crate’s
name prefixed by with-
. For example, the with-serde_json-1
feature enables
the implementation for the serde_json::Value
type.
Rust type | Postgres type(s) |
---|---|
chrono::NaiveDateTime | TIMESTAMP |
chrono::DateTime<Utc> | TIMESTAMP WITH TIME ZONE |
chrono::DateTime<Local> | TIMESTAMP WITH TIME ZONE |
chrono::DateTime<FixedOffset> | TIMESTAMP WITH TIME ZONE |
chrono::NaiveDate | DATE |
chrono::NaiveTime | TIME |
time::PrimitiveDateTime | TIMESTAMP |
time::OffsetDateTime | TIMESTAMP WITH TIME ZONE |
time::Date | DATE |
time::Time | TIME |
eui48::MacAddress | MACADDR |
geo_types::Point<f64> | POINT |
geo_types::Rect<f64> | BOX |
geo_types::LineString<f64> | PATH |
serde_json::Value | JSON, JSONB |
uuid::Uuid | UUID |
bit_vec::BitVec | BIT, VARBIT |
eui48::MacAddress | MACADDR |
§Nullability
In addition to the types listed above, ToSql
is implemented for
Option<T>
where T
implements ToSql
. An Option<T>
represents a
nullable Postgres value.
§Arrays
ToSql
is implemented for [u8; N]
, Vec<T>
, &[T]
, Box<[T]>
and [T; N]
where T
implements ToSql
and N
is const usize, and corresponds to one-dimensional
Postgres arrays with an index offset of 1.
Note: the impl for arrays only exist when the Cargo feature array-impls
is enabled.
Required Methods§
sourcefn to_sql(
&self,
ty: &Type,
out: &mut BytesMut,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>where
Self: Sized,
fn to_sql(
&self,
ty: &Type,
out: &mut BytesMut,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>where
Self: Sized,
Converts the value of self
into the binary format of the specified
Postgres Type
, appending it to out
.
The caller of this method is responsible for ensuring that this type
is compatible with the Postgres Type
.
The return value indicates if this value should be represented as
NULL
. If this is the case, implementations must not write
anything to out
.
Provided Methods§
sourcefn encode_format(&self, _ty: &Type) -> Format
fn encode_format(&self, _ty: &Type) -> Format
Specify the encode format
Trait Implementations§
source§impl BorrowToSql for &dyn ToSql
impl BorrowToSql for &dyn ToSql
source§fn borrow_to_sql(&self) -> &dyn ToSql
fn borrow_to_sql(&self) -> &dyn ToSql
self
as a ToSql
trait object.source§impl BorrowToSql for &(dyn ToSql + Sync)
impl BorrowToSql for &(dyn ToSql + Sync)
In async contexts it is sometimes necessary to have the additional Sync requirement on parameters for queries since this enables the resulting Futures to be Send, hence usable in, e.g., tokio::spawn. This instance is provided for those cases.
source§fn borrow_to_sql(&self) -> &dyn ToSql
fn borrow_to_sql(&self) -> &dyn ToSql
self
as a ToSql
trait object.source§impl<'a> BorrowToSql for Box<dyn ToSql + Sync + Send + 'a>
impl<'a> BorrowToSql for Box<dyn ToSql + Sync + Send + 'a>
source§fn borrow_to_sql(&self) -> &dyn ToSql
fn borrow_to_sql(&self) -> &dyn ToSql
self
as a ToSql
trait object.source§impl<'a> BorrowToSql for Box<dyn ToSql + Sync + 'a>
impl<'a> BorrowToSql for Box<dyn ToSql + Sync + 'a>
source§fn borrow_to_sql(&self) -> &dyn ToSql
fn borrow_to_sql(&self) -> &dyn ToSql
self
as a ToSql
trait object.