本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
2.7KB

  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. "xorm.io/core"
  10. )
  11. // tbNameWithSchema 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. if v.Type().Implements(tpTableName) {
  40. return v.Interface().(TableName).TableName()
  41. }
  42. if v.Kind() == reflect.Ptr {
  43. v = v.Elem()
  44. if v.Type().Implements(tpTableName) {
  45. return v.Interface().(TableName).TableName()
  46. }
  47. }
  48. return engine.TableMapper.Obj2Table(v.Type().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. case reflect.Value:
  91. v := tablename.(reflect.Value)
  92. return engine.tbNameForMap(v)
  93. default:
  94. v := rValue(tablename)
  95. t := v.Type()
  96. if t.Kind() == reflect.Struct {
  97. return engine.tbNameForMap(v)
  98. }
  99. return engine.Quote(fmt.Sprintf("%v", tablename))
  100. }
  101. return ""
  102. }
上海开阖软件有限公司 沪ICP备12045867号-1