Skip to content

Commit

Permalink
feat: optimize relation Field
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1v3r committed Sep 27, 2021
1 parent e9d360b commit 2ce6763
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 4 additions & 1 deletion field/association.go
Original file line number Diff line number Diff line change
@@ -59,7 +59,10 @@ func (r Relation) Path() string { return r.path }
func (r Relation) Type() string { return r.varType }

func (r Relation) Field(member ...string) Expr {
return NewString("", r.varName+"."+strings.Join(member, "."))
if len(member) > 0 {
return NewString("", r.varName+"."+strings.Join(member, ".")).appendBuildOpts(WithoutQuote)
}
return NewString("", r.varName).appendBuildOpts(WithoutQuote)
}

func (r *Relation) On(conds ...Expr) RelationField {
10 changes: 8 additions & 2 deletions field/expr.go
Original file line number Diff line number Diff line change
@@ -33,7 +33,8 @@ func (e sql) String() string { return string(e) }
type expr struct {
col clause.Column

e clause.Expression
e clause.Expression
buildOpts []BuildOpt
}

func (e expr) BeCond() interface{} { return e.expression() }
@@ -63,7 +64,7 @@ const (

func (e expr) BuildColumn(stmt *gorm.Statement, opts ...BuildOpt) sql {
col := clause.Column{Name: e.col.Name}
for _, opt := range opts {
for _, opt := range append(e.buildOpts, opts...) {
switch opt {
case WithTable:
col.Table = e.col.Table
@@ -101,6 +102,11 @@ func (e expr) setE(expression clause.Expression) expr {
return e
}

func (e expr) appendBuildOpts(opts ...BuildOpt) expr {
e.buildOpts = append(e.buildOpts, opts...)
return e
}

// ======================== basic function ========================
func (e expr) IsNull() Expr {
return e.setE(clause.Expr{SQL: "? IS NULL", Vars: []interface{}{e.RawExpr()}})

0 comments on commit 2ce6763

Please sign in to comment.