From 0b04b2150f25be6e5bc6fe454ada391f0374f011 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 9 Jun 2018 16:17:14 +0800 Subject: [PATCH] join support builder --- statement.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/statement.go b/statement.go index ec43ce56..00a3bff9 100644 --- a/statement.go +++ b/statement.go @@ -757,9 +757,28 @@ func (statement *Statement) Join(joinOP string, tablename interface{}, condition fmt.Fprintf(&buf, "%v JOIN ", joinOP) } - tbName := statement.Engine.TableName(tablename, true) + switch tp := tablename.(type) { + case *builder.Builder: + subSQL, subQueryArgs, err := tp.ToSQL() + if err != nil { + statement.lastError = err + return statement + } + fmt.Fprintf(&buf, "(%s) ON %v", subSQL, condition) + statement.joinArgs = append(statement.joinArgs, subQueryArgs...) + case builder.Builder: + subSQL, subQueryArgs, err := tp.ToSQL() + if err != nil { + statement.lastError = err + return statement + } + fmt.Fprintf(&buf, "(%s) ON %v", subSQL, condition) + statement.joinArgs = append(statement.joinArgs, subQueryArgs...) + default: + tbName := statement.Engine.TableName(tablename, true) + fmt.Fprintf(&buf, "%s ON %v", tbName, condition) + } - fmt.Fprintf(&buf, "%s ON %v", tbName, condition) statement.JoinStr = buf.String() statement.joinArgs = append(statement.joinArgs, args...) return statement