1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
use crate::{
tds::{codec::ColumnData, Numeric},
xml::XmlData,
};
use std::borrow::Cow;
use uuid::Uuid;
/// 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)`|
/// |`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.
///
/// See the [`time`] module for more information about the date and time structs.
///
/// [`Client#query`]: struct.Client.html#method.query
/// [`Client#execute`]: struct.Client.html#method.execute
/// [`time`]: time/index.html
/// [`Uuid`]: struct.Uuid.html
/// [`Numeric`]: numeric/struct.Numeric.html
/// [`Decimal`]: numeric/struct.Decimal.html
/// [`BigDecimal`]: numeric/struct.BigDecimal.html
/// [`XmlData`]: xml/struct.XmlData.html
/// [`NaiveDateTime`]: time/chrono/struct.NaiveDateTime.html
/// [`NaiveDate`]: time/chrono/struct.NaiveDate.html
/// [`NaiveTime`]: time/chrono/struct.NaiveTime.html
/// [`DateTime`]: time/chrono/struct.DateTime.html
pub trait ToSql: Send + Sync {
/// Convert to a value understood by the SQL Server. Conversion
/// by-reference.
fn to_sql(&self) -> ColumnData<'_>;
}
/// A by-value conversion trait to a TDS type.
pub trait IntoSql<'a>: Send + Sync {
/// Convert to a value understood by the SQL Server. Conversion by-value.
fn into_sql(self) -> ColumnData<'a>;
}
impl<'a> IntoSql<'a> for &'a str {
fn into_sql(self) -> ColumnData<'a> {
ColumnData::String(Some(Cow::Borrowed(self)))
}
}
impl<'a> IntoSql<'a> for Option<&'a str> {
fn into_sql(self) -> ColumnData<'a> {
ColumnData::String(self.map(Cow::Borrowed))
}
}
impl<'a> IntoSql<'a> for &'a String {
fn into_sql(self) -> ColumnData<'a> {
ColumnData::String(Some(Cow::Borrowed(self)))
}
}
impl<'a> IntoSql<'a> for Option<&'a String> {
fn into_sql(self) -> ColumnData<'a> {
ColumnData::String(self.map(Cow::from))
}
}
impl<'a> IntoSql<'a> for &'a [u8] {
fn into_sql(self) -> ColumnData<'a> {
ColumnData::Binary(Some(Cow::Borrowed(self)))
}
}
impl<'a> IntoSql<'a> for Option<&'a [u8]> {
fn into_sql(self) -> ColumnData<'a> {
ColumnData::Binary(self.map(Cow::Borrowed))
}
}
impl<'a> IntoSql<'a> for &'a Vec<u8> {
fn into_sql(self) -> ColumnData<'a> {
ColumnData::Binary(Some(Cow::from(self)))
}
}
impl<'a> IntoSql<'a> for Option<&'a Vec<u8>> {
fn into_sql(self) -> ColumnData<'a> {
ColumnData::Binary(self.map(Cow::from))
}
}
impl<'a> IntoSql<'a> for Cow<'a, str> {
fn into_sql(self) -> ColumnData<'a> {
ColumnData::String(Some(self))
}
}
impl<'a> IntoSql<'a> for Option<Cow<'a, str>> {
fn into_sql(self) -> ColumnData<'a> {
ColumnData::String(self)
}
}
impl<'a> IntoSql<'a> for Cow<'a, [u8]> {
fn into_sql(self) -> ColumnData<'a> {
ColumnData::Binary(Some(self))
}
}
impl<'a> IntoSql<'a> for Option<Cow<'a, [u8]>> {
fn into_sql(self) -> ColumnData<'a> {
ColumnData::Binary(self)
}
}
impl<'a> IntoSql<'a> for &'a XmlData {
fn into_sql(self) -> ColumnData<'a> {
ColumnData::Xml(Some(Cow::Borrowed(self)))
}
}
impl<'a> IntoSql<'a> for Option<&'a XmlData> {
fn into_sql(self) -> ColumnData<'a> {
ColumnData::Xml(self.map(Cow::Borrowed))
}
}
impl<'a> IntoSql<'a> for &'a Uuid {
fn into_sql(self) -> ColumnData<'a> {
ColumnData::Guid(Some(*self))
}
}
impl<'a> IntoSql<'a> for Option<&'a Uuid> {
fn into_sql(self) -> ColumnData<'a> {
ColumnData::Guid(self.copied())
}
}
into_sql!(self_,
String: (ColumnData::String, Cow::from(self_));
Vec<u8>: (ColumnData::Binary, Cow::from(self_));
Numeric: (ColumnData::Numeric, self_);
XmlData: (ColumnData::Xml, Cow::Owned(self_));
Uuid: (ColumnData::Guid, self_);
bool: (ColumnData::Bit, self_);
u8: (ColumnData::U8, self_);
i16: (ColumnData::I16, self_);
i32: (ColumnData::I32, self_);
i64: (ColumnData::I64, self_);
f32: (ColumnData::F32, self_);
f64: (ColumnData::F64, self_);
);
to_sql!(self_,
bool: (ColumnData::Bit, *self_);
u8: (ColumnData::U8, *self_);
i16: (ColumnData::I16, *self_);
i32: (ColumnData::I32, *self_);
i64: (ColumnData::I64, *self_);
f32: (ColumnData::F32, *self_);
f64: (ColumnData::F64, *self_);
&str: (ColumnData::String, Cow::from(*self_));
String: (ColumnData::String, Cow::from(self_));
Cow<'_, str>: (ColumnData::String, self_.clone());
&[u8]: (ColumnData::Binary, Cow::from(*self_));
Cow<'_, [u8]>: (ColumnData::Binary, self_.clone());
Vec<u8>: (ColumnData::Binary, Cow::from(self_));
Numeric: (ColumnData::Numeric, *self_);
XmlData: (ColumnData::Xml, Cow::Borrowed(self_));
Uuid: (ColumnData::Guid, *self_);
);