finisher_api.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package gorm
  2. import (
  3. "database/sql"
  4. "github.com/jinzhu/gorm/clause"
  5. )
  6. // Create insert the value into database
  7. func (db *DB) Create(value interface{}) (tx *DB) {
  8. tx = db.getInstance()
  9. tx.Statement.Dest = value
  10. tx.callbacks.Create().Execute(tx)
  11. return
  12. }
  13. // Save update value in database, if the value doesn't have primary key, will insert it
  14. func (db *DB) Save(value interface{}) (tx *DB) {
  15. tx = db.getInstance()
  16. return
  17. }
  18. // First find first record that match given conditions, order by primary key
  19. func (db *DB) First(out interface{}, where ...interface{}) (tx *DB) {
  20. tx = db.getInstance().Limit(1).Order(clause.OrderByColumn{
  21. Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey},
  22. Desc: true,
  23. })
  24. tx.Statement.Dest = out
  25. tx.callbacks.Query().Execute(tx)
  26. return
  27. }
  28. // Take return a record that match given conditions, the order will depend on the database implementation
  29. func (db *DB) Take(out interface{}, where ...interface{}) (tx *DB) {
  30. tx = db.getInstance()
  31. return
  32. }
  33. // Last find last record that match given conditions, order by primary key
  34. func (db *DB) Last(out interface{}, where ...interface{}) (tx *DB) {
  35. tx = db.getInstance()
  36. return
  37. }
  38. // Find find records that match given conditions
  39. func (db *DB) Find(out interface{}, where ...interface{}) (tx *DB) {
  40. tx = db.getInstance()
  41. return
  42. }
  43. func (db *DB) FirstOrInit(out interface{}, where ...interface{}) (tx *DB) {
  44. tx = db.getInstance()
  45. return
  46. }
  47. func (db *DB) FirstOrCreate(out interface{}, where ...interface{}) (tx *DB) {
  48. tx = db.getInstance()
  49. return
  50. }
  51. // Update update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
  52. func (db *DB) Update(column string, value interface{}) (tx *DB) {
  53. tx = db.getInstance()
  54. return
  55. }
  56. // Updates update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
  57. func (db *DB) Updates(values interface{}) (tx *DB) {
  58. tx = db.getInstance()
  59. return
  60. }
  61. func (db *DB) UpdateColumn(column string, value interface{}) (tx *DB) {
  62. tx = db.getInstance()
  63. return
  64. }
  65. func (db *DB) UpdateColumns(values interface{}) (tx *DB) {
  66. tx = db.getInstance()
  67. return
  68. }
  69. // Delete delete value match given conditions, if the value has primary key, then will including the primary key as condition
  70. func (db *DB) Delete(value interface{}, where ...interface{}) (tx *DB) {
  71. tx = db.getInstance()
  72. return
  73. }
  74. func (db *DB) Related(value interface{}, foreignKeys ...string) (tx *DB) {
  75. tx = db.getInstance()
  76. return
  77. }
  78. //Preloads only preloads relations, don`t touch out
  79. func (db *DB) Preloads(out interface{}) (tx *DB) {
  80. tx = db.getInstance()
  81. return
  82. }
  83. func (db *DB) Association(column string) *Association {
  84. return nil
  85. }
  86. func (db *DB) Count(value interface{}) (tx *DB) {
  87. tx = db.getInstance()
  88. return
  89. }
  90. func (db *DB) Row() *sql.Row {
  91. tx := db.getInstance()
  92. tx.callbacks.Row().Execute(tx)
  93. return tx.Statement.Dest.(*sql.Row)
  94. }
  95. func (db *DB) Rows() (*sql.Rows, error) {
  96. tx := db.Set("rows", true)
  97. tx.callbacks.Row().Execute(tx)
  98. return tx.Statement.Dest.(*sql.Rows), tx.Error
  99. }
  100. // Scan scan value to a struct
  101. func (db *DB) Scan(dest interface{}) (tx *DB) {
  102. tx = db.getInstance()
  103. return
  104. }
  105. func (db *DB) ScanRows(rows *sql.Rows, result interface{}) error {
  106. return nil
  107. }
  108. func (db *DB) Transaction(fc func(tx *DB) error, opts ...*sql.TxOptions) (err error) {
  109. panicked := true
  110. tx := db.Begin(opts...)
  111. defer func() {
  112. // Make sure to rollback when panic, Block error or Commit error
  113. if panicked || err != nil {
  114. tx.Rollback()
  115. }
  116. }()
  117. err = fc(tx)
  118. if err == nil {
  119. err = tx.Commit().Error
  120. }
  121. panicked = false
  122. return
  123. }
  124. func (db *DB) Begin(opts ...*sql.TxOptions) (tx *DB) {
  125. tx = db.getInstance()
  126. return
  127. }
  128. func (db *DB) Commit() (tx *DB) {
  129. tx = db.getInstance()
  130. return
  131. }
  132. func (db *DB) Rollback() (tx *DB) {
  133. tx = db.getInstance()
  134. return
  135. }
  136. func (db *DB) Exec(sql string, values ...interface{}) (tx *DB) {
  137. tx = db.getInstance()
  138. tx.callbacks.Raw().Execute(tx)
  139. return
  140. }