dialect.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package migrator
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/go-xorm/xorm"
  6. )
  7. type Dialect interface {
  8. DriverName() 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. DateTimeFunc(string) string
  21. CreateIndexSql(tableName string, index *Index) string
  22. CreateTableSql(table *Table) string
  23. AddColumnSql(tableName string, col *Column) string
  24. CopyTableData(sourceTable string, targetTable string, sourceCols []string, targetCols []string) string
  25. DropTable(tableName string) string
  26. DropIndexSql(tableName string, index *Index) string
  27. TableCheckSql(tableName string) (string, []interface{})
  28. RenameTable(oldName string, newName string) string
  29. UpdateTableSql(tableName string, columns []*Column) string
  30. ColString(*Column) string
  31. ColStringNoPk(*Column) string
  32. Limit(limit int64) string
  33. LimitOffset(limit int64, offset int64) string
  34. PreInsertId(table string, sess *xorm.Session) error
  35. PostInsertId(table string, sess *xorm.Session) error
  36. CleanDB() error
  37. NoOpSql() string
  38. IsUniqueConstraintViolation(err error) bool
  39. }
  40. func NewDialect(engine *xorm.Engine) Dialect {
  41. name := engine.DriverName()
  42. switch name {
  43. case MYSQL:
  44. return NewMysqlDialect(engine)
  45. case SQLITE:
  46. return NewSqlite3Dialect(engine)
  47. case POSTGRES:
  48. return NewPostgresDialect(engine)
  49. }
  50. panic("Unsupported database type: " + name)
  51. }
  52. type BaseDialect struct {
  53. dialect Dialect
  54. engine *xorm.Engine
  55. driverName string
  56. }
  57. func (d *BaseDialect) DriverName() string {
  58. return d.driverName
  59. }
  60. func (b *BaseDialect) ShowCreateNull() bool {
  61. return true
  62. }
  63. func (b *BaseDialect) AndStr() string {
  64. return "AND"
  65. }
  66. func (b *BaseDialect) LikeStr() string {
  67. return "LIKE"
  68. }
  69. func (b *BaseDialect) OrStr() string {
  70. return "OR"
  71. }
  72. func (b *BaseDialect) EqStr() string {
  73. return "="
  74. }
  75. func (b *BaseDialect) Default(col *Column) string {
  76. return col.Default
  77. }
  78. func (db *BaseDialect) DateTimeFunc(value string) string {
  79. return value
  80. }
  81. func (b *BaseDialect) CreateTableSql(table *Table) string {
  82. sql := "CREATE TABLE IF NOT EXISTS "
  83. sql += b.dialect.Quote(table.Name) + " (\n"
  84. pkList := table.PrimaryKeys
  85. for _, col := range table.Columns {
  86. if col.IsPrimaryKey && len(pkList) == 1 {
  87. sql += col.String(b.dialect)
  88. } else {
  89. sql += col.StringNoPk(b.dialect)
  90. }
  91. sql = strings.TrimSpace(sql)
  92. sql += "\n, "
  93. }
  94. if len(pkList) > 1 {
  95. quotedCols := []string{}
  96. for _, col := range pkList {
  97. quotedCols = append(quotedCols, b.dialect.Quote(col))
  98. }
  99. sql += "PRIMARY KEY ( " + strings.Join(quotedCols, ",") + " ), "
  100. }
  101. sql = sql[:len(sql)-2] + ")"
  102. if b.dialect.SupportEngine() {
  103. sql += " ENGINE=InnoDB DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci"
  104. }
  105. sql += ";"
  106. return sql
  107. }
  108. func (db *BaseDialect) AddColumnSql(tableName string, col *Column) string {
  109. return fmt.Sprintf("alter table %s ADD COLUMN %s", db.dialect.Quote(tableName), col.StringNoPk(db.dialect))
  110. }
  111. func (db *BaseDialect) CreateIndexSql(tableName string, index *Index) string {
  112. quote := db.dialect.Quote
  113. var unique string
  114. if index.Type == UniqueIndex {
  115. unique = " UNIQUE"
  116. }
  117. idxName := index.XName(tableName)
  118. quotedCols := []string{}
  119. for _, col := range index.Cols {
  120. quotedCols = append(quotedCols, db.dialect.Quote(col))
  121. }
  122. return fmt.Sprintf("CREATE%s INDEX %v ON %v (%v);", unique, quote(idxName), quote(tableName), strings.Join(quotedCols, ","))
  123. }
  124. func (db *BaseDialect) QuoteColList(cols []string) string {
  125. var sourceColsSql = ""
  126. for _, col := range cols {
  127. sourceColsSql += db.dialect.Quote(col)
  128. sourceColsSql += "\n, "
  129. }
  130. return strings.TrimSuffix(sourceColsSql, "\n, ")
  131. }
  132. func (db *BaseDialect) CopyTableData(sourceTable string, targetTable string, sourceCols []string, targetCols []string) string {
  133. sourceColsSql := db.QuoteColList(sourceCols)
  134. targetColsSql := db.QuoteColList(targetCols)
  135. quote := db.dialect.Quote
  136. return fmt.Sprintf("INSERT INTO %s (%s) SELECT %s FROM %s", quote(targetTable), targetColsSql, sourceColsSql, quote(sourceTable))
  137. }
  138. func (db *BaseDialect) DropTable(tableName string) string {
  139. quote := db.dialect.Quote
  140. return fmt.Sprintf("DROP TABLE IF EXISTS %s", quote(tableName))
  141. }
  142. func (db *BaseDialect) RenameTable(oldName string, newName string) string {
  143. quote := db.dialect.Quote
  144. return fmt.Sprintf("ALTER TABLE %s RENAME TO %s", quote(oldName), quote(newName))
  145. }
  146. func (db *BaseDialect) DropIndexSql(tableName string, index *Index) string {
  147. quote := db.dialect.Quote
  148. name := index.XName(tableName)
  149. return fmt.Sprintf("DROP INDEX %v ON %s", quote(name), quote(tableName))
  150. }
  151. func (db *BaseDialect) UpdateTableSql(tableName string, columns []*Column) string {
  152. return "-- NOT REQUIRED"
  153. }
  154. func (db *BaseDialect) ColString(col *Column) string {
  155. sql := db.dialect.Quote(col.Name) + " "
  156. sql += db.dialect.SqlType(col) + " "
  157. if col.IsPrimaryKey {
  158. sql += "PRIMARY KEY "
  159. if col.IsAutoIncrement {
  160. sql += db.dialect.AutoIncrStr() + " "
  161. }
  162. }
  163. if db.dialect.ShowCreateNull() {
  164. if col.Nullable {
  165. sql += "NULL "
  166. } else {
  167. sql += "NOT NULL "
  168. }
  169. }
  170. if col.Default != "" {
  171. sql += "DEFAULT " + db.dialect.Default(col) + " "
  172. }
  173. return sql
  174. }
  175. func (db *BaseDialect) ColStringNoPk(col *Column) string {
  176. sql := db.dialect.Quote(col.Name) + " "
  177. sql += db.dialect.SqlType(col) + " "
  178. if db.dialect.ShowCreateNull() {
  179. if col.Nullable {
  180. sql += "NULL "
  181. } else {
  182. sql += "NOT NULL "
  183. }
  184. }
  185. if col.Default != "" {
  186. sql += "DEFAULT " + db.dialect.Default(col) + " "
  187. }
  188. return sql
  189. }
  190. func (db *BaseDialect) Limit(limit int64) string {
  191. return fmt.Sprintf(" LIMIT %d", limit)
  192. }
  193. func (db *BaseDialect) LimitOffset(limit int64, offset int64) string {
  194. return fmt.Sprintf(" LIMIT %d OFFSET %d", limit, offset)
  195. }
  196. func (db *BaseDialect) PreInsertId(table string, sess *xorm.Session) error {
  197. return nil
  198. }
  199. func (db *BaseDialect) PostInsertId(table string, sess *xorm.Session) error {
  200. return nil
  201. }
  202. func (db *BaseDialect) CleanDB() error {
  203. return nil
  204. }
  205. func (db *BaseDialect) NoOpSql() string {
  206. return "SELECT 0;"
  207. }