pub trait ToSql: Send + Sync {
// Required method
fn to_sql(&self) -> ColumnData<'_>;
}Expand description
A conversion trait to a TDS type.
A ToSql implementation for a Rust type is needed for using it as a
parameter in the Client#query or Client#execute methods. The
following Rust types are already implemented to match the given server
types:
| Rust type | Server type |
|---|---|
u8 | tinyint |
i16 | smallint |
i32 | int |
i64 | bigint |
f32 | float(24) |
f64 | float(53) |
bool | bit |
String/&str (< 4000 characters) | nvarchar(4000) |
String/&str | nvarchar(max) |
ColumnData::VarChar (< 8000 bytes) | varchar(8000) |
ColumnData::VarChar | varchar(max) |
Vec<u8>/&[u8] (< 8000 bytes) | varbinary(8000) |
Vec<u8>/&[u8] | varbinary(max) |
Uuid | uniqueidentifier |
Numeric | numeric/decimal |
Decimal (with feature flag rust_decimal) | numeric/decimal |
BigDecimal (with feature flag bigdecimal) | numeric/decimal |
XmlData | xml |
NaiveDate (with chrono feature, TDS 7.3 >) | date |
NaiveTime (with chrono feature, TDS 7.3 >) | time |
DateTime (with chrono feature, TDS 7.3 >) | datetimeoffset |
NaiveDateTime (with chrono feature, TDS 7.3 >) | datetime2 |
NaiveDateTime (with chrono feature, TDS 7.2) | datetime |
It is possible to use some of the types to write into columns that are not
of the same type. For example on systems following the TDS 7.3 standard (SQL
Server 2008 and later), the chrono type NaiveDateTime can also be used to
write to datetime, datetime2 and smalldatetime columns. All string
types can also be used with ntext, text, varchar, nchar and char
columns. All binary types can also be used with binary and image
columns.
A string compared with an indexed varchar column under a SQL_*
collation is better sent as ColumnData::VarChar: an nvarchar
parameter outranks the column there, so the server converts every row and
scans the index instead of seeking it.
See the time module for more information about the date and time structs.
A value the server cannot hold, such as a date outside 0001-01-01 to
9999-12-31 (in UTC, for datetimeoffset), a BigDecimal that
decimal(38, s) cannot hold exactly, a chrono leap second or a chrono
UTC offset with seconds, still converts, and the query using it fails
with Error::Conversion.
Required Methods§
Sourcefn to_sql(&self) -> ColumnData<'_>
fn to_sql(&self) -> ColumnData<'_>
Convert to a value understood by the SQL Server. Conversion by-reference.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl ToSql for &Option<BigDecimal>
Available on crate feature bigdecimal only.
impl ToSql for &Option<BigDecimal>
bigdecimal only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for &Option<Date>
Available on crate feature time only.
impl ToSql for &Option<Date>
time only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for &Option<DateTime<FixedOffset>>
Available on crate feature chrono only.
impl ToSql for &Option<DateTime<FixedOffset>>
chrono only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for &Option<DateTime<Utc>>
Available on crate feature chrono only.
impl ToSql for &Option<DateTime<Utc>>
chrono only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for &Option<Decimal>
Available on crate feature rust_decimal only.
impl ToSql for &Option<Decimal>
rust_decimal only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for &Option<NaiveDate>
Available on crate feature chrono only.
impl ToSql for &Option<NaiveDate>
chrono only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for &Option<NaiveDateTime>
Available on crate feature chrono only.
impl ToSql for &Option<NaiveDateTime>
chrono only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for &Option<NaiveTime>
Available on crate feature chrono only.
impl ToSql for &Option<NaiveTime>
chrono only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for &Option<OffsetDateTime>
Available on crate feature time only.
impl ToSql for &Option<OffsetDateTime>
time only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for &Option<PrimitiveDateTime>
Available on crate feature time only.
impl ToSql for &Option<PrimitiveDateTime>
time only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for &Option<Time>
Available on crate feature time only.
impl ToSql for &Option<Time>
time only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for Option<BigDecimal>
Available on crate feature bigdecimal only.
impl ToSql for Option<BigDecimal>
bigdecimal only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for Option<Date>
Available on crate feature time only.
impl ToSql for Option<Date>
time only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for Option<DateTime<FixedOffset>>
Available on crate feature chrono only.
impl ToSql for Option<DateTime<FixedOffset>>
chrono only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for Option<DateTime<Utc>>
Available on crate feature chrono only.
impl ToSql for Option<DateTime<Utc>>
chrono only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for Option<Decimal>
Available on crate feature rust_decimal only.
impl ToSql for Option<Decimal>
rust_decimal only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for Option<NaiveDate>
Available on crate feature chrono only.
impl ToSql for Option<NaiveDate>
chrono only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for Option<NaiveDateTime>
Available on crate feature chrono only.
impl ToSql for Option<NaiveDateTime>
chrono only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for Option<NaiveTime>
Available on crate feature chrono only.
impl ToSql for Option<NaiveTime>
chrono only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for Option<OffsetDateTime>
Available on crate feature time only.
impl ToSql for Option<OffsetDateTime>
time only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for Option<PrimitiveDateTime>
Available on crate feature time only.
impl ToSql for Option<PrimitiveDateTime>
time only.fn to_sql(&self) -> ColumnData<'_>
Source§impl ToSql for Option<Time>
Available on crate feature time only.
impl ToSql for Option<Time>
time only.fn to_sql(&self) -> ColumnData<'_>
Implementors§
impl ToSql for BigDecimal
bigdecimal only.impl ToSql for Date
time only.impl ToSql for DateTime<FixedOffset>
chrono only.impl ToSql for DateTime<Utc>
chrono only.impl ToSql for Decimal
rust_decimal only.impl ToSql for NaiveDate
chrono only.impl ToSql for NaiveDateTime
chrono only.impl ToSql for NaiveTime
chrono only.impl ToSql for Numeric
impl ToSql for OffsetDateTime
time only.impl ToSql for PrimitiveDateTime
time only.impl ToSql for Time
time only.impl ToSql for Uuid
impl ToSql for XmlData
impl<'a> ToSql for ColumnData<'a>
Binds a ColumnData directly as a query parameter, by reference.