finisher_api.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. tx = db.getInstance().Limit(1).Order(clause.OrderByColumn{
  22. Column: clause.Column{Table: clause.CurrentTable, Name: clause.PrimaryKey},
  23. Desc: true,
  24. })
  25. tx.Statement.Dest = out
  26. tx.callbacks.Query().Execute(tx)
  27. return
  28. }
  29. // Take return a record that match given conditions, the order will depend on the database implementation
  30. func (db *DB) Take(out interface{}, where ...interface{}) (tx *DB) {
  31. tx = db.getInstance()
  32. return
  33. }
  34. // Last find last record that match given conditions, order by primary key
  35. func (db *DB) Last(out interface{}, where ...interface{}) (tx *DB) {
  36. tx = db.getInstance()
  37. return
  38. }
  39. // Find find records that match given conditions
  40. func (db *DB) Find(out interface{}, where ...interface{}) (tx *DB) {
  41. tx = db.getInstance()
  42. return
  43. }
  44. func (db *DB) FirstOrInit(out interface{}, where ...interface{}) (tx *DB) {
  45. tx = db.getInstance()
  46. return
  47. }
  48. func (db *DB) FirstOrCreate(out interface{}, where ...interface{}) (tx *DB) {
  49. tx = db.getInstance()
  50. return
  51. }
  52. // Update update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
  53. func (db *DB) Update(column string, value interface{}) (tx *DB) {
  54. tx = db.getInstance()
  55. return
  56. }
  57. // Updates update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
  58. func (db *DB) Updates(values interface{}) (tx *DB) {
  59. tx = db.getInstance()
  60. return
  61. }
  62. func (db *DB) UpdateColumn(column string, value interface{}) (tx *DB) {
  63. tx = db.getInstance()
  64. return
  65. }
  66. func (db *DB) UpdateColumns(values interface{}) (tx *DB) {
  67. tx = db.getInstance()
  68. return
  69. }
  70. // Delete delete value match given conditions, if the value has primary key, then will including the primary key as condition
  71. func (db *DB) Delete(value interface{}, where ...interface{}) (tx *DB) {
  72. tx = db.getInstance()
  73. return
  74. }
  75. func (db *DB) Related(value interface{}, foreignKeys ...string) (tx *DB) {
  76. tx = db.getInstance()
  77. return
  78. }
  79. //Preloads only preloads relations, don`t touch out
  80. func (db *DB) Preloads(out interface{}) (tx *DB) {
  81. tx = db.getInstance()
  82. return
  83. }
  84. func (db *DB) Association(column string) *Association {
  85. return nil
  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. func (db *DB) Transaction(fc func(tx *DB) error, opts ...*sql.TxOptions) (err error) {
  110. panicked := true
  111. tx := db.Begin(opts...)
  112. defer func() {
  113. // Make sure to rollback when panic, Block error or Commit error
  114. if panicked || err != nil {
  115. tx.Rollback()
  116. }
  117. }()
  118. err = fc(tx)
  119. if err == nil {
  120. err = tx.Commit().Error
  121. }
  122. panicked = false
  123. return
  124. }
  125. func (db *DB) Begin(opts ...*sql.TxOptions) (tx *DB) {
  126. tx = db.getInstance()
  127. return
  128. }
  129. func (db *DB) Commit() (tx *DB) {
  130. tx = db.getInstance()
  131. return
  132. }
  133. func (db *DB) Rollback() (tx *DB) {
  134. tx = db.getInstance()
  135. return
  136. }
  137. func (db *DB) Exec(sql string, values ...interface{}) (tx *DB) {
  138. tx = db.getInstance()
  139. tx.Statement.SQL = strings.Builder{}
  140. clause.Expr{SQL: sql, Vars: values}.Build(tx.Statement)
  141. tx.callbacks.Raw().Execute(tx)
  142. return
  143. }