Minor refactor in translate/insert

This commit is contained in:
PThorpe92
2025-09-16 16:18:51 -04:00
parent 8dd1cf11eb
commit 97c11898fe

View File

@@ -119,59 +119,53 @@ pub fn translate_insert(
let mut upsert_opt: Option<Upsert> = None;
let mut param_idx = 1;
let inserting_multiple_rows;
match &mut body {
InsertBody::Select(select, upsert) => {
match &mut select.body.select {
// TODO see how to avoid clone
OneSelect::Values(values_expr) if values_expr.len() <= 1 => {
if values_expr.is_empty() {
crate::bail_parse_error!("no values to insert");
}
for expr in values_expr.iter_mut().flat_map(|v| v.iter_mut()) {
match expr.as_mut() {
Expr::Id(name) => {
if name.is_double_quoted() {
*expr = Expr::Literal(ast::Literal::String(name.to_string()))
.into();
} else {
// an INSERT INTO ... VALUES (...) cannot reference columns
crate::bail_parse_error!("no such column: {name}");
}
}
Expr::Qualified(first_name, second_name) => {
let mut inserting_multiple_rows = false;
if let InsertBody::Select(select, upsert) = &mut body {
match &mut select.body.select {
// TODO see how to avoid clone
OneSelect::Values(values_expr) if values_expr.len() <= 1 => {
if values_expr.is_empty() {
crate::bail_parse_error!("no values to insert");
}
for expr in values_expr.iter_mut().flat_map(|v| v.iter_mut()) {
match expr.as_mut() {
Expr::Id(name) => {
if name.is_double_quoted() {
*expr =
Expr::Literal(ast::Literal::String(name.to_string())).into();
} else {
// an INSERT INTO ... VALUES (...) cannot reference columns
crate::bail_parse_error!(
"no such column: {first_name}.{second_name}"
);
crate::bail_parse_error!("no such column: {name}");
}
_ => {}
}
rewrite_expr(expr, &mut param_idx)?;
Expr::Qualified(first_name, second_name) => {
// an INSERT INTO ... VALUES (...) cannot reference columns
crate::bail_parse_error!("no such column: {first_name}.{second_name}");
}
_ => {}
}
values = values_expr.pop();
inserting_multiple_rows = false;
rewrite_expr(expr, &mut param_idx)?;
}
_ => inserting_multiple_rows = true,
values = values_expr.pop();
}
if let Some(ref mut upsert) = upsert {
if let UpsertDo::Set {
ref mut sets,
ref mut where_clause,
} = &mut upsert.do_clause
{
for set in sets.iter_mut() {
rewrite_expr(set.expr.as_mut(), &mut param_idx)?;
}
if let Some(ref mut where_expr) = where_clause {
rewrite_expr(where_expr.as_mut(), &mut param_idx)?;
}
}
}
upsert_opt = upsert.as_deref().cloned();
_ => inserting_multiple_rows = true,
}
InsertBody::DefaultValues => inserting_multiple_rows = false,
};
if let Some(ref mut upsert) = upsert {
if let UpsertDo::Set {
ref mut sets,
ref mut where_clause,
} = &mut upsert.do_clause
{
for set in sets.iter_mut() {
rewrite_expr(set.expr.as_mut(), &mut param_idx)?;
}
if let Some(ref mut where_expr) = where_clause {
rewrite_expr(where_expr.as_mut(), &mut param_idx)?;
}
}
}
upsert_opt = upsert.as_deref().cloned();
}
let halt_label = program.allocate_label();
let loop_start_label = program.allocate_label();