Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String ending with backslash breaks PostgreSQL value replacements #14693

Closed
3 of 6 tasks
tomquist opened this issue Jun 28, 2022 · 3 comments · Fixed by #14700
Closed
3 of 6 tasks

String ending with backslash breaks PostgreSQL value replacements #14693

tomquist opened this issue Jun 28, 2022 · 3 comments · Fixed by #14700
Assignees

Comments

@tomquist
Copy link

tomquist commented Jun 28, 2022

Using PostgreSQL dialect replacements don't work if query contains a string with a backslash at the end.

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • I have read the contribution guidelines

Bug Description

When the query contains a fixed string that ends with a backslash all placeholders after the string aren't replaced anymore.

Reproducible Example

it("properly replaced placeholders", async () => {
  const result = await sequelize.query<{ backslash: string; replaced: string }>(
    "select '\\' as backslash, :test as replaced",
    {
      replacements: { test: "replaced" },
      type: QueryTypes.SELECT,
    }
  );
  expect(result).toEqual([{ baskslash: "\\", replaced: "replaced" }]);
});

What do you expect to happen?

All placeholders get successfully replaced and the test above succeeds.

What is actually happening?

Placeholders after the string which ends with a backslash aren't replaced resulting into a syntax error syntax error at or near ":". The query that gets executed is this:

select '\' as backslash, :test as replaced

Environment

  • Sequelize version: 6.21.0
  • Node.js version: 16.14.2
  • If TypeScript related: TypeScript version: 4.7.4
  • Database & Version: PostgreSQL 11.5
  • Connector library & Version: pg 8.7.3

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I will need guidance.
  • No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

@ephys ephys self-assigned this Jun 28, 2022
@adsilva-ivre
Copy link

adsilva-ivre commented Jun 28, 2022

not sure if this is related, but when I do the following,

searchQuery = 'cms hello'

name: { [Op.iLike]: `%${searchQuery.toLowerCase()}%` }

the generated query looks like:

"name" ILIKE 'ms hello%' 

it skips the first character

@tomquist
Copy link
Author

tomquist commented Jun 28, 2022

Note that a backslash within a string in PostgreSQL is not interpreted in any way, e.g. this query works:

select '\' as backslash;

PostgreSQL supports C-style escapes only if the string is prefixed with an e:

select e'\\' as backslash;

https://www.postgresql.org/docs/current/sql-syntax-lexical.html

@github-actions
Copy link
Contributor

github-actions bot commented Aug 7, 2022

🎉 This issue has been resolved in version 7.0.0-alpha.15 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants