dbwrap

package module
v0.0.0-...-a60abef Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2018 License: MIT Imports: 4 Imported by: 2

README

dbwrap

Build Status

Only works with Go >= 1.1

This library offers two utilities for dealing with the Go database/sql package:

  1. a wrapper that can wrap any database driver that is compatible to sql/driver and intercept calls to it

  2. a fake driver that does nothing but tracking the queries and values that are delivered to him

Why?

Use them to

  • debug a database driver
  • run code before each query
  • transform sql before submitting it to database driver
  • test code that emits sql
  • do logging of sql statements
  • do statistics
  • track long running sql queries

How?

see examples directory

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fake

type Fake struct {
	// contains filtered or unexported fields
}

func NewFake

func NewFake() (*Fake, *sql.DB)

returns a new database and a fake object that you can use to check, what has been queried. Before running a query, first use SetNumInputs() to set the number of parameters you want to pass to Exec or Query and then run the query. After the Query you may run LastQuery() to get the last query and the last parameters that have been committed to the database.

The fake database is not threadsafe and is not meant to be used in an asynchronous way. Instead, open a new fake database for every goroutine.

func (*Fake) Begin

func (f *Fake) Begin() (tx driver.Tx, e error)

func (*Fake) Close

func (f *Fake) Close() (err error)

func (*Fake) LastQuery

func (f *Fake) LastQuery() (string, []driver.Value)

func (*Fake) Name

func (f *Fake) Name() string

func (*Fake) Open

func (f *Fake) Open(name string) (con driver.Conn, err error)

func (*Fake) Prepare

func (f *Fake) Prepare(q string) (driver.Stmt, error)

func (*Fake) SetNumInputs

func (f *Fake) SetNumInputs(i int)

type FakeStmt

type FakeStmt struct {
	*Fake
	// contains filtered or unexported fields
}

func (*FakeStmt) Exec

func (f *FakeStmt) Exec(v []driver.Value) (r driver.Result, e error)

func (*FakeStmt) NumInput

func (f *FakeStmt) NumInput() int

func (*FakeStmt) Query

func (f *FakeStmt) Query(v []driver.Value) (r driver.Rows, e error)

type Wrapper

type Wrapper struct {
	driver.Driver

	// is called after a new connection (innerConn) has been successfully returned by innerDriver
	// you have to return a driver.Conn here to the library. in most cases you
	// will want to return innerConn here
	HandleOpen func(name string, innerConn driver.Conn) (driver.Conn, error)

	// is called instead of innerConn.Begin, which you might want to call at some point
	HandleBegin func(innerConn driver.Conn) (driver.Tx, error)

	// is called instead of innerConn.Prepare, which you might want to call at some point
	HandlePrepare func(innerConn driver.Conn, query string) (driver.Stmt, error)

	// is called instead of innerConn.Close, which you might want to call at some point
	HandleClose func(innerConn driver.Conn) error

	// is called instead of innerConn.(driver.Execer), if innerConn may be casted to a driver.Execer
	// you might want to call conn.Exec at some point
	HandleExec func(conn driver.Execer, query string, args []driver.Value) (driver.Result, error)

	// is called instead of innerConn.(driver.Queryer), if innerConn may be casted to a driver.Queryer
	// you might want to call conn.Query at some point
	HandleQuery func(conn driver.Queryer, query string, args []driver.Value) (driver.Rows, error)

	// is called before each method
	BeforeAll func(conn driver.Conn, event string, data ...interface{})

	// is called after each method
	AfterAll func(conn driver.Conn, event string, data ...interface{})
}

func New

func New(name string, innerDriver driver.Driver) (ø *Wrapper)

registeres a Wrapper as a database driver with the given name and returns it.

the Wrapper is based on the given inner driver and will forward any method call to the underlying innerDriver

before you open a connection to the Wrapper, you may set the different handlers on it it set, they are called instead of the method of the innerDriver and they are given the underlying driver.Conn, so that you may call it

to open a connection, use it the usual way, e.g.

sql.Open(name, connectString)

where name is the given name here and connectString is the same you would pass to innerDriver

func (*Wrapper) Open

func (ø *Wrapper) Open(name string) (con driver.Conn, err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL