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

Handle parsing of quoted identifiers #39

Merged
merged 6 commits into from
May 28, 2024
Merged

Handle parsing of quoted identifiers #39

merged 6 commits into from
May 28, 2024

Conversation

crisptrutski
Copy link
Contributor

@crisptrutski crisptrutski commented May 24, 2024

Refs #34

Before this change, inconsistent use of quotes could cause issues with reading and rewriting the identifiers within SQL strings. With this change they will be deduplicated when scanning, and matched when replacing.

In keeping with the dialect agnosticism of the JSQLParser grammar, we simply handle the two most common quotes for now. Many dialects event support both of them. In future we may allow the set of allowed quotes to be passed in as an optional parameter.

The next quote I'd like to support is [, which is interesting for two reasons - 1. the closing brace is different, and 2. the aforementioned grammar does not currently support them (🙀). We could work around this by find-replacing them with supported braces, tracking their original positions with entries in replacements, or we might want to fix the grammar. It's a pity perhaps that the parser itself doesn't treat quotes as a first-class AST citizen, that might also simplify things. The replacement technique however would extend nicely to 3rd party drivers requiring further quotes.

There's some overlap with our case insensitivity logic, as some databases are case sensitive for quoted identifiers only. This creates the need for another optional flag. We should probably document all these public flags we're accumulating.

I decided to not bother with implicit re-quoting where necessary - for now we can leave it up to library users to quote their replacement values where necessary. This way we can remain more database agnostic for now.

(when-let [schema-rename (find schema-renames (.getSchemaName t))]
(vswap! updated-nodes conj [(.getSchemaName t) schema-rename])
(.setSchemaName t (val schema-rename))))
(let [schema-name (collect/normalize-reference (.getSchemaName t) _ctx)]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A miss from the previous PR, but I don't think it's worth cherry-picking.

@crisptrutski crisptrutski self-assigned this May 24, 2024
@crisptrutski crisptrutski requested a review from a team May 24, 2024 14:50
Base automatically changed from case-insensitive to master May 27, 2024 09:18
src/macaw/rewrite.clj Outdated Show resolved Hide resolved
Copy link
Contributor

@piranha piranha left a comment

Choose a reason for hiding this comment

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

Nice!

@crisptrutski crisptrutski merged commit cf4614e into master May 28, 2024
4 checks passed
@crisptrutski crisptrutski deleted the handle-quotes branch May 28, 2024 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants