Add support for bstr::BString (under the bstr feature flag)

This allows queries to convert between BString and SQL data types.
This commit is contained in:
Josh Triplett
2020-11-21 13:58:48 -08:00
parent bfa58417a1
commit 47b0973bdb
6 changed files with 51 additions and 0 deletions

1
Cargo.lock generated
View File

@@ -2206,6 +2206,7 @@ dependencies = [
"bigdecimal",
"bit-vec",
"bitflags",
"bstr",
"byteorder",
"bytes",
"chrono",

View File

@@ -89,6 +89,7 @@ uuid = [ "sqlx-core/uuid", "sqlx-macros/uuid" ]
json = [ "sqlx-core/json", "sqlx-macros/json" ]
time = [ "sqlx-core/time", "sqlx-macros/time" ]
bit-vec = [ "sqlx-core/bit-vec", "sqlx-macros/bit-vec"]
bstr = [ "sqlx-core/bstr" ]
[dependencies]
sqlx-core = { version = "=0.4.0", path = "sqlx-core", default-features = false }

View File

@@ -152,6 +152,8 @@ sqlx = { version = "0.4.0", features = [ "runtime-async-std-native-tls" ] }
- `time`: Add support for date and time types from `time` crate (alternative to `chrono`, prefered by `query!` macro, if both enabled)
- `bstr`: Add support for `bstr::BString`.
- `bigdecimal`: Add support for `NUMERIC` using the `bigdecimal` crate.
- `decimal`: Add support for `NUMERIC` using the `rust_decimal` crate.

View File

@@ -107,3 +107,4 @@ webpki-roots = { version = "0.20.0", optional = true }
whoami = "0.9.0"
stringprep = "0.1.2"
lru-cache = "0.1.2"
bstr = { version = "0.2.14", default-features = false, features = [ "std" ], optional = true }

View File

@@ -0,0 +1,42 @@
/// Conversions between `bstr` types and SQL types.
use crate::database::{Database, HasArguments, HasValueRef};
use crate::decode::Decode;
use crate::encode::{Encode, IsNull};
use crate::error::BoxDynError;
use crate::types::Type;
pub use bstr::{BString, ByteSlice};
impl<DB> Type<DB> for BString
where
DB: Database,
[u8]: Type<DB>,
{
fn type_info() -> DB::TypeInfo {
<&[u8] as Type<DB>>::type_info()
}
fn compatible(ty: &DB::TypeInfo) -> bool {
<&[u8] as Type<DB>>::compatible(ty)
}
}
impl<'r, DB> Decode<'r, DB> for BString
where
DB: Database,
Vec<u8>: Decode<'r, DB>,
{
fn decode(value: <DB as HasValueRef<'r>>::ValueRef) -> Result<Self, BoxDynError> {
<Vec<u8> as Decode<DB>>::decode(value).map(BString::from)
}
}
impl<'q, DB: Database> Encode<'q, DB> for BString
where
DB: Database,
[u8]: Encode<'q, DB>,
{
fn encode_by_ref(&self, buf: &mut <DB as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
<[u8] as Encode<DB>>::encode_by_ref(self.as_bytes(), buf)
}
}

View File

@@ -20,6 +20,10 @@
use crate::database::Database;
#[cfg(feature = "bstr")]
#[cfg_attr(docsrs, doc(cfg(feature = "bstr")))]
mod bstr;
#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
mod json;