Function quaint::ast::json_extract
source · pub fn json_extract<'a, C, P>(
column: C,
path: P,
extract_as_string: bool
) -> Function<'a>where
C: Into<Expression<'a>>,
P: Into<JsonPath<'a>>,
Expand description
Extracts a subset of a JSON blob given a path. Two types of paths can be used:
String
paths, referring to JSON paths. This is supported by MySQL only.Array
paths, supported by Postgres only.
For PostgreSQL:
let extract: Expression = json_extract(Column::from(("users", "json")), JsonPath::array(["a", "b"]), false).into();
let query = Select::from_table("users").so_that(extract.equals("c"));
let (sql, params) = Postgres::build(query)?;
assert_eq!("SELECT \"users\".* FROM \"users\" WHERE (\"users\".\"json\"#>ARRAY[$1, $2]::text[])::jsonb = $3", sql);
assert_eq!(vec![Value::text("a"), Value::text("b"), Value::text("c")], params);
For MySQL:
let extract: Expression = json_extract(Column::from(("users", "json")), JsonPath::string("$.a.b"), false).into();
let query = Select::from_table("users").so_that(extract.equals("c"));
let (sql, params) = Mysql::build(query)?;
assert_eq!(r#"SELECT `users`.* FROM `users` WHERE (JSON_CONTAINS(JSON_EXTRACT(`users`.`json`, ?), ?) AND JSON_CONTAINS(?, JSON_EXTRACT(`users`.`json`, ?)))"#, sql);
assert_eq!(vec![Value::text("$.a.b"), Value::text("c"), Value::text("c"), Value::text("$.a.b")], params);