fakedriver

package
v0.47.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package fakedriver is a minimal in-memory database/sql driver used by the sentrysql tests. It provides three distinct driver shapes:

  • CtxDriver: implements driver.Driver and driver.DriverContext; connections satisfy ExecerContext, QueryerContext, ConnPrepareContext, ConnBeginTx, and Pinger.
  • LegacyDriver: implements only driver.Driver (no DriverContext); connections implement the pre-context Execer, Queryer, Conn.Begin, and Conn.Prepare.
  • MinimalDriver: implements only driver.Driver; connections implement only the required driver.Conn methods (Prepare, Close, Begin). No Execer or Queryer, which forces database/sql to fall back to Prepare + Stmt.Exec/ Stmt.Query, exercising the deepest wrapper fallback paths.
  • SkipDriver: implements the context-aware query/exec interfaces but returns driver.ErrSkip from them, forcing database/sql to fall back to prepared statements.

Use NewCtx / NewLegacy / NewMinimal to construct each shape and Register to expose it by name via database/sql.

Index

Constants

This section is empty.

Variables

View Source
var ErrDriver = errors.New("fakedriver: error")

ErrDriver is a reusable error for failure-injection tests.

Functions

func Register

func Register(name string, d driver.Driver)

Register registers d under name with database/sql. Safe to call multiple times with the same name.

Types

type CtxConnector

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

CtxConnector is an exported connector wrapper backed by a CtxDriver. Unlike the internal ctxConnector returned by CtxDriver.OpenConnector, this one implements io.Closer with an observable close counter so tests can verify that the sentrysql wrapper propagates DB.Close through to inner connectors.

func NewCtxConnector

func NewCtxConnector(drv *CtxDriver) *CtxConnector

NewCtxConnector constructs a CtxConnector.

func (*CtxConnector) Close

func (c *CtxConnector) Close() error

Close implements io.Closer. The counter it increments is observable via CloseCount for verification in tests.

func (*CtxConnector) CloseCount

func (c *CtxConnector) CloseCount() int

CloseCount reports how many times Close was invoked on this connector.

func (*CtxConnector) Connect

func (c *CtxConnector) Connect(_ context.Context) (driver.Conn, error)

Connect implements driver.Connector.

func (*CtxConnector) Driver

func (c *CtxConnector) Driver() driver.Driver

Driver implements driver.Connector.

type CtxDriver

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

CtxDriver is a modern driver that implements driver.DriverContext in addition to driver.Driver. Connections it returns satisfy the full context-aware interface set.

func NewCtx

func NewCtx() *CtxDriver

NewCtx returns a new CtxDriver.

func (*CtxDriver) Open

func (d *CtxDriver) Open(_ string) (driver.Conn, error)

Open implements driver.Driver.

func (*CtxDriver) OpenConnector

func (d *CtxDriver) OpenConnector(_ string) (driver.Connector, error)

OpenConnector implements driver.DriverContext.

func (*CtxDriver) SetFailure

func (d *CtxDriver) SetFailure(err error)

SetFailure makes subsequent Exec/Query calls return err. Pass nil to clear.

type LegacyConnector

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

LegacyConnector wraps a LegacyDriver as a driver.Connector so tests can exercise the sentrysql wrapper's behavior when a connector's Driver() does not implement driver.DriverContext. It does not implement io.Closer.

func NewLegacyConnector

func NewLegacyConnector(drv *LegacyDriver) *LegacyConnector

NewLegacyConnector constructs a LegacyConnector.

func (*LegacyConnector) Connect

func (c *LegacyConnector) Connect(_ context.Context) (driver.Conn, error)

Connect implements driver.Connector.

func (*LegacyConnector) Driver

func (c *LegacyConnector) Driver() driver.Driver

Driver implements driver.Connector.

type LegacyDriver

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

LegacyDriver implements only the pre-context driver.Driver interface.

func NewLegacy

func NewLegacy() *LegacyDriver

NewLegacy returns a new LegacyDriver.

func (*LegacyDriver) Open

func (d *LegacyDriver) Open(_ string) (driver.Conn, error)

Open implements driver.Driver.

func (*LegacyDriver) SetFailure

func (d *LegacyDriver) SetFailure(err error)

SetFailure makes subsequent Exec/Query calls return err. Pass nil to clear.

type MinimalDriver

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

MinimalDriver implements only driver.Driver. Connections it returns implement only the required driver.Conn methods — no Execer, Queryer, ConnBeginTx, ConnPrepareContext, or Pinger. Used to exercise the wrapper's deepest fallback path where ExecContext returns driver.ErrSkip and database/sql falls back to Prepare + Stmt.Exec/Stmt.Query.

func NewMinimal

func NewMinimal() *MinimalDriver

NewMinimal returns a new MinimalDriver.

func (*MinimalDriver) Open

func (d *MinimalDriver) Open(_ string) (driver.Conn, error)

Open implements driver.Driver.

func (*MinimalDriver) SetFailure

func (d *MinimalDriver) SetFailure(err error)

SetFailure makes subsequent Stmt.Exec/Stmt.Query calls return err. Pass nil to clear.

type SkipDriver

type SkipDriver struct{}

SkipDriver forces database/sql to fall back after the context-aware methods return driver.ErrSkip.

func NewSkip

func NewSkip() *SkipDriver

NewSkip returns a new SkipDriver.

func (*SkipDriver) Open

func (d *SkipDriver) Open(_ string) (driver.Conn, error)

Open implements driver.Driver.

Jump to

Keyboard shortcuts

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