Skip to content

feat(sqlite): support ESCAPE in LIKE expressions#2496

Merged
taozhi8833998 merged 2 commits intotaozhi8833998:masterfrom
sgress454:sgress454/add-escape-to-sqlite-like
Jul 16, 2025
Merged

feat(sqlite): support ESCAPE in LIKE expressions#2496
taozhi8833998 merged 2 commits intotaozhi8833998:masterfrom
sgress454:sgress454/add-escape-to-sqlite-like

Conversation

@sgress454
Copy link
Copy Markdown
Contributor

This PR adds support for ESCAPE in SQLite LIKE expressions. This support is documented in the official SQLite docs.

ast = parser.astify(sql, { database: 'mariadb' })
expect(parser.sqlify(ast, DEFAULT_OPT)).to.be.equal('CREATE TABLE "Test" ("id" INT(11) NOT NULL, "name" VARCHAR(255) NOT NULL, PRIMARY KEY ("id"), UNIQUE ("name"))')
sql = `SELECT
sql = `SELECT
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is from the trim_trailing_whitespace editor rule, I can back it out if you prefer formatting fixes to be in separate PRs.

@taozhi8833998 taozhi8833998 added the enhancement New feature or request label Jul 16, 2025
@taozhi8833998 taozhi8833998 added this to the 5.3.11 milestone Jul 16, 2025
Copy link
Copy Markdown
Owner

@taozhi8833998 taozhi8833998 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@taozhi8833998 taozhi8833998 merged commit b0621fd into taozhi8833998:master Jul 16, 2025
1 check passed
@taozhi8833998
Copy link
Copy Markdown
Owner

taozhi8833998 commented Jul 16, 2025

@sgress454 Thanks for the pr, merged.

@sgress454
Copy link
Copy Markdown
Contributor Author

@taozhi8833998 any ETA on 5.3.11 release? 😉

sgress454 added a commit to fleetdm/fleet that referenced this pull request Jul 25, 2025
for #30109

# Details

This PR fixes an issue in our current SQL parsing library that was
causing queries like this to be marked invalid:

```
SELECT * FROM table_name WHERE column_name LIKE '\_%' ESCAPE '\'
```

This is valid in SQLite because the `\` is not considered an escape
character by default. From [the SQLite
docs](https://www.sqlite.org/lang_expr.html) (see section 3 "Literal
Values (Constants)"; emphasis mine):

> A string constant is formed by enclosing the string in single quotes
('). A single quote within the string can be encoded by putting two
single quotes in a row - as in Pascal. C-style escapes using the
backslash character are not supported because they are not standard SQL.

# Use of forked code

Part of the fix for this was [submitted as a PR to the node-sql-parser
library](taozhi8833998/node-sql-parser#2496) we
now use, and merged. I then found that another fix was needed, which I
submitted as [a separate
PR](taozhi8833998/node-sql-parser#2512). As
these fixes have yet to be made part of an official release of the
library, I made a fork off of the release we were using (5.3.10) and
bundled the necessary build artifacts with Fleet. We have an [ADR
proposing the use of submodules for this
purpose](#31079); I'm happy to
implement that instead if we approve that, although for a front-end
module with a build step it's a bit more complicated. Hopefully this
code will be released in `node-sql-parser` soon and we can revert back
to using the dependency.

Here is the [full set of
changes](taozhi8833998/node-sql-parser@master...sgress454:node-sql-parser:5.3.10-plus).

# Checklist for submitter

- [X] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
See [Changes
files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files)
for more information.
- [X] Manual QA for all new/changed functionality
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants