sqlite_dialect.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package migrator
  2. import (
  3. "fmt"
  4. "github.com/go-xorm/xorm"
  5. sqlite3 "github.com/mattn/go-sqlite3"
  6. )
  7. type Sqlite3 struct {
  8. BaseDialect
  9. }
  10. func NewSqlite3Dialect(engine *xorm.Engine) *Sqlite3 {
  11. d := Sqlite3{}
  12. d.BaseDialect.dialect = &d
  13. d.BaseDialect.engine = engine
  14. d.BaseDialect.driverName = SQLITE
  15. return &d
  16. }
  17. func (db *Sqlite3) SupportEngine() bool {
  18. return false
  19. }
  20. func (db *Sqlite3) Quote(name string) string {
  21. return "`" + name + "`"
  22. }
  23. func (db *Sqlite3) AutoIncrStr() string {
  24. return "AUTOINCREMENT"
  25. }
  26. func (db *Sqlite3) BooleanStr(value bool) string {
  27. if value {
  28. return "1"
  29. }
  30. return "0"
  31. }
  32. func (db *Sqlite3) DateTimeFunc(value string) string {
  33. return "datetime(" + value + ")"
  34. }
  35. func (db *Sqlite3) SqlType(c *Column) string {
  36. switch c.Type {
  37. case DB_Date, DB_DateTime, DB_TimeStamp, DB_Time:
  38. return DB_DateTime
  39. case DB_TimeStampz:
  40. return DB_Text
  41. case DB_Char, DB_Varchar, DB_NVarchar, DB_TinyText, DB_Text, DB_MediumText, DB_LongText:
  42. return DB_Text
  43. case DB_Bit, DB_TinyInt, DB_SmallInt, DB_MediumInt, DB_Int, DB_Integer, DB_BigInt, DB_Bool:
  44. return DB_Integer
  45. case DB_Float, DB_Double, DB_Real:
  46. return DB_Real
  47. case DB_Decimal, DB_Numeric:
  48. return DB_Numeric
  49. case DB_TinyBlob, DB_Blob, DB_MediumBlob, DB_LongBlob, DB_Bytea, DB_Binary, DB_VarBinary:
  50. return DB_Blob
  51. case DB_Serial, DB_BigSerial:
  52. c.IsPrimaryKey = true
  53. c.IsAutoIncrement = true
  54. c.Nullable = false
  55. return DB_Integer
  56. default:
  57. return c.Type
  58. }
  59. }
  60. func (db *Sqlite3) IndexCheckSql(tableName, indexName string) (string, []interface{}) {
  61. args := []interface{}{tableName, indexName}
  62. sql := "SELECT 1 FROM " + db.Quote("sqlite_master") + " WHERE " + db.Quote("type") + "='index' AND " + db.Quote("tbl_name") + "=? AND " + db.Quote("name") + "=?"
  63. return sql, args
  64. }
  65. func (db *Sqlite3) DropIndexSql(tableName string, index *Index) string {
  66. quote := db.Quote
  67. //var unique string
  68. idxName := index.XName(tableName)
  69. return fmt.Sprintf("DROP INDEX %v", quote(idxName))
  70. }
  71. func (db *Sqlite3) CleanDB() error {
  72. return nil
  73. }
  74. func (db *Sqlite3) isThisError(err error, errcode int) bool {
  75. if driverErr, ok := err.(sqlite3.Error); ok {
  76. if int(driverErr.ExtendedCode) == errcode {
  77. return true
  78. }
  79. }
  80. return false
  81. }
  82. func (db *Sqlite3) IsUniqueConstraintViolation(err error) bool {
  83. return db.isThisError(err, int(sqlite3.ErrConstraintUnique))
  84. }
  85. func (db *Sqlite3) IsDeadlock(err error) bool {
  86. return false // No deadlock
  87. }