dialect_postgres.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package gorm
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strings"
  6. "time"
  7. )
  8. type postgres struct {
  9. commonDialect
  10. }
  11. func init() {
  12. RegisterDialect("postgres", &postgres{})
  13. }
  14. func (postgres) GetName() string {
  15. return "postgres"
  16. }
  17. func (postgres) BindVar(i int) string {
  18. return fmt.Sprintf("$%v", i)
  19. }
  20. func (s *postgres) DataTypeOf(field *StructField) string {
  21. var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field, s)
  22. if sqlType == "" {
  23. switch dataValue.Kind() {
  24. case reflect.Bool:
  25. sqlType = "boolean"
  26. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uintptr:
  27. if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
  28. field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
  29. sqlType = "serial"
  30. } else {
  31. sqlType = "integer"
  32. }
  33. case reflect.Int64, reflect.Uint32, reflect.Uint64:
  34. if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
  35. field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
  36. sqlType = "bigserial"
  37. } else {
  38. sqlType = "bigint"
  39. }
  40. case reflect.Float32, reflect.Float64:
  41. sqlType = "numeric"
  42. case reflect.String:
  43. if _, ok := field.TagSettings["SIZE"]; !ok {
  44. size = 0 // if SIZE haven't been set, use `text` as the default type, as there are no performance different
  45. }
  46. if size > 0 && size < 65532 {
  47. sqlType = fmt.Sprintf("varchar(%d)", size)
  48. } else {
  49. sqlType = "text"
  50. }
  51. case reflect.Struct:
  52. if _, ok := dataValue.Interface().(time.Time); ok {
  53. sqlType = "timestamp with time zone"
  54. }
  55. case reflect.Map:
  56. if dataValue.Type().Name() == "Hstore" {
  57. sqlType = "hstore"
  58. }
  59. default:
  60. if IsByteArrayOrSlice(dataValue) {
  61. sqlType = "bytea"
  62. } else if isUUID(dataValue) {
  63. sqlType = "uuid"
  64. }
  65. }
  66. }
  67. if sqlType == "" {
  68. panic(fmt.Sprintf("invalid sql type %s (%s) for postgres", dataValue.Type().Name(), dataValue.Kind().String()))
  69. }
  70. if strings.TrimSpace(additionalType) == "" {
  71. return sqlType
  72. }
  73. return fmt.Sprintf("%v %v", sqlType, additionalType)
  74. }
  75. func (s postgres) HasIndex(tableName string, indexName string) bool {
  76. var count int
  77. s.db.QueryRow("SELECT count(*) FROM pg_indexes WHERE tablename = $1 AND indexname = $2", tableName, indexName).Scan(&count)
  78. return count > 0
  79. }
  80. func (s postgres) HasForeignKey(tableName string, foreignKeyName string) bool {
  81. var count int
  82. s.db.QueryRow("SELECT count(con.conname) FROM pg_constraint con WHERE $1::regclass::oid = con.conrelid AND con.conname = $2 AND con.contype='f'", tableName, foreignKeyName).Scan(&count)
  83. return count > 0
  84. }
  85. func (s postgres) HasTable(tableName string) bool {
  86. var count int
  87. s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.tables WHERE table_name = $1 AND table_type = 'BASE TABLE'", tableName).Scan(&count)
  88. return count > 0
  89. }
  90. func (s postgres) HasColumn(tableName string, columnName string) bool {
  91. var count int
  92. s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.columns WHERE table_name = $1 AND column_name = $2", tableName, columnName).Scan(&count)
  93. return count > 0
  94. }
  95. func (s postgres) CurrentDatabase() (name string) {
  96. s.db.QueryRow("SELECT CURRENT_DATABASE()").Scan(&name)
  97. return
  98. }
  99. func (s postgres) LastInsertIDReturningSuffix(tableName, key string) string {
  100. return fmt.Sprintf("RETURNING %v.%v", tableName, key)
  101. }
  102. func (postgres) SupportLastInsertID() bool {
  103. return false
  104. }
  105. func isUUID(value reflect.Value) bool {
  106. if value.Kind() != reflect.Array || value.Type().Len() != 16 {
  107. return false
  108. }
  109. typename := value.Type().Name()
  110. lower := strings.ToLower(typename)
  111. return "uuid" == lower || "guid" == lower
  112. }