Add support for git2::Oid (under the git2 feature flag)

This allows queries to convert between Oid and SQL data types.
This commit is contained in:
Josh Triplett 2020-11-21 14:15:27 -08:00
parent d396e5a948
commit 71a72e7c67
6 changed files with 100 additions and 0 deletions

50
Cargo.lock generated
View File

@ -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",

View File

@ -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 }

View File

@ -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.

View File

@ -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 }

View File

@ -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)
}
}

View File

@ -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;