1
0
mirror of https://github.com/Bayselonarrend/OpenIntegrations.git synced 2025-08-10 22:41:43 +02:00

PG: JSON и JSONB

This commit is contained in:
Anton Titovets
2025-02-11 16:44:01 +03:00
parent 02af8cc7d4
commit 423e01efd8
8 changed files with 17 additions and 1 deletions

View File

@@ -506,6 +506,8 @@ dependencies = [
"bytes",
"fallible-iterator",
"postgres-protocol",
"serde",
"serde_json",
]
[[package]]

View File

@@ -15,7 +15,7 @@ opt-level = "z"
[dependencies]
addin1c = "0.5.0"
postgres = { version = "0.19.9"}
postgres = { version = "0.19.9", features = ["with-serde_json-1"]}
serde_json = "1.0"
base64 = "0.22.1"
chrono = "0.4.39"

View File

@@ -159,6 +159,15 @@ fn process_object(object: &Map<String, Value>) -> Result<Box<dyn ToSql + Sync>,
.and_then(|s| s.parse::<IpAddr>().ok())
.map(|ip| Box::new(ip) as Box<dyn ToSql + Sync>)
.ok_or_else(|| "Invalid value for INET".to_string()),
"JSON" | "JSONB" => {
if value.is_object() || value.is_array() {
Ok(Box::new(value.to_string()) as Box<dyn ToSql + Sync>)
} else if value.is_string() {
Ok(Box::new(value.as_str().unwrap().to_string()) as Box<dyn ToSql + Sync>)
} else {
Err("Invalid value for JSON/JSONB: must be an object, array, or string".to_string())
}
}
_ => Err(format!("Unsupported type: {}", key)),
}
}
@@ -238,6 +247,11 @@ fn rows_to_json(rows: Vec<postgres::Row>) -> String {
"inet" => row.get::<_, Option<IpAddr>>(column_name)
.map(|ip| Value::String(ip.to_string()))
.unwrap_or(Value::Null),
"json" | "jsonb" => {
row.get::<_, Option<String>>(column_name)
.and_then(|s| serde_json::from_str(&s).ok())
.unwrap_or(Value::Null)
},
_ => Value::Null,
};

Binary file not shown.

Binary file not shown.