From 67ff5c4ae13b766d71878c4595b75054d92d3878 Mon Sep 17 00:00:00 2001 From: Kingsley Yung Date: Tue, 18 Feb 2025 23:26:56 +0800 Subject: [PATCH] Fix invalid text columns generated by dump In the INSERT statement generated by dump function, if the type affinity of the value is TEXT, replace each single quotation mark with two single quotation marks, and wrap it with single quotation marks. --- cli/app.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cli/app.rs b/cli/app.rs index 5a93da77f..eb82958c0 100644 --- a/cli/app.rs +++ b/cli/app.rs @@ -273,12 +273,15 @@ impl<'a> Limbo<'a> { fn dump_table(&mut self, name: &str) -> Result<(), LimboError> { let query = format!("pragma table_info={}", name); let mut cols = vec![]; + let mut value_types = vec![]; query_internal!( self, query, |row: &limbo_core::Row| -> Result<(), LimboError> { let name: &str = row.get::<&str>(1)?; cols.push(name.to_string()); + let value_type: &str = row.get::<&str>(2)?; + value_types.push(value_type.to_string()); Ok(()) } )?; @@ -294,7 +297,20 @@ impl<'a> Limbo<'a> { let values = row .get_values() .into_iter() - .map(|x| x.to_string()) + .zip(value_types.iter()) + .map(|(value, value_type)| { + // If the type affinity is TEXT, replace each single + // quotation mark with two single quotation marks, and + // wrap it with single quotation marks. + if value_type.contains("CHAR") + || value_type.contains("CLOB") + || value_type.contains("TEXT") + { + format!("'{}'", value.to_string().replace("'", "''")) + } else { + value.to_string() + } + }) .collect::>() .join(","); let _ = self.write_fmt(format_args!("INSERT INTO {} VALUES({});", name, values))?;