Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type info access in Any database driver #1848

Merged
merged 5 commits into from
Jul 14, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions sqlx-core/src/any/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::any::type_name;

use crate::any::type_info::AnyTypeInfo;
use crate::any::Any;
use crate::error::BoxDynError;
use crate::type_info::TypeInfo;
use crate::types::Type;

pub(super) fn mismatched_types<T: Type<Any>>(ty: &AnyTypeInfo) -> BoxDynError {
format!(
"mismatched types; Rust type `{}` is not compatible with SQL type `{}`",
type_name::<T>(),
ty.name()
)
.into()
}
1 change: 1 addition & 0 deletions sqlx-core/src/any/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod arguments;
pub(crate) mod column;
mod connection;
mod database;
mod error;
mod kind;
mod options;
mod query_result;
Expand Down
24 changes: 24 additions & 0 deletions sqlx-core/src/any/row.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
use crate::any::error::mismatched_types;
use crate::any::{Any, AnyColumn, AnyColumnIndex};
use crate::column::ColumnIndex;
use crate::database::HasValueRef;
use crate::decode::Decode;
use crate::error::Error;
use crate::row::Row;
use crate::type_info::TypeInfo;
use crate::types::Type;
use crate::value::ValueRef;

#[cfg(feature = "postgres")]
use crate::postgres::PgRow;
Expand Down Expand Up @@ -67,6 +72,25 @@ impl Row for AnyRow {
AnyRowKind::Mssql(row) => row.try_get_raw(index).map(Into::into),
}
}

fn try_get<'r, T, I>(&'r self, index: I) -> Result<T, Error>
where
I: ColumnIndex<Self>,
T: Decode<'r, Self::Database> + Type<Self::Database>,
{
let value = self.try_get_raw(&index)?;
let ty = value.type_info();

if !value.is_null() && !ty.is_null() && !T::compatible(&ty) {
Err(mismatched_types::<T>(&ty))
} else {
T::decode(value)
}
.map_err(|source| Error::ColumnDecode {
index: format!("{:?}", index),
source,
})
}
}

impl<'i> ColumnIndex<AnyRow> for &'i str
Expand Down
23 changes: 22 additions & 1 deletion sqlx-core/src/any/value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
use std::borrow::Cow;

use crate::any::error::mismatched_types;
use crate::any::{Any, AnyTypeInfo};
use crate::database::HasValueRef;
use crate::decode::Decode;
use crate::error::Error;
use crate::type_info::TypeInfo;
use crate::types::Type;
use crate::value::{Value, ValueRef};
use std::borrow::Cow;

#[cfg(feature = "postgres")]
use crate::postgres::{PgValue, PgValueRef};
Expand Down Expand Up @@ -91,6 +97,21 @@ impl Value for AnyValue {
AnyValueKind::Mssql(value) => value.is_null(),
}
}

fn try_decode<'r, T>(&'r self) -> Result<T, Error>
where
T: Decode<'r, Self::Database> + Type<Self::Database>,
{
if !self.is_null() {
let ty = self.type_info();

if !ty.is_null() && !T::compatible(&ty) {
return Err(Error::Decode(mismatched_types::<T>(&ty)));
}
}

self.try_decode_unchecked()
}
}

impl<'r> ValueRef<'r> for AnyValueRef<'r> {
Expand Down