dialect.go 6.0 KB

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