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

Add support for bstr::BString and git2::Oid, behind feature flags #839

Merged
merged 4 commits into from
Dec 19, 2020
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
51 changes: 51 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ 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" ]
git2 = [ "sqlx-core/git2" ]

[dependencies]
sqlx-core = { version = "=0.4.0", path = "sqlx-core", default-features = false }
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ 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`.

- `git2`: Add support for `git2::Oid`.

- `bigdecimal`: Add support for `NUMERIC` using the `bigdecimal` crate.

- `decimal`: Add support for `NUMERIC` using the `rust_decimal` crate.
Expand Down
2 changes: 2 additions & 0 deletions sqlx-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,5 @@ 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 }
git2 = { version = "0.13.12", default-features = false, optional = true }
42 changes: 42 additions & 0 deletions sqlx-core/src/types/bstr.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
42 changes: 42 additions & 0 deletions sqlx-core/src/types/git2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/// Conversions between `git2::Oid` 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 git2::Oid;

impl<DB> Type<DB> for Oid
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 Oid
where
DB: Database,
&'r [u8]: Decode<'r, DB>,
{
fn decode(value: <DB as HasValueRef<'r>>::ValueRef) -> Result<Self, BoxDynError> {
<&[u8] as Decode<DB>>::decode(value).and_then(|bytes| Ok(Oid::from_bytes(bytes)?))
}
}

impl<'q, DB: Database> Encode<'q, DB> for Oid
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)
}
}
8 changes: 8 additions & 0 deletions sqlx-core/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@

use crate::database::Database;

#[cfg(feature = "bstr")]
#[cfg_attr(docsrs, doc(cfg(feature = "bstr")))]
mod bstr;

#[cfg(feature = "git2")]
#[cfg_attr(docsrs, doc(cfg(feature = "git2")))]
mod git2;

#[cfg(feature = "json")]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
mod json;
Expand Down
28 changes: 28 additions & 0 deletions tests/sqlite/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,31 @@ mod chrono {
"datetime('2016-11-08T03:50:23-05:00')" == FixedOffset::west(5 * 3600).ymd(2016, 11, 08).and_hms(3, 50, 23)
));
}

#[cfg(feature = "bstr")]
mod bstr {
use super::*;
use sqlx::types::bstr::BString;

test_type!(BString(Sqlite, "'abc123'" == BString::from(b"abc123")));
test_type!(BString(
Sqlite,
"x'0001020304'" == BString::from(b"\x00\x01\x02\x03\x04")
));
}

#[cfg(feature = "git2")]
mod git2 {
use super::*;
use sqlx::types::git2::Oid;

test_type!(Oid(
Sqlite,
"x'0000000000000000000000000000000000000000'" == Oid::zero()
));
test_type!(Oid(
Sqlite,
"x'000102030405060708090a0b0c0d0e0f10111213'"
== Oid::from_str("000102030405060708090a0b0c0d0e0f10111213")
));
}