From e004d6db7464d5cb30a37cfbe189a1cc7076741b Mon Sep 17 00:00:00 2001 From: pcp <1182658898@qq.com> Date: Sat, 4 Jan 2025 11:23:58 +0800 Subject: [PATCH] fix suggestion is_null error --- api/src/utils.rs | 73 ++++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/api/src/utils.rs b/api/src/utils.rs index 46dffb8..e046ea2 100644 --- a/api/src/utils.rs +++ b/api/src/utils.rs @@ -17,6 +17,15 @@ pub fn get_column_names(rows: &[Row]) -> Vec { } } +macro_rules! get_json_option { + ($row:expr, $idx:expr, $type:ty) => { + { + let v: Option<$type> = $row.try_get($idx).unwrap_or(None); + json!(v) + } + }; +} + pub fn rows_to_json(rows: &[Row]) -> Vec { if rows.is_empty() { return vec![]; @@ -32,39 +41,37 @@ pub fn rows_to_json(rows: &[Row]) -> Vec { for (i, &column_type) in column_types.iter().enumerate() { let column_name = column_names[i]; - let column_info = row.columns()[i].type_(); - let value: Value = if column_info.is_null() { - Value::Null - } else { - match *column_type { - Type::INT2 => json!(row.get::<_, i16>(i)), - Type::INT4 => json!(row.get::<_, i32>(i)), - Type::INT8 => json!(row.get::<_, i64>(i)), - Type::FLOAT4 => json!(row.get::<_, f32>(i)), - Type::FLOAT8 => json!(row.get::<_, f64>(i)), - Type::BOOL => json!(row.get::<_, bool>(i)), - Type::VARCHAR | Type::TEXT | Type::BPCHAR => json!(row.get::<_, String>(i)), - Type::TIMESTAMP => { - let ts: NaiveDateTime = row.get(i); - json!(ts.to_string()) - } - Type::TIMESTAMPTZ => { - let ts: DateTime = row.get(i); - json!(ts.to_rfc3339()) - } - Type::DATE => { - let date: NaiveDate = row.get(i); - json!(date.to_string()) - } - Type::JSON | Type::JSONB => row.get::<_, Value>(i), - Type::UUID => { - let uuid: Uuid = row.get(i); - json!(uuid.to_string()) - } - _ => { - log::warn!("Unhandled column type: {:?}", column_type); - Value::Null - } + let value: Value = match *column_type { + Type::INT2 => get_json_option!(row, i, i16), + Type::INT4 => get_json_option!(row, i, i32), + Type::INT8 => get_json_option!(row, i, i64), + Type::FLOAT4 => get_json_option!(row, i, f32), + Type::FLOAT8 => get_json_option!(row, i, f64), + Type::BOOL => get_json_option!(row, i, bool), + Type::VARCHAR | Type::TEXT | Type::BPCHAR => get_json_option!(row, i, String), + Type::TIMESTAMP => { + let v: Option = row.try_get(i).unwrap_or(None); + v.map(|ts| json!(ts.to_string())).unwrap_or(Value::Null) + } + Type::TIMESTAMPTZ => { + let v: Option> = row.try_get(i).unwrap_or(None); + v.map(|ts| json!(ts.to_rfc3339())).unwrap_or(Value::Null) + } + Type::DATE => { + let v: Option = row.try_get(i).unwrap_or(None); + v.map(|date| json!(date.to_string())).unwrap_or(Value::Null) + } + Type::JSON | Type::JSONB => { + let v: Option = row.try_get(i).unwrap_or(None); + v.unwrap_or(Value::Null) + } + Type::UUID => { + let v: Option = row.try_get(i).unwrap_or(None); + v.map(|uuid| json!(uuid.to_string())).unwrap_or(Value::Null) + } + _ => { + log::warn!("Unhandled column type: {:?}", column_type); + Value::Null } }; map.insert(column_name.to_string(), value);