postgres_dialect.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. } else {
  39. return "TRUE"
  40. }
  41. }
  42. return col.Default
  43. }
  44. func (db *Postgres) SqlType(c *Column) string {
  45. var res string
  46. switch t := c.Type; t {
  47. case DB_TinyInt:
  48. res = DB_SmallInt
  49. return res
  50. case DB_MediumInt, DB_Int, DB_Integer:
  51. if c.IsAutoIncrement {
  52. return DB_Serial
  53. }
  54. return DB_Integer
  55. case DB_Serial, DB_BigSerial:
  56. c.IsAutoIncrement = true
  57. c.Nullable = false
  58. res = t
  59. case DB_Binary, DB_VarBinary:
  60. return DB_Bytea
  61. case DB_DateTime:
  62. res = DB_TimeStamp
  63. case DB_TimeStampz:
  64. return "timestamp with time zone"
  65. case DB_Float:
  66. res = DB_Real
  67. case DB_TinyText, DB_MediumText, DB_LongText:
  68. res = DB_Text
  69. case DB_NVarchar:
  70. res = DB_Varchar
  71. case DB_Uuid:
  72. res = DB_Uuid
  73. case DB_Blob, DB_TinyBlob, DB_MediumBlob, DB_LongBlob:
  74. return DB_Bytea
  75. case DB_Double:
  76. return "DOUBLE PRECISION"
  77. default:
  78. if c.IsAutoIncrement {
  79. return DB_Serial
  80. }
  81. res = t
  82. }
  83. var hasLen1 bool = (c.Length > 0)
  84. var hasLen2 bool = (c.Length2 > 0)
  85. if hasLen2 {
  86. res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
  87. } else if hasLen1 {
  88. res += "(" + strconv.Itoa(c.Length) + ")"
  89. }
  90. return res
  91. }
  92. func (db *Postgres) TableCheckSql(tableName string) (string, []interface{}) {
  93. args := []interface{}{"grafana", tableName}
  94. sql := "SELECT table_name FROM information_schema.tables WHERE table_schema=? and table_name=?"
  95. return sql, args
  96. }
  97. func (db *Postgres) DropIndexSql(tableName string, index *Index) string {
  98. quote := db.Quote
  99. idxName := index.XName(tableName)
  100. return fmt.Sprintf("DROP INDEX %v", quote(idxName))
  101. }
  102. func (db *Postgres) UpdateTableSql(tableName string, columns []*Column) string {
  103. var statements = []string{}
  104. for _, col := range columns {
  105. statements = append(statements, "ALTER "+db.QuoteStr()+col.Name+db.QuoteStr()+" TYPE "+db.SqlType(col))
  106. }
  107. return "ALTER TABLE " + db.Quote(tableName) + " " + strings.Join(statements, ", ") + ";"
  108. }