dialect.go 4.1 KB

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