dialect_postgres.go 3.7 KB

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