mysql_dialect.go 1.5 KB

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