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.

table_name.go 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2015 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 dialects
  5. import (
  6. "fmt"
  7. "reflect"
  8. "strings"
  9. "xorm.io/xorm/internal/utils"
  10. "xorm.io/xorm/names"
  11. )
  12. // TableNameWithSchema will add schema prefix on table name if possible
  13. func TableNameWithSchema(dialect Dialect, tableName string) string {
  14. // Add schema name as prefix of table name.
  15. // Only for postgres database.
  16. if dialect.URI().Schema != "" &&
  17. strings.Index(tableName, ".") == -1 {
  18. return fmt.Sprintf("%s.%s", dialect.URI().Schema, tableName)
  19. }
  20. return tableName
  21. }
  22. // TableNameNoSchema returns table name with given tableName
  23. func TableNameNoSchema(dialect Dialect, mapper names.Mapper, tableName interface{}) string {
  24. quote := dialect.Quoter().Quote
  25. switch tableName.(type) {
  26. case []string:
  27. t := tableName.([]string)
  28. if len(t) > 1 {
  29. return fmt.Sprintf("%v AS %v", quote(t[0]), quote(t[1]))
  30. } else if len(t) == 1 {
  31. return quote(t[0])
  32. }
  33. case []interface{}:
  34. t := tableName.([]interface{})
  35. l := len(t)
  36. var table string
  37. if l > 0 {
  38. f := t[0]
  39. switch f.(type) {
  40. case string:
  41. table = f.(string)
  42. case names.TableName:
  43. table = f.(names.TableName).TableName()
  44. default:
  45. v := utils.ReflectValue(f)
  46. t := v.Type()
  47. if t.Kind() == reflect.Struct {
  48. table = names.GetTableName(mapper, v)
  49. } else {
  50. table = quote(fmt.Sprintf("%v", f))
  51. }
  52. }
  53. }
  54. if l > 1 {
  55. return fmt.Sprintf("%v AS %v", quote(table), quote(fmt.Sprintf("%v", t[1])))
  56. } else if l == 1 {
  57. return quote(table)
  58. }
  59. case names.TableName:
  60. return tableName.(names.TableName).TableName()
  61. case string:
  62. return tableName.(string)
  63. case reflect.Value:
  64. v := tableName.(reflect.Value)
  65. return names.GetTableName(mapper, v)
  66. default:
  67. v := utils.ReflectValue(tableName)
  68. t := v.Type()
  69. if t.Kind() == reflect.Struct {
  70. return names.GetTableName(mapper, v)
  71. }
  72. return quote(fmt.Sprintf("%v", tableName))
  73. }
  74. return ""
  75. }
  76. // FullTableName returns table name with quote and schema according parameter
  77. func FullTableName(dialect Dialect, mapper names.Mapper, bean interface{}, includeSchema ...bool) string {
  78. tbName := TableNameNoSchema(dialect, mapper, bean)
  79. if len(includeSchema) > 0 && includeSchema[0] && !utils.IsSubQuery(tbName) {
  80. tbName = TableNameWithSchema(dialect, tbName)
  81. }
  82. return tbName
  83. }