db_operations.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. // 执行SQL语句,使用Raw之后,为了触发SQL执行,需要调用Row或者Rows
  16. // 如果是查询语句,使用Rows或Row均可,主要看自己需要查询的是单行还是多行
  17. // 如果是写语句,必须使用Rows,否则由于没有返回结果,Rows会报错
  18. Raw(sql string, values ...any) DBOperations
  19. // 组织SQL语句相关的方法
  20. Select(query string, args ...any) DBOperations
  21. Where(query string, args ...any) DBOperations
  22. Or(query string, args ...any) DBOperations
  23. GroupBy(groupBy string) DBOperations
  24. OrderBy(orderBy string) DBOperations
  25. Joins(query string, args ...any) DBOperations
  26. Having(query string, args ...any) DBOperations
  27. Paging(pageNo int, pageSize int) DBOperations
  28. // 写方法
  29. Create(tableRow map[string]any) error
  30. CreateBatch(tableRows []map[string]any) error
  31. Delete() error
  32. Updates(updateData map[string]any) error
  33. UpdatesWithRowsAffected(updateData map[string]any) (int64, error)
  34. // 查询方法
  35. Rows(pageNo int, pageSize int) ([]map[string]any, error)
  36. Row() (map[string]any, error)
  37. // 其他方法
  38. Count(count *int64) error
  39. }