mysql_dialect.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. return strconv.FormatBool(value)
  26. }
  27. func (db *Mysql) SqlType(c *Column) string {
  28. var res string
  29. switch c.Type {
  30. case DB_Bool:
  31. res = DB_TinyInt
  32. c.Length = 1
  33. case DB_Serial:
  34. c.IsAutoIncrement = true
  35. c.IsPrimaryKey = true
  36. c.Nullable = false
  37. res = DB_Int
  38. case DB_BigSerial:
  39. c.IsAutoIncrement = true
  40. c.IsPrimaryKey = true
  41. c.Nullable = false
  42. res = DB_BigInt
  43. case DB_Bytea:
  44. res = DB_Blob
  45. case DB_TimeStampz:
  46. res = DB_Char
  47. c.Length = 64
  48. case DB_NVarchar:
  49. res = DB_Varchar
  50. default:
  51. res = c.Type
  52. }
  53. var hasLen1 bool = (c.Length > 0)
  54. var hasLen2 bool = (c.Length2 > 0)
  55. if res == DB_BigInt && !hasLen1 && !hasLen2 {
  56. c.Length = 20
  57. hasLen1 = true
  58. }
  59. if hasLen2 {
  60. res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
  61. } else if hasLen1 {
  62. res += "(" + strconv.Itoa(c.Length) + ")"
  63. }
  64. return res
  65. }
  66. func (db *Mysql) TableCheckSql(tableName string) (string, []interface{}) {
  67. args := []interface{}{"grafana", tableName}
  68. sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?"
  69. return sql, args
  70. }