Skip to content

Conversation

idseefeld
Copy link
Contributor

Prerequisites

All test have to be successful.
Find details in issue #19954

@Copilot Copilot AI review requested due to automatic review settings August 20, 2025 19:01
Copy link

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:

  • It's clear what problem this is solving, there's a connected issue or a description of what the changes do and how to test them
  • The automated tests all pass (see "Checks" tab on this PR)
  • The level of security for this contribution is the same or improved
  • The level of performance for this contribution is the same or improved
  • Avoids creating breaking changes; note that behavioral changes might also be perceived as breaking
  • If this is a new feature, Umbraco HQ provided guidance on the implementation beforehand
  • 💡 The contribution looks original and the contributor is presumably allowed to share it

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 🤖 🙂

Copy link
Contributor

@Copilot Copilot AI left a 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

@idseefeld idseefeld changed the title fix sql syntax issues fix sql syntax inn DatabaseCacheRepository Aug 20, 2025
@idseefeld idseefeld changed the title fix sql syntax inn DatabaseCacheRepository fix sql syntax in DatabaseCacheRepository Aug 20, 2025
Copy link
Contributor

@AndyButland AndyButland left a 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.

Sql<ISqlContext> sql = Sql()
.Delete<ContentNuDto>()
.Where<ContentNuDto>(x => x.NodeId == id);
_ = await Database.ExecuteAsync(sql);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
_ = await Database.ExecuteAsync(sql);
await Database.ExecuteAsync(sql);

Minor nit-pick just for consistency: we tend not use the discard for these types of method calls where don't do anything with the returned value.

Sql<ISqlContext> sql = Sql()
.Delete<ContentNuDto>()
.Where<ContentNuDto>(x => x.NodeId == contentCacheNode.Id && x.Published);
_ = await Database.ExecuteAsync(sql);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
_ = await Database.ExecuteAsync(sql);
await Database.ExecuteAsync(sql);

Comment on lines +148 to +150
.Append(SqlObjectTypeNotTrashed(SqlContext, Constants.ObjectTypes.Document))
.Append(SqlWhereNodeKey(SqlContext, key))
.Append(SqlOrderByLevelIdSortOrder(SqlContext));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
.Append(SqlObjectTypeNotTrashed(SqlContext, Constants.ObjectTypes.Document))
.Append(SqlWhereNodeKey(SqlContext, key))
.Append(SqlOrderByLevelIdSortOrder(SqlContext));
.Append(SqlObjectTypeNotTrashed(SqlContext, Constants.ObjectTypes.Document))
.Append(SqlWhereNodeKey(SqlContext, key))
.Append(SqlOrderByLevelIdSortOrder(SqlContext));

Would prefer to keep the indentation here.

AND {Constants.DatabaseSchema.Tables.Content}.contentTypeId IN (@ctypes)
)",
new { objType = nodeObjectType, ctypes = contentTypeIds });
private void RemoveByObjectType(Guid objectType, IReadOnlyCollection<int>? contentTypeIds)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
private void RemoveByObjectType(Guid objectType, IReadOnlyCollection<int>? contentTypeIds)
private void RemoveByObjectType(Guid objectType, IReadOnlyCollection<int> contentTypeIds)

We have early returns for null, so can update this parameter.

{
// remove all - if anything fails the transaction will rollback
Sql<ISqlContext> sql;
if (contentTypeIds is null || contentTypeIds.Count == 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (contentTypeIds is null || contentTypeIds.Count == 0)
if (contentTypeIds.Count == 0)

.Where<NodeDto, ContentDto>((n, c) =>
n.NodeObjectType == objectType
&& contentTypeIds.Contains(c.ContentTypeId)));
_ = Database.Execute(sql);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
_ = Database.Execute(sql);
Database.Execute(sql);

Comment on lines +450 to +451
.Where<NodeDto>(n => n.NodeObjectType == objectType));
_ = Database.Execute(sql);
Copy link
Contributor

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.

Comment on lines +467 to +468
n.NodeObjectType == objectType
&& contentTypeIds.Contains(c.ContentTypeId)));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
n.NodeObjectType == objectType
&& contentTypeIds.Contains(c.ContentTypeId)));
n.NodeObjectType == objectType &&
contentTypeIds.Contains(c.ContentTypeId)));

Nit-pick: I just more typically see code with && and || laid out this way.

/// Rebuilds the content database cache for media.
/// </summary>
/// <remarks>
/// Rebuilds the media database cache by clearing and repopulating the cache with the latest media data.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a nice comment added, but if we are including, could we add the similar on RebuildContentDbCache and RebuildMemberDbCache for consistency please?

Comment on lines +290 to +306
dto.RawData ??= [];
dto.Rv++;
ContentNuDto? existing = await Database.FirstOrDefaultAsync<ContentNuDto>(
Sql()
.SelectAll()
.From<ContentNuDto>()
.Where<ContentNuDto>(x => x.NodeId == dto.NodeId && x.Published == dto.Published));
if (existing is null)
{
_ = await Database.InsertAsync(dto);
}
else
{
Sql<ISqlContext> updateSql = Sql().Update<ContentNuDto>(u => u.Set(d => d.Data, dto.Data).Set(rd => rd.RawData, dto.RawData).Set(v => v.Rv, dto.Rv))
.Where<ContentNuDto>(x => x.NodeId == dto.NodeId && x.Published == dto.Published);
_ = await Database.ExecuteAsync(updateSql);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not really too keen on this update, as it by-passes the helper we have for InsertOrUpdateAsync.

Firstly, why is is necessary to do this and not just use the helper? I assume you are looking to replace the SQL statement for the SET, but this looks to be standard SQL - is it a problem for particular database providers?

If it is necessary to change, could you look to amend the InsertOrUpdateAsync helper itself? That way we'd fix all instances of it's usage, and not have to apply the more complicated code you've added here in potentially other places. Perhaps an overload taking a Sql parameter instead of a string for the SET statement could work.

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

Successfully merging this pull request may close these issues.

2 participants