sqlw.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package sqlw
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. "time"
  6. )
  7. type Tx = sql.Tx
  8. type Rows = sql.Rows
  9. type DB struct {
  10. db *sql.DB
  11. }
  12. var ErrNoRows = sql.ErrNoRows
  13. func Open(driverName, dataSourceName string) (*DB, error) {
  14. db, err := sql.Open(driverName, dataSourceName)
  15. if err != nil {
  16. return nil, err
  17. }
  18. return &DB{db: db}, nil
  19. }
  20. func (this *DB) Close() error {
  21. return this.db.Close()
  22. }
  23. func (this *DB) Ping() error {
  24. return this.db.Ping()
  25. }
  26. func (this *DB) SetConnMaxLifetime(d time.Duration) {
  27. this.db.SetConnMaxLifetime(d)
  28. }
  29. func (this *DB) SetMaxIdleConns(n int) {
  30. this.db.SetMaxIdleConns(n)
  31. }
  32. func (this *DB) SetMaxOpenConns(n int) {
  33. this.db.SetMaxOpenConns(n)
  34. }
  35. func (this *DB) QueryRow(query string, args ...interface{}) *sql.Row {
  36. return this.db.QueryRow(query, args...)
  37. }
  38. func (this *DB) Begin() (*sql.Tx, error) {
  39. return this.db.Begin()
  40. }
  41. func (this *DB) Query(query string, args ...interface{}) (*sql.Rows, error) {
  42. return this.db.Query(query, args...)
  43. }
  44. func (this *DB) Exec(query string, args ...interface{}) (sql.Result, error) {
  45. return this.db.Exec(query, args...)
  46. }