Support table alias
This commit is contained in:
parent
58d33844ce
commit
730ed76c5d
16
engine.go
16
engine.go
|
@ -560,6 +560,13 @@ func (engine *Engine) Table(tableNameOrBean interface{}) *Session {
|
||||||
return session.Table(tableNameOrBean)
|
return session.Table(tableNameOrBean)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set the table alias
|
||||||
|
func (engine *Engine) Alias(alias string) *Session {
|
||||||
|
session := engine.NewSession()
|
||||||
|
session.IsAutoClose = true
|
||||||
|
return session.Alias(alias)
|
||||||
|
}
|
||||||
|
|
||||||
// This method will generate "LIMIT start, limit"
|
// This method will generate "LIMIT start, limit"
|
||||||
func (engine *Engine) Limit(limit int, start ...int) *Session {
|
func (engine *Engine) Limit(limit int, start ...int) *Session {
|
||||||
session := engine.NewSession()
|
session := engine.NewSession()
|
||||||
|
@ -595,7 +602,7 @@ func (engine *Engine) OrderBy(order string) *Session {
|
||||||
}
|
}
|
||||||
|
|
||||||
// The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
// The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
||||||
func (engine *Engine) Join(join_operator, tablename, condition string) *Session {
|
func (engine *Engine) Join(join_operator string, tablename interface{}, condition string) *Session {
|
||||||
session := engine.NewSession()
|
session := engine.NewSession()
|
||||||
session.IsAutoClose = true
|
session.IsAutoClose = true
|
||||||
return session.Join(join_operator, tablename, condition)
|
return session.Join(join_operator, tablename, condition)
|
||||||
|
@ -1246,6 +1253,13 @@ func (engine *Engine) Query(sql string, paramStr ...interface{}) (resultsSlice [
|
||||||
return session.Query(sql, paramStr...)
|
return session.Query(sql, paramStr...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Exec a raw sql and return records as []map[string]string
|
||||||
|
func (engine *Engine) Q(sql string, paramStr ...interface{}) (resultsSlice []map[string]string, err error) {
|
||||||
|
session := engine.NewSession()
|
||||||
|
defer session.Close()
|
||||||
|
return session.Q(sql, paramStr...)
|
||||||
|
}
|
||||||
|
|
||||||
// Insert one or more records
|
// Insert one or more records
|
||||||
func (engine *Engine) Insert(beans ...interface{}) (int64, error) {
|
func (engine *Engine) Insert(beans ...interface{}) (int64, error) {
|
||||||
session := engine.NewSession()
|
session := engine.NewSession()
|
||||||
|
|
|
@ -134,6 +134,12 @@ func (session *Session) Table(tableNameOrBean interface{}) *Session {
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set the table alias
|
||||||
|
func (session *Session) Alias(alias string) *Session {
|
||||||
|
session.Statement.Alias(alias)
|
||||||
|
return session
|
||||||
|
}
|
||||||
|
|
||||||
// Method In provides a query string like "id in (1, 2, 3)"
|
// Method In provides a query string like "id in (1, 2, 3)"
|
||||||
func (session *Session) In(column string, args ...interface{}) *Session {
|
func (session *Session) In(column string, args ...interface{}) *Session {
|
||||||
session.Statement.In(column, args...)
|
session.Statement.In(column, args...)
|
||||||
|
@ -257,7 +263,7 @@ func (session *Session) NoCache() *Session {
|
||||||
}
|
}
|
||||||
|
|
||||||
//The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
//The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
||||||
func (session *Session) Join(join_operator, tablename, condition string) *Session {
|
func (session *Session) Join(join_operator string, tablename interface{}, condition string) *Session {
|
||||||
session.Statement.Join(join_operator, tablename, condition)
|
session.Statement.Join(join_operator, tablename, condition)
|
||||||
return session
|
return session
|
||||||
}
|
}
|
||||||
|
|
80
statement.go
80
statement.go
|
@ -55,6 +55,7 @@ type Statement struct {
|
||||||
UseCache bool
|
UseCache bool
|
||||||
UseAutoTime bool
|
UseAutoTime bool
|
||||||
IsDistinct bool
|
IsDistinct bool
|
||||||
|
TableAlias string
|
||||||
allUseBool bool
|
allUseBool bool
|
||||||
checkVersion bool
|
checkVersion bool
|
||||||
unscoped bool
|
unscoped bool
|
||||||
|
@ -88,6 +89,7 @@ func (statement *Statement) Init() {
|
||||||
statement.UseCache = true
|
statement.UseCache = true
|
||||||
statement.UseAutoTime = true
|
statement.UseAutoTime = true
|
||||||
statement.IsDistinct = false
|
statement.IsDistinct = false
|
||||||
|
statement.TableAlias = ""
|
||||||
statement.allUseBool = false
|
statement.allUseBool = false
|
||||||
statement.useAllCols = false
|
statement.useAllCols = false
|
||||||
statement.mustColumnMap = make(map[string]bool)
|
statement.mustColumnMap = make(map[string]bool)
|
||||||
|
@ -105,6 +107,12 @@ func (statement *Statement) Sql(querystring string, args ...interface{}) *Statem
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set the table alias
|
||||||
|
func (statement *Statement) Alias(alias string) *Statement {
|
||||||
|
statement.TableAlias = alias
|
||||||
|
return statement
|
||||||
|
}
|
||||||
|
|
||||||
// add Where statment
|
// add Where statment
|
||||||
func (statement *Statement) Where(querystring string, args ...interface{}) *Statement {
|
func (statement *Statement) Where(querystring string, args ...interface{}) *Statement {
|
||||||
if !strings.Contains(querystring, statement.Engine.dialect.EqStr()) {
|
if !strings.Contains(querystring, statement.Engine.dialect.EqStr()) {
|
||||||
|
@ -145,6 +153,9 @@ func (statement *Statement) Table(tableNameOrBean interface{}) *Statement {
|
||||||
t := v.Type()
|
t := v.Type()
|
||||||
if t.Kind() == reflect.String {
|
if t.Kind() == reflect.String {
|
||||||
statement.AltTableName = tableNameOrBean.(string)
|
statement.AltTableName = tableNameOrBean.(string)
|
||||||
|
if statement.AltTableName[0] == '~' {
|
||||||
|
statement.AltTableName = statement.Engine.TableMapper.TableName(statement.AltTableName[1:])
|
||||||
|
}
|
||||||
} else if t.Kind() == reflect.Struct {
|
} else if t.Kind() == reflect.Struct {
|
||||||
statement.RefTable = statement.Engine.autoMapType(v)
|
statement.RefTable = statement.Engine.autoMapType(v)
|
||||||
}
|
}
|
||||||
|
@ -912,13 +923,61 @@ func (statement *Statement) Asc(colNames ...string) *Statement {
|
||||||
}
|
}
|
||||||
|
|
||||||
//The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
//The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
||||||
func (statement *Statement) Join(join_operator, tablename, condition string) *Statement {
|
func (statement *Statement) Join(join_operator string, tablename interface{}, condition string) *Statement {
|
||||||
|
var joinTable string
|
||||||
|
switch tablename.(type) {
|
||||||
|
case []string:
|
||||||
|
t := tablename.([]string)
|
||||||
|
l := len(t)
|
||||||
|
if l > 1 {
|
||||||
|
table := t[0]
|
||||||
|
if table[0] == '~' {
|
||||||
|
table = statement.Engine.TableMapper.TableName(table[1:])
|
||||||
|
}
|
||||||
|
joinTable = statement.Engine.Quote(table) + " AS " + statement.Engine.Quote(t[1])
|
||||||
|
} else if l == 1 {
|
||||||
|
table := t[0]
|
||||||
|
if table[0] == '~' {
|
||||||
|
table = statement.Engine.TableMapper.TableName(table[1:])
|
||||||
|
}
|
||||||
|
joinTable = statement.Engine.Quote(table)
|
||||||
|
}
|
||||||
|
case []interface{}:
|
||||||
|
t := tablename.([]interface{})
|
||||||
|
l := len(t)
|
||||||
|
table := ""
|
||||||
|
if l > 0 {
|
||||||
|
f := t[0]
|
||||||
|
v := rValue(f)
|
||||||
|
t := v.Type()
|
||||||
|
if t.Kind() == reflect.String {
|
||||||
|
table = f.(string)
|
||||||
|
if table[0] == '~' {
|
||||||
|
table = statement.Engine.TableMapper.TableName(table[1:])
|
||||||
|
}
|
||||||
|
} else if t.Kind() == reflect.Struct {
|
||||||
|
r := statement.Engine.autoMapType(v)
|
||||||
|
table = r.Name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if l > 1 {
|
||||||
|
joinTable = statement.Engine.Quote(table) + " AS " + statement.Engine.Quote(fmt.Sprintf("%v", t[1]))
|
||||||
|
} else if l == 1 {
|
||||||
|
joinTable = statement.Engine.Quote(table)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
t := fmt.Sprintf("%v", tablename)
|
||||||
|
if t[0] == '~' {
|
||||||
|
t = statement.Engine.TableMapper.TableName(t[1:])
|
||||||
|
}
|
||||||
|
joinTable = statement.Engine.Quote(t)
|
||||||
|
}
|
||||||
if statement.JoinStr != "" {
|
if statement.JoinStr != "" {
|
||||||
statement.JoinStr = statement.JoinStr + fmt.Sprintf(" %v JOIN %v ON %v", join_operator,
|
statement.JoinStr = statement.JoinStr + fmt.Sprintf(" %v JOIN %v ON %v", join_operator,
|
||||||
statement.Engine.Quote(tablename), condition)
|
joinTable, condition)
|
||||||
} else {
|
} else {
|
||||||
statement.JoinStr = fmt.Sprintf("%v JOIN %v ON %v", join_operator,
|
statement.JoinStr = fmt.Sprintf("%v JOIN %v ON %v", join_operator,
|
||||||
statement.Engine.Quote(tablename), condition)
|
joinTable, condition)
|
||||||
}
|
}
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
@ -955,16 +1014,22 @@ func (statement *Statement) genColumnStr() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
if statement.JoinStr != "" {
|
if statement.JoinStr != "" {
|
||||||
name := statement.Engine.Quote(statement.TableName()) + "." + statement.Engine.Quote(col.Name)
|
var name string
|
||||||
|
if statement.TableAlias != "" {
|
||||||
|
name = statement.Engine.Quote(statement.TableAlias)
|
||||||
|
} else {
|
||||||
|
name = statement.Engine.Quote(statement.TableName())
|
||||||
|
}
|
||||||
|
name += "." + statement.Engine.Quote(col.Name)
|
||||||
if col.IsPrimaryKey && statement.Engine.Dialect().DBType() == "ql" {
|
if col.IsPrimaryKey && statement.Engine.Dialect().DBType() == "ql" {
|
||||||
colNames = append(colNames, "id() as "+name)
|
colNames = append(colNames, "id() AS "+name)
|
||||||
} else {
|
} else {
|
||||||
colNames = append(colNames, name)
|
colNames = append(colNames, name)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
name := statement.Engine.Quote(col.Name)
|
name := statement.Engine.Quote(col.Name)
|
||||||
if col.IsPrimaryKey && statement.Engine.Dialect().DBType() == "ql" {
|
if col.IsPrimaryKey && statement.Engine.Dialect().DBType() == "ql" {
|
||||||
colNames = append(colNames, "id() as "+name)
|
colNames = append(colNames, "id() AS "+name)
|
||||||
} else {
|
} else {
|
||||||
colNames = append(colNames, name)
|
colNames = append(colNames, name)
|
||||||
}
|
}
|
||||||
|
@ -1134,6 +1199,9 @@ func (statement *Statement) genSelectSql(columnStr string) (a string) {
|
||||||
whereStr = fmt.Sprintf(" WHERE %v", statement.ConditionStr)
|
whereStr = fmt.Sprintf(" WHERE %v", statement.ConditionStr)
|
||||||
}
|
}
|
||||||
var fromStr string = " FROM " + statement.Engine.Quote(statement.TableName())
|
var fromStr string = " FROM " + statement.Engine.Quote(statement.TableName())
|
||||||
|
if statement.TableAlias != "" {
|
||||||
|
fromStr += " AS " + statement.Engine.Quote(statement.TableAlias)
|
||||||
|
}
|
||||||
if statement.JoinStr != "" {
|
if statement.JoinStr != "" {
|
||||||
fromStr = fmt.Sprintf("%v %v", fromStr, statement.JoinStr)
|
fromStr = fmt.Sprintf("%v %v", fromStr, statement.JoinStr)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue