use super::ddl::Dialect;
impl Dialect {
fn is_duckdb(self) -> bool {
matches!(self, Dialect::DuckDb)
}
pub(super) fn build_array1(self, x: &str) -> String {
if self.is_duckdb() {
format!("json_array({x})")
} else {
format!("jsonb_build_array({x})")
}
}
pub(super) fn empty_array(self) -> &'static str {
if self.is_duckdb() {
"'[]'::JSON"
} else {
"'[]'::jsonb"
}
}
pub(super) fn agg(self, x: &str) -> String {
if self.is_duckdb() {
format!("coalesce(json_group_array({x}),'[]'::JSON)")
} else {
format!("coalesce(jsonb_agg({x}),'[]'::jsonb)")
}
}
pub(super) fn scalar_text(self, j: &str) -> String {
if self.is_duckdb() {
format!("({j} ->> '$')")
} else {
format!("({j} #>> '{{}}')")
}
}
pub(super) fn is_array(self, j: &str) -> String {
if self.is_duckdb() {
format!("json_type({j}) = 'ARRAY'")
} else {
format!("jsonb_typeof({j}) = 'array'")
}
}
pub(super) fn is_string(self, j: &str) -> String {
if self.is_duckdb() {
format!("json_type({j}) = 'VARCHAR'")
} else {
format!("jsonb_typeof({j}) = 'string'")
}
}
pub(super) fn is_number(self, j: &str) -> String {
if self.is_duckdb() {
format!(
"json_type({j}) IN ('UBIGINT','BIGINT','DOUBLE','DECIMAL','UINTEGER','INTEGER')"
)
} else {
format!("jsonb_typeof({j}) = 'number'")
}
}
pub(super) fn is_json_null(self, j: &str) -> String {
if self.is_duckdb() {
format!("json_type({j}) = 'NULL'")
} else {
format!("jsonb_typeof({j}) = 'null'")
}
}
pub(super) fn array_length(self, j: &str) -> String {
if self.is_duckdb() {
format!("json_array_length({j})")
} else {
format!("jsonb_array_length({j})")
}
}
pub(super) fn to_json_scalar(self, x: &str) -> String {
if self.is_duckdb() {
format!("to_json({x})")
} else {
format!("to_jsonb({x})")
}
}
pub(super) fn num_cast(self) -> &'static str {
if self.is_duckdb() {
"DECIMAL(38,9)"
} else {
"numeric"
}
}
pub(super) fn int_cast(self) -> &'static str {
if self.is_duckdb() { "BIGINT" } else { "bigint" }
}
pub(super) fn bool_cast(self) -> &'static str {
if self.is_duckdb() {
"BOOLEAN"
} else {
"boolean"
}
}
pub(super) fn text_cast(self) -> &'static str {
if self.is_duckdb() { "VARCHAR" } else { "text" }
}
pub(super) fn elements_table(self, arr: &str, alias: &str, col: &str) -> String {
if self.is_duckdb() {
format!("(SELECT unnest(json_extract({arr}, '$[*]')) AS {col}) AS {alias}")
} else {
format!("jsonb_array_elements({arr}) AS {alias}({col})")
}
}
pub(super) fn elements_ord_table(self, coll: &str, alias: &str) -> String {
if self.is_duckdb() {
format!(
"(SELECT unnest(json_extract({coll},'$[*]')) AS value, \
unnest(range(1, ({len}+1)::BIGINT)) AS ord) AS {alias}",
len = self.array_length(coll)
)
} else {
format!("jsonb_array_elements({coll}) WITH ORDINALITY AS {alias}(value, ord)")
}
}
}