driver

package
v0.0.0-...-31b29a2 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

This package implements database/sql/driver interface, so we can use go-mysql with database/sql

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddNamedValueChecker

func AddNamedValueChecker(nvCheckFunc ...CheckNamedValueFunc)

AddNamedValueChecker sets a custom NamedValueChecker for the driver connection which allows for more control in handling Go and database types beyond the default Value types. See https://pkg.go.dev/database/sql/driver#NamedValueChecker Usage requires a full import of the driver (not by side-effects only). Also note that this function is not concurrent-safe, and should only be executed while setting up the driver before establishing any connections via `sql.Open()`.

func CollationOption

func CollationOption(c *client.Conn, value string) error

func CompressOption

func CompressOption(c *client.Conn, value string) error

func ReadTimeoutOption

func ReadTimeoutOption(c *client.Conn, value string) error

func SetCustomTLSConfig

func SetCustomTLSConfig(dsn string, caPem []byte, certPem []byte, keyPem []byte, insecureSkipVerify bool, serverName string) error

SetCustomTLSConfig sets a custom TLSConfig for the address (host:port) of the supplied DSN. It requires a full import of the driver (not by side-effects only). Example of supplying a custom CA, no client cert, no key, validating the certificate, and supplying a serverName for the validation:

driver.SetCustomTLSConfig(CaPem, make([]byte, 0), make([]byte, 0), false, "my.domain.name")

func SetDSNOptions

func SetDSNOptions(customOptions map[string]DriverOption)

SetDSNOptions sets custom options to the driver that allows modifications to the connection. It requires a full import of the driver (not by side-effects only). Example of supplying a custom option:

driver.SetDSNOptions(map[string]DriverOption{
		"my_option": func(c *client.Conn, value string) error {
			c.SetCapability(mysql.CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS)
			return nil
		},
	})

func UseSslOption

func UseSslOption(c *client.Conn) error

UseSslOption sets the connection to use a tls.Config with InsecureSkipVerify set to true. Use SetTLSConfig() if you need a custom tls.Config

func UseSslSkipVerifyOption

func UseSslSkipVerifyOption(c *client.Conn) error

UseSslSkipVerifyOption sets the connection to use a tls.Config with InsecureSkipVerify set to true. Currently the same as `SetTLSConfig`.

func WriteTimeoutOption

func WriteTimeoutOption(c *client.Conn, value string) error

Types

type CheckNamedValueFunc

type CheckNamedValueFunc func(*sqldriver.NamedValue) error

type Conn

type Conn struct {
	*client.Conn
	// contains filtered or unexported fields
}

func (*Conn) Begin

func (c *Conn) Begin() (sqldriver.Tx, error)

func (*Conn) BeginTx

func (c *Conn) BeginTx(ctx context.Context, opts sqldriver.TxOptions) (sqldriver.Tx, error)

func (*Conn) CheckNamedValue

func (c *Conn) CheckNamedValue(nv *sqldriver.NamedValue) error

func (*Conn) Close

func (c *Conn) Close() error

func (*Conn) Exec

func (c *Conn) Exec(query string, args []sqldriver.Value) (sqldriver.Result, error)

func (*Conn) ExecContext

func (c *Conn) ExecContext(ctx context.Context, query string, args []sqldriver.NamedValue) (sqldriver.Result, error)

func (*Conn) IsValid

func (c *Conn) IsValid() bool

func (*Conn) Ping

func (c *Conn) Ping(ctx context.Context) error

func (*Conn) Prepare

func (c *Conn) Prepare(query string) (sqldriver.Stmt, error)

func (*Conn) PrepareContext

func (c *Conn) PrepareContext(ctx context.Context, query string) (sqldriver.Stmt, error)

func (*Conn) Query

func (c *Conn) Query(query string, args []sqldriver.Value) (sqldriver.Rows, error)

func (*Conn) QueryContext

func (c *Conn) QueryContext(ctx context.Context, query string, args []sqldriver.NamedValue) (sqldriver.Rows, error)

type Connector

type Connector struct {
	Addr     string
	User     string
	Password string
	DB       string
	Params   url.Values
}

Connector implements database/sql/driver.Connector so it can be used with database/sql.OpenDB to avoid constructing a DSN string manually.

Most configuration is provided via Params, using the same option keys as the standard DSN form:

[user[:password]@]addr[/db[?param=X]]

Note: the legacy DSN form is still supported when using ParseDSN, but it cannot carry URL parameters.

func ParseDSN

func ParseDSN(dsn string) (Connector, error)

ParseDSN takes a DSN string and splits it up into struct containing addr, user, password and db. It returns an error if unable to parse.

Legacy form uses a `?` as the path separator: user:password@addr[?db] Standard form uses a `/`: user:password@addr/db?param=value

Optional URL parameters are supported in the standard DSN form. The legacy form cannot carry URL parameters.

func (Connector) Connect

func (ci Connector) Connect(ctx context.Context) (sqldriver.Conn, error)

func (Connector) Driver

func (ci Connector) Driver() sqldriver.Driver

type DriverOption

type DriverOption func(c *client.Conn, value string) error

DriverOption sets configuration on a client connection before the MySQL handshake. The value represents the query string parameter value supplied by in the DNS.

Jump to

Keyboard shortcuts

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