db_operations.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package db_operations
  2. type DBOperations interface {
  3. BeginTransaction() TransactionDBOperations
  4. BaseDBOperations
  5. }
  6. type TransactionDBOperations interface {
  7. BaseDBOperations
  8. RollbackTransaction()
  9. CommitTransaction()
  10. }
  11. type BaseDBOperations interface {
  12. // 会重置数据库连接的方法
  13. NewSession() DBOperations
  14. Table(name string, args ...any) DBOperations
  15. Raw(sql string, values ...any) DBOperations
  16. // 组织SQL语句相关的方法
  17. Select(query string, args ...any) DBOperations
  18. Where(query string, args ...any) DBOperations
  19. Or(query string, args ...any) DBOperations
  20. GroupBy(groupBy string) DBOperations
  21. OrderBy(orderBy string) DBOperations
  22. Joins(query string, args ...any) DBOperations
  23. Having(query string, args ...any) DBOperations
  24. Paging(pageNo int, pageSize int) DBOperations
  25. // 写方法
  26. Create(model interface{}) error
  27. Delete(idModel interface{}) error
  28. Updates(idModel interface{}, updateData map[string]any) error
  29. UpdatesWithRowsAffected(idModel interface{}, updateData map[string]any) (int64, error)
  30. // 使用Model的查询方法
  31. Query(models interface{}, pageNo int, pageSize int) error
  32. QueryOne(model interface{}) error
  33. // 不使用Model的查询方法,需要结合Table或者Raw使用,如需分页,需要配合Paging
  34. Row() (map[string]any, error)
  35. Rows() ([]map[string]any, error)
  36. // 其他方法
  37. Count(count *int64) error
  38. }