engine_table.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2018 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "fmt"
  7. "reflect"
  8. "strings"
  9. "github.com/xormplus/core"
  10. )
  11. // TableNameWithSchema will automatically add schema prefix on table name
  12. func (engine *Engine) tbNameWithSchema(v string) string {
  13. // Add schema name as prefix of table name.
  14. // Only for postgres database.
  15. if engine.dialect.DBType() == core.POSTGRES &&
  16. engine.dialect.URI().Schema != "" &&
  17. engine.dialect.URI().Schema != postgresPublicSchema &&
  18. strings.Index(v, ".") == -1 {
  19. return engine.dialect.URI().Schema + "." + v
  20. }
  21. return v
  22. }
  23. // TableName returns table name with schema prefix if has
  24. func (engine *Engine) TableName(bean interface{}, includeSchema ...bool) string {
  25. tbName := engine.tbNameNoSchema(bean)
  26. if len(includeSchema) > 0 && includeSchema[0] {
  27. tbName = engine.tbNameWithSchema(tbName)
  28. }
  29. return tbName
  30. }
  31. // tbName get some table's table name
  32. func (session *Session) tbNameNoSchema(table *core.Table) string {
  33. if len(session.statement.AltTableName) > 0 {
  34. return session.statement.AltTableName
  35. }
  36. return table.Name
  37. }
  38. func (engine *Engine) tbNameForMap(v reflect.Value) string {
  39. t := v.Type()
  40. if tb, ok := v.Interface().(TableName); ok {
  41. return tb.TableName()
  42. }
  43. if v.CanAddr() {
  44. if tb, ok := v.Addr().Interface().(TableName); ok {
  45. return tb.TableName()
  46. }
  47. }
  48. return engine.TableMapper.Obj2Table(t.Name())
  49. }
  50. func (engine *Engine) tbNameNoSchema(tablename interface{}) string {
  51. switch tablename.(type) {
  52. case []string:
  53. t := tablename.([]string)
  54. if len(t) > 1 {
  55. return fmt.Sprintf("%v AS %v", engine.Quote(t[0]), engine.Quote(t[1]))
  56. } else if len(t) == 1 {
  57. return engine.Quote(t[0])
  58. }
  59. case []interface{}:
  60. t := tablename.([]interface{})
  61. l := len(t)
  62. var table string
  63. if l > 0 {
  64. f := t[0]
  65. switch f.(type) {
  66. case string:
  67. table = f.(string)
  68. case TableName:
  69. table = f.(TableName).TableName()
  70. default:
  71. v := rValue(f)
  72. t := v.Type()
  73. if t.Kind() == reflect.Struct {
  74. table = engine.tbNameForMap(v)
  75. } else {
  76. table = engine.Quote(fmt.Sprintf("%v", f))
  77. }
  78. }
  79. }
  80. if l > 1 {
  81. return fmt.Sprintf("%v AS %v", engine.Quote(table),
  82. engine.Quote(fmt.Sprintf("%v", t[1])))
  83. } else if l == 1 {
  84. return engine.Quote(table)
  85. }
  86. case TableName:
  87. return tablename.(TableName).TableName()
  88. case string:
  89. return tablename.(string)
  90. default:
  91. v := rValue(tablename)
  92. t := v.Type()
  93. if t.Kind() == reflect.Struct {
  94. return engine.tbNameForMap(v)
  95. }
  96. return engine.Quote(fmt.Sprintf("%v", tablename))
  97. }
  98. return ""
  99. }