sqlconn.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // copy from core/stores/sqlx/sqlconn.go
  2. package mocksql
  3. import (
  4. "database/sql"
  5. "github.com/tal-tech/go-zero/core/stores/sqlx"
  6. )
  7. type (
  8. // MockConn defines a mock connection instance for mysql
  9. MockConn struct {
  10. db *sql.DB
  11. }
  12. statement struct {
  13. stmt *sql.Stmt
  14. }
  15. )
  16. // NewMockConn creates an instance for MockConn
  17. func NewMockConn(db *sql.DB) *MockConn {
  18. return &MockConn{db: db}
  19. }
  20. // Exec executes sql and returns the result
  21. func (conn *MockConn) Exec(query string, args ...interface{}) (sql.Result, error) {
  22. return exec(conn.db, query, args...)
  23. }
  24. // Prepare executes sql by sql.DB
  25. func (conn *MockConn) Prepare(query string) (sqlx.StmtSession, error) {
  26. st, err := conn.db.Prepare(query)
  27. return statement{stmt: st}, err
  28. }
  29. // QueryRow executes sql and returns a query row
  30. func (conn *MockConn) QueryRow(v interface{}, q string, args ...interface{}) error {
  31. return query(conn.db, func(rows *sql.Rows) error {
  32. return unmarshalRow(v, rows, true)
  33. }, q, args...)
  34. }
  35. // QueryRowPartial executes sql and returns a partial query row
  36. func (conn *MockConn) QueryRowPartial(v interface{}, q string, args ...interface{}) error {
  37. return query(conn.db, func(rows *sql.Rows) error {
  38. return unmarshalRow(v, rows, false)
  39. }, q, args...)
  40. }
  41. // QueryRows executes sql and returns query rows
  42. func (conn *MockConn) QueryRows(v interface{}, q string, args ...interface{}) error {
  43. return query(conn.db, func(rows *sql.Rows) error {
  44. return unmarshalRows(v, rows, true)
  45. }, q, args...)
  46. }
  47. // QueryRowsPartial executes sql and returns partial query rows
  48. func (conn *MockConn) QueryRowsPartial(v interface{}, q string, args ...interface{}) error {
  49. return query(conn.db, func(rows *sql.Rows) error {
  50. return unmarshalRows(v, rows, false)
  51. }, q, args...)
  52. }
  53. // Transact is the implemention of sqlx.SqlConn, nothing to do
  54. func (conn *MockConn) Transact(func(session sqlx.Session) error) error {
  55. return nil
  56. }
  57. func (s statement) Close() error {
  58. return s.stmt.Close()
  59. }
  60. func (s statement) Exec(args ...interface{}) (sql.Result, error) {
  61. return execStmt(s.stmt, args...)
  62. }
  63. func (s statement) QueryRow(v interface{}, args ...interface{}) error {
  64. return queryStmt(s.stmt, func(rows *sql.Rows) error {
  65. return unmarshalRow(v, rows, true)
  66. }, args...)
  67. }
  68. func (s statement) QueryRowPartial(v interface{}, args ...interface{}) error {
  69. return queryStmt(s.stmt, func(rows *sql.Rows) error {
  70. return unmarshalRow(v, rows, false)
  71. }, args...)
  72. }
  73. func (s statement) QueryRows(v interface{}, args ...interface{}) error {
  74. return queryStmt(s.stmt, func(rows *sql.Rows) error {
  75. return unmarshalRows(v, rows, true)
  76. }, args...)
  77. }
  78. func (s statement) QueryRowsPartial(v interface{}, args ...interface{}) error {
  79. return queryStmt(s.stmt, func(rows *sql.Rows) error {
  80. return unmarshalRows(v, rows, false)
  81. }, args...)
  82. }