Skip to content

Commit f8be8a8

Browse files
committed
Add support for UUID in MsSql
Adds support for reading and writing UUID data (using the `uuid` feature) in MsSql
1 parent 473b0cd commit f8be8a8

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## 0.6.48
9+
- Added support for reading and writing `uuid` data (using the `uuid` feature) in MsSql and Any.
10+
811
## 0.6.47
912
- Fixed a bug with postgres interval to string conversion.
1013

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
> - Latest SQLite version
88
> - Improved support for Microsoft SQL Server, including:
99
> - Support for reading and writing `binary` and `varbinary` data
10-
> - Support for reading and writing `date`, `datetime`, and `datetimeoffset` data using the `chrono` feature.
11-
> - Support for reading and writing `numeric` and `decimal`.
10+
> - Support for reading and writing
11+
> - `numeric`, `decimal`,
12+
> - `date`, `datetime`, and `datetimeoffset` data (using the `chrono` feature),
13+
> - `uuid` data (using the `uuid` feature),
1214
> - Multiple bug fixes around string handling, including better support for long strings
1315
> - Support for packet chunking, which fixes a bug where large bound parameters or large queries would fail
1416
> - Support for TLS encrypted connections

sqlx-core/src/mssql/types/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ mod decimal;
2323
#[cfg(feature = "bigdecimal")]
2424
mod bigdecimal;
2525

26+
#[cfg(feature = "uuid")]
27+
mod uuid;
28+
2629
impl<'q, T: 'q + Encode<'q, Mssql>> Encode<'q, Mssql> for Option<T> {
2730
fn encode(self, buf: &mut Vec<u8>) -> IsNull {
2831
if let Some(v) = self {

sqlx-core/src/mssql/types/uuid.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use crate::decode::Decode;
2+
use crate::encode::{Encode, IsNull};
3+
use crate::error::BoxDynError;
4+
use crate::mssql::protocol::type_info::{DataType, TypeInfo};
5+
use crate::mssql::{Mssql, MssqlTypeInfo, MssqlValueRef};
6+
use crate::types::Type;
7+
use uuid::Uuid;
8+
use std::convert::TryInto;
9+
10+
impl Type<Mssql> for Uuid {
11+
fn type_info() -> MssqlTypeInfo {
12+
// MSSQL's UNIQUEIDENTIFIER type
13+
MssqlTypeInfo(TypeInfo {
14+
ty: DataType::Guid,
15+
size: 16,
16+
scale: 0,
17+
precision: 0,
18+
collation: None,
19+
})
20+
}
21+
22+
fn compatible(ty: &MssqlTypeInfo) -> bool {
23+
matches!(ty.0.ty, DataType::Guid)
24+
}
25+
}
26+
27+
impl Encode<'_, Mssql> for Uuid {
28+
fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
29+
buf.extend_from_slice(&self.to_bytes_le());
30+
IsNull::No
31+
}
32+
}
33+
34+
impl Decode<'_, Mssql> for Uuid {
35+
fn decode(value: MssqlValueRef<'_>) -> Result<Self, BoxDynError> {
36+
let bytes: [u8; 16] = value.as_bytes()?.try_into()?;
37+
Ok(Uuid::from_bytes_le(bytes))
38+
}
39+
}

tests/mssql/types.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,15 @@ test_type!(cross_type_decimal_to_integers<i64>(
389389
"CAST(-123456789 AS DECIMAL(15,0))" == -123456789_i64,
390390
"CAST(0 AS DECIMAL(15,0))" == 0_i64,
391391
));
392+
393+
#[cfg(feature = "uuid")]
394+
mod uuid {
395+
use super::*;
396+
use sqlx_oldapi::types::Uuid;
397+
398+
test_type!(uuid<Uuid>(
399+
Mssql,
400+
"CAST('00000000-0000-0000-0000-000000000000' AS UNIQUEIDENTIFIER)" == Uuid::nil(),
401+
"CAST('6F9619FF-8B86-D011-B42D-00C04FC964FF' AS UNIQUEIDENTIFIER)" == Uuid::parse_str("6F9619FF-8B86-D011-B42D-00C04FC964FF").unwrap()
402+
));
403+
}

0 commit comments

Comments
 (0)