github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/sqlx/builder/builder_addition_join.go (about) 1 package builder 2 3 import ( 4 "context" 5 "strings" 6 ) 7 8 type join struct { 9 AdditionType 10 11 prefix string 12 target SqlExpr 13 joinCond SqlCondition 14 joinCols []*Column 15 } 16 17 func (j join) On(c SqlCondition) *join { j.joinCond = c; return &j } 18 19 func (j join) Using(cols ...*Column) *join { j.joinCols = cols; return &j } 20 21 func (j *join) IsNil() bool { 22 return j == nil || IsNilExpr(j.target) || 23 (j.prefix != "CROSS" && IsNilExpr(j.joinCond) && len(j.joinCols) == 0) 24 } 25 26 func (j *join) Ex(ctx context.Context) *Ex { 27 e := Expr("JOIN ") 28 if j.prefix != "" { 29 e = Expr(j.prefix + " JOIN ") 30 } 31 e.WriteExpr(j.target) 32 if !IsNilExpr(j.joinCond) { 33 e.WriteExpr(ExprBy(func(ctx context.Context) *Ex { 34 e := Expr(" ON ") 35 e.WriteExpr(j.joinCond) 36 return e.Ex(ctx) 37 })) 38 } 39 if len(j.joinCols) > 0 { 40 e.WriteExpr(ExprBy(func(ctx context.Context) *Ex { 41 e := Expr(" USING ") 42 e.WriteGroup(func(e *Ex) { 43 for i := range j.joinCols { 44 if i > 0 { 45 e.WriteQuery(", ") 46 } 47 e.WriteExpr(j.joinCols[i]) 48 } 49 }) 50 return e.Ex(ContextWithToggleMultiTable(ctx, false)) 51 })) 52 } 53 return e.Ex(ctx) 54 } 55 56 func Join(tar SqlExpr, prefixes ...string) *join { 57 return &join{ 58 AdditionType: AdditionJoin, 59 prefix: strings.Join(prefixes, " "), 60 target: tar, 61 } 62 } 63 64 func InnerJoin(tar SqlExpr) *join { return Join(tar, "INNER") } 65 func LeftJoin(tar SqlExpr) *join { return Join(tar, "LEFT") } 66 func RightJoin(tar SqlExpr) *join { return Join(tar, "RIGHT") } 67 func FullJoin(tar SqlExpr) *join { return Join(tar, "FULL") } 68 func CrossJoin(tar SqlExpr) *join { return Join(tar, "CROSS") }