Skip to content

Commit

Permalink
TryGetable for MySQL String
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Aug 6, 2024
1 parent 2209212 commit 97e3f1c
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/executor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,6 @@ try_getable_unsigned!(u16);
try_getable_mysql!(u64);
try_getable_all!(f32);
try_getable_all!(f64);
try_getable_all!(String);
try_getable_all!(Vec<u8>);

#[cfg(feature = "with-json")]
Expand Down Expand Up @@ -705,6 +704,51 @@ impl TryGetable for u32 {
}
}

impl TryGetable for String {
#[allow(unused_variables)]
fn try_get_by<I: ColIdx>(res: &QueryResult, idx: I) -> Result<Self, TryGetError> {
match &res.row {
#[cfg(feature = "sqlx-mysql")]
QueryResultRow::SqlxMySql(row) => row
.try_get::<Option<Vec<u8>>, _>(idx.as_sqlx_mysql_index())
.map_err(|e| sqlx_error_to_query_err(e).into())
.and_then(|opt| opt.ok_or_else(|| err_null_idx_col(idx)))
.map(|bytes| {
String::from_utf8(bytes).map_err(|e| {
DbErr::TryIntoErr {
from: "Vec<u8>",
into: "String",
source: Box::new(e),
}
.into()
})
})?,
#[cfg(feature = "sqlx-postgres")]
QueryResultRow::SqlxPostgres(row) => row
.try_get::<Option<String>, _>(idx.as_sqlx_postgres_index())
.map_err(|e| sqlx_error_to_query_err(e).into())
.and_then(|opt| opt.ok_or_else(|| err_null_idx_col(idx))),
#[cfg(feature = "sqlx-sqlite")]
QueryResultRow::SqlxSqlite(row) => row
.try_get::<Option<String>, _>(idx.as_sqlx_sqlite_index())
.map_err(|e| sqlx_error_to_query_err(e).into())
.and_then(|opt| opt.ok_or_else(|| err_null_idx_col(idx))),
#[cfg(feature = "mock")]
QueryResultRow::Mock(row) => row.try_get(idx).map_err(|e| {
debug_print!("{:#?}", e.to_string());
err_null_idx_col(idx)
}),
#[cfg(feature = "proxy")]
QueryResultRow::Proxy(row) => row.try_get(idx).map_err(|e| {
debug_print!("{:#?}", e.to_string());
err_null_idx_col(idx)
}),
#[allow(unreachable_patterns)]
_ => unreachable!(),
}
}
}

#[allow(dead_code)]
fn err_null_idx_col<I: ColIdx>(idx: I) -> TryGetError {
TryGetError::Null(format!("{idx:?}"))
Expand Down

0 comments on commit 97e3f1c

Please sign in to comment.