-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix sql syntax in DatabaseCacheRepository #19955
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
base: main
Are you sure you want to change the base?
fix sql syntax in DatabaseCacheRepository #19955
Conversation
Hi there @idseefeld, thank you for this contribution! 👍 While we wait for one of the Core Collaborators team to have a look at your work, we wanted to let you know about that we have a checklist for some of the things we will consider during review:
Don't worry if you got something wrong. We like to think of a pull request as the start of a conversation, we're happy to provide guidance on improving your contribution. If you realize that you might want to make some changes then you can do that by adding new commits to the branch you created for this work and pushing new commits. They should then automatically show up as updates to this pull request. Thanks, from your friendly Umbraco GitHub bot 🤖 🙂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes SQL syntax issues in the DatabaseCacheRepository.cs
file by replacing raw SQL strings with strongly-typed SQL builder methods and improving SQL syntax compatibility across different database providers.
Key Changes
- Replaced raw SQL DELETE statements with strongly-typed SQL builder pattern
- Updated column name quoting to use proper SQL syntax provider methods
- Consolidated duplicate deletion logic into a single reusable method
src/Umbraco.PublishedCache.HybridCache/Persistence/DatabaseCacheRepository.cs
Outdated
Show resolved
Hide resolved
src/Umbraco.PublishedCache.HybridCache/Persistence/DatabaseCacheRepository.cs
Outdated
Show resolved
Hide resolved
src/Umbraco.PublishedCache.HybridCache/Persistence/DatabaseCacheRepository.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this @idseefeld. I'm mostly happy with the clean-up apart from a few nit-picky comments. The main concern I have is the complexity introduced by by-passing the InsertOrUpdateAsync
method, so please see my inline comment on that.
src/Umbraco.PublishedCache.HybridCache/Persistence/DatabaseCacheRepository.cs
Outdated
Show resolved
Hide resolved
src/Umbraco.PublishedCache.HybridCache/Persistence/DatabaseCacheRepository.cs
Outdated
Show resolved
Hide resolved
src/Umbraco.PublishedCache.HybridCache/Persistence/DatabaseCacheRepository.cs
Outdated
Show resolved
Hide resolved
src/Umbraco.PublishedCache.HybridCache/Persistence/DatabaseCacheRepository.cs
Outdated
Show resolved
Hide resolved
src/Umbraco.PublishedCache.HybridCache/Persistence/DatabaseCacheRepository.cs
Outdated
Show resolved
Hide resolved
src/Umbraco.PublishedCache.HybridCache/Persistence/DatabaseCacheRepository.cs
Outdated
Show resolved
Hide resolved
.Where<NodeDto>(n => n.NodeObjectType == objectType)); | ||
_ = Database.Execute(sql); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to define a parameter in Where
and pass that parameter as the second argument to Execute
? Maybe it doesn't really matter, so just flagging for you to consider. If you do update, there are further instances of this in the proposed changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No I don't agree. I think this is the proper way to avoid raw partial sql statments.
Previously the raw sql was:
DELETE FROM cmsContentNu
WHERE cmsContentNu.nodeId IN (
SELECT id FROM umbracoNode
JOIN {Constants.DatabaseSchema.Tables.Content} ON {Constants.DatabaseSchema.Tables.Content}.nodeId=umbracoNode.id
WHERE umbracoNode.nodeObjectType = @objType
AND {Constants.DatabaseSchema.Tables.Content}.contentTypeId IN (@ctypes)
which had to be extended with the SqlSyntax.GetQuoted...
methods like:
DELETE FROM {SqlSyntax.GetQuotedTableName("cmsContentNu")}
WHERE {SqlSyntax.GetQuotedTableName("cmsContentNu")}.{SqlSyntax.GetQuotedColumnName("nodeId")} IN (
SELECT id FROM {SqlSyntax.GetQuotedTableName("umbracoNode")}
JOIN {SqlSyntax.GetQuotedTableName(Constants.DatabaseSchema.Tables.Content)} ON {SqlSyntax.GetQuotedTableName(Constants.DatabaseSchema.Tables.Content)}.{SqlSyntax.GetQuotedColumnName("nodeId")}={SqlSyntax.GetQuotedTableName("umbracoNode")}.id
WHERE {SqlSyntax.GetQuotedTableName("umbracoNode")}.{SqlSyntax.GetQuotedColumnName("nodeObjectType")} = @objType
AND {SqlSyntax.GetQuotedTableName(Constants.DatabaseSchema.Tables.Content)}.{SqlSyntax.GetQuotedColumnName("contentTypeId")} IN (@ctypes)
Actually I haven't found a solution for mixing Sql<ISqlContext>()
objects with named parameters / arguments.
Somewhere I have seen methods for this kind of subqueries to build a sql string snippet where the actual parameters where replaced by arguments in an Excecute
call. In that approach I have indeed extended the raw sql, because I could not think of another solution without extensive refactoring.
Prerequisites
All test have to be successful.
Find details in issue #19954