mysql_dialect.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package migrator
  2. import "strconv"
  3. type Mysql struct {
  4. BaseDialect
  5. }
  6. func NewMysqlDialect() *Mysql {
  7. d := Mysql{}
  8. d.BaseDialect.dialect = &d
  9. d.BaseDialect.driverName = MYSQL
  10. return &d
  11. }
  12. func (db *Mysql) SupportEngine() bool {
  13. return true
  14. }
  15. func (db *Mysql) Quote(name string) string {
  16. return "`" + name + "`"
  17. }
  18. func (db *Mysql) QuoteStr() string {
  19. return "`"
  20. }
  21. func (db *Mysql) AutoIncrStr() string {
  22. return "AUTO_INCREMENT"
  23. }
  24. func (db *Mysql) BooleanStr(value bool) string {
  25. if value {
  26. return "1"
  27. }
  28. return "0"
  29. }
  30. func (db *Mysql) SqlType(c *Column) string {
  31. var res string
  32. switch c.Type {
  33. case DB_Bool:
  34. res = DB_TinyInt
  35. c.Length = 1
  36. case DB_Serial:
  37. c.IsAutoIncrement = true
  38. c.IsPrimaryKey = true
  39. c.Nullable = false
  40. res = DB_Int
  41. case DB_BigSerial:
  42. c.IsAutoIncrement = true
  43. c.IsPrimaryKey = true
  44. c.Nullable = false
  45. res = DB_BigInt
  46. case DB_Bytea:
  47. res = DB_Blob
  48. case DB_TimeStampz:
  49. res = DB_Char
  50. c.Length = 64
  51. case DB_NVarchar:
  52. res = DB_Varchar
  53. default:
  54. res = c.Type
  55. }
  56. var hasLen1 bool = (c.Length > 0)
  57. var hasLen2 bool = (c.Length2 > 0)
  58. if res == DB_BigInt && !hasLen1 && !hasLen2 {
  59. c.Length = 20
  60. hasLen1 = true
  61. }
  62. if hasLen2 {
  63. res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
  64. } else if hasLen1 {
  65. res += "(" + strconv.Itoa(c.Length) + ")"
  66. }
  67. return res
  68. }
  69. func (db *Mysql) TableCheckSql(tableName string) (string, []interface{}) {
  70. args := []interface{}{"grafana", tableName}
  71. sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?"
  72. return sql, args
  73. }