finisher_api.go 4.8 KB

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