Skip to content

Commit

Permalink
fix(postgres): treat lowercase e as valid e-string prefix (#14733)
Browse files Browse the repository at this point in the history
  • Loading branch information
ephys committed Jul 9, 2022
1 parent beb4eca commit 7a15a4d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/utils/sql.ts
Expand Up @@ -114,8 +114,8 @@ function mapBindParametersAndReplacements(
// checking if this is a postgres-style E-prefixed string, which also supports backslash escaping
|| (
dialect.supports.escapeStringConstants
// is this a E-prefixed string, such as `E'abc'` ?
&& sqlString[i - 1] === 'E'
// is this a E-prefixed string, such as `E'abc'`, `e'abc'` ?
&& (sqlString[i - 1] === 'E' || sqlString[i - 1] === 'e')
// reject things such as `AE'abc'` (the prefix must be exactly E)
&& canPrecedeNewToken(sqlString[i - 2])
);
Expand Down
16 changes: 16 additions & 0 deletions test/unit/utils/sql.test.ts
Expand Up @@ -222,6 +222,22 @@ SELECT * FROM users WHERE id = E'\\' $id' OR id = $id`),
});
});

it('treats strings prefixed with a lowercase e as E-prefixed strings too', () => {
expectPerDialect(() => mapBindParameters(`SELECT * FROM users WHERE id = e'\\' $id' OR id = $id`, dialect), {
default: new Error(`The following SQL query includes an unterminated string literal:
SELECT * FROM users WHERE id = e'\\' $id' OR id = $id`),

'mysql mariadb': toHaveProperties({
sql: toMatchSql(`SELECT * FROM users WHERE id = e'\\' $id' OR id = ?`),
bindOrder: ['id'],
}),
postgres: toHaveProperties({
sql: `SELECT * FROM users WHERE id = e'\\' $id' OR id = $1`,
bindOrder: ['id'],
}),
});
});

it('considers the token to be a bind parameter if it is outside a string ending with an escaped backslash', () => {
const { sql, bindOrder } = mapBindParameters(`SELECT * FROM users WHERE id = '\\\\' OR id = $id`, dialect);

Expand Down

0 comments on commit 7a15a4d

Please sign in to comment.