54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
|
package mySql
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func (t *Query) Join(join string, rest ...interface{}) *Query {
|
||
|
t.join = append(t.join, NewJoin("", join, rest))
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
func (t *Query) LeftJoin(join string, rest ...interface{}) *Query {
|
||
|
t.join = append(t.join, NewJoin("left", join, rest))
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
func (t *Query) RightJoin(join string, rest ...interface{}) *Query {
|
||
|
t.join = append(t.join, NewJoin("right", join, rest))
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
func (t *Query) InnerJoin(join string, rest ...interface{}) *Query {
|
||
|
t.join = append(t.join, NewJoin("inner", join, rest))
|
||
|
return t
|
||
|
}
|
||
|
|
||
|
func (t *Query) LeftJoinOn(tableName string, pk string, pk2 string, columns ...string) *Query {
|
||
|
as := string(rune(len(t.join) + 98))
|
||
|
|
||
|
for _, column := range columns {
|
||
|
//看一下有没有as
|
||
|
if find := strings.Contains(column, " as "); find {
|
||
|
//劈开前面一段和后面一段
|
||
|
countSplit := strings.SplitN(column, " as ", 2)
|
||
|
if len(countSplit) == 2 {
|
||
|
t.AddSelectRow(fmt.Sprintf("`%s`.`%s` as `%s`", as, countSplit[0], countSplit[1]))
|
||
|
} else {
|
||
|
t.AddSelectRow(column)
|
||
|
}
|
||
|
|
||
|
} else {
|
||
|
t.AddSelectRow(fmt.Sprintf("`%s`.`%s`", as, column))
|
||
|
}
|
||
|
|
||
|
}
|
||
|
if t.alias == "" {
|
||
|
t.LeftJoin(fmt.Sprintf("`%s` `%s` ON `%s`.`%s` = `%s`.`%s`", tableName, as, as, pk, t.tableName, pk2))
|
||
|
} else {
|
||
|
t.LeftJoin(fmt.Sprintf("`%s` `%s` ON `%s`.`%s` = `%s`.`%s`", tableName, as, as, pk, t.alias, pk2))
|
||
|
}
|
||
|
return t
|
||
|
}
|