chainable_api.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package gorm
  2. // Model specify the model you would like to run db operations
  3. // // update all users's name to `hello`
  4. // db.Model(&User{}).Update("name", "hello")
  5. // // if user's primary key is non-blank, will use it as condition, then will only update the user's name to `hello`
  6. // db.Model(&user).Update("name", "hello")
  7. func (db *DB) Model(value interface{}) (tx *DB) {
  8. tx = db.getInstance()
  9. return
  10. }
  11. // Table specify the table you would like to run db operations
  12. func (db *DB) Table(name string) (tx *DB) {
  13. tx = db.getInstance()
  14. return
  15. }
  16. // Select specify fields that you want when querying, creating, updating
  17. func (db *DB) Select(query interface{}, args ...interface{}) (tx *DB) {
  18. tx = db.getInstance()
  19. return
  20. }
  21. // Omit specify fields that you want to ignore when creating, updating and querying
  22. func (db *DB) Omit(columns ...string) (tx *DB) {
  23. tx = db.getInstance()
  24. return
  25. }
  26. func (db *DB) Where(query interface{}, args ...interface{}) (tx *DB) {
  27. tx = db.getInstance()
  28. return
  29. }
  30. // Not add NOT condition
  31. func (db *DB) Not(query interface{}, args ...interface{}) (tx *DB) {
  32. tx = db.getInstance()
  33. return
  34. }
  35. // Or add OR conditions
  36. func (db *DB) Or(query interface{}, args ...interface{}) (tx *DB) {
  37. tx = db.getInstance()
  38. return
  39. }
  40. // Joins specify Joins conditions
  41. // db.Joins("JOIN emails ON emails.user_id = users.id AND emails.email = ?", "jinzhu@example.org").Find(&user)
  42. func (db *DB) Joins(query string, args ...interface{}) (tx *DB) {
  43. tx = db.getInstance()
  44. return
  45. }
  46. // Group specify the group method on the find
  47. func (db *DB) Group(column string) (tx *DB) {
  48. tx = db.getInstance()
  49. return
  50. }
  51. // Having specify HAVING conditions for GROUP BY
  52. func (db *DB) Having(query interface{}, args ...interface{}) (tx *DB) {
  53. tx = db.getInstance()
  54. return
  55. }
  56. // Order specify order when retrieve records from database
  57. // db.Order("name DESC")
  58. // db.Order(gorm.Expr("name = ? DESC", "first")) // sql expression
  59. func (db *DB) Order(value interface{}) (tx *DB) {
  60. tx = db.getInstance()
  61. return
  62. }
  63. // Limit specify the number of records to be retrieved
  64. func (db *DB) Limit(limit int64) (tx *DB) {
  65. tx = db.getInstance()
  66. return
  67. }
  68. // Offset specify the number of records to skip before starting to return the records
  69. func (db *DB) Offset(offset int64) (tx *DB) {
  70. tx = db.getInstance()
  71. return
  72. }
  73. // Scopes pass current database connection to arguments `func(*DB) *DB`, which could be used to add conditions dynamically
  74. // func AmountGreaterThan1000(db *gorm.DB) *gorm.DB {
  75. // return db.Where("amount > ?", 1000)
  76. // }
  77. //
  78. // func OrderStatus(status []string) func (db *gorm.DB) *gorm.DB {
  79. // return func (db *gorm.DB) *gorm.DB {
  80. // return db.Scopes(AmountGreaterThan1000).Where("status in (?)", status)
  81. // }
  82. // }
  83. //
  84. // db.Scopes(AmountGreaterThan1000, OrderStatus([]string{"paid", "shipped"})).Find(&orders)
  85. // Refer https://jinzhu.github.io/gorm/crud.html#scopes
  86. func (db *DB) Scopes(funcs ...func(*DB) *DB) (tx *DB) {
  87. for _, f := range funcs {
  88. db = f(db)
  89. }
  90. return db
  91. }
  92. //Preloads only preloads relations, don`t touch out
  93. func (db *DB) Preloads(out interface{}) (tx *DB) {
  94. tx = db.getInstance()
  95. return
  96. }
  97. // Preload preload associations with given conditions
  98. // db.Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
  99. func (db *DB) Preload(column string, conditions ...interface{}) (tx *DB) {
  100. tx = db.getInstance()
  101. return
  102. }
  103. func (db *DB) Assign(attrs ...interface{}) (tx *DB) {
  104. tx = db.getInstance()
  105. return
  106. }
  107. func (db *DB) Attrs(attrs ...interface{}) (tx *DB) {
  108. tx = db.getInstance()
  109. return
  110. }
  111. func (db *DB) Unscoped() (tx *DB) {
  112. tx = db.getInstance()
  113. return
  114. }
  115. func (db *DB) Raw(sql string, values ...interface{}) (tx *DB) {
  116. tx = db.getInstance()
  117. return
  118. }