sqlite_dialect.go 1.7 KB

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