This commit is contained in:
Lunny Xiao 2021-08-05 22:10:52 +08:00
parent 074ade75c2
commit 69ff215387
2 changed files with 5 additions and 3 deletions

View File

@ -353,7 +353,7 @@ func TestJoinWithSubQuery(t *testing.T) {
assert.EqualValues(t, q, querys[0]) assert.EqualValues(t, q, querys[0])
querys = make([]JoinWithSubQuery1, 0, 1) querys = make([]JoinWithSubQuery1, 0, 1)
err = testEngine.Join("INNER", "(SELECT `id` FROM `"+tbName+"`) join_with_sub_query_depart", "`join_with_sub_query_depart`.`id` = `join_with_sub_query1`.`depart_id`"). err = testEngine.Join("INNER", "(SELECT `id` FROM "+tbName+") `a`", "`a`.`id` = `join_with_sub_query1`.`depart_id`").
Find(&querys) Find(&querys)
assert.NoError(t, err) assert.NoError(t, err)
assert.EqualValues(t, 1, len(querys)) assert.EqualValues(t, 1, len(querys))

View File

@ -539,7 +539,7 @@ func (statement *Statement) Join(joinOP string, tablename interface{}, condition
aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1]) aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1])
aliasName = schemas.CommonQuoter.Trim(aliasName) aliasName = schemas.CommonQuoter.Trim(aliasName)
fmt.Fprintf(&buf, "(%s) %s ON %v", statement.ReplaceQuote(subSQL), aliasName, statement.ReplaceQuote(condition)) fmt.Fprintf(&buf, "(%s) %s ON %v", statement.ReplaceQuote(subSQL), statement.quote(aliasName), statement.ReplaceQuote(condition))
statement.joinArgs = append(statement.joinArgs, subQueryArgs...) statement.joinArgs = append(statement.joinArgs, subQueryArgs...)
case *builder.Builder: case *builder.Builder:
subSQL, subQueryArgs, err := tp.ToSQL() subSQL, subQueryArgs, err := tp.ToSQL()
@ -552,7 +552,7 @@ func (statement *Statement) Join(joinOP string, tablename interface{}, condition
aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1]) aliasName := statement.dialect.Quoter().Trim(fields[len(fields)-1])
aliasName = schemas.CommonQuoter.Trim(aliasName) aliasName = schemas.CommonQuoter.Trim(aliasName)
fmt.Fprintf(&buf, "(%s) %s ON %v", statement.ReplaceQuote(subSQL), aliasName, statement.ReplaceQuote(condition)) fmt.Fprintf(&buf, "(%s) %s ON %v", statement.ReplaceQuote(subSQL), statement.quote(aliasName), statement.ReplaceQuote(condition))
statement.joinArgs = append(statement.joinArgs, subQueryArgs...) statement.joinArgs = append(statement.joinArgs, subQueryArgs...)
default: default:
tbName := dialects.FullTableName(statement.dialect, statement.tagParser.GetTableMapper(), tablename, true) tbName := dialects.FullTableName(statement.dialect, statement.tagParser.GetTableMapper(), tablename, true)
@ -560,6 +560,8 @@ func (statement *Statement) Join(joinOP string, tablename interface{}, condition
var buf strings.Builder var buf strings.Builder
statement.dialect.Quoter().QuoteTo(&buf, tbName) statement.dialect.Quoter().QuoteTo(&buf, tbName)
tbName = buf.String() tbName = buf.String()
} else {
tbName = statement.ReplaceQuote(tbName)
} }
fmt.Fprintf(&buf, "%s ON %v", tbName, statement.ReplaceQuote(condition)) fmt.Fprintf(&buf, "%s ON %v", tbName, statement.ReplaceQuote(condition))
} }