sqlite_dialect.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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) TableCheckSql(tableName string) (string, []interface{}) {
  61. args := []interface{}{tableName}
  62. return "SELECT name FROM sqlite_master WHERE type='table' and name = ?", args
  63. }
  64. func (db *Sqlite3) DropIndexSql(tableName string, index *Index) string {
  65. quote := db.Quote
  66. //var unique string
  67. idxName := index.XName(tableName)
  68. return fmt.Sprintf("DROP INDEX %v", quote(idxName))
  69. }
  70. func (db *Sqlite3) CleanDB() error {
  71. return nil
  72. }
  73. func (db *Sqlite3) IsUniqueConstraintViolation(err error) bool {
  74. if driverErr, ok := err.(sqlite3.Error); ok {
  75. if driverErr.ExtendedCode == sqlite3.ErrConstraintUnique {
  76. return true
  77. }
  78. }
  79. return false
  80. }