Skip to content

Commit

Permalink
Merge pull request #130 from blackbeam/quoted-identifier
Browse files Browse the repository at this point in the history
Respect quoted identifiers in `ParsedNamedParams::parse` (fix #129)
  • Loading branch information
blackbeam authored Apr 1, 2024
2 parents 037d4f5 + 234bf43 commit c5b7918
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/named_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum ParserState {
MaybeInCComment2,
InCComment,
MaybeExitCComment,
InQuotedIdentifier,
}

use self::ParserState::*;
Expand Down Expand Up @@ -61,6 +62,7 @@ impl<'a> ParsedNamedParams<'a> {
b'\'' => state = InStringLiteral(b'\'', b'\''),
b'"' => state = InStringLiteral(b'"', b'"'),
b'?' => have_positional = true,
b'`' => state = InQuotedIdentifier,
_ => (),
},
InStringLiteral(separator, prev_char) => match c {
Expand Down Expand Up @@ -119,6 +121,11 @@ impl<'a> ParsedNamedParams<'a> {
b'/' => state = TopLevel,
_ => state = InCComment,
},
InQuotedIdentifier => {
if *c == b'`' {
state = TopLevel
}
}
}
if rematch {
match c {
Expand Down Expand Up @@ -253,6 +260,17 @@ mod test {
);
}

#[test]
fn quoted_identifier() {
let result = ParsedNamedParams::parse(b"INSERT INTO `my:table` VALUES (?)").unwrap();
assert_eq!(result.query(), b"INSERT INTO `my:table` VALUES (?)");
assert!(result.params().is_empty());

let result = ParsedNamedParams::parse(b"INSERT INTO `my:table` VALUES (:foo)").unwrap();
assert_eq!(result.query(), b"INSERT INTO `my:table` VALUES (?)");
assert_eq!(result.params(), cows!(b"foo"));
}

#[cfg(feature = "nightly")]
mod bench {
use super::*;
Expand Down

0 comments on commit c5b7918

Please sign in to comment.