interface.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package operations
  2. type DBOperations interface {
  3. BeginTransaction() TransactionDBOperations
  4. // BeginEvent() EventDBOperations
  5. BaseDBOperations
  6. }
  7. type TransactionDBOperations interface {
  8. BaseDBOperations
  9. RollbackTransaction()
  10. CommitTransaction()
  11. }
  12. type EventDBOperations interface {
  13. RollbackEvent()
  14. CommitEvent()
  15. // TODO 需要重新定义创建等方法,传递必要的key
  16. EventRows(table string, keys []string, pageSize int, pageNo string)
  17. Replay(table string, keys []string)
  18. }
  19. type BaseDBOperations interface {
  20. // AutoMigrate 创建表
  21. AutoMigrate(tables ...Table) error
  22. // 会重置数据库连接的方法
  23. Table(name string, args ...any) DBOperations
  24. // 会重置数据库连接的方法,一般配合Raw使用
  25. NewSession() DBOperations
  26. // 执行SQL语句,使用Raw之后,为了触发SQL执行,需要调用Row或者Rows
  27. // 如果是查询语句,使用Rows或Row均可,主要看自己需要查询的是单行还是多行
  28. // 如果是写语句,必须使用Rows,否则由于没有返回结果,Rows会报错
  29. // 使用Raw以后,所有分页相关的参数都无效,需要使用SQL语句进行分页
  30. Raw(sql string, values ...any) DBOperations
  31. // 组织SQL语句相关的方法
  32. Select(query string, args ...any) DBOperations
  33. Joins(query string, args ...any) DBOperations
  34. Where(conditions *Conditions) DBOperations
  35. Or(conditions *Conditions) DBOperations
  36. Having(conditions *Conditions) DBOperations
  37. GroupBy(groupBy string) DBOperations
  38. OrderBy(orderBy string) DBOperations
  39. Paging(pageNo int, pageSize int) DBOperations
  40. // 写方法
  41. Create(tableRow *TableRow) error
  42. CreateBatch(tableRows []TableRow) error
  43. Delete() error
  44. Updates(newTableRow *TableRow) error
  45. UpdatesWithRowsAffected(newTableRow *TableRow) (int64, error)
  46. // 查询方法
  47. Rows(pageNo int, pageSize int) ([]TableRow, error)
  48. Row() (*TableRow, error)
  49. // 其他方法
  50. Count() (int64, error)
  51. CheckExist() (bool, error)
  52. CheckHasOnlyOne() (bool, error)
  53. }