postgres_dialect.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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) 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. } else {
  38. return "TRUE"
  39. }
  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 bool = (c.Length > 0)
  83. var hasLen2 bool = (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. }