Skip to content

(WIP) Implement Generics API #7424

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

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion callbacks/preload.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ func preload(tx *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
column, values := schema.ToQueryValues(clause.CurrentTable, relForeignKeys, foreignValues)

if len(values) != 0 {
tx = tx.Model(reflectResults.Addr().Interface()).Where(clause.IN{Column: column, Values: values})

for _, cond := range conds {
if fc, ok := cond.(func(*gorm.DB) *gorm.DB); ok {
tx = fc(tx)
Expand All @@ -283,7 +285,11 @@ func preload(tx *gorm.DB, rel *schema.Relationship, conds []interface{}, preload
}
}

if err := tx.Where(clause.IN{Column: column, Values: values}).Find(reflectResults.Addr().Interface(), inlineConds...).Error; err != nil {
if len(inlineConds) > 0 {
tx = tx.Where(inlineConds[0], inlineConds[1:]...)
}

if err := tx.Find(reflectResults.Addr().Interface()).Error; err != nil {
return err
}
}
Expand Down
17 changes: 14 additions & 3 deletions callbacks/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,13 @@ func BuildQuerySQL(db *gorm.DB) {

if isRelations {
genJoinClause := func(joinType clause.JoinType, parentTableName string, relation *schema.Relationship) clause.Join {
Copy link
Preview

Copilot AI May 21, 2025

Choose a reason for hiding this comment

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

The variable 'join' is not defined in the closure; consider using the appropriate parameter or property (e.g., relation's alias) for alias resolution.

Suggested change
genJoinClause := func(joinType clause.JoinType, parentTableName string, relation *schema.Relationship) clause.Join {
genJoinClause := func(join clause.Join, joinType clause.JoinType, parentTableName string, relation *schema.Relationship) clause.Join {

Copilot uses AI. Check for mistakes.

tableAliasName := relation.Name
if parentTableName != clause.CurrentTable {
tableAliasName = utils.NestedRelationName(parentTableName, tableAliasName)
tableAliasName := join.Alias

if tableAliasName == "" {
tableAliasName = relation.Name
if parentTableName != clause.CurrentTable {
tableAliasName = utils.NestedRelationName(parentTableName, tableAliasName)
}
}

columnStmt := gorm.Statement{
Expand All @@ -167,6 +171,13 @@ func BuildQuerySQL(db *gorm.DB) {
}
}

if join.Expression != nil {
return clause.Join{
Type: join.JoinType,
Expression: join.Expression,
}
}

exprs := make([]clause.Expression, len(relation.References))
for idx, ref := range relation.References {
if ref.OwnPrimaryKey {
Expand Down
7 changes: 4 additions & 3 deletions chainable_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,10 @@ func (db *DB) Assign(attrs ...interface{}) (tx *DB) {
// Unscoped allows queries to include records marked as deleted,
// overriding the soft deletion behavior.
// Example:
// var users []User
// db.Unscoped().Find(&users)
// // Retrieves all users, including deleted ones.
//
// var users []User
// db.Unscoped().Find(&users)
// // Retrieves all users, including deleted ones.
func (db *DB) Unscoped() (tx *DB) {
tx = db.getInstance()
tx.Statement.Unscoped = true
Expand Down
32 changes: 32 additions & 0 deletions clause/joins.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package clause

import "gorm.io/gorm/utils"

type JoinType string

const (
Expand All @@ -9,6 +11,30 @@ const (
RightJoin JoinType = "RIGHT"
)

type JoinTarget struct {
Type JoinType
Association string
Subquery Expression
Table string
}

func Has(name string) JoinTarget {
return JoinTarget{Type: InnerJoin, Association: name}
}

func (jt JoinType) Association(name string) JoinTarget {
return JoinTarget{Type: jt, Association: name}
}

func (jt JoinType) AssociationFrom(name string, subquery Expression) JoinTarget {
return JoinTarget{Type: jt, Association: name, Subquery: subquery}
}

func (jt JoinTarget) As(name string) JoinTarget {
jt.Table = name
return jt
}

// Join clause for from
type Join struct {
Type JoinType
Expand All @@ -18,6 +44,12 @@ type Join struct {
Expression Expression
}

func JoinTable(names ...string) Table {
return Table{
Name: utils.JoinNestedRelationNames(names),
}
}

func (join Join) Build(builder Builder) {
if join.Expression != nil {
join.Expression.Build(builder)
Expand Down
Loading
Loading