Convert a `tokio_postgres` column value to `nodedb_types::Value`.
(
row: &tokio_postgres::Row,
idx: usize,
ty: &tokio_postgres::types::Type,
)
| 12 | |
| 13 | /// Convert a `tokio_postgres` column value to `nodedb_types::Value`. |
| 14 | pub(crate) fn pg_value_to_value( |
| 15 | row: &tokio_postgres::Row, |
| 16 | idx: usize, |
| 17 | ty: &tokio_postgres::types::Type, |
| 18 | ) -> Value { |
| 19 | use tokio_postgres::types::Type; |
| 20 | |
| 21 | match *ty { |
| 22 | Type::BOOL => row |
| 23 | .try_get::<_, bool>(idx) |
| 24 | .map(Value::Bool) |
| 25 | .unwrap_or(Value::Null), |
| 26 | Type::INT2 => row |
| 27 | .try_get::<_, i16>(idx) |
| 28 | .map(|v| Value::Integer(v as i64)) |
| 29 | .unwrap_or(Value::Null), |
| 30 | Type::INT4 => row |
| 31 | .try_get::<_, i32>(idx) |
| 32 | .map(|v| Value::Integer(v as i64)) |
| 33 | .unwrap_or(Value::Null), |
| 34 | Type::INT8 => row |
| 35 | .try_get::<_, i64>(idx) |
| 36 | .map(Value::Integer) |
| 37 | .unwrap_or(Value::Null), |
| 38 | Type::FLOAT4 => row |
| 39 | .try_get::<_, f32>(idx) |
| 40 | .map(|v| Value::Float(v as f64)) |
| 41 | .unwrap_or(Value::Null), |
| 42 | Type::FLOAT8 => row |
| 43 | .try_get::<_, f64>(idx) |
| 44 | .map(Value::Float) |
| 45 | .unwrap_or(Value::Null), |
| 46 | Type::TEXT | Type::VARCHAR | Type::NAME => row |
| 47 | .try_get::<_, String>(idx) |
| 48 | .map(Value::String) |
| 49 | .unwrap_or(Value::Null), |
| 50 | Type::BYTEA => row |
| 51 | .try_get::<_, Vec<u8>>(idx) |
| 52 | .map(Value::Bytes) |
| 53 | .unwrap_or(Value::Null), |
| 54 | Type::JSON | Type::JSONB => row |
| 55 | .try_get::<_, serde_json::Value>(idx) |
| 56 | .map(|v| json_to_value(&v)) |
| 57 | .unwrap_or(Value::Null), |
| 58 | _ => { |
| 59 | // Fallback: try as string. |
| 60 | row.try_get::<_, String>(idx) |
| 61 | .map(Value::String) |
| 62 | .unwrap_or(Value::Null) |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /// Convert `serde_json::Value` to `nodedb_types::Value`. |
| 68 | pub(crate) fn json_to_value(v: &serde_json::Value) -> Value { |
no test coverage detected