|
| 1 | +//! Canonical migration AST values. |
| 2 | +
|
| 3 | +use crate::planner_migrations::constants::{HAVING_MAX_KEY, OPAQUE_TUPLE_FIELDS}; |
| 4 | +use serde_json::Value; |
| 5 | +use std::collections::BTreeSet; |
| 6 | + |
| 7 | +/// Tuple-valued AST fields are opaque to the historical Python dict/list walkers. |
| 8 | +pub(crate) fn opaque_field(key: &str, value: &Value) -> bool { |
| 9 | + key == HAVING_MAX_KEY |
| 10 | + || (OPAQUE_TUPLE_FIELDS.contains(&key) |
| 11 | + && value |
| 12 | + .as_array() |
| 13 | + .is_some_and(|items| !items.is_empty() && items.iter().all(Value::is_array))) |
| 14 | +} |
| 15 | + |
| 16 | +pub(crate) fn strip_formatting(node: &mut Value) { |
| 17 | + match node { |
| 18 | + Value::Object(object) => { |
| 19 | + object.shift_remove("span"); |
| 20 | + for (key, value) in object { |
| 21 | + if opaque_field(key, value) { |
| 22 | + continue; |
| 23 | + } |
| 24 | + if key.ends_with("comments") { |
| 25 | + *value = Value::Array(Vec::new()); |
| 26 | + } else { |
| 27 | + strip_formatting(value); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + Value::Array(items) => items.iter_mut().for_each(strip_formatting), |
| 32 | + _ => {} |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +pub(crate) fn canonical(node: &mut Value) { |
| 37 | + match node { |
| 38 | + Value::Array(items) => items.iter_mut().for_each(canonical), |
| 39 | + Value::Object(object) => { |
| 40 | + for (key, value) in object.iter_mut() { |
| 41 | + if !opaque_field(key, value) { |
| 42 | + canonical(value); |
| 43 | + } |
| 44 | + } |
| 45 | + if object.get("quoted") == Some(&Value::Bool(false)) |
| 46 | + && let Some(Value::String(name)) = object.get_mut("name") |
| 47 | + { |
| 48 | + *name = name.to_lowercase(); |
| 49 | + } |
| 50 | + } |
| 51 | + _ => {} |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +pub(crate) fn identifier(node: &Value) -> Option<&str> { |
| 56 | + node.get("name")?.as_str() |
| 57 | +} |
| 58 | + |
| 59 | +pub(crate) fn identifiers(node: &Value) -> Vec<String> { |
| 60 | + items(node) |
| 61 | + .iter() |
| 62 | + .filter_map(identifier) |
| 63 | + .map(str::to_lowercase) |
| 64 | + .collect() |
| 65 | +} |
| 66 | + |
| 67 | +pub(crate) fn items(node: &Value) -> &[Value] { |
| 68 | + node.as_array().map_or(&[], Vec::as_slice) |
| 69 | +} |
| 70 | + |
| 71 | +pub(crate) fn clause_items(node: &Value) -> &[Value] { |
| 72 | + if node.is_array() { |
| 73 | + items(node) |
| 74 | + } else { |
| 75 | + items(&node["expressions"]) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +pub(crate) fn references(node: &Value, unqualified: bool) -> BTreeSet<String> { |
| 80 | + matching_fields(node, "column") |
| 81 | + .into_iter() |
| 82 | + .filter(|column| !unqualified || column["table"].is_null()) |
| 83 | + .filter_map(|column| identifier(&column["name"])) |
| 84 | + .map(str::to_lowercase) |
| 85 | + .collect() |
| 86 | +} |
| 87 | + |
| 88 | +pub(crate) fn matching_fields<'a>(node: &'a Value, key: &str) -> Vec<&'a Value> { |
| 89 | + let mut found: Vec<&Value> = Vec::new(); |
| 90 | + let mut pending: Vec<&Value> = vec![node]; |
| 91 | + while let Some(current) = pending.pop() { |
| 92 | + match current { |
| 93 | + Value::Array(items) => pending.extend(items), |
| 94 | + Value::Object(object) => { |
| 95 | + if let Some(value) = object.get(key) { |
| 96 | + found.push(value); |
| 97 | + } |
| 98 | + pending.extend( |
| 99 | + object |
| 100 | + .iter() |
| 101 | + .filter(|(key, value)| !opaque_field(key, value)) |
| 102 | + .map(|(_, value)| value), |
| 103 | + ); |
| 104 | + } |
| 105 | + _ => {} |
| 106 | + } |
| 107 | + } |
| 108 | + found |
| 109 | +} |
0 commit comments