dialect.go 3.7 KB

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