sqlite_dialect.go 1.8 KB

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