sqlw.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package sqlw
  2. import (
  3. "context"
  4. "database/sql"
  5. _ "github.com/go-sql-driver/mysql"
  6. "errors"
  7. "time"
  8. "golang-fave/engine/consts"
  9. )
  10. type Rows = sql.Rows
  11. type DB struct {
  12. db *sql.DB
  13. }
  14. var ErrNoRows = sql.ErrNoRows
  15. func Open(driverName, dataSourceName string) (*DB, error) {
  16. db, err := sql.Open(driverName, dataSourceName)
  17. if err != nil {
  18. if consts.ParamDebug {
  19. log("[CM] OPEN", time.Now(), err, true)
  20. }
  21. return nil, err
  22. }
  23. if consts.ParamDebug {
  24. log("[CM] OPEN", time.Now(), err, true)
  25. }
  26. return &DB{db: db}, err
  27. }
  28. func (this *DB) Begin(ctx context.Context) (*Tx, error) {
  29. tx, err := this.db.BeginTx(ctx, &sql.TxOptions{
  30. Isolation: sql.LevelDefault,
  31. })
  32. if err != nil {
  33. if consts.ParamDebug {
  34. log("[TX] TRANSACTION START", time.Now(), err, true)
  35. }
  36. return nil, err
  37. }
  38. if consts.ParamDebug {
  39. s := time.Now()
  40. log("[TX] TRANSACTION START", s, err, true)
  41. return &Tx{tx, s}, err
  42. }
  43. return &Tx{tx, time.Now()}, err
  44. }
  45. func (this *DB) Close() error {
  46. if consts.ParamDebug {
  47. err := this.db.Close()
  48. log("[CM] CLOSE", time.Now(), err, true)
  49. return err
  50. }
  51. return this.db.Close()
  52. }
  53. func (this *DB) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
  54. if consts.ParamDebug {
  55. s := time.Now()
  56. r, e := this.db.ExecContext(ctx, query, args...)
  57. log(query, s, e, false)
  58. return r, e
  59. }
  60. return this.db.ExecContext(ctx, query, args...)
  61. }
  62. func (this *DB) Ping(ctx context.Context) error {
  63. if consts.ParamDebug {
  64. err := this.db.PingContext(ctx)
  65. log("[CM] PING", time.Now(), err, true)
  66. return err
  67. }
  68. return this.db.PingContext(ctx)
  69. }
  70. func (this *DB) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) {
  71. if consts.ParamDebug {
  72. s := time.Now()
  73. r, e := this.db.QueryContext(ctx, query, args...)
  74. log(query, s, e, false)
  75. return r, e
  76. }
  77. return this.db.Query(query, args...)
  78. }
  79. func (this *DB) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row {
  80. if consts.ParamDebug {
  81. s := time.Now()
  82. r := this.db.QueryRowContext(ctx, query, args...)
  83. log(query, s, nil, false)
  84. return r
  85. }
  86. return this.db.QueryRow(query, args...)
  87. }
  88. func (this *DB) SetConnMaxLifetime(d time.Duration) {
  89. this.db.SetConnMaxLifetime(d)
  90. }
  91. func (this *DB) SetMaxIdleConns(n int) {
  92. this.db.SetMaxIdleConns(n)
  93. }
  94. func (this *DB) SetMaxOpenConns(n int) {
  95. this.db.SetMaxOpenConns(n)
  96. }
  97. func (this *DB) Transaction(ctx context.Context, queries func(ctx context.Context, tx *Tx) error) error {
  98. if queries == nil {
  99. return errors.New("queries is not set for transaction")
  100. }
  101. tx, err := this.Begin(ctx)
  102. if err != nil {
  103. return err
  104. }
  105. if err := queries(ctx, tx); err != nil {
  106. tx.Rollback()
  107. return err
  108. }
  109. return tx.Commit()
  110. }