Fix unexpected sqlite insert behaviour (attempt 2) (#12128)

- fixes #11429
- fixes #12011

Refers to: https://github.com/nushell/nushell/pull/12039

In general looks a bit faster now.
This commit is contained in:
Patryk Nowacki 2024-03-08 16:50:18 +01:00 committed by GitHub
parent 8822750048
commit a9ddc58f21
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -209,10 +209,22 @@ fn insert_in_transaction(
let table_name = table.name().clone();
let tx = table.try_init(first_val)?;
// insert all the records
stream.try_for_each(|stream_value| {
if let Some(ref ctrlc) = ctrlc {
if ctrlc.load(Ordering::Relaxed) {
return Err(ShellError::InterruptedByUser { span: None });
}
}
let val = stream_value.as_record()?;
let insert_statement = format!(
"INSERT INTO [{}] VALUES ({})",
"INSERT INTO [{}] ({}) VALUES ({})",
table_name,
["?"].repeat(first_val.values().len()).join(", ")
val.cols.join(", "),
["?"].repeat(val.values().len()).join(", ")
);
let mut insert_statement =
@ -225,16 +237,7 @@ fn insert_in_transaction(
inner: Vec::new(),
})?;
// insert all the records
stream.try_for_each(|stream_value| {
if let Some(ref ctrlc) = ctrlc {
if ctrlc.load(Ordering::Relaxed) {
return Err(ShellError::InterruptedByUser { span: None });
}
}
insert_value(stream_value, &mut insert_statement)
})?;
let result = insert_value(stream_value, &mut insert_statement);
insert_statement
.finalize()
@ -246,6 +249,9 @@ fn insert_in_transaction(
inner: Vec::new(),
})?;
result
})?;
tx.commit().map_err(|e| ShellError::GenericError {
error: "Failed to commit SQLite transaction".into(),
msg: e.to_string(),