Skip to content

Commit 1a31aad

Browse files
committed
PostgreSQL u32 was wrapped in Oid (launchbadge/sqlx#1602)
1 parent 2fffadc commit 1a31aad

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/executor/query.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ try_getable_all!(i32);
298298
try_getable_all!(i64);
299299
try_getable_unsigned!(u8);
300300
try_getable_unsigned!(u16);
301-
try_getable_all!(u32);
302301
try_getable_mysql!(u64);
303302
try_getable_all!(f32);
304303
try_getable_all!(f64);
@@ -391,6 +390,47 @@ impl TryGetable for Decimal {
391390
#[cfg(feature = "with-uuid")]
392391
try_getable_all!(uuid::Uuid);
393392

393+
impl TryGetable for u32 {
394+
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TryGetError> {
395+
let _column = format!("{}{}", pre, col);
396+
match &res.row {
397+
#[cfg(feature = "sqlx-mysql")]
398+
QueryResultRow::SqlxMySql(row) => {
399+
use sqlx::Row;
400+
row.try_get::<Option<u32>, _>(_column.as_str())
401+
.map_err(|e| TryGetError::DbErr(crate::sqlx_error_to_query_err(e)))
402+
.and_then(|opt| opt.ok_or(TryGetError::Null))
403+
}
404+
#[cfg(feature = "sqlx-postgres")]
405+
QueryResultRow::SqlxPostgres(row) => {
406+
use sqlx::postgres::types::Oid;
407+
// Since 0.6.0, SQLx has dropped direct mapping from PostgreSQL's OID to Rust's `u32`;
408+
// Instead, `u32` was wrapped by a `sqlx::Oid`.
409+
use sqlx::Row;
410+
row.try_get::<Option<Oid>, _>(_column.as_str())
411+
.map_err(|e| TryGetError::DbErr(crate::sqlx_error_to_query_err(e)))
412+
.and_then(|opt| opt.ok_or(TryGetError::Null))
413+
.map(|oid| oid.0)
414+
}
415+
#[cfg(feature = "sqlx-sqlite")]
416+
QueryResultRow::SqlxSqlite(row) => {
417+
use sqlx::Row;
418+
row.try_get::<Option<u32>, _>(_column.as_str())
419+
.map_err(|e| TryGetError::DbErr(crate::sqlx_error_to_query_err(e)))
420+
.and_then(|opt| opt.ok_or(TryGetError::Null))
421+
}
422+
#[cfg(feature = "mock")]
423+
#[allow(unused_variables)]
424+
QueryResultRow::Mock(row) => row.try_get(_column.as_str()).map_err(|e| {
425+
debug_print!("{:#?}", e.to_string());
426+
TryGetError::Null
427+
}),
428+
#[allow(unreachable_patterns)]
429+
_ => unreachable!(),
430+
}
431+
}
432+
}
433+
394434
// TryGetableMany //
395435

396436
/// Perform a query on multiple columns

src/query/json.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl FromQueryResult for JsonValue {
6767
#[cfg(feature = "sqlx-postgres")]
6868
QueryResultRow::SqlxPostgres(row) => {
6969
use serde_json::json;
70-
use sqlx::{Column, Postgres, Row, Type};
70+
use sqlx::{Column, Postgres, Row, Type, postgres::types::Oid};
7171
for column in row.columns() {
7272
let col = if !column.name().starts_with(pre) {
7373
continue;
@@ -89,7 +89,11 @@ impl FromQueryResult for JsonValue {
8989
match_postgres_type!(i64);
9090
// match_postgres_type!(u8); // unsupported by SQLx Postgres
9191
// match_postgres_type!(u16); // unsupported by SQLx Postgres
92-
match_postgres_type!(u32);
92+
// Since 0.6.0, SQLx has dropped direct mapping from PostgreSQL's OID to Rust's `u32`;
93+
// Instead, `u32` was wrapped by a `sqlx::Oid`.
94+
if <Oid as Type<Postgres>>::type_info().eq(col_type) {
95+
try_get_type!(u32, col)
96+
}
9397
// match_postgres_type!(u64); // unsupported by SQLx Postgres
9498
match_postgres_type!(f32);
9599
match_postgres_type!(f64);

0 commit comments

Comments
 (0)