mysql_dialect.go 1.4 KB

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