xorm/engine_table.go

110 lines
2.6 KiB
Go
Raw Normal View History

2018-04-09 06:40:30 +00:00
// Copyright 2018 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xorm
import (
"fmt"
"reflect"
"strings"
"github.com/go-xorm/core"
)
// TableNameWithSchema will automatically add schema prefix on table name
func (engine *Engine) tbNameWithSchema(v string) string {
2018-04-09 06:40:30 +00:00
// Add schema name as prefix of table name.
// Only for postgres database.
if engine.dialect.DBType() == core.POSTGRES &&
engine.dialect.URI().Schema != "" &&
engine.dialect.URI().Schema != postgresPublicSchema &&
strings.Index(v, ".") == -1 {
return engine.dialect.URI().Schema + "." + v
}
return v
}
// TableName returns table name with schema prefix if has
func (engine *Engine) TableName(bean interface{}, includeSchema ...bool) string {
tbName := engine.tbNameNoSchema(bean)
if len(includeSchema) > 0 && includeSchema[0] {
tbName = engine.tbNameWithSchema(tbName)
}
return tbName
}
// tbName get some table's table name
func (session *Session) tbNameNoSchema(table *core.Table) string {
if len(session.statement.AltTableName) > 0 {
return session.statement.AltTableName
}
return table.Name
2018-04-09 06:40:30 +00:00
}
func (engine *Engine) tbNameForMap(v reflect.Value) string {
t := v.Type()
if tb, ok := v.Interface().(TableName); ok {
return tb.TableName()
2018-04-09 09:16:23 +00:00
}
if v.CanAddr() {
if tb, ok := v.Addr().Interface().(TableName); ok {
return tb.TableName()
2018-04-09 06:40:30 +00:00
}
}
return engine.TableMapper.Obj2Table(t.Name())
}
2018-04-09 15:18:31 +00:00
func (engine *Engine) tbNameNoSchema(tablename interface{}) string {
2018-04-09 06:40:30 +00:00
switch tablename.(type) {
case []string:
t := tablename.([]string)
if len(t) > 1 {
2018-04-09 15:18:31 +00:00
return fmt.Sprintf("%v AS %v", engine.Quote(t[0]), engine.Quote(t[1]))
2018-04-09 06:40:30 +00:00
} else if len(t) == 1 {
2018-04-09 15:18:31 +00:00
return engine.Quote(t[0])
2018-04-09 06:40:30 +00:00
}
case []interface{}:
t := tablename.([]interface{})
l := len(t)
var table string
if l > 0 {
f := t[0]
switch f.(type) {
case string:
table = f.(string)
case TableName:
table = f.(TableName).TableName()
default:
v := rValue(f)
t := v.Type()
if t.Kind() == reflect.Struct {
2018-04-09 15:18:31 +00:00
table = engine.tbNameForMap(v)
2018-04-09 06:40:30 +00:00
} else {
2018-04-09 15:18:31 +00:00
table = engine.Quote(fmt.Sprintf("%v", f))
2018-04-09 06:40:30 +00:00
}
}
}
if l > 1 {
2018-04-09 15:18:31 +00:00
return fmt.Sprintf("%v AS %v", engine.Quote(table),
2018-04-09 06:40:30 +00:00
engine.Quote(fmt.Sprintf("%v", t[1])))
} else if l == 1 {
2018-04-09 15:18:31 +00:00
return engine.Quote(table)
2018-04-09 06:40:30 +00:00
}
case TableName:
2018-04-09 15:18:31 +00:00
return tablename.(TableName).TableName()
2018-04-09 06:40:30 +00:00
case string:
2018-04-09 15:18:31 +00:00
return tablename.(string)
2018-04-09 06:40:30 +00:00
default:
v := rValue(tablename)
t := v.Type()
if t.Kind() == reflect.Struct {
2018-04-09 15:18:31 +00:00
return engine.tbNameForMap(v)
2018-04-09 06:40:30 +00:00
}
2018-04-09 15:18:31 +00:00
return engine.Quote(fmt.Sprintf("%v", tablename))
2018-04-09 06:40:30 +00:00
}
2018-04-09 15:18:31 +00:00
return ""
2018-04-09 06:40:30 +00:00
}