postgres_dialect.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package migrator
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. )
  7. type Postgres struct {
  8. BaseDialect
  9. }
  10. func NewPostgresDialect() *Postgres {
  11. d := Postgres{}
  12. d.BaseDialect.dialect = &d
  13. d.BaseDialect.driverName = POSTGRES
  14. return &d
  15. }
  16. func (db *Postgres) SupportEngine() bool {
  17. return false
  18. }
  19. func (db *Postgres) Quote(name string) string {
  20. return "\"" + name + "\""
  21. }
  22. func (db *Postgres) QuoteStr() string {
  23. return "\""
  24. }
  25. func (b *Postgres) LikeStr() string {
  26. return "ILIKE"
  27. }
  28. func (db *Postgres) AutoIncrStr() string {
  29. return ""
  30. }
  31. func (db *Postgres) BooleanStr(value bool) string {
  32. return strconv.FormatBool(value)
  33. }
  34. func (b *Postgres) Default(col *Column) string {
  35. if col.Type == DB_Bool {
  36. if col.Default == "0" {
  37. return "FALSE"
  38. }
  39. return "TRUE"
  40. }
  41. return col.Default
  42. }
  43. func (db *Postgres) SqlType(c *Column) string {
  44. var res string
  45. switch t := c.Type; t {
  46. case DB_TinyInt:
  47. res = DB_SmallInt
  48. return res
  49. case DB_MediumInt, DB_Int, DB_Integer:
  50. if c.IsAutoIncrement {
  51. return DB_Serial
  52. }
  53. return DB_Integer
  54. case DB_Serial, DB_BigSerial:
  55. c.IsAutoIncrement = true
  56. c.Nullable = false
  57. res = t
  58. case DB_Binary, DB_VarBinary:
  59. return DB_Bytea
  60. case DB_DateTime:
  61. res = DB_TimeStamp
  62. case DB_TimeStampz:
  63. return "timestamp with time zone"
  64. case DB_Float:
  65. res = DB_Real
  66. case DB_TinyText, DB_MediumText, DB_LongText:
  67. res = DB_Text
  68. case DB_NVarchar:
  69. res = DB_Varchar
  70. case DB_Uuid:
  71. res = DB_Uuid
  72. case DB_Blob, DB_TinyBlob, DB_MediumBlob, DB_LongBlob:
  73. return DB_Bytea
  74. case DB_Double:
  75. return "DOUBLE PRECISION"
  76. default:
  77. if c.IsAutoIncrement {
  78. return DB_Serial
  79. }
  80. res = t
  81. }
  82. var hasLen1 = (c.Length > 0)
  83. var hasLen2 = (c.Length2 > 0)
  84. if hasLen2 {
  85. res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
  86. } else if hasLen1 {
  87. res += "(" + strconv.Itoa(c.Length) + ")"
  88. }
  89. return res
  90. }
  91. func (db *Postgres) TableCheckSql(tableName string) (string, []interface{}) {
  92. args := []interface{}{"grafana", tableName}
  93. sql := "SELECT table_name FROM information_schema.tables WHERE table_schema=? and table_name=?"
  94. return sql, args
  95. }
  96. func (db *Postgres) DropIndexSql(tableName string, index *Index) string {
  97. quote := db.Quote
  98. idxName := index.XName(tableName)
  99. return fmt.Sprintf("DROP INDEX %v", quote(idxName))
  100. }
  101. func (db *Postgres) UpdateTableSql(tableName string, columns []*Column) string {
  102. var statements = []string{}
  103. for _, col := range columns {
  104. statements = append(statements, "ALTER "+db.QuoteStr()+col.Name+db.QuoteStr()+" TYPE "+db.SqlType(col))
  105. }
  106. return "ALTER TABLE " + db.Quote(tableName) + " " + strings.Join(statements, ", ") + ";"
  107. }