From 47b0973bdb0ac9845437fd50c4bd738c0657ef40 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Sat, 21 Nov 2020 13:58:48 -0800 Subject: [PATCH 1/4] Add support for bstr::BString (under the `bstr` feature flag) This allows queries to convert between BString and SQL data types. --- Cargo.lock | 1 + Cargo.toml | 1 + README.md | 2 ++ sqlx-core/Cargo.toml | 1 + sqlx-core/src/types/bstr.rs | 42 +++++++++++++++++++++++++++++++++++++ sqlx-core/src/types/mod.rs | 4 ++++ 6 files changed, 51 insertions(+) create mode 100644 sqlx-core/src/types/bstr.rs diff --git a/Cargo.lock b/Cargo.lock index 24f199fd9c..3e089946dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2206,6 +2206,7 @@ dependencies = [ "bigdecimal", "bit-vec", "bitflags", + "bstr", "byteorder", "bytes", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 7c7610cd91..17968994e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/README.md b/README.md index 886e3989a9..c464193ac1 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/sqlx-core/Cargo.toml b/sqlx-core/Cargo.toml index 8ab9740df8..ef3239acdf 100644 --- a/sqlx-core/Cargo.toml +++ b/sqlx-core/Cargo.toml @@ -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 } diff --git a/sqlx-core/src/types/bstr.rs b/sqlx-core/src/types/bstr.rs new file mode 100644 index 0000000000..db1e8f6ea1 --- /dev/null +++ b/sqlx-core/src/types/bstr.rs @@ -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 Type for BString +where + DB: Database, + [u8]: Type, +{ + fn type_info() -> DB::TypeInfo { + <&[u8] as Type>::type_info() + } + + fn compatible(ty: &DB::TypeInfo) -> bool { + <&[u8] as Type>::compatible(ty) + } +} + +impl<'r, DB> Decode<'r, DB> for BString +where + DB: Database, + Vec: Decode<'r, DB>, +{ + fn decode(value: >::ValueRef) -> Result { + as Decode>::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 >::ArgumentBuffer) -> IsNull { + <[u8] as Encode>::encode_by_ref(self.as_bytes(), buf) + } +} diff --git a/sqlx-core/src/types/mod.rs b/sqlx-core/src/types/mod.rs index 4e21d115d8..0eb93b9c61 100644 --- a/sqlx-core/src/types/mod.rs +++ b/sqlx-core/src/types/mod.rs @@ -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; From d396e5a94812ca4c957d4cd3e0063a986e02f403 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Sat, 21 Nov 2020 14:29:01 -0800 Subject: [PATCH 2/4] Add tests for bstr --- tests/sqlite/types.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/sqlite/types.rs b/tests/sqlite/types.rs index 61cd13a5ec..3e26c902ae 100644 --- a/tests/sqlite/types.rs +++ b/tests/sqlite/types.rs @@ -106,3 +106,15 @@ 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") + )); +} From 71a72e7c676e27b9372ef2f57768124df0f4e398 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Sat, 21 Nov 2020 14:15:27 -0800 Subject: [PATCH 3/4] Add support for git2::Oid (under the `git2` feature flag) This allows queries to convert between Oid and SQL data types. --- Cargo.lock | 50 +++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + README.md | 2 ++ sqlx-core/Cargo.toml | 1 + sqlx-core/src/types/git2.rs | 42 +++++++++++++++++++++++++++++++ sqlx-core/src/types/mod.rs | 4 +++ 6 files changed, 100 insertions(+) create mode 100644 sqlx-core/src/types/git2.rs diff --git a/Cargo.lock b/Cargo.lock index 3e089946dd..6980920fdb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -389,6 +389,9 @@ name = "cc" version = "1.0.62" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1770ced377336a88a67c473594ccc14eca6f4559217c34f64aac8f83d641b40" +dependencies = [ + "jobserver", +] [[package]] name = "cfg-if" @@ -988,6 +991,19 @@ dependencies = [ "wasi 0.9.0+wasi-snapshot-preview1", ] +[[package]] +name = "git2" +version = "0.13.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca6f1a0238d7f8f8fd5ee642f4ebac4dbc03e03d1f78fbe7a3ede35dcf7e2224" +dependencies = [ + "bitflags", + "libc", + "libgit2-sys", + "log", + "url", +] + [[package]] name = "glob" version = "0.3.0" @@ -1122,6 +1138,15 @@ version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6" +[[package]] +name = "jobserver" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c71313ebb9439f74b00d9d2dcec36440beaf57a6aa0623068441dd7cd81a7f2" +dependencies = [ + "libc", +] + [[package]] name = "js-sys" version = "0.3.45" @@ -1180,6 +1205,18 @@ version = "0.2.80" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" +[[package]] +name = "libgit2-sys" +version = "0.12.14+1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f25af58e6495f7caf2919d08f212de550cfa3ed2f5e744988938ea292b9f549" +dependencies = [ + "cc", + "libc", + "libz-sys", + "pkg-config", +] + [[package]] name = "libm" version = "0.2.1" @@ -1197,6 +1234,18 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "libz-sys" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "602113192b08db8f38796c4e85c39e960c145965140e918018bcde1952429655" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "linked-hash-map" version = "0.5.3" @@ -2221,6 +2270,7 @@ dependencies = [ "futures-core", "futures-util", "generic-array", + "git2", "hex", "hmac", "ipnetwork", diff --git a/Cargo.toml b/Cargo.toml index 17968994e6..08e4a58a20 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -90,6 +90,7 @@ 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 } diff --git a/README.md b/README.md index c464193ac1..c5713b9ee3 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,8 @@ sqlx = { version = "0.4.0", features = [ "runtime-async-std-native-tls" ] } - `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. diff --git a/sqlx-core/Cargo.toml b/sqlx-core/Cargo.toml index ef3239acdf..7063d97660 100644 --- a/sqlx-core/Cargo.toml +++ b/sqlx-core/Cargo.toml @@ -108,3 +108,4 @@ 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 } diff --git a/sqlx-core/src/types/git2.rs b/sqlx-core/src/types/git2.rs new file mode 100644 index 0000000000..f4fd576a8b --- /dev/null +++ b/sqlx-core/src/types/git2.rs @@ -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 Type for Oid +where + DB: Database, + [u8]: Type, +{ + fn type_info() -> DB::TypeInfo { + <&[u8] as Type>::type_info() + } + + fn compatible(ty: &DB::TypeInfo) -> bool { + <&[u8] as Type>::compatible(ty) + } +} + +impl<'r, DB> Decode<'r, DB> for Oid +where + DB: Database, + &'r [u8]: Decode<'r, DB>, +{ + fn decode(value: >::ValueRef) -> Result { + <&[u8] as Decode>::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 >::ArgumentBuffer) -> IsNull { + <[u8] as Encode>::encode_by_ref(self.as_bytes(), buf) + } +} diff --git a/sqlx-core/src/types/mod.rs b/sqlx-core/src/types/mod.rs index 0eb93b9c61..76c49fb1a3 100644 --- a/sqlx-core/src/types/mod.rs +++ b/sqlx-core/src/types/mod.rs @@ -24,6 +24,10 @@ use crate::database::Database; #[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; From 603c1950fee8d070efc19119ac2575f130f289dd Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Sat, 21 Nov 2020 14:32:11 -0800 Subject: [PATCH 4/4] Add tests for git2 --- tests/sqlite/types.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/sqlite/types.rs b/tests/sqlite/types.rs index 3e26c902ae..0d07d2d88c 100644 --- a/tests/sqlite/types.rs +++ b/tests/sqlite/types.rs @@ -118,3 +118,19 @@ mod bstr { "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") + )); +}