mysql_dialect.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package migrations
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. type Mysql struct {
  7. BaseDialect
  8. }
  9. func NewMysqlDialect() *Mysql {
  10. d := Mysql{}
  11. d.BaseDialect.dialect = &d
  12. d.BaseDialect.driverName = MYSQL
  13. return &d
  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) ToDBTypeSql(columnType ColumnType, length int) string {
  64. switch columnType {
  65. case DB_TYPE_STRING:
  66. return fmt.Sprintf("NVARCHAR(%d)", length)
  67. }
  68. panic("Unsupported db type")
  69. }
  70. func (db *Mysql) TableCheckSql(tableName string) (string, []interface{}) {
  71. args := []interface{}{"grafana", tableName}
  72. sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?"
  73. return sql, args
  74. }