mysql.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package mysql
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "math"
  6. _ "github.com/go-sql-driver/mysql"
  7. "github.com/jinzhu/gorm"
  8. "github.com/jinzhu/gorm/callbacks"
  9. "github.com/jinzhu/gorm/logger"
  10. "github.com/jinzhu/gorm/migrator"
  11. "github.com/jinzhu/gorm/schema"
  12. )
  13. type Dialector struct {
  14. DSN string
  15. }
  16. func Open(dsn string) gorm.Dialector {
  17. return &Dialector{DSN: dsn}
  18. }
  19. func (dialector Dialector) Initialize(db *gorm.DB) (err error) {
  20. // register callbacks
  21. callbacks.RegisterDefaultCallbacks(db)
  22. db.DB, err = sql.Open("mysql", dialector.DSN)
  23. return
  24. }
  25. func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator {
  26. return Migrator{migrator.Migrator{Config: migrator.Config{
  27. DB: db,
  28. Dialector: dialector,
  29. }}}
  30. }
  31. func (dialector Dialector) BindVar(stmt *gorm.Statement, v interface{}) string {
  32. return "?"
  33. }
  34. func (dialector Dialector) QuoteChars() [2]byte {
  35. return [2]byte{'`', '`'} // `name`
  36. }
  37. func (dialector Dialector) Explain(sql string, vars ...interface{}) string {
  38. return logger.ExplainSQL(sql, nil, `"`, vars...)
  39. }
  40. func (dialector Dialector) DataTypeOf(field *schema.Field) string {
  41. switch field.DataType {
  42. case schema.Bool:
  43. return "boolean"
  44. case schema.Int, schema.Uint:
  45. sqlType := "int"
  46. switch {
  47. case field.Size <= 8:
  48. sqlType = "tinyint"
  49. case field.Size <= 16:
  50. sqlType = "smallint"
  51. case field.Size <= 32:
  52. sqlType = "int"
  53. default:
  54. sqlType = "bigint"
  55. }
  56. if field.DataType == schema.Uint {
  57. sqlType += " unsigned"
  58. }
  59. if field.AutoIncrement {
  60. sqlType += " AUTO_INCREMENT"
  61. }
  62. return sqlType
  63. case schema.Float:
  64. if field.Size <= 32 {
  65. return "float"
  66. }
  67. return "double"
  68. case schema.String:
  69. size := field.Size
  70. if field.PrimaryKey && size == 0 {
  71. size = 256
  72. }
  73. if size >= 65536 && size <= int(math.Pow(2, 24)) {
  74. return "mediumtext"
  75. } else if size > int(math.Pow(2, 24)) || size <= 0 {
  76. return "longtext"
  77. }
  78. return fmt.Sprintf("varchar(%d)", size)
  79. case schema.Time:
  80. precision := ""
  81. if field.Precision > 0 {
  82. precision = fmt.Sprintf("(%d)", field.Precision)
  83. }
  84. if field.NotNull || field.PrimaryKey {
  85. return "datetime" + precision
  86. }
  87. return "datetime" + precision + " NULL"
  88. case schema.Bytes:
  89. if field.Size > 0 && field.Size < 65536 {
  90. return fmt.Sprintf("varbinary(%d)", field.Size)
  91. }
  92. if field.Size >= 65536 && field.Size <= int(math.Pow(2, 24)) {
  93. return "mediumblob"
  94. }
  95. return "longblob"
  96. }
  97. return ""
  98. }