From 89b3fb92aa97aaeac12a0e4133ecea1ba63d2c73 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Thu, 7 Mar 2024 21:15:46 -0600 Subject: [PATCH] Revert "fix: now sqlite insert handles column names as expected" (#12112) Reverts nushell/nushell#12039 --- .../src/database/commands/into_sqlite.rs | 69 +++++++++---------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/crates/nu-command/src/database/commands/into_sqlite.rs b/crates/nu-command/src/database/commands/into_sqlite.rs index 81d86f0927..924508ef60 100644 --- a/crates/nu-command/src/database/commands/into_sqlite.rs +++ b/crates/nu-command/src/database/commands/into_sqlite.rs @@ -201,8 +201,29 @@ fn insert_in_transaction( span: Span, mut table: Table, ) -> Result { - let mut stream = stream; + let mut stream = stream.peekable(); + let first_val = match stream.peek() { + None => return Ok(Value::nothing(span)), + Some(val) => val.as_record()?, + }; + 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| { @@ -212,47 +233,25 @@ fn insert_in_transaction( } } - let val = stream_value.as_record()?; + insert_value(stream_value, &mut insert_statement) + })?; - 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(), + 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(), })?; - result + 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(), })?; Ok(Value::nothing(span))