Skip to content

Commit

Permalink
feat: optimize alias
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r committed Apr 6, 2022
1 parent e3eb97b commit 068730b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 30 deletions.
13 changes: 7 additions & 6 deletions README.ZH_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,10 @@ users, err := u.WithContext(ctx).Distinct(u.Name, u.Age).Order(u.Name, u.Age.Des
联表查询方法,`Join` 方法对应 `inner join`,此外还有 `LeftJoin` 方法和 `RightJoin` 方法。

```go
u := query.Use(db).User
e := query.Use(db).Email
c := query.Use(db).CreditCard
q := query.Use(db)
u := q.User
e := q.Email
c := q.CreditCard

type Result struct {
Name string
Expand All @@ -734,14 +735,14 @@ err := u.WithContext(ctx).Select(u.Name, e.Email).LeftJoin(e, e.UserID.EqCol(u.I

// self join
var result Result
u2 := query.Use(db).User.As("u2")
err := u.WithContext(ctx).Select(u.Name, u.ID).LeftJoin(u2, u2.ID.EqCol(u.ID)).Scan(&result)
u2 := u.As("u2")
err := u.WithContext(ctx).Select(u.Name, u2.ID).LeftJoin(u2, u2.ID.EqCol(u.ID)).Scan(&result)
// SELECT users.name, u2.id FROM `users` left join `users` u2 on u2.id = users.id

//join with sub query
var result Result
e2 := e.As("e2")
err := u.WithContext(ctx).Select(u.Name, e2.Email).LeftJoin(e.Select(e.Email, e.UserID).Where(e.UserID.Gt(100)).As("e2"), e2.UserID.EqCol(u.ID)).Scan(&result)
err := u.WithContext(ctx).Select(u.Name, e2.Email).LeftJoin(e.WithContext(ctx).Select(e.Email, e.UserID).Where(e.UserID.Gt(100)).As("e2"), e2.UserID.EqCol(u.ID)).Scan(&result)
// SELECT users.name, e2.email FROM `users` left join (select email,user_id from emails where user_id > 100) as e2 on e2.user_id = users.id

rows, err := u.WithContext(ctx).Select(u.Name, e.Email).LeftJoin(e, e.UserID.EqCol(u.ID)).Rows()
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -739,14 +739,14 @@ err := u.WithContext(ctx).Select(u.Name, e.Email).LeftJoin(e, e.UserID.EqCol(u.I

// self join
var result Result
u2 := query.Use(db).User.As("u2")
err := u.WithContext(ctx).Select(u.Name, u.ID).LeftJoin(u2, u2.ID.EqCol(u.ID)).Scan(&result)
u2 := u.As("u2")
err := u.WithContext(ctx).Select(u.Name, u2.ID).LeftJoin(u2, u2.ID.EqCol(u.ID)).Scan(&result)
// SELECT users.name, u2.id FROM `users` left join `users` u2 on u2.id = users.id

//join with sub query
var result Result
e2 := e.As("e2")
err := u.WithContext(ctx).Select(u.Name, e2.Email).LeftJoin(e.Select(e.Email, e.UserID).Where(e.UserID.Gt(100)).As("e2"), e2.UserID.EqCol(u.ID)).Scan(&result)
err := u.WithContext(ctx).Select(u.Name, e2.Email).LeftJoin(e.WithContext(ctx).Select(e.Email, e.UserID).Where(e.UserID.Gt(100)).As("e2"), e2.UserID.EqCol(u.ID)).Scan(&result)
// SELECT users.name, e2.email FROM `users` left join (select email,user_id from emails where user_id > 100) as e2 on e2.user_id = users.id

rows, err := u.WithContext(ctx).Select(u.Name, e.Email).LeftJoin(e, e.UserID.EqCol(u.ID)).Rows()
Expand Down
11 changes: 7 additions & 4 deletions do.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"reflect"
"strings"

"gorm.io/gen/helper"
"gorm.io/gorm"
"gorm.io/gorm/callbacks"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"

"gorm.io/gen/field"
"gorm.io/gen/helper"
)

var (
Expand Down Expand Up @@ -173,7 +173,8 @@ func (d DO) As(alias string) Dao {
return &d
}

func (d DO) Alias() string {
// Alias return alias name
func (d *DO) Alias() string {
return d.alias
}

Expand Down Expand Up @@ -321,17 +322,19 @@ func (d *DO) join(table schema.Tabler, joinType clause.JoinType, conds []field.E
if len(conds) == 0 {
return d.withError(ErrEmptyCondition)
}

join := clause.Join{
Type: joinType,
Table: clause.Table{Name: table.TableName()},
ON: clause.Where{Exprs: toExpression(conds...)},
}
if do, ok := table.(Dao); ok {
join.Expression = helper.NewJoinExpr(join, Table(do).underlyingDB().Statement.TableExpr)
join.Expression = helper.NewJoinTblExpr(join, Table(do).underlyingDB().Statement.TableExpr)
}
if al, ok := table.(Alias); ok {
if al, ok := table.(interface{ Alias() string }); ok {
join.Table.Alias = al.Alias()
}

from := getFromClause(d.db)
from.Joins = append(from.Joins, join)
return d.getInstance(d.db.Clauses(from))
Expand Down
12 changes: 7 additions & 5 deletions helper/clause.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,17 @@ func JoinSetBuilder(src *strings.Builder, setValue strings.Builder) {
}
}

// Join join clause for from
type JoinExpr struct {
// JoinTblExpr join clause with table expression(sub query...)
type JoinTblExpr struct {
clause.Join
TableExpr clause.Expression
}

func NewJoinExpr(join clause.Join, tbExpr clause.Expression) JoinExpr {
return JoinExpr{Join: join, TableExpr: tbExpr}
func NewJoinTblExpr(join clause.Join, tbExpr clause.Expression) JoinTblExpr {
return JoinTblExpr{Join: join, TableExpr: tbExpr}
}
func (join JoinExpr) Build(builder clause.Builder) {

func (join JoinTblExpr) Build(builder clause.Builder) {
if builder == nil {
return
}
Expand All @@ -145,6 +146,7 @@ func (join JoinExpr) Build(builder clause.Builder) {
if join.TableExpr != nil {
join.TableExpr.Build(builder)
}

if len(join.ON.Exprs) > 0 {
_, _ = builder.WriteString(" ON ")
join.ON.Build(builder)
Expand Down
4 changes: 0 additions & 4 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@ type subQuery interface {

Condition
}
type Alias interface {
Alias() string
}

// Dao CRUD methods
type Dao interface {
subQuery
schema.Tabler
Alias
As(alias string) Dao

Not(conds ...Condition) Dao
Expand Down
13 changes: 5 additions & 8 deletions internal/template/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ const (
{{.NewStructName}}Do {{.NewStructName}}Do
` + fields + `
}
` + tableMethod + asMethond + aliasMethod + updateFieldMethod + `
` + tableMethod + asMethond + updateFieldMethod + `
func ({{.S}} *{{.NewStructName}}) WithContext(ctx context.Context) *{{.NewStructName}}Do { return {{.S}}.{{.NewStructName}}Do.WithContext(ctx)}
func ({{.S}} *{{.NewStructName}}) WithContext(ctx context.Context) *{{.NewStructName}}Do { return {{.S}}.{{.NewStructName}}Do.WithContext(ctx) }
func ({{.S}} {{.NewStructName}}) TableName() string { return {{.S}}.{{.NewStructName}}Do.TableName()}
func ({{.S}} {{.NewStructName}}) TableName() string { return {{.S}}.{{.NewStructName}}Do.TableName() }
func ({{.S}} {{.NewStructName}}) Alias() string { return {{.S}}.{{.NewStructName}}Do.Alias() }
` + getFieldMethod + fillFieldMapMethod + cloneMethod + relationship + defineMethodStruct
)
Expand Down Expand Up @@ -73,11 +75,6 @@ func ({{.S}} {{.NewStructName}}) As(alias string) *{{.NewStructName}} {
{{.S}}.{{.NewStructName}}Do.DO = *({{.S}}.{{.NewStructName}}Do.As(alias).(*gen.DO))
return {{.S}}.updateTableName(alias)
}
`
aliasMethod = `
func ({{.S}} {{.NewStructName}}) Alias() string {
return {{.S}}.{{.NewStructName}}Do.Alias()
}
`
updateFieldMethod = `
func ({{.S}} *{{.NewStructName}}) updateTableName(table string) *{{.NewStructName}} {
Expand Down

0 comments on commit 068730b

Please sign in to comment.