vantage_sql/primitives/
json_extract.rs1use std::fmt::{Debug, Display};
2
3use vantage_core::util::IntoVec;
4use vantage_expressions::{Expression, Expressive, ExpressiveEnum};
5
6#[derive(Debug, Clone)]
31pub struct JsonExtract<T: Debug + Display + Clone> {
32 source: Expression<T>,
33 path: Vec<String>,
34 as_json: bool,
37}
38
39impl<T: Debug + Display + Clone> JsonExtract<T> {
40 pub fn new(source: impl Expressive<T>, path: impl IntoVec<String>) -> Self {
41 Self {
42 source: source.expr(),
43 path: path.into_vec(),
44 as_json: false,
45 }
46 }
47
48 pub fn as_json(mut self) -> Self {
50 self.as_json = true;
51 self
52 }
53}
54
55pub fn json_extract<T: Debug + Display + Clone>(
57 source: impl Expressive<T>,
58 path: impl IntoVec<String>,
59) -> JsonExtract<T> {
60 JsonExtract::new(source, path)
61}
62
63fn sql_lit<T: Debug + Display + Clone>(s: &str) -> Expression<T> {
65 let escaped = s.replace('\'', "''");
66 Expression::new(format!("'{escaped}'"), vec![])
67}
68
69#[cfg(feature = "sqlite")]
70impl Expressive<crate::sqlite::types::AnySqliteType>
71 for JsonExtract<crate::sqlite::types::AnySqliteType>
72{
73 fn expr(&self) -> Expression<crate::sqlite::types::AnySqliteType> {
74 let json_path = format!("$.{}", self.path.join("."));
75 Expression::new(
76 "JSON_EXTRACT({}, {})",
77 vec![
78 ExpressiveEnum::Nested(self.source.clone()),
79 ExpressiveEnum::Nested(sql_lit(&json_path)),
80 ],
81 )
82 }
83}
84
85#[cfg(feature = "mysql")]
88impl Expressive<crate::mysql::types::AnyMysqlType>
89 for JsonExtract<crate::mysql::types::AnyMysqlType>
90{
91 fn expr(&self) -> Expression<crate::mysql::types::AnyMysqlType> {
92 let json_path = format!("$.{}", self.path.join("."));
93 let template = if self.as_json {
94 "JSON_EXTRACT({}, {})"
95 } else {
96 "JSON_UNQUOTE(JSON_EXTRACT({}, {}))"
97 };
98 Expression::new(
99 template,
100 vec![
101 ExpressiveEnum::Nested(self.source.clone()),
102 ExpressiveEnum::Nested(sql_lit(&json_path)),
103 ],
104 )
105 }
106}
107
108#[cfg(feature = "postgres")]
109impl Expressive<crate::postgres::types::AnyPostgresType>
110 for JsonExtract<crate::postgres::types::AnyPostgresType>
111{
112 fn expr(&self) -> Expression<crate::postgres::types::AnyPostgresType> {
113 assert!(
116 !self.path.is_empty(),
117 "JsonExtract requires at least one path segment"
118 );
119 let mut current = self.source.clone();
120 let last = self.path.len() - 1;
121
122 for (i, field) in self.path.iter().enumerate() {
123 let op = if i == last && !self.as_json {
124 " ->> "
125 } else {
126 " -> "
127 };
128 current = Expression::new(
129 format!("{{}}{op}{{}}"),
130 vec![
131 ExpressiveEnum::Nested(current),
132 ExpressiveEnum::Nested(sql_lit(field)),
133 ],
134 );
135 }
136
137 current
138 }
139}