Skip to content

Commit

Permalink
revert it to handle private joins
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsoltan committed Aug 12, 2024
1 parent f9315d3 commit a1d9d7d
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion callbacks/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func Delete(config *Config) func(db *gorm.DB) {
func(db *gorm.DB) {
deleteClause.Modifier = db.Statement.Table
},
func(db *gorm.DB, tableAliasName string, join gorm.Join, relation *schema.Relationship) {
func(db *gorm.DB, tableAliasName string, idx int, relation *schema.Relationship) {
},
)

Expand Down
6 changes: 3 additions & 3 deletions callbacks/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
)

func HandleJoins(db *gorm.DB, prejoinCallback func(db *gorm.DB), perFieldNameCallback func(db *gorm.DB, tableAliasName string, join gorm.Join, relation *schema.Relationship)) {
func HandleJoins(db *gorm.DB, prejoinCallback func(db *gorm.DB), perFieldNameCallback func(db *gorm.DB, tableAliasName string, idx int, relation *schema.Relationship)) {

Check failure on line 11 in callbacks/join.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 calculated cyclomatic complexity for function HandleJoins is 25, max is 10 (cyclop) Raw Output: callbacks/join.go:11:1: calculated cyclomatic complexity for function HandleJoins is 25, max is 10 (cyclop) func HandleJoins(db *gorm.DB, prejoinCallback func(db *gorm.DB), perFieldNameCallback func(db *gorm.DB, tableAliasName string, idx int, relation *schema.Relationship)) { ^
// inline joins
fromClause := clause.From{}
if v, ok := db.Statement.Clauses["FROM"].Expression.(clause.From); ok {
Expand All @@ -19,7 +19,7 @@ func HandleJoins(db *gorm.DB, prejoinCallback func(db *gorm.DB), perFieldNameCal
prejoinCallback(db)

specifiedRelationsName := make(map[string]interface{})
for _, join := range db.Statement.Joins {
for idx, join := range db.Statement.Joins {
if db.Statement.Schema != nil {
var isRelations bool // is relations or raw sql
var relations []*schema.Relationship
Expand Down Expand Up @@ -59,7 +59,7 @@ func HandleJoins(db *gorm.DB, prejoinCallback func(db *gorm.DB), perFieldNameCal
tableAliasName = utils.NestedRelationName(parentTableName, tableAliasName)
}

perFieldNameCallback(db, tableAliasName, join, relation)
perFieldNameCallback(db, tableAliasName, idx, relation)

exprs := make([]clause.Expression, len(relation.References))
for idx, ref := range relation.References {
Expand Down
4 changes: 2 additions & 2 deletions callbacks/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ func BuildQuerySQL(db *gorm.DB) {
}
}
},
func(db *gorm.DB, tableAliasName string, join gorm.Join, relation *schema.Relationship) {
func(db *gorm.DB, tableAliasName string, idx int, relation *schema.Relationship) {
columnStmt := gorm.Statement{
Table: tableAliasName, DB: db, Schema: relation.FieldSchema,
Selects: join.Selects, Omits: join.Omits,
Selects: db.Statement.Joins[idx].Selects, Omits: db.Statement.Joins[idx].Omits,
}

selectColumns, restricted := columnStmt.SelectAndOmitColumns(false, false)
Expand Down
4 changes: 2 additions & 2 deletions chainable_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func joins(db *DB, joinType clause.JoinType, query string, args ...interface{})

if len(args) == 1 {
if db, ok := args[0].(*DB); ok {
j := Join{
j := join{
Name: query, Conds: args, Selects: db.Statement.Selects,
Omits: db.Statement.Omits, JoinType: joinType,
}
Expand All @@ -272,7 +272,7 @@ func joins(db *DB, joinType clause.JoinType, query string, args ...interface{})
}
}

tx.Statement.Joins = append(tx.Statement.Joins, Join{Name: query, Conds: args, JoinType: joinType})
tx.Statement.Joins = append(tx.Statement.Joins, join{Name: query, Conds: args, JoinType: joinType})
return
}

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gorm.io/gorm v1.25.11 h1:/Wfyg1B/je1hnDx3sMkX+gAlxrlZpn6X0BXRlwXlvHg=
gorm.io/gorm v1.25.11/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
6 changes: 3 additions & 3 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Statement struct {
Selects []string // selected columns
Omits []string // omit columns
ColumnMapping map[string]string // map columns
Joins []Join
Joins []join
Preloads map[string][]interface{}
Settings sync.Map
ConnPool ConnPool
Expand All @@ -49,7 +49,7 @@ type Statement struct {
scopes []func(*DB) *DB
}

type Join struct {
type join struct {
Name string
Conds []interface{}
On *clause.Where
Expand Down Expand Up @@ -538,7 +538,7 @@ func (stmt *Statement) clone() *Statement {
}

if len(stmt.Joins) > 0 {
newStmt.Joins = make([]Join, len(stmt.Joins))
newStmt.Joins = make([]join, len(stmt.Joins))
copy(newStmt.Joins, stmt.Joins)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
gorm.io/driver/postgres v1.5.9
gorm.io/driver/sqlite v1.5.6
gorm.io/driver/sqlserver v1.5.3
gorm.io/gorm v1.25.10
gorm.io/gorm v1.25.11
)

require (
Expand Down

0 comments on commit a1d9d7d

Please sign in to comment.