postgres_dialect.go 2.9 KB

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