dialect_sqlite3.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package gorm
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strings"
  6. "time"
  7. )
  8. type sqlite3 struct {
  9. commonDialect
  10. }
  11. func init() {
  12. RegisterDialect("sqlite3", &sqlite3{})
  13. }
  14. func (sqlite3) GetName() string {
  15. return "sqlite3"
  16. }
  17. // Get Data Type for Sqlite Dialect
  18. func (s *sqlite3) DataTypeOf(field *StructField) string {
  19. var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field, s)
  20. if sqlType == "" {
  21. switch dataValue.Kind() {
  22. case reflect.Bool:
  23. sqlType = "bool"
  24. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
  25. if field.IsPrimaryKey {
  26. field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
  27. sqlType = "integer primary key autoincrement"
  28. } else {
  29. sqlType = "integer"
  30. }
  31. case reflect.Int64, reflect.Uint64:
  32. if field.IsPrimaryKey {
  33. field.TagSettings["AUTO_INCREMENT"] = "AUTO_INCREMENT"
  34. sqlType = "integer primary key autoincrement"
  35. } else {
  36. sqlType = "bigint"
  37. }
  38. case reflect.Float32, reflect.Float64:
  39. sqlType = "real"
  40. case reflect.String:
  41. if size > 0 && size < 65532 {
  42. sqlType = fmt.Sprintf("varchar(%d)", size)
  43. } else {
  44. sqlType = "text"
  45. }
  46. case reflect.Struct:
  47. if _, ok := dataValue.Interface().(time.Time); ok {
  48. sqlType = "datetime"
  49. }
  50. default:
  51. if IsByteArrayOrSlice(dataValue) {
  52. sqlType = "blob"
  53. }
  54. }
  55. }
  56. if sqlType == "" {
  57. panic(fmt.Sprintf("invalid sql type %s (%s) for sqlite3", dataValue.Type().Name(), dataValue.Kind().String()))
  58. }
  59. if strings.TrimSpace(additionalType) == "" {
  60. return sqlType
  61. }
  62. return fmt.Sprintf("%v %v", sqlType, additionalType)
  63. }
  64. func (s sqlite3) HasIndex(tableName string, indexName string) bool {
  65. var count int
  66. s.db.QueryRow(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE tbl_name = ? AND sql LIKE '%%INDEX %v ON%%'", indexName), tableName).Scan(&count)
  67. return count > 0
  68. }
  69. func (s sqlite3) HasTable(tableName string) bool {
  70. var count int
  71. s.db.QueryRow("SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?", tableName).Scan(&count)
  72. return count > 0
  73. }
  74. func (s sqlite3) HasColumn(tableName string, columnName string) bool {
  75. var count int
  76. s.db.QueryRow(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE tbl_name = ? AND (sql LIKE '%%\"%v\" %%' OR sql LIKE '%%%v %%');\n", columnName, columnName), tableName).Scan(&count)
  77. return count > 0
  78. }
  79. func (s sqlite3) CurrentDatabase() (name string) {
  80. var (
  81. ifaces = make([]interface{}, 3)
  82. pointers = make([]*string, 3)
  83. i int
  84. )
  85. for i = 0; i < 3; i++ {
  86. ifaces[i] = &pointers[i]
  87. }
  88. if err := s.db.QueryRow("PRAGMA database_list").Scan(ifaces...); err != nil {
  89. return
  90. }
  91. if pointers[1] != nil {
  92. name = *pointers[1]
  93. }
  94. return
  95. }