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

[core] [macros] Add optional support for compact_str #1922

Closed
wants to merge 2 commits into from
Closed
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
27 changes: 27 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 @@ -65,6 +65,7 @@ all-types = [
"bit-vec",
"bstr",
"git2",
"compact_str",
]

# previous runtimes, available as features for error messages better than just
Expand Down Expand Up @@ -130,6 +131,7 @@ 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"]
compact_str = ["sqlx-core/compact_str", "sqlx-macros/compact_str"]

[dependencies]
sqlx-core = { version = "0.6.0", path = "sqlx-core", default-features = false }
Expand Down
3 changes: 3 additions & 0 deletions sqlx-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ all-types = [
"json",
"uuid",
"bit-vec",
"compact_str"
]
bigdecimal = ["bigdecimal_", "num-bigint"]
decimal = ["rust_decimal", "num-bigint"]
json = ["serde", "serde_json"]
compact_str = ["compact_str_"]

# runtimes
runtime-actix-native-tls = [
Expand Down Expand Up @@ -126,6 +128,7 @@ bitflags = { version = "1.3.2", default-features = false }
bytes = "1.1.0"
byteorder = { version = "1.4.3", default-features = false, features = ["std"] }
chrono = { version = "0.4.19", default-features = false, features = ["clock"], optional = true }
compact_str_ = { version = "0.5.0", optional = true, package = "compact_str" }
crc = { version = "3", optional = true }
crossbeam-queue = "0.3.2"
digest = { version = "0.10.0", default-features = false, optional = true, features = ["std"] }
Expand Down
1 change: 1 addition & 0 deletions sqlx-core/src/any/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub(crate) mod value;
#[cfg(feature = "migrate")]
mod migrate;

pub(crate) use arguments::AnyArgumentBufferKind;
pub use arguments::{AnyArgumentBuffer, AnyArguments};
pub use column::{AnyColumn, AnyColumnIndex};
pub use connection::AnyConnection;
Expand Down
114 changes: 114 additions & 0 deletions sqlx-core/src/types/compact_str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#[cfg(feature = "sqlite")]
use std::borrow::Cow;

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

#[cfg(all(
any(
feature = "postgres",
feature = "mysql",
feature = "mssql",
feature = "sqlite"
),
feature = "any"
))]
use crate::any::{Any, AnyArgumentBuffer, AnyArgumentBufferKind, AnyEncode};
#[cfg(feature = "mssql")]
use crate::mssql::Mssql;
#[cfg(feature = "mysql")]
use crate::mysql::MySql;
#[cfg(feature = "postgres")]
use crate::postgres::Postgres;
#[cfg(feature = "sqlite")]
use crate::sqlite::{Sqlite, SqliteArgumentValue};

#[doc(no_inline)]
pub use compact_str_::CompactString;

impl<DB> Type<DB> for CompactString
where
DB: Database,
String: Type<DB>,
{
fn type_info() -> DB::TypeInfo {
<String as Type<DB>>::type_info()
}

fn compatible(ty: &DB::TypeInfo) -> bool {
<String as Type<DB>>::compatible(ty)
}
}

impl<'r, DB> Decode<'r, DB> for CompactString
where
DB: Database,
&'r str: Decode<'r, DB>,
{
fn decode(value: <DB as HasValueRef<'r>>::ValueRef) -> Result<Self, BoxDynError> {
<&str as Decode<DB>>::decode(value).map(CompactString::new)
}
}

#[cfg(feature = "postgres")]
impl<'q> Encode<'q, Postgres> for CompactString {
fn encode_by_ref(&self, buf: &mut <Postgres as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
<&str as Encode<Postgres>>::encode(self.as_str(), buf)
}
}

#[cfg(feature = "mysql")]
impl<'q> Encode<'q, MySql> for CompactString {
fn encode_by_ref(&self, buf: &mut <MySql as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
<&str as Encode<MySql>>::encode(self.as_str(), buf)
}
}

#[cfg(feature = "mssql")]
impl<'q> Encode<'q, Mssql> for CompactString {
fn encode_by_ref(&self, buf: &mut <Mssql as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
<&str as Encode<Mssql>>::encode(self.as_str(), buf)
}
}

#[cfg(feature = "sqlite")]
impl<'q> Encode<'q, Sqlite> for CompactString {
fn encode_by_ref(&self, buf: &mut <Sqlite as HasArguments<'q>>::ArgumentBuffer) -> IsNull {
buf.push(SqliteArgumentValue::Blob(Cow::Owned(
self.as_bytes().to_vec(),
)));
IsNull::No
}
}

#[cfg(all(
any(
feature = "postgres",
feature = "mysql",
feature = "mssql",
feature = "sqlite"
),
feature = "any"
))]
impl<'q> Encode<'q, Any> for CompactString
where
CompactString: AnyEncode<'q>,
{
fn encode_by_ref(&self, buf: &mut AnyArgumentBuffer<'q>) -> IsNull {
match &mut buf.0 {
#[cfg(feature = "postgres")]
AnyArgumentBufferKind::Postgres(args, _) => args.add(self),
#[cfg(feature = "mysql")]
AnyArgumentBufferKind::MySql(args, _) => args.add(self),
#[cfg(feature = "mssql")]
AnyArgumentBufferKind::Mssql(args, _) => args.add(self),
#[cfg(feature = "sqlite")]
AnyArgumentBufferKind::Sqlite(args) => args.add(self),
}
IsNull::No
}
}
4 changes: 4 additions & 0 deletions sqlx-core/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pub mod chrono {
};
}

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

#[cfg(feature = "bit-vec")]
#[cfg_attr(docsrs, doc(cfg(feature = "bit-vec")))]
#[doc(no_inline)]
Expand Down
1 change: 1 addition & 0 deletions sqlx-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ mssql = ["sqlx-core/mssql"]
bigdecimal = ["sqlx-core/bigdecimal"]
decimal = ["sqlx-core/decimal"]
chrono = ["sqlx-core/chrono"]
compact_str = ["sqlx-core/compact_str"]
time = ["sqlx-core/time"]
ipnetwork = ["sqlx-core/ipnetwork"]
mac_address = ["sqlx-core/mac_address"]
Expand Down
3 changes: 3 additions & 0 deletions sqlx-macros/src/database/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ impl_database_ext! {

// ordering is important here as otherwise we might infer strings to be binary
// CHAR, VAR_CHAR, TEXT
#[cfg(feature = "compact_str")]
sqlx::types::compact_str::CompactString,

String,

// BINARY, VAR_BINARY, BLOB
Expand Down
3 changes: 3 additions & 0 deletions sqlx-macros/src/database/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ impl_database_ext! {

sqlx::postgres::types::PgLQuery,

#[cfg(feature = "compact_str")]
sqlx::types::compact_str::CompactString,

#[cfg(feature = "uuid")]
sqlx::types::Uuid,

Expand Down
3 changes: 3 additions & 0 deletions sqlx-macros/src/database/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ impl_database_ext! {
String,
Vec<u8>,

#[cfg(feature = "compact_str")]
sqlx::types::compact_str::CompactString,

#[cfg(feature = "chrono")]
sqlx::types::chrono::NaiveDateTime,

Expand Down