postgres_dialect.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package migrator
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. type Postgres struct {
  7. BaseDialect
  8. }
  9. func NewPostgresDialect() *Postgres {
  10. d := Postgres{}
  11. d.BaseDialect.dialect = &d
  12. d.BaseDialect.driverName = POSTGRES
  13. return &d
  14. }
  15. func (db *Postgres) SupportEngine() bool {
  16. return false
  17. }
  18. func (db *Postgres) Quote(name string) string {
  19. return "\"" + name + "\""
  20. }
  21. func (db *Postgres) QuoteStr() string {
  22. return "\""
  23. }
  24. func (b *Postgres) LikeStr() string {
  25. return "ILIKE"
  26. }
  27. func (db *Postgres) AutoIncrStr() string {
  28. return ""
  29. }
  30. func (db *Postgres) SqlType(c *Column) string {
  31. var res string
  32. switch t := c.Type; t {
  33. case DB_TinyInt:
  34. res = DB_SmallInt
  35. return res
  36. case DB_MediumInt, DB_Int, DB_Integer:
  37. if c.IsAutoIncrement {
  38. return DB_Serial
  39. }
  40. return DB_Integer
  41. case DB_Serial, DB_BigSerial:
  42. c.IsAutoIncrement = true
  43. c.Nullable = false
  44. res = t
  45. case DB_Binary, DB_VarBinary:
  46. return DB_Bytea
  47. case DB_DateTime:
  48. res = DB_TimeStamp
  49. case DB_TimeStampz:
  50. return "timestamp with time zone"
  51. case DB_Float:
  52. res = DB_Real
  53. case DB_TinyText, DB_MediumText, DB_LongText:
  54. res = DB_Text
  55. case DB_NVarchar:
  56. res = DB_Varchar
  57. case DB_Uuid:
  58. res = DB_Uuid
  59. case DB_Blob, DB_TinyBlob, DB_MediumBlob, DB_LongBlob:
  60. return DB_Bytea
  61. case DB_Double:
  62. return "DOUBLE PRECISION"
  63. default:
  64. if c.IsAutoIncrement {
  65. return DB_Serial
  66. }
  67. res = t
  68. }
  69. var hasLen1 bool = (c.Length > 0)
  70. var hasLen2 bool = (c.Length2 > 0)
  71. if hasLen2 {
  72. res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
  73. } else if hasLen1 {
  74. res += "(" + strconv.Itoa(c.Length) + ")"
  75. }
  76. return res
  77. }
  78. func (db *Postgres) TableCheckSql(tableName string) (string, []interface{}) {
  79. args := []interface{}{"grafana", tableName}
  80. sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?"
  81. return sql, args
  82. }
  83. func (db *Postgres) DropIndexSql(tableName string, index *Index) string {
  84. quote := db.Quote
  85. idxName := index.XName(tableName)
  86. return fmt.Sprintf("DROP INDEX %v", quote(idxName))
  87. }