Skip to content

Commit

Permalink
add support for integer types in postgres and any
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa committed Nov 7, 2024
1 parent edec90b commit 659cf57
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 17 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.6.35

- Add support for unsigned integers in the `Any` driver
- Add support for unsigned integers in the `postgres` driver
- Add a `max_size` method to `MySqlTypeInfo` allowing to retrieve the maximum size of the type (for example, `TINYINT(1)` has a maximum size of 1)

## 0.6.34

- Add support for decoding `DateTime<FixedOffset>` in the `Any` driver
Expand Down
10 changes: 5 additions & 5 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ members = [

[package]
name = "sqlx-oldapi"
version = "0.6.34"
version = "0.6.35"
license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/lovasoa/sqlx"
Expand Down Expand Up @@ -125,8 +125,8 @@ bstr = ["sqlx-core/bstr"]
git2 = ["sqlx-core/git2"]

[dependencies]
sqlx-core = { package = "sqlx-core-oldapi", version = "0.6.34", path = "sqlx-core", default-features = false }
sqlx-macros = { package = "sqlx-macros-oldapi", version = "0.6.34", path = "sqlx-macros", default-features = false, optional = true }
sqlx-core = { package = "sqlx-core-oldapi", version = "0.6.35", path = "sqlx-core", default-features = false }
sqlx-macros = { package = "sqlx-macros-oldapi", version = "0.6.35", path = "sqlx-macros", default-features = false, optional = true }

[dev-dependencies]
anyhow = "1.0.52"
Expand Down
2 changes: 1 addition & 1 deletion examples/postgres/axum-social-with-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
[dependencies]
# Primary crates
axum = { version = "0.5.13", features = ["macros"] }
sqlx = { package = "sqlx-oldapi", version = "0.6.34", path = "../../../", features = ["runtime-tokio-rustls", "postgres", "time", "uuid"] }
sqlx = { package = "sqlx-oldapi", version = "0.6.35", path = "../../../", features = ["runtime-tokio-rustls", "postgres", "time", "uuid"] }
tokio = { version = "1.20.1", features = ["rt-multi-thread", "macros"] }

# Important secondary crates
Expand Down
2 changes: 1 addition & 1 deletion sqlx-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sqlx-cli"
version = "0.6.34"
version = "0.6.35"
description = "Command-line utility for SQLx, the Rust SQL toolkit."
edition = "2021"
readme = "README.md"
Expand Down
4 changes: 2 additions & 2 deletions sqlx-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sqlx-core-oldapi"
version = "0.6.34"
version = "0.6.35"
repository = "https://github.com/lovasoa/sqlx"
description = "Core of SQLx, the rust SQL toolkit. Not intended to be used directly."
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -101,7 +101,7 @@ offline = ["serde", "either/serde"]
paste = "1.0.6"
ahash = "0.8.3"
atoi = "2.0.0"
sqlx-rt = { path = "../sqlx-rt", version = "0.6.34", package = "sqlx-rt-oldapi" }
sqlx-rt = { path = "../sqlx-rt", version = "0.6.35", package = "sqlx-rt-oldapi" }
base64 = { version = "0.22", default-features = false, optional = true, features = ["std"] }
bigdecimal_ = { version = "0.4.1", optional = true, package = "bigdecimal" }
rust_decimal = { version = "1.19.0", optional = true }
Expand Down
12 changes: 12 additions & 0 deletions sqlx-core/src/any/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ impl_any_type!(f64);
impl_any_type!(str);
impl_any_type!(String);

impl_any_type!(u16);
impl_any_type!(u32);
impl_any_type!(u64);

// Encode

impl_any_encode!(bool);
Expand All @@ -46,6 +50,10 @@ impl_any_encode!(f64);
impl_any_encode!(&'q str);
impl_any_encode!(String);

impl_any_encode!(u16);
impl_any_encode!(u32);
impl_any_encode!(u64);

// Decode

impl_any_decode!(bool);
Expand All @@ -60,6 +68,10 @@ impl_any_decode!(f64);
impl_any_decode!(&'r str);
impl_any_decode!(String);

impl_any_decode!(u16);
impl_any_decode!(u32);
impl_any_decode!(u64);

// Conversions for Blob SQL types
// Type
impl_any_type!([u8]);
Expand Down
4 changes: 4 additions & 0 deletions sqlx-core/src/mysql/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ impl MySqlTypeInfo {
max_size: Some(column.max_size),
}
}

pub fn max_size(&self) -> Option<u32> {
self.max_size
}
}

impl Display for MySqlTypeInfo {
Expand Down
85 changes: 85 additions & 0 deletions sqlx-core/src/postgres/types/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,88 @@ impl Decode<'_, Postgres> for i64 {
})
}
}

impl Type<Postgres> for u16 {
fn type_info() -> PgTypeInfo {
PgTypeInfo::INT4
}
}

impl PgHasArrayType for u16 {
fn array_type_info() -> PgTypeInfo {
PgTypeInfo::INT4_ARRAY
}
}

impl Encode<'_, Postgres> for u16 {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
buf.extend(&i32::from(*self).to_be_bytes());
IsNull::No
}
}

impl Decode<'_, Postgres> for u16 {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
let decoded = match value.format() {
PgValueFormat::Binary => BigEndian::read_i32(value.as_bytes()?),
PgValueFormat::Text => value.as_str()?.parse::<i32>()?,
};
Ok(u16::try_from(decoded)?)
}
}

impl Type<Postgres> for u32 {
fn type_info() -> PgTypeInfo {
PgTypeInfo::INT8
}
}

impl PgHasArrayType for u32 {
fn array_type_info() -> PgTypeInfo {
PgTypeInfo::INT8_ARRAY
}
}

impl Encode<'_, Postgres> for u32 {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
buf.extend(&i64::from(*self).to_be_bytes());
IsNull::No
}
}

impl Decode<'_, Postgres> for u32 {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
let decoded = match value.format() {
PgValueFormat::Binary => BigEndian::read_i64(value.as_bytes()?),
PgValueFormat::Text => value.as_str()?.parse::<i64>()?,
};
Ok(u32::try_from(decoded)?)
}
}

impl Type<Postgres> for u64 {
fn type_info() -> PgTypeInfo {
PgTypeInfo::NUMERIC
}
}

impl PgHasArrayType for u64 {
fn array_type_info() -> PgTypeInfo {
PgTypeInfo::NUMERIC_ARRAY
}
}

impl Encode<'_, Postgres> for u64 {
fn encode_by_ref(&self, buf: &mut PgArgumentBuffer) -> IsNull {
let numeric_str = self.to_string();
buf.extend(numeric_str.as_bytes());
IsNull::No
}
}

impl Decode<'_, Postgres> for u64 {
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError> {
let decoded = value.as_str()?.parse::<u64>()?;
Ok(decoded)
}
}
6 changes: 3 additions & 3 deletions sqlx-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sqlx-macros-oldapi"
version = "0.6.34"
version = "0.6.35"
repository = "https://github.com/lovasoa/sqlx"
description = "Macros for SQLx, the rust SQL toolkit. Not intended to be used directly."
license = "MIT OR Apache-2.0"
Expand Down Expand Up @@ -75,8 +75,8 @@ heck = { version = "0.5" }
either = "1.6.1"
once_cell = "1.9.0"
proc-macro2 = { version = "1.0.36", default-features = false }
sqlx-core = { package = "sqlx-core-oldapi", version = "0.6.34", default-features = false, features = ["any"], path = "../sqlx-core" }
sqlx-rt = { version = "0.6.34", default-features = false, path = "../sqlx-rt", package = "sqlx-rt-oldapi" }
sqlx-core = { package = "sqlx-core-oldapi", version = "0.6.35", default-features = false, features = ["any"], path = "../sqlx-core" }
sqlx-rt = { version = "0.6.35", default-features = false, path = "../sqlx-rt", package = "sqlx-rt-oldapi" }
serde = { version = "1.0.132", features = ["derive"], optional = true }
serde_json = { version = "1.0.73", optional = true }
sha2 = { version = "0.10.0", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion sqlx-rt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sqlx-rt-oldapi"
version = "0.6.34"
version = "0.6.35"
repository = "https://github.com/launchbadge/sqlx"
license = "MIT OR Apache-2.0"
description = "Runtime abstraction used by SQLx, the Rust SQL toolkit. Not intended to be used directly."
Expand Down
5 changes: 4 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ docker compose -f tests/docker-compose.yml run -it -p 5432:5432 --name postgres_
DATABASE_URL="postgres://postgres@localhost:5432/sqlx?sslmode=verify-ca&sslrootcert=./tests/certs/ca.crt&sslcert=./tests/certs/client.crt&sslkey=./tests/keys/client.key" cargo test --features any,postgres,macros,all-types,runtime-actix-rustls --

docker compose -f tests/docker-compose.yml run -it -p 1433:1433 --name mssql_2022 mssql_2022
DATABASE_URL='mssql://sa:Password123!@localhost/sqlx' cargo test --features any,mssql,macros,all-types,runtime-actix-rustls --
DATABASE_URL='mssql://sa:Password123!@localhost/sqlx' cargo test --features any,mssql,macros,all-types,runtime-actix-rustls --

docker compose -f tests/docker-compose.yml run -it -p 3306:3306 --name mysql_8 mysql_8
DATABASE_URL='mysql://root:password@localhost/sqlx' cargo test --features any,mysql,macros,all-types,runtime-actix-rustls --
12 changes: 12 additions & 0 deletions tests/any/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,15 @@ async fn it_can_fail_and_recover_with_pool() -> anyhow::Result<()> {

Ok(())
}

#[sqlx_macros::test]
async fn it_has_unsigned_integers() -> anyhow::Result<()> {
let max_value = u64::MAX;
let expr = if cfg!(feature = "mysql") {
format!("CAST({} AS UNSIGNED)", max_value)
} else {
max_value.to_string()
};
assert_eq!(max_value, get_val::<u64>(&expr).await?);
Ok(())
}

0 comments on commit 659cf57

Please sign in to comment.