finisher_api.go 3.5 KB

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