dialect.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package migrator
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type Dialect interface {
  7. DriverName() string
  8. QuoteStr() string
  9. Quote(string) string
  10. AndStr() string
  11. AutoIncrStr() string
  12. OrStr() string
  13. EqStr() string
  14. ShowCreateNull() bool
  15. SqlType(col *Column) string
  16. SupportEngine() bool
  17. LikeStr() string
  18. Default(col *Column) string
  19. BooleanStr(bool) string
  20. DateTimeFunc(string) string
  21. CreateIndexSql(tableName string, index *Index) string
  22. CreateTableSql(table *Table) string
  23. AddColumnSql(tableName string, col *Column) string
  24. CopyTableData(sourceTable string, targetTable string, sourceCols []string, targetCols []string) string
  25. DropTable(tableName string) string
  26. DropIndexSql(tableName string, index *Index) string
  27. TableCheckSql(tableName string) (string, []interface{})
  28. RenameTable(oldName string, newName string) string
  29. UpdateTableSql(tableName string, columns []*Column) string
  30. }
  31. func NewDialect(name string) Dialect {
  32. switch name {
  33. case MYSQL:
  34. return NewMysqlDialect()
  35. case SQLITE:
  36. return NewSqlite3Dialect()
  37. case POSTGRES:
  38. return NewPostgresDialect()
  39. }
  40. panic("Unsupported database type: " + name)
  41. }
  42. type BaseDialect struct {
  43. dialect Dialect
  44. driverName string
  45. }
  46. func (d *BaseDialect) DriverName() string {
  47. return d.driverName
  48. }
  49. func (b *BaseDialect) ShowCreateNull() bool {
  50. return true
  51. }
  52. func (b *BaseDialect) AndStr() string {
  53. return "AND"
  54. }
  55. func (b *BaseDialect) LikeStr() string {
  56. return "LIKE"
  57. }
  58. func (b *BaseDialect) OrStr() string {
  59. return "OR"
  60. }
  61. func (b *BaseDialect) EqStr() string {
  62. return "="
  63. }
  64. func (b *BaseDialect) Default(col *Column) string {
  65. return col.Default
  66. }
  67. func (db *BaseDialect) DateTimeFunc(value string) string {
  68. return value
  69. }
  70. func (b *BaseDialect) CreateTableSql(table *Table) string {
  71. sql := "CREATE TABLE IF NOT EXISTS "
  72. sql += b.dialect.Quote(table.Name) + " (\n"
  73. pkList := table.PrimaryKeys
  74. for _, col := range table.Columns {
  75. if col.IsPrimaryKey && len(pkList) == 1 {
  76. sql += col.String(b.dialect)
  77. } else {
  78. sql += col.StringNoPk(b.dialect)
  79. }
  80. sql = strings.TrimSpace(sql)
  81. sql += "\n, "
  82. }
  83. if len(pkList) > 1 {
  84. sql += "PRIMARY KEY ( "
  85. sql += b.dialect.Quote(strings.Join(pkList, b.dialect.Quote(",")))
  86. sql += " ), "
  87. }
  88. sql = sql[:len(sql)-2] + ")"
  89. if b.dialect.SupportEngine() {
  90. sql += " ENGINE=InnoDB DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci"
  91. }
  92. sql += ";"
  93. return sql
  94. }
  95. func (db *BaseDialect) AddColumnSql(tableName string, col *Column) string {
  96. return fmt.Sprintf("alter table %s ADD COLUMN %s", db.dialect.Quote(tableName), col.StringNoPk(db.dialect))
  97. }
  98. func (db *BaseDialect) CreateIndexSql(tableName string, index *Index) string {
  99. quote := db.dialect.Quote
  100. var unique string
  101. if index.Type == UniqueIndex {
  102. unique = " UNIQUE"
  103. }
  104. idxName := index.XName(tableName)
  105. return fmt.Sprintf("CREATE%s INDEX %v ON %v (%v);", unique,
  106. quote(idxName), quote(tableName),
  107. quote(strings.Join(index.Cols, quote(","))))
  108. }
  109. func (db *BaseDialect) QuoteColList(cols []string) string {
  110. var sourceColsSql = ""
  111. for _, col := range cols {
  112. sourceColsSql += db.dialect.Quote(col)
  113. sourceColsSql += "\n, "
  114. }
  115. return strings.TrimSuffix(sourceColsSql, "\n, ")
  116. }
  117. func (db *BaseDialect) CopyTableData(sourceTable string, targetTable string, sourceCols []string, targetCols []string) string {
  118. sourceColsSql := db.QuoteColList(sourceCols)
  119. targetColsSql := db.QuoteColList(targetCols)
  120. quote := db.dialect.Quote
  121. return fmt.Sprintf("INSERT INTO %s (%s) SELECT %s FROM %s", quote(targetTable), targetColsSql, sourceColsSql, quote(sourceTable))
  122. }
  123. func (db *BaseDialect) DropTable(tableName string) string {
  124. quote := db.dialect.Quote
  125. return fmt.Sprintf("DROP TABLE IF EXISTS %s", quote(tableName))
  126. }
  127. func (db *BaseDialect) RenameTable(oldName string, newName string) string {
  128. quote := db.dialect.Quote
  129. return fmt.Sprintf("ALTER TABLE %s RENAME TO %s", quote(oldName), quote(newName))
  130. }
  131. func (db *BaseDialect) DropIndexSql(tableName string, index *Index) string {
  132. quote := db.dialect.Quote
  133. name := index.XName(tableName)
  134. return fmt.Sprintf("DROP INDEX %v ON %s", quote(name), quote(tableName))
  135. }
  136. func (db *BaseDialect) UpdateTableSql(tableName string, columns []*Column) string {
  137. return "-- NOT REQUIRED"
  138. }