sqlite.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package db
  2. import (
  3. "fmt"
  4. "sync"
  5. "github.com/jinzhu/gorm"
  6. )
  7. type SqliteMaster struct {
  8. Type string `json:"type"`
  9. TblName string `json:"tbl_name"`
  10. RootPage string `json:"root_page"`
  11. Name string `json:"name"`
  12. Sql string `json:"sql"`
  13. }
  14. type TableInfo struct {
  15. Cid uint `json:"cid"`
  16. Type string `json:"type"`
  17. Name string `json:"name"`
  18. Notnull string `json:"not_null"`
  19. Pk int `json:"pk"`
  20. DfltValue interface{} `json:"deflt_value"`
  21. }
  22. type Sqlite struct {
  23. orm *gorm.DB
  24. mutex *sync.Mutex
  25. }
  26. func (Sqlite) Hello() string {
  27. return "Sqlite"
  28. }
  29. func (s Sqlite) Orm() *gorm.DB {
  30. return s.orm
  31. }
  32. func (s Sqlite) Mutex() *sync.Mutex {
  33. return s.mutex
  34. }
  35. func (db Sqlite) Insert(obj interface{}) error {
  36. tx := db.orm.Begin()
  37. err := tx.Create(obj).Error
  38. if err != nil {
  39. tx.Rollback()
  40. return err
  41. }
  42. err = tx.Commit().Error
  43. if err != nil {
  44. tx.Rollback()
  45. return err
  46. }
  47. return nil
  48. }
  49. func (db Sqlite) Delete(obj interface{}, para map[string]interface{}) error {
  50. tx := db.orm.Begin()
  51. err := tx.Where(para).Delete(obj).Error
  52. if err != nil {
  53. tx.Rollback()
  54. return err
  55. }
  56. err = tx.Commit().Error
  57. if err != nil {
  58. tx.Rollback()
  59. return err
  60. }
  61. return nil
  62. }
  63. func (db Sqlite) Get(objs interface{}, where map[string]interface{}, order string, limit uint64, offset uint64, deleted bool) error {
  64. con := db.orm
  65. if deleted {
  66. con = con.Unscoped()
  67. }
  68. keys, values := whereExactKeyValues(where)
  69. con = con.Where(keys, values)
  70. if deleted {
  71. con = con.Where("deleted_at is not null")
  72. }
  73. if order != "" {
  74. con = con.Order(order)
  75. }
  76. if limit != 0 {
  77. con = con.Limit(limit)
  78. }
  79. if offset != 0 {
  80. con = con.Offset(offset)
  81. }
  82. err := con.Find(objs).Error
  83. if err != nil {
  84. return err
  85. }
  86. return nil
  87. }
  88. func (db Sqlite) Update(obj interface{}, para map[string]interface{}) error {
  89. tx := db.orm.Begin()
  90. err := tx.Model(obj).Updates(para).Error
  91. if err != nil {
  92. tx.Rollback()
  93. return err
  94. }
  95. err = tx.Commit().Error
  96. if err != nil {
  97. tx.Rollback()
  98. return err
  99. }
  100. return nil
  101. }
  102. func (db Sqlite) GetTables() (interface{}, error) {
  103. var sqlMaster []SqliteMaster
  104. if err := db.Get(&sqlMaster, map[string]interface{}{"type": "table"}, "", 0, 0, false); err != nil {
  105. return nil, err
  106. }
  107. return sqlMaster, nil
  108. }
  109. func (db Sqlite) GetMeta(tableName interface{}) (interface{}, error) {
  110. var info []TableInfo
  111. str := fmt.Sprint(tableName)
  112. q := "pragma table_info(" + str + ")"
  113. row, err := db.orm.DB().Query(q)
  114. if err != nil {
  115. return info, err
  116. }
  117. var cid uint
  118. var t string
  119. var name string
  120. var notnull string
  121. var pk int
  122. var dfltValue interface{}
  123. for row.Next() {
  124. err = row.Scan(&cid, &name, &t, &notnull, &dfltValue, &pk)
  125. if err != nil {
  126. return info, err
  127. }
  128. table_info := TableInfo{
  129. Cid: cid,
  130. Name: name,
  131. Type: t,
  132. Notnull: notnull,
  133. DfltValue: dfltValue,
  134. Pk: pk,
  135. }
  136. info = append(info, table_info)
  137. }
  138. return info, nil
  139. }