postgres_dialect.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package migrations
  2. import (
  3. "strconv"
  4. "github.com/go-xorm/core"
  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) Quote(name string) string {
  16. return "\"" + name + "\""
  17. }
  18. func (db *Postgres) QuoteStr() string {
  19. return "\""
  20. }
  21. func (db *Postgres) AutoIncrStr() string {
  22. return ""
  23. }
  24. func (db *Postgres) SqlType(c *Column) string {
  25. var res string
  26. switch t := c.Type; t {
  27. case DB_TinyInt:
  28. res = DB_SmallInt
  29. return res
  30. case DB_MediumInt, core.Int, core.Integer:
  31. if c.IsAutoIncrement {
  32. return DB_Serial
  33. }
  34. return DB_Integer
  35. case DB_Serial, core.BigSerial:
  36. c.IsAutoIncrement = true
  37. c.Nullable = false
  38. res = t
  39. case DB_Binary, core.VarBinary:
  40. return DB_Bytea
  41. case DB_DateTime:
  42. res = DB_TimeStamp
  43. case DB_TimeStampz:
  44. return "timestamp with time zone"
  45. case DB_Float:
  46. res = DB_Real
  47. case DB_TinyText, core.MediumText, core.LongText:
  48. res = DB_Text
  49. case DB_NVarchar:
  50. res = DB_Varchar
  51. case DB_Uuid:
  52. res = DB_Uuid
  53. case DB_Blob, core.TinyBlob, core.MediumBlob, core.LongBlob:
  54. return DB_Bytea
  55. case DB_Double:
  56. return "DOUBLE PRECISION"
  57. default:
  58. if c.IsAutoIncrement {
  59. return DB_Serial
  60. }
  61. res = t
  62. }
  63. var hasLen1 bool = (c.Length > 0)
  64. var hasLen2 bool = (c.Length2 > 0)
  65. if hasLen2 {
  66. res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
  67. } else if hasLen1 {
  68. res += "(" + strconv.Itoa(c.Length) + ")"
  69. }
  70. return res
  71. }
  72. func (db *Postgres) TableCheckSql(tableName string) (string, []interface{}) {
  73. args := []interface{}{"grafana", tableName}
  74. sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?"
  75. return sql, args
  76. }