Skip to content

Fix Clone() in Query.#748

Closed
cemahseri wants to merge 2 commits intosqlkata:mainfrom
cemahseri:main
Closed

Fix Clone() in Query.#748
cemahseri wants to merge 2 commits intosqlkata:mainfrom
cemahseri:main

Conversation

@cemahseri
Copy link
Copy Markdown

Fixes: #747

Copy link
Copy Markdown

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 the Clone() method in the Query class to perform deep cloning instead of shallow cloning, addressing issue #747. The fix ensures that nested objects like Parent queries and Includes are properly cloned rather than sharing references.

  • Implements deep cloning for Parent query references
  • Adds proper cloning for Includes collection with new Clone() method
  • Creates independent copy of Variables dictionary

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
QueryBuilder/Query.cs Updates Clone() method to deep clone Parent, Includes, and Variables properties
QueryBuilder/Include.cs Adds new Clone() method to enable deep cloning of Include objects

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@ahmad-moussawi
Copy link
Copy Markdown
Contributor

@cemahseri the PR is failing to build

@ahmad-moussawi ahmad-moussawi force-pushed the main branch 2 times, most recently from c3ae82e to 981f444 Compare September 27, 2025 14:48
@cemahseri
Copy link
Copy Markdown
Author

copilot is high on k2/spice, don't trust it. the pr builds just fine

1>------ Build started: Project: QueryBuilder, Configuration: Release Any CPU ------
1>QueryBuilder -> C:\Windows\System32\querybuilder\QueryBuilder\bin\Release\net8.0\SqlKata.Core.dll
2>------ Build started: Project: SqlKata.Execution, Configuration: Release Any CPU ------
2>SqlKata.Execution -> C:\Windows\System32\querybuilder\SqlKata.Execution\bin\Release\net8.0\SqlKata.Execution.dll
3>------ Build started: Project: QueryBuilder.Tests, Configuration: Release Any CPU ------
3>QueryBuilder.Tests -> C:\Windows\System32\querybuilder\QueryBuilder.Tests\bin\Release\net8.0\QueryBuilder.Tests.dll
========== Build: 3 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

@ahmad-moussawi
Copy link
Copy Markdown
Contributor

ahmad-moussawi commented Oct 1, 2025

copilot is high on k2/spice, don't trust it. the pr builds just fine

1>------ Build started: Project: QueryBuilder, Configuration: Release Any CPU ------
1>QueryBuilder -> C:\Windows\System32\querybuilder\QueryBuilder\bin\Release\net8.0\SqlKata.Core.dll
2>------ Build started: Project: SqlKata.Execution, Configuration: Release Any CPU ------
2>SqlKata.Execution -> C:\Windows\System32\querybuilder\SqlKata.Execution\bin\Release\net8.0\SqlKata.Execution.dll
3>------ Build started: Project: QueryBuilder.Tests, Configuration: Release Any CPU ------
3>QueryBuilder.Tests -> C:\Windows\System32\querybuilder\QueryBuilder.Tests\bin\Release\net8.0\QueryBuilder.Tests.dll
========== Build: 3 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

I was trying to build it locally but maybe I was missing something, I will try again tonight

@ahmad-moussawi
Copy link
Copy Markdown
Contributor

There was a recent PR inspired by this one that addresses this issue with a similar one related to the XQuery (coauthored with copilot)
so I will close this one now.

Thanks @cemahseri for the contribution

@cemahseri
Copy link
Copy Markdown
Author

cemahseri commented Apr 10, 2026

you are welcome!

so the ai did basically what i did, but slightly worse. but hey, it added the unit test!
the problem with current fix is, the parent is still not shallow copied;

clone.Parent = Parent;

it means that if i clone a query and make changes to the parent query of the copied query, the original query's parent query will also be changed

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.

Clone() method in Query class doesn't Clone deeply enough.

3 participants