123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- // copy from core/stores/sqlx/sqlconn.go
- package mocksql
- import (
- "context"
- "database/sql"
- "github.com/zeromicro/go-zero/core/stores/sqlx"
- )
- type (
- // MockConn defines a mock connection instance for mysql
- MockConn struct {
- db *sql.DB
- }
- statement struct {
- stmt *sql.Stmt
- }
- )
- // NewMockConn creates an instance for MockConn
- func NewMockConn(db *sql.DB) *MockConn {
- return &MockConn{db: db}
- }
- // Exec executes sql and returns the result
- func (conn *MockConn) Exec(query string, args ...interface{}) (sql.Result, error) {
- return exec(conn.db, query, args...)
- }
- // ExecCtx executes sql and returns the result
- func (conn *MockConn) ExecCtx(_ context.Context, query string, args ...interface{}) (sql.Result, error) {
- return exec(conn.db, query, args...)
- }
- // Prepare executes sql by sql.DB
- func (conn *MockConn) Prepare(query string) (sqlx.StmtSession, error) {
- st, err := conn.db.Prepare(query)
- return statement{stmt: st}, err
- }
- // PrepareCtx executes sql by sql.DB
- func (conn *MockConn) PrepareCtx(_ context.Context, query string) (sqlx.StmtSession, error) {
- return conn.Prepare(query)
- }
- // QueryRow executes sql and returns a query row
- func (conn *MockConn) QueryRow(v interface{}, q string, args ...interface{}) error {
- return query(conn.db, func(rows *sql.Rows) error {
- return unmarshalRow(v, rows, true)
- }, q, args...)
- }
- // QueryRowCtx executes sql and returns a query row
- func (conn *MockConn) QueryRowCtx(_ context.Context, v interface{}, query string, args ...interface{}) error {
- return conn.QueryRow(v, query, args...)
- }
- // QueryRowPartial executes sql and returns a partial query row
- func (conn *MockConn) QueryRowPartial(v interface{}, q string, args ...interface{}) error {
- return query(conn.db, func(rows *sql.Rows) error {
- return unmarshalRow(v, rows, false)
- }, q, args...)
- }
- // QueryRowPartialCtx executes sql and returns a partial query row
- func (conn *MockConn) QueryRowPartialCtx(_ context.Context, v interface{}, query string, args ...interface{}) error {
- return conn.QueryRowPartial(v, query, args...)
- }
- // QueryRows executes sql and returns query rows
- func (conn *MockConn) QueryRows(v interface{}, q string, args ...interface{}) error {
- return query(conn.db, func(rows *sql.Rows) error {
- return unmarshalRows(v, rows, true)
- }, q, args...)
- }
- // QueryRowsCtx executes sql and returns query rows
- func (conn *MockConn) QueryRowsCtx(_ context.Context, v interface{}, query string, args ...interface{}) error {
- return conn.QueryRows(v, query, args...)
- }
- // QueryRowsPartial executes sql and returns partial query rows
- func (conn *MockConn) QueryRowsPartial(v interface{}, q string, args ...interface{}) error {
- return query(conn.db, func(rows *sql.Rows) error {
- return unmarshalRows(v, rows, false)
- }, q, args...)
- }
- // QueryRowsPartialCtx executes sql and returns partial query rows
- func (conn *MockConn) QueryRowsPartialCtx(_ context.Context, v interface{}, query string, args ...interface{}) error {
- return conn.QueryRowsPartial(v, query, args...)
- }
- // RawDB returns the underlying sql.DB.
- func (conn *MockConn) RawDB() (*sql.DB, error) {
- return conn.db, nil
- }
- // Transact is the implemention of sqlx.SqlConn, nothing to do
- func (conn *MockConn) Transact(func(session sqlx.Session) error) error {
- return nil
- }
- // TransactCtx is the implemention of sqlx.SqlConn, nothing to do
- func (conn *MockConn) TransactCtx(ctx context.Context, fn func(context.Context, sqlx.Session) error) error {
- return nil
- }
- func (s statement) Close() error {
- return s.stmt.Close()
- }
- func (s statement) Exec(args ...interface{}) (sql.Result, error) {
- return execStmt(s.stmt, args...)
- }
- func (s statement) ExecCtx(_ context.Context, args ...interface{}) (sql.Result, error) {
- return s.Exec(args...)
- }
- func (s statement) QueryRow(v interface{}, args ...interface{}) error {
- return queryStmt(s.stmt, func(rows *sql.Rows) error {
- return unmarshalRow(v, rows, true)
- }, args...)
- }
- func (s statement) QueryRowCtx(_ context.Context, v interface{}, args ...interface{}) error {
- return s.QueryRow(v, args...)
- }
- func (s statement) QueryRowPartial(v interface{}, args ...interface{}) error {
- return queryStmt(s.stmt, func(rows *sql.Rows) error {
- return unmarshalRow(v, rows, false)
- }, args...)
- }
- func (s statement) QueryRowPartialCtx(_ context.Context, v interface{}, args ...interface{}) error {
- return s.QueryRowPartial(v, args...)
- }
- func (s statement) QueryRows(v interface{}, args ...interface{}) error {
- return queryStmt(s.stmt, func(rows *sql.Rows) error {
- return unmarshalRows(v, rows, true)
- }, args...)
- }
- func (s statement) QueryRowsCtx(_ context.Context, v interface{}, args ...interface{}) error {
- return s.QueryRows(v, args...)
- }
- func (s statement) QueryRowsPartial(v interface{}, args ...interface{}) error {
- return queryStmt(s.stmt, func(rows *sql.Rows) error {
- return unmarshalRows(v, rows, false)
- }, args...)
- }
- func (s statement) QueryRowsPartialCtx(_ context.Context, v interface{}, args ...interface{}) error {
- return s.QueryRowsPartial(v, args...)
- }
|