Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion sqlx-core/src/mssql/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ impl MssqlArguments {
T: Encode<'q, Mssql> + Type<Mssql>,
{
let ty = value.produces().unwrap_or_else(T::type_info);
// `DataType::Null` is required in TDS for null values, but the
// `sp_executesql` declaration needs the parameter's Rust type.
let declaration_ty = if ty.0.is_null() {
T::type_info()
} else {
ty.clone()
};

// produce an ordinal parameter name
// @p1, @p2, ... @pN
Expand All @@ -91,7 +98,7 @@ impl MssqlArguments {

declarations.push_str(name);
declarations.push(' ');
ty.0.fmt(declarations);
declaration_ty.0.fmt(declarations);

// write out the parameter

Expand Down
2 changes: 1 addition & 1 deletion sqlx-core/src/mssql/types/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::borrow::Cow;

impl Type<Mssql> for [u8] {
fn type_info() -> MssqlTypeInfo {
MssqlTypeInfo(TypeInfo::new(DataType::BigVarBinary, 0))
MssqlTypeInfo(TypeInfo::new(DataType::BigVarBinary, 0xFF_FF))
}

fn compatible(ty: &MssqlTypeInfo) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion sqlx-core/src/mssql/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize};

impl<T> Type<Mssql> for Json<T> {
fn type_info() -> MssqlTypeInfo {
MssqlTypeInfo(TypeInfo::new(DataType::BigVarBinary, 0))
MssqlTypeInfo(TypeInfo::new(DataType::BigVarBinary, 0xFF_FF))
}

fn compatible(ty: &MssqlTypeInfo) -> bool {
Expand Down
15 changes: 15 additions & 0 deletions tests/mssql/mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,21 @@ async fn it_binds_empty_string_and_null() -> anyhow::Result<()> {
Ok(())
}

// Regression test for https://github.com/sqlpage/sqlx-oldapi/issues/58
#[sqlx_macros::test]
async fn it_binds_null_with_type_in_isnull_expression() -> anyhow::Result<()> {
let mut conn = new::<Mssql>().await?;

let (val,): (String,) = sqlx_oldapi::query_as("SELECT ISNULL(@p1, N'hello')")
.bind(None::<String>)
.fetch_one(&mut conn)
.await?;

assert_eq!(val, "hello");

Ok(())
}

#[sqlx_macros::test]
async fn it_binds_string_with_special_chars() -> anyhow::Result<()> {
let mut conn = new::<Mssql>().await?;
Expand Down