dialect.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. CreateIndexSql(tableName string, index *Index) string
  18. CreateTableSql(table *Table) string
  19. AddColumnSql(tableName string, Col *Column) string
  20. TableCheckSql(tableName string) (string, []interface{})
  21. }
  22. func NewDialect(name string) Dialect {
  23. switch name {
  24. case MYSQL:
  25. return NewMysqlDialect()
  26. case SQLITE:
  27. return NewSqlite3Dialect()
  28. case POSTGRES:
  29. return NewPostgresDialect()
  30. }
  31. panic("Unsupported database type: " + name)
  32. }
  33. type BaseDialect struct {
  34. dialect Dialect
  35. driverName string
  36. }
  37. func (d *BaseDialect) DriverName() string {
  38. return d.driverName
  39. }
  40. func (b *BaseDialect) ShowCreateNull() bool {
  41. return true
  42. }
  43. func (b *BaseDialect) AndStr() string {
  44. return "AND"
  45. }
  46. func (b *BaseDialect) OrStr() string {
  47. return "OR"
  48. }
  49. func (b *BaseDialect) EqStr() string {
  50. return "="
  51. }
  52. func (b *BaseDialect) CreateTableSql(table *Table) string {
  53. var sql string
  54. sql = "CREATE TABLE IF NOT EXISTS "
  55. sql += b.dialect.Quote(table.Name) + " (\n"
  56. pkList := table.PrimaryKeys
  57. for _, col := range table.Columns {
  58. if col.IsPrimaryKey && len(pkList) == 1 {
  59. sql += col.String(b.dialect)
  60. } else {
  61. sql += col.StringNoPk(b.dialect)
  62. }
  63. sql = strings.TrimSpace(sql)
  64. sql += "\n, "
  65. }
  66. if len(pkList) > 1 {
  67. sql += "PRIMARY KEY ( "
  68. sql += b.dialect.Quote(strings.Join(pkList, b.dialect.Quote(",")))
  69. sql += " ), "
  70. }
  71. sql = sql[:len(sql)-2] + ")"
  72. if b.dialect.SupportEngine() {
  73. sql += " ENGINE=InnoDB DEFAULT CHARSET UTF8 "
  74. }
  75. sql += ";"
  76. return sql
  77. }
  78. func (db *BaseDialect) AddColumnSql(tableName string, col *Column) string {
  79. return fmt.Sprintf("alter table %s ADD COLUMN %s", tableName, col.StringNoPk(db.dialect))
  80. }
  81. func (db *BaseDialect) CreateIndexSql(tableName string, index *Index) string {
  82. quote := db.dialect.Quote
  83. var unique string
  84. var idxName string
  85. if index.Type == UniqueIndex {
  86. unique = " UNIQUE"
  87. idxName = fmt.Sprintf("UQE_%v_%v", tableName, index.Name)
  88. } else {
  89. idxName = fmt.Sprintf("IDX_%v_%v", tableName, index.Name)
  90. }
  91. return fmt.Sprintf("CREATE%s INDEX %v ON %v (%v);", unique,
  92. quote(idxName), quote(tableName),
  93. quote(strings.Join(index.Cols, quote(","))))
  94. }