dialect.go 6.2 KB

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