sql

package
v0.51.3 Latest Latest
Warning

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

Go to latest
Published: May 9, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptySqlDbId = errors.New("empty sql db id")

ErrEmptySqlDbId is returned if the sql db id is empty.

Functions

func ConvertToNamedValues

func ConvertToNamedValues(values []driver.Value) []driver.NamedValue

ConvertToNamedValues converts the driver.Value slice to a driver.NamedValue slice.

func NewSqlDb

func NewSqlDb(store SqlStore, dsn string) *sql.DB

NewSqlDb opens the sql database driver with the given default dsn.

Types

type Conn

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

Conn implements sql/driver.Conn with a SqlStore.

The Conn can service a single query at a time. When starting a new SQL transaction and/or when executing the first statement, the Conn will call SqlStore.NewSqlTransaction to build a SqlTransaction handle.

When rolling back a sql transaction and/or resetting the conn, if non-nil, SqlTransaction.Discard is called to discard the transaction.

Because there can be multiple underlying sql Conn instances as transactions are built and discarded, the database name (USE) must be remembered by Conn.

func NewConn

func NewConn(store SqlStore, dsn string) *Conn

NewConn constructs a new Conn.

func (*Conn) Begin deprecated

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

Begin starts and returns a new transaction.

Deprecated: Drivers should implement ConnBeginTx instead (or additionally).

func (*Conn) BeginTx

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

BeginTx starts and returns a new transaction. If the context is canceled by the user the sql package will call Tx.Rollback before discarding and closing the connection.

This must check opts.Isolation to determine if there is a set isolation level. If the driver does not support a non-default level and one is set or if there is a non-default isolation level that is not supported, an error must be returned.

This must also check opts.ReadOnly to determine if the read-only value is true to either set the read-only transaction property if supported or return an error if it is not supported.

func (*Conn) Close

func (c *Conn) Close() error

Close invalidates and potentially stops any current prepared statements and transactions, marking this connection as no longer in use.

Because the sql package maintains a free pool of connections and only calls Close when there's a surplus of idle connections, it shouldn't be necessary for drivers to do their own connection caching.

Drivers must ensure all network calls made by Close do not block indefinitely (e.g. apply a timeout).

func (*Conn) Exec deprecated

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

Exec executes a query that doesn't return rows, such as an INSERT or UPDATE.

Deprecated: Drivers should implement StmtExecContext instead (or additionally).

func (*Conn) ExecContext

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

ExecContext executes a query in the Exec mode.

func (*Conn) IsValid

func (c *Conn) IsValid() bool

IsValid is called prior to placing the connection into the connection pool. The connection will be discarded if false is returned.

func (*Conn) Prepare

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

Prepare returns a prepared statement, bound to this connection.

func (*Conn) Query deprecated

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

Query executes a query that may return rows, such as a SELECT.

Deprecated: Drivers should implement StmtQueryContext instead (or additionally).

func (*Conn) QueryContext

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

QueryContext executes a query in the Query mode.

func (*Conn) Release

func (c *Conn) Release()

Release releases the conn fully.

func (*Conn) ResetSession

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

ResetSession is called prior to executing a query on the connection if the connection has been used before. If the driver returns ErrBadConn the connection is discarded.

type ConnStmt

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

ConnStmt implements driver.Stmt with a Conn. Construct with conn.Prepare.

func (*ConnStmt) Close

func (s *ConnStmt) Close() error

Close closes the statement.

As of Go 1.1, a Stmt will not be closed if it's in use by any queries.

Drivers must ensure all network calls made by Close do not block indefinitely (e.g. apply a timeout).

func (*ConnStmt) Exec deprecated

func (s *ConnStmt) Exec(args []driver.Value) (driver.Result, error)

Exec executes a query that doesn't return rows, such as an INSERT or UPDATE.

Deprecated: Drivers should implement StmtExecContext instead (or additionally).

func (*ConnStmt) ExecContext

func (s *ConnStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error)

ExecContext executes a query that doesn't return rows, such as an INSERT or UPDATE.

ExecContext must honor the context timeout and return when it is canceled.

func (*ConnStmt) NumInput

func (s *ConnStmt) NumInput() int

NumInput returns the number of placeholder parameters.

If NumInput returns >= 0, the sql package will sanity check argument counts from callers and return errors to the caller before the statement's Exec or Query methods are called.

NumInput may also return -1, if the driver doesn't know its number of placeholders. In that case, the sql package will not sanity check Exec or Query argument counts.

func (*ConnStmt) Query deprecated

func (s *ConnStmt) Query(args []driver.Value) (driver.Rows, error)

Query executes a query that may return rows, such as a SELECT.

Deprecated: Drivers should implement StmtQueryContext instead (or additionally).

func (*ConnStmt) QueryContext

func (s *ConnStmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error)

QueryContext executes a query that may return rows, such as a SELECT.

QueryContext must honor the context timeout and return when it is canceled.

type ConnTx

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

ConnTx implements driver.Tx attached to a Conn. NOTE: you should use BeginTx to construct this.

func (*ConnTx) Commit

func (c *ConnTx) Commit() error

func (*ConnTx) Rollback

func (c *ConnTx) Rollback() error

type Controller

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

Controller is a common implementation of a SQL engine controller.

func NewController

func NewController(
	info *controller.Info,
	dbID string,
	execute func(ctx context.Context, ctr *ccontainer.CContainer[*SqlStore]) error,
) *Controller

NewController constructs a common SQL engine controller.

func (*Controller) Close

func (c *Controller) Close() error

Close releases any resources used by the controller.

func (*Controller) Execute

func (c *Controller) Execute(ctx context.Context) error

Execute executes the controller.

func (*Controller) GetControllerInfo

func (c *Controller) GetControllerInfo() *controller.Info

GetControllerInfo returns information about the controller.

func (*Controller) GetSqlStore

func (c *Controller) GetSqlStore(ctx context.Context) (SqlStore, error)

GetSqlStore waits for the store to be built.

func (*Controller) HandleDirective

func (c *Controller) HandleDirective(
	ctx context.Context,
	inst directive.Instance,
) ([]directive.Resolver, error)

HandleDirective asks if the handler can resolve the directive.

type Driver

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

Driver implements sql.Driver with a common SqlStore.

func NewDriver

func NewDriver(store SqlStore, dsn string) *Driver

NewDriver constructs a new Driver from a SqlStore.

dsn is the default database name string.

func (*Driver) Connect

func (d *Driver) Connect(ctx context.Context) (driver.Conn, error)

Connect returns a connection to the database. Connect may return a cached connection (one previously closed), but doing so is unnecessary; the sql package maintains a pool of idle connections for efficient re-use.

The provided context.Context is for dialing purposes only (see net.DialContext) and should not be stored or used for other purposes. A default timeout should still be used when dialing as a connection pool may call Connect asynchronously to any query.

The returned connection is only used by one goroutine at a time.

func (*Driver) Driver

func (d *Driver) Driver() driver.Driver

Driver returns the underlying Driver of the Connector, mainly to maintain compatibility with the Driver method on sql.DB.

func (*Driver) Open

func (d *Driver) Open(dsn string) (driver.Conn, error)

Open returns a new connection to the database. The name is a string in a driver-specific format.

Open may return a cached connection (one previously closed), but doing so is unnecessary; the sql package maintains a pool of idle connections for efficient re-use.

The returned connection is only used by one goroutine at a time.

func (*Driver) OpenConnector

func (d *Driver) OpenConnector(dsn string) (driver.Connector, error)

OpenConnector must parse the name in the same format that Driver.Open parses the name parameter.

type DriverConnector

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

DriverConnector implements sql.Connector with a Driver.

func NewDriverConnector

func NewDriverConnector(driver *Driver, dsn string) *DriverConnector

NewDriverConnector constructs a new DriverConnector from a SqlStore.

func (*DriverConnector) Connect

func (d *DriverConnector) Connect(ctx context.Context) (driver.Conn, error)

Connect returns a connection to the database. Connect may return a cached connection (one previously closed), but doing so is unnecessary; the sql package maintains a pool of idle connections for efficient re-use.

The provided context.Context is for dialing purposes only (see net.DialContext) and should not be stored or used for other purposes. A default timeout should still be used when dialing as a connection pool may call Connect asynchronously to any query.

The returned connection is only used by one goroutine at a time.

func (*DriverConnector) Driver

func (d *DriverConnector) Driver() driver.Driver

Driver returns the underlying Driver of the Connector, mainly to maintain compatibility with the Driver method on sql.DB.

type LookupSqlStore

type LookupSqlStore interface {
	// Directive indicates LookupSqlStore is a directive.
	directive.Directive

	// LookupSqlStoreId returns the sql db id to lookup.
	LookupSqlStoreId() string
}

LookupSqlStore is a directive to lookup a SQL store.

func NewLookupSqlStore

func NewLookupSqlStore(dbID string) LookupSqlStore

NewLookupSqlStore constructs a new LookupSqlStore directive.

type LookupSqlStoreValue

type LookupSqlStoreValue = SqlStore

LookupSqlStoreValue is the result type for LookupSqlStore. Multiple results may be pushed to the directive.

func ExLookupSqlStore

func ExLookupSqlStore(
	ctx context.Context,
	b bus.Bus,
	dbID string,
	returnIfIdle bool,
	valDisposeCb func(),
) (LookupSqlStoreValue, directive.Instance, directive.Reference, error)

ExLookupSqlStore waits for the sql db to be resolved. if returnIfIdle is set and the directive becomes idle, returns nil, nil, nil,

type SqlConn

SqlConn is the set of interfaces that Conn implements.

type SqlOps

type SqlOps interface {
	driver.Execer //nolint:staticcheck
	driver.ExecerContext

	driver.Queryer //nolint:staticcheck
	driver.QueryerContext
}

SqlOps are operations on the SQL DB transaction.

type SqlStore

type SqlStore interface {
	// NewSqlTransaction starts a new SqlStore transaction.
	//
	// If !write, the transaction should be read-only.
	// dsn is the default database name for the transaction.
	NewSqlTransaction(
		ctx context.Context,
		write bool,
		dsn string,
	) (SqlTransaction, error)
}

SqlStore is a transactional MySQL store.

type SqlTransaction

type SqlTransaction interface {
	// Tx is the transaction interface.
	tx.Tx
	// GetReadOnly returns if the transaction is read-only.
	GetReadOnly() bool
	// GetSqlOps returns the sql operations interface.
	// see the comments in the stdlib sql/driver package for more information.
	GetSqlOps(ctx context.Context) (SqlOps, error)
}

SqlTransaction is a SQL DB transaction.

Directories

Path Synopsis
rpc

Jump to

Keyboard shortcuts

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