fix: now sqlite insert handles column names as expected (#12039)

- fixes #11429
- fixes #12011
This commit is contained in:
Patryk Nowacki 2024-03-07 22:51:45 +01:00 committed by GitHub
parent ce116b5d5f
commit 93188b3eda
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -201,29 +201,8 @@ fn insert_in_transaction(
span: Span,
mut table: Table,
) -> Result<Value, ShellError> {
let mut stream = stream.peekable();
let first_val = match stream.peek() {
None => return Ok(Value::nothing(span)),
Some(val) => val.as_record()?,
};
let mut stream = stream;
let table_name = table.name().clone();
let tx = table.try_init(first_val)?;
let insert_statement = format!(
"INSERT INTO [{}] VALUES ({})",
table_name,
["?"].repeat(first_val.values().len()).join(", ")
);
let mut insert_statement =
tx.prepare(&insert_statement)
.map_err(|e| ShellError::GenericError {
error: "Failed to prepare SQLite statement".into(),
msg: e.to_string(),
span: None,
help: None,
inner: Vec::new(),
})?;
// insert all the records
stream.try_for_each(|stream_value| {
@ -233,25 +212,47 @@ fn insert_in_transaction(
}
}
insert_value(stream_value, &mut insert_statement)
})?;
let val = stream_value.as_record()?;
insert_statement
.finalize()
.map_err(|e| ShellError::GenericError {
error: "Failed to finalize SQLite prepared statement".into(),
let tx = table.try_init(val)?;
let insert_statement = format!(
"INSERT INTO [{}] ({}) VALUES ({})",
table_name,
val.cols.join(", "),
["?"].repeat(val.values().len()).join(", ")
);
let mut insert_statement =
tx.prepare(&insert_statement)
.map_err(|e| ShellError::GenericError {
error: "Failed to prepare SQLite statement".into(),
msg: e.to_string(),
span: None,
help: None,
inner: Vec::new(),
})?;
let result = insert_value(stream_value, &mut insert_statement);
insert_statement
.finalize()
.map_err(|e| ShellError::GenericError {
error: "Failed to finalize SQLite prepared statement".into(),
msg: e.to_string(),
span: None,
help: None,
inner: Vec::new(),
})?;
tx.commit().map_err(|e| ShellError::GenericError {
error: "Failed to commit SQLite transaction".into(),
msg: e.to_string(),
span: None,
help: None,
inner: Vec::new(),
})?;
tx.commit().map_err(|e| ShellError::GenericError {
error: "Failed to commit SQLite transaction".into(),
msg: e.to_string(),
span: None,
help: None,
inner: Vec::new(),
result
})?;
Ok(Value::nothing(span))