From 3ae6fe2114ac2d0b8f70a99017a67da104c3c2e2 Mon Sep 17 00:00:00 2001 From: Maxime Jacob Date: Sat, 4 May 2024 09:12:44 -0400 Subject: [PATCH] Enable columns with spaces for into_sqlite by adding quotes to column names (#12759) # Description Spaces were causing an issue with into_sqlite when they appeared in column names. This is because the column names were not properly wrapped with backticks that allow sqlite to properly interpret the column. The issue has been addressed by adding backticks to the column names of into sqlite. The output of the column names when using open is unchanged, and the column names appear without backticks as expected. fixes #12700 # User-Facing Changes N/A # Tests + Formatting Formatting has been respected. Repro steps from the issue have been done, and ran multiple times. New values get added to the correct columns as expected. --- .../nu-command/src/database/commands/into_sqlite.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/nu-command/src/database/commands/into_sqlite.rs b/crates/nu-command/src/database/commands/into_sqlite.rs index 1b4d0b8073..7f61b6bc06 100644 --- a/crates/nu-command/src/database/commands/into_sqlite.rs +++ b/crates/nu-command/src/database/commands/into_sqlite.rs @@ -269,7 +269,8 @@ fn insert_in_transaction( let insert_statement = format!( "INSERT INTO [{}] ({}) VALUES ({})", table_name, - Itertools::intersperse(val.columns().map(String::as_str), ", ").collect::(), + Itertools::intersperse(val.columns().map(|c| format!("`{}`", c)), ", ".to_string()) + .collect::(), Itertools::intersperse(itertools::repeat_n("?", val.len()), ", ").collect::(), ); @@ -390,8 +391,12 @@ fn get_columns_with_sqlite_types( let mut columns: Vec<(String, &'static str)> = vec![]; for (c, v) in record { - if !columns.iter().any(|(name, _)| name == c) { - columns.push((c.clone(), nu_value_to_sqlite_type(v)?)); + if !columns + .iter() + .map(|name| (format!("`{}`", name.0), name.1)) + .any(|(name, _)| name == *c) + { + columns.push((format!("`{}`", c), nu_value_to_sqlite_type(v)?)); } }