Ensure double quote characters are always escaped properly

Closes #1488
This commit is contained in:
Tobie Morgan Hitchcock 2022-11-27 11:43:16 +00:00
parent 8b5a99c321
commit 77b24cda26
2 changed files with 41 additions and 4 deletions

View file

@ -15,11 +15,11 @@ const BACKTICK: char = '`';
const BACKTICK_ESC: &str = r#"\`"#;
#[inline]
pub fn escape_str(s: &str) -> String {
pub fn escape_str(s: &str) -> Cow<'_, str> {
if s.contains(SINGLE) {
format!("{}{}{}", DOUBLE, s, DOUBLE)
escape_normal(s, DOUBLE, DOUBLE, DOUBLE_ESC)
} else {
format!("{}{}{}", SINGLE, s, SINGLE)
Cow::Owned(format!("{}{}{}", SINGLE, s, SINGLE))
}
}

View file

@ -6,7 +6,7 @@ use surrealdb::Error;
use surrealdb::Session;
#[tokio::test]
async fn complex_string() -> Result<(), Error> {
async fn complex_ids() -> Result<(), Error> {
let sql = r#"
CREATE person:100 SET test = 'One';
CREATE person:00100;
@ -84,3 +84,40 @@ async fn complex_string() -> Result<(), Error> {
//
Ok(())
}
#[tokio::test]
async fn complex_strings() -> Result<(), Error> {
let sql = r#"
RETURN 'String with no complex characters';
RETURN 'String with some "double quoted" characters';
RETURN 'String with some \'escaped single quoted\' characters';
RETURN "String with some \"escaped double quoted\" characters";
RETURN "String with some 'single' and \"double\" quoted characters";
"#;
let dbs = Datastore::new("memory").await?;
let ses = Session::for_kv().with_ns("test").with_db("test");
let res = &mut dbs.execute(&sql, &ses, None, false).await?;
assert_eq!(res.len(), 5);
//
let tmp = res.remove(0).result?;
let val = Value::parse(r#"'String with no complex characters'"#);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse(r#"'String with some "double quoted" characters'"#);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse(r#""String with some 'escaped single quoted' characters""#);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse(r#"'String with some "escaped double quoted" characters'"#);
assert_eq!(tmp, val);
//
let tmp = res.remove(0).result?;
let val = Value::parse(r#""String with some 'single' and \"double\" quoted characters""#);
assert_eq!(tmp, val);
//
Ok(())
}