mirror of
https://github.com/trailbaseio/trailbase.git
synced 2026-01-29 21:28:42 -06:00
Quote col names in expansion queries.
This commit is contained in:
@@ -149,6 +149,9 @@ pub async fn list_records_handler(
|
||||
};
|
||||
|
||||
let get_total_count = count.unwrap_or(false);
|
||||
let selects = selects.map_or(EMPTY, |v| format!(", {}", v.join(", ")));
|
||||
let joins = joins.map_or(EMPTY, |j| j.join(" "));
|
||||
|
||||
let query = if get_total_count {
|
||||
formatdoc!(
|
||||
r#"
|
||||
@@ -162,8 +165,8 @@ pub async fn list_records_handler(
|
||||
)
|
||||
|
||||
SELECT
|
||||
_ROW_.*,
|
||||
{selects}
|
||||
_ROW_.*
|
||||
{selects},
|
||||
total_count._value_
|
||||
FROM
|
||||
'{table_name}' as _ROW_ {joins},
|
||||
@@ -174,9 +177,7 @@ pub async fn list_records_handler(
|
||||
ORDER BY
|
||||
{order_clause}
|
||||
LIMIT :limit
|
||||
"#,
|
||||
selects = selects.map_or(EMPTY, |v| format!("{}, ", v.join(", "))),
|
||||
joins = joins.map_or(EMPTY, |j| j.join(" ")),
|
||||
"#
|
||||
)
|
||||
} else {
|
||||
formatdoc!(
|
||||
@@ -192,9 +193,7 @@ pub async fn list_records_handler(
|
||||
ORDER BY
|
||||
{order_clause}
|
||||
LIMIT :limit
|
||||
"#,
|
||||
selects = selects.map_or(EMPTY, |v| format!(", {}", v.join(", "))),
|
||||
joins = joins.map_or(EMPTY, |j| j.join(" ")),
|
||||
"#
|
||||
)
|
||||
};
|
||||
|
||||
|
||||
@@ -94,10 +94,11 @@ impl Expansions {
|
||||
let mut indexes = vec![(root_table.schema.columns.len(), root_table.clone())];
|
||||
|
||||
for (idx, col_name) in expand.iter().enumerate() {
|
||||
if col_name.as_ref().is_empty() {
|
||||
let col_name = col_name.as_ref();
|
||||
if col_name.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let Some((column, _col_metadata)) = root_table.column_by_name(col_name.as_ref()) else {
|
||||
let Some((column, _col_metadata)) = root_table.column_by_name(col_name) else {
|
||||
return Err(RecordError::ApiRequiresTable);
|
||||
};
|
||||
|
||||
@@ -126,13 +127,11 @@ impl Expansions {
|
||||
// TODO: Check that `referred_columns` and foreign_pk_column are the same. It's already
|
||||
// validated as part of config validation.
|
||||
|
||||
joins.push(format!(
|
||||
r#"LEFT JOIN "{foreign_table_name}" AS F{idx} ON {col_name} = F{idx}.{foreign_pk_column}"#,
|
||||
col_name = prefix.map_or_else(
|
||||
|| col_name.as_ref().to_owned(),
|
||||
|p| format!("{p}.{}", col_name.as_ref())
|
||||
),
|
||||
));
|
||||
joins.push(if let Some(ref prefix) = prefix {
|
||||
format!(r#"LEFT JOIN "{foreign_table_name}" AS F{idx} ON {prefix}."{col_name}" = F{idx}."{foreign_pk_column}""#)
|
||||
} else {
|
||||
format!(r#"LEFT JOIN "{foreign_table_name}" AS F{idx} ON "{col_name}" = F{idx}."{foreign_pk_column}""#)
|
||||
});
|
||||
indexes.push((foreign_table.schema.columns.len(), foreign_table));
|
||||
}
|
||||
|
||||
@@ -180,8 +179,13 @@ impl SelectQueryBuilder {
|
||||
expand: &[&str],
|
||||
) -> Result<Vec<(Arc<TableMetadata>, trailbase_sqlite::Row)>, RecordError> {
|
||||
let table_metadata = state.table_metadata();
|
||||
let Expansions { indexes, joins, .. } =
|
||||
Expansions::build(table_metadata, table_name, expand, None)?;
|
||||
let Expansions {
|
||||
indexes,
|
||||
joins,
|
||||
selects,
|
||||
} = Expansions::build(table_metadata, table_name, expand, None)?;
|
||||
|
||||
assert!(selects.is_none());
|
||||
|
||||
let sql = format!(
|
||||
r#"SELECT * FROM "{table_name}" AS R {} WHERE R.{pk_column} = $1"#,
|
||||
|
||||
Reference in New Issue
Block a user