SQL Builder - statement builder#1191
Open
michalkrzyz wants to merge 8 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors MariaDB query construction by introducing a shared statement builder and a new Order helper type, then migrates multiple query builders to use these abstractions for consistent join/filter/cursor handling.
Changes:
- Added generic
BuildStatement(...)+Statementconfiguration to centralize SQL assembly (joins, WHERE/HAVING cursor clauses, ORDER BY, parameters). - Replaced
[]entity.Orderstring-building helpers with a dedicatedOrdertype (String(),By*()helpers) and updated join-condition signatures accordingly. - Removed per-entity
ensure*Filter(...)helpers in favor of a genericEnsureFilter(...).
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| internal/database/mariadb/user.go | Migrates user statement construction to Statement/BuildStatement and EnsureFilter. |
| internal/database/mariadb/support_group.go | Migrates support group statement construction to Statement/BuildStatement and EnsureFilter. |
| internal/database/mariadb/service.go | Migrates service statement construction to Statement/BuildStatement, introduces Order usage in aggregation queries. |
| internal/database/mariadb/service_issue_variant.go | Migrates statement construction to Statement/BuildStatement; adjusts base query placeholders. |
| internal/database/mariadb/scanner_run.go | Replaces local filter initialization with EnsureFilter. |
| internal/database/mariadb/remediation.go | Migrates remediation statement construction to Statement/BuildStatement; adjusts base query placeholders and EnsureFilter. |
| internal/database/mariadb/patch.go | Migrates patch statement construction to Statement/BuildStatement; adjusts base query placeholders and EnsureFilter. |
| internal/database/mariadb/order.go | Introduces Order type (string formatting + helper predicates like ByCount). |
| internal/database/mariadb/mv_vulnerabilities.go | Replaces issue filter initialization with EnsureFilter. |
| internal/database/mariadb/issue.go | Refactors issue statement building into Statement/BuildStatement; updates join conditions to use *Order. |
| internal/database/mariadb/issue_variant.go | Migrates issue-variant statement construction to Statement/BuildStatement and EnsureFilter. |
| internal/database/mariadb/issue_repository.go | Migrates issue-repository statement construction to Statement/BuildStatement and EnsureFilter. |
| internal/database/mariadb/issue_match.go | Migrates issue-match statement construction to Statement/BuildStatement; updates column handling and base query formatting. |
| internal/database/mariadb/db_object.go | Adds shared statement builder, EnsureFilter, IsNil, and updates join planning/cursor query helpers to use *Order. |
| internal/database/mariadb/db_object_test.go | Updates join condition tests to the new *Order signature. |
| internal/database/mariadb/component.go | Migrates component statement construction to Statement/BuildStatement; updates join conditions/order predicates. |
| internal/database/mariadb/component_version.go | Migrates component-version statement construction to Statement/BuildStatement; updates join conditions/order predicates. |
| internal/database/mariadb/component_instance.go | Migrates component-instance statement construction to Statement/BuildStatement; updates join conditions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+184
to
+191
| } else { | ||
| if filter != nil { | ||
| if do.GetFilterQuery(filter) != "" && withCursor && cursorQuery != "" { | ||
| cursorQuery = fmt.Sprintf(" AND (%s)", cursorQuery) | ||
| } | ||
| } else { | ||
| panic("hasFilter for not aggregated query has to be passed (has to be not nil).") | ||
| } |
Comment on lines
+217
to
+228
| func BuildStatement[ | ||
| T any, | ||
| PT interface { | ||
| *T | ||
| entity.HasPagination | ||
| }, | ||
| ](s Statement, filter PT) (Stmt, []any, error) { | ||
| filter = EnsureFilter(filter) | ||
| s.L.WithFields(logrus.Fields{"filter": filter}) | ||
|
|
||
| joins := s.Obj.GetJoins(filter, s.Order) | ||
| whereClause := s.Obj.GetFilterWhereClause(filter, s.CheckCursorInWhere && s.WithCursor) |
- add Order in db layer - change some parameters from []entity.Order to *Order - move Order functions to new type On-behalf-of: SAP Michal Krzyz <michal.krzyz@sap.com> Signed-off-by: Michal Krzyz <michalkrzyz@gmail.com>
On-behalf-of: SAP Michal Krzyz <michal.krzyz@sap.com> Signed-off-by: Michal Krzyz <michalkrzyz@gmail.com>
On-behalf-of: SAP Michal Krzyz <michal.krzyz@sap.com> Signed-off-by: Michal Krzyz <michalkrzyz@gmail.com>
On-behalf-of: SAP Michal Krzyz <michal.krzyz@sap.com> Signed-off-by: Michal Krzyz <michalkrzyz@gmail.com>
On-behalf-of: SAP Michal Krzyz <michal.krzyz@sap.com> Signed-off-by: Michal Krzyz <michalkrzyz@gmail.com>
On-behalf-of: SAP Michal Krzyz <michal.krzyz@sap.com> Signed-off-by: Michal Krzyz <michalkrzyz@gmail.com>
2428c9d to
44bb26c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change.
What type of PR is this? (check all applicable)
Related Tickets & Documents
Added tests?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
Added to documentation?
Checklist