internal

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDriver

func NewDriver(d driver.Driver, logger Logger) driver.Driver

NewDriver returns a new wrapped driver.

func NewUID

func NewUID() string

NewUID generate default 8 byte unique id using math/rand.

Types

type Conn

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

func NewConn

func NewConn(conn driver.Conn, logger Logger) *Conn

NewConn returns a new wrapped Conn.

func (*Conn) Begin deprecated

func (c *Conn) Begin() (_ driver.Tx, err 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, err 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) CheckNamedValue

func (c *Conn) CheckNamedValue(namedValue *driver.NamedValue) (err error)

func (*Conn) Close

func (c *Conn) Close() (err error)

func (*Conn) Exec deprecated

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

Execer is an optional interface that may be implemented by a Conn.

If a Conn implements neither ExecerContext nor Execer, the sql package's DB.Exec will first prepare a query, execute the statement, and then close the statement.

Exec may return ErrSkip.

Deprecated: Drivers should implement ExecerContext instead.

func (*Conn) ExecContext

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

ExecerContext is an optional interface that may be implemented by a Conn.

If a Conn does not implement ExecerContext, the sql package's DB.Exec will fall back to Execer; if the Conn does not implement Execer either, DB.Exec will first prepare a query, execute the statement, and then close the statement.

ExecContext may return ErrSkip.

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

func (*Conn) Ping

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

Pinger is an optional interface that may be implemented by a Conn.

If a Conn does not implement Pinger, the sql package's DB.Ping and DB.PingContext will check if there is at least one Conn available.

If Conn.Ping returns ErrBadConn, DB.Ping and DB.PingContext will remove the Conn from pool.

func (*Conn) Prepare

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

Prepare returns a prepared statement, bound to this connection.

func (*Conn) PrepareContext

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

ConnPrepareContext enhances the Conn interface with context.

func (*Conn) Query deprecated

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

Queryer is an optional interface that may be implemented by a Conn.

If a Conn implements neither QueryerContext nor Queryer, the sql package's DB.Query will first prepare a query, execute the statement, and then close the statement.

Query may return ErrSkip.

Deprecated: Drivers should implement QueryerContext instead.

func (*Conn) QueryContext

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

QueryerContext is an optional interface that may be implemented by a Conn.

If a Conn does not implement QueryerContext, the sql package's DB.Query will fall back to Queryer; if the Conn does not implement Queryer either, DB.Query will first prepare a query, execute the statement, and then close the statement.

QueryContext may return ErrSkip.

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

func (*Conn) ResetSession

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

SessionResetter may be implemented by Conn to allow drivers to reset the session state associated with the connection and to signal a bad connection.

type Connector

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

Connector represents a driver in a fixed configuration.

func NewConnector

func NewConnector(dsn string, d driver.Driver, logger Logger) *Connector

func (*Connector) Connect

func (c *Connector) Connect(ctx context.Context) (_ driver.Conn, err 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 (*Connector) Driver

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

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

type Driver

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

Driver is the interface that must be implemented by a database.

func (*Driver) Open

func (d *Driver) Open(name 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(name string) (driver.Connector, error)

If a Driver implements DriverContext, then sql.DB will call OpenConnector to obtain a Connector and then invoke that Connector's Connect method to obtain each needed connection, instead of invoking the Driver's Open method for each connection. The two-step sequence allows drivers to parse the name just once and also provides access to per-Conn contexts.

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

type Logger

type Logger struct {
	*slog.Logger
	BaseLevel    slog.Level
	BasePrefix   string
	StmtPrefix   string
	TxPrefix     string
	WithDuration bool
	WarnErrSkip  bool
}

func (Logger) Log

func (l Logger) Log(ctx context.Context, level slog.Level, msg string, started time.Time, err error, attrs ...slog.Attr)

func (Logger) With

func (l Logger) With(attrs ...any) Logger

type Stmt

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

func NewStmt

func NewStmt(stmt driver.Stmt, query string, logger Logger) *Stmt

func (*Stmt) CheckNamedValue

func (s *Stmt) CheckNamedValue(namedValue *driver.NamedValue) (err error)

CheckNamedValue is called before passing arguments to the driver and is called in place of any ColumnConverter. CheckNamedValue must do type validation and conversion as appropriate for the driver.

func (*Stmt) Close

func (s *Stmt) Close() (err 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 (*Stmt) Exec deprecated

func (s *Stmt) Exec(args []driver.Value) (_ driver.Result, err 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 (*Stmt) ExecContext

func (s *Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (_ driver.Result, err 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 (*Stmt) NumInput

func (s *Stmt) 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 (*Stmt) Query deprecated

func (s *Stmt) Query(args []driver.Value) (_ driver.Rows, err error)

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

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

func (*Stmt) QueryContext

func (s *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (_ driver.Rows, err 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 Tx

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

Tx is a transaction.

func NewTx

func NewTx(tx driver.Tx, logger Logger) *Tx

func (*Tx) Commit

func (t *Tx) Commit() (err error)

func (*Tx) Rollback

func (t *Tx) Rollback() (err error)

Jump to

Keyboard shortcuts

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