mirror of
https://github.com/nushell/nushell
synced 2024-12-27 05:23:11 +00:00
fix: now sqlite insert handles column names as expected (#12039)
- fixes #11429 - fixes #12011
This commit is contained in:
parent
ce116b5d5f
commit
93188b3eda
1 changed files with 35 additions and 34 deletions
|
@ -201,18 +201,25 @@ 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)?;
|
||||
|
||||
// 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 tx = table.try_init(val)?;
|
||||
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 +232,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()
|
||||
|
@ -254,6 +252,9 @@ fn insert_in_transaction(
|
|||
inner: Vec::new(),
|
||||
})?;
|
||||
|
||||
result
|
||||
})?;
|
||||
|
||||
Ok(Value::nothing(span))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue