dialect.go 3.8 KB

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