MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / pg_value_to_value

Function pg_value_to_value

nodedb-client/src/remote_parse.rs:14–65  ·  view source on GitHub ↗

Convert a `tokio_postgres` column value to `nodedb_types::Value`.

(
    row: &tokio_postgres::Row,
    idx: usize,
    ty: &tokio_postgres::types::Type,
)

Source from the content-addressed store, hash-verified

12
13/// Convert a `tokio_postgres` column value to `nodedb_types::Value`.
14pub(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`.
68pub(crate) fn json_to_value(v: &serde_json::Value) -> Value {

Callers 1

query_rawMethod · 0.85

Calls 1

json_to_valueFunction · 0.70

Tested by

no test coverage detected