mysqltest

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package mysqltest is a small fake MySQL server for tests, in this package and in others. It speaks the text query protocol only (handshake v10 and mysql_native_password), so a test must use the driver's default query_mode (simple). It does not parse SQL: the test gives it a Handler that answers each SQL text.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Column

type Column struct {
	Name string
	Type string
}

Column is one column of a Result. Type is a server type name such as "BIGINT", "VARCHAR" or "UNSIGNED BIGINT". Use Text and Int for the usual ones.

func Int

func Int(name string) Column

Int makes a BIGINT column.

func Text

func Text(name string) Column

Text and Int make a VARCHAR column and a BIGINT column.

type Handler

type Handler func(sql string) []Result

Handler answers one query text. It returns one Result for each statement in the text. No result at all means an OK packet with nothing changed.

type Login

type Login struct {
	User string
	DB   string
}

Login is one client that finished the handshake.

type Result

type Result struct {
	Columns      []Column
	Rows         [][]any
	Affected     int64
	LastInsertID int64
	ErrNumber    uint16
	ErrState     string
	ErrMessage   string
}

Result is the answer to one statement. A cell is printed as text, or nil for NULL. Set ErrNumber to make the statement fail. The statements after it in the same reply are not sent.

type Server

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

Server is a running fake server.

func Start

func Start(t testing.TB, h Handler) *Server

Start starts a server on a free local port and stops it when the test ends.

func StartTLS added in v0.8.0

func StartTLS(t testing.TB, h Handler, cfg *tls.Config) *Server

StartTLS is Start, and it offers TLS with cfg after the client asks.

func StartWithPassword

func StartWithPassword(t testing.TB, h Handler, password string) *Server

StartWithPassword is Start, but a client must log in with this password.

func (*Server) ConnCount

func (s *Server) ConnCount() int

ConnCount is how many connections have logged in.

func (*Server) Inits

func (s *Server) Inits() []string

Inits are the session statements the client sent, such as the read-only setting and the sql_mode query. They are not in Queries.

func (*Server) Logins

func (s *Server) Logins() []Login

Logins are the user and database of each connection.

func (*Server) Queries

func (s *Server) Queries() []string

Queries are the SQL texts received, in order, without the session setup the driver sends on each new connection.

func (*Server) SetSQLMode

func (s *Server) SetSQLMode(mode string)

SetSQLMode sets the text SELECT @@SESSION.sql_mode returns.

func (*Server) URL

func (s *Server) URL() string

URL is the target to give the driver.

Jump to

Keyboard shortcuts

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