sqlite_dialect.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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) DateTimeFunc(value string) string {
  31. return "datetime(" + value + ")"
  32. }
  33. func (db *Sqlite3) SqlType(c *Column) string {
  34. switch c.Type {
  35. case DB_Date, DB_DateTime, DB_TimeStamp, DB_Time:
  36. return DB_DateTime
  37. case DB_TimeStampz:
  38. return DB_Text
  39. case DB_Char, DB_Varchar, DB_NVarchar, DB_TinyText, DB_Text, DB_MediumText, DB_LongText:
  40. return DB_Text
  41. case DB_Bit, DB_TinyInt, DB_SmallInt, DB_MediumInt, DB_Int, DB_Integer, DB_BigInt, DB_Bool:
  42. return DB_Integer
  43. case DB_Float, DB_Double, DB_Real:
  44. return DB_Real
  45. case DB_Decimal, DB_Numeric:
  46. return DB_Numeric
  47. case DB_TinyBlob, DB_Blob, DB_MediumBlob, DB_LongBlob, DB_Bytea, DB_Binary, DB_VarBinary:
  48. return DB_Blob
  49. case DB_Serial, DB_BigSerial:
  50. c.IsPrimaryKey = true
  51. c.IsAutoIncrement = true
  52. c.Nullable = false
  53. return DB_Integer
  54. default:
  55. return c.Type
  56. }
  57. }
  58. func (db *Sqlite3) TableCheckSql(tableName string) (string, []interface{}) {
  59. args := []interface{}{tableName}
  60. return "SELECT name FROM sqlite_master WHERE type='table' and name = ?", args
  61. }
  62. func (db *Sqlite3) DropIndexSql(tableName string, index *Index) string {
  63. quote := db.Quote
  64. //var unique string
  65. idxName := index.XName(tableName)
  66. return fmt.Sprintf("DROP INDEX %v", quote(idxName))
  67. }