使用 Rust 的 u32 让 PostgreSQL 实现 MySQL 的 INT UNSIGNED
51498
2022/10/13 03:02:57
i32
和 u32
的转换
在 Rust 和 PostgreSQL 中,Rust 的 i32
对应 PostgreSQL 的 INTEGER(INT)
,它们都是有符号的 32 位整数。而 Rust 的 u32
是无符号的 32 位整数。
无论 Rust 的 i32
还是 PostgreSQL 的 INT
,当它们的值达到取值范围的最大值时,它们会溢出为最小值。
#[test]
fn test_max_i32_plus_1() {
let max_i32 = i32::max_value();
let min_i32 = i32::min_value();
let from_max_i32 = (max_i32 as u32) + 1;
let from_min_i32 = min_i32 as u32;
assert!(from_max_i32 == from_min_i32);
}
使用 Rust 的 u32
让 PostgreSQL 实现 MySQL 的 INT UNSIGNED
利用上述的方法,我们可以实现 MySQL 的 INT UNSIGNED
。
SQL 语句
CREATE TABLE test_int_to_u32(
hit INT NOT NULL
);
CREATE TABLE test_serial_to_u32(
id SERIAL PRIMARY KEY
);
依赖
代码演示
fn main() {
println!("Hello, world!");
}
pub struct PgIntAndRustU32 {
pub hit: u32,
}
pub struct PgSerialAndRustU32 {
pub id: u32,
}
#[cfg(test)]
mod test {
use super::*;
use sqlx::{postgres::PgPoolOptions, Row};
#[test]
fn test_max_i32_plus_1() {
let max_i32 = i32::max_value();
let min_i32 = i32::min_value();
let from_max_i32 = (max_i32 as u32) + 1;
let from_min_i32 = min_i32 as u32;
assert!(from_max_i32 == from_min_i32);
}
#[tokio::test]
async fn test_pg_int_and_rust_u32() {
let pool = PgPoolOptions::new().max_connections(3).connect("postgres://axum_rs:[email protected]/axum_rs").await.unwrap();
let max_i32 = i32::max_value();
let i: u32 = (max_i32 as u32) + 1;
sqlx::query("INSERT INTO test_int_to_u32(hit) VALUES ($1)")
.bind(i as i32)
.execute(&pool)
.await
.unwrap();
}
#[tokio::test]
async fn test_pg_int_and_rust_u32_read() {
let pool = PgPoolOptions::new().max_connections(3).connect("postgres://axum_rs:[email protected]/axum_rs").await.unwrap();
let row = sqlx::query("SELECT hit FROM test_int_to_u32")
.fetch_one(&pool)
.await
.unwrap();
let hit: i32 = row.get(0);
let obj = PgIntAndRustU32 { hit: hit as u32 };
assert!(obj.hit == (i32::max_value() as u32) + 1);
assert!(obj.hit as i32 == i32::min_value());
}
#[tokio::test]
async fn test_pg_serial_and_rust_u32() {
let pool = PgPoolOptions::new().max_connections(3).connect("postgres://axum_rs:[email protected]/axum_rs").await.unwrap();
let max_i32 = i32::max_value();
let i: u32 = (max_i32 as u32) + 1;
sqlx::query("INSERT INTO test_serial_to_u32(id) VALUES ($1)")
.bind(i as i32)
.execute(&pool)
.await
.unwrap();
}
#[tokio::test]
async fn test_pg_serial_and_rust_u32read() {
let pool = PgPoolOptions::new().max_connections(3).connect("postgres://axum_rs:[email protected]/axum_rs").await.unwrap();
let row = sqlx::query("SELECT hit FROM test_serial_to_u32")
.fetch_one(&pool)
.await
.unwrap();
let id: i32 = row.get(0);
let obj = PgSerialAndRustU32 { id: id as u32 };
assert!(obj.id == (i32::max_value() as u32) + 1);
assert!(obj.id as i32 == i32::min_value());
}
}