Skip to content

feat: [#646] The Where function of Orm supports func condition#986

Merged
almas-x merged 3 commits intomasterfrom
almas/#646
Apr 3, 2025
Merged

feat: [#646] The Where function of Orm supports func condition#986
almas-x merged 3 commits intomasterfrom
almas/#646

Conversation

@almas-x
Copy link
Contributor

@almas-x almas-x commented Apr 2, 2025

📑 Description

Closes goravel/goravel#646

var user models.User
facades.Orm().Query().Where(func(query orm.Query) orm.Query {
    return query.Where("name = ?", "where_user").OrWhere("name", "where_user1")
}).Where("avatar", "where_avatar").FirstOrFail(&user)

Print SQL

SELECT *
FROM "users"
WHERE (name = 'where_user' OR "name" = 'where_user1')
  AND "avatar" = 'where_avatar'
  AND "users"."deleted_at" IS NULL
ORDER BY "users"."id"
LIMIT 1;

✅ Checks

  • Added test cases for my code

Copilot AI review requested due to automatic review settings April 2, 2025 07:33
@almas-x almas-x requested a review from a team as a code owner April 2, 2025 07:33
Copy link
Contributor

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 adds support for passing a function as a condition to the Orm's Where query function while updating references from ormcontract to contractsorm.

  • Updated tests to assert the new function condition.
  • Added the buildSubquery helper in the query package to handle function conditions.
  • Updated import references in the conditions package.

Reviewed Changes

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

File Description
tests/query_test.go Added test case for the function condition in the Where clause
database/gorm/query.go Introduced buildSubquery and updated buildWhere logic to handle function conditions
database/gorm/conditions.go Updated import from ormcontract to contractsorm

@codecov
Copy link

codecov bot commented Apr 2, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 70.04%. Comparing base (9bc11ed) to head (77db541).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #986   +/-   ##
=======================================
  Coverage   70.04%   70.04%           
=======================================
  Files         168      168           
  Lines       11353    11353           
=======================================
  Hits         7952     7952           
  Misses       3050     3050           
  Partials      351      351           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

}

for _, item := range r.conditions.where {
if sub, ok := item.query.(func(contractsorm.Query) contractsorm.Query); ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can it support OrWhere(func(contractsorm.Query) contractsorm.Query)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is implemented through Gorm’s Group Conditions, so theoretically, OrWhere should be supported. However, since Gorm processes OR conditions based on their position, there might be some issues with how it’s handled.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 77db541 Previous: 9bc11ed Ratio
BenchmarkFile_ReadWrite 350170 ns/op 2072 B/op 28 allocs/op 227847 ns/op 2073 B/op 28 allocs/op 1.54
BenchmarkFile_ReadWrite - ns/op 350170 ns/op 227847 ns/op 1.54

This comment was automatically generated by workflow using github-action-benchmark.

return queryImpl.buildWhere(db)
}

return db
Copy link
Contributor

Choose a reason for hiding this comment

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

Can nil be returned here? Then continue at line 1231 if r.buildSubquery(sub) == nil.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, to prevent a panic when returning nil.

Copy link
Contributor

@hwbrzzl hwbrzzl Apr 2, 2025

Choose a reason for hiding this comment

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

Em, I mean we can return nil here, don't need to return an empty db.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, since the closure function returns an orm.Query interface, returning nil will still compile successfully. Therefore, it’s better to handle the nil case properly.

@almas-x almas-x requested a review from hwbrzzl April 3, 2025 09:15
Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

Nice

@almas-x almas-x merged commit 804d217 into master Apr 3, 2025
12 of 13 checks passed
@almas-x almas-x deleted the almas/#646 branch April 3, 2025 10:59
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.

The Where function of Orm supports func condition

3 participants