dialect.go 3.9 KB

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