xsql

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2021 License: Apache-2.0 Imports: 14 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunInTransaction

func RunInTransaction(ctx context.Context, db DB, fn func(tx *Tx) error) (err error)

RunInTransaction executes given function based on provided 'db' within a transaction.

Types

type Config added in v0.0.16

type Config struct {
	// LongQueriesTime is the duration at which the query is marked to be long-running.
	LongQueriesTime time.Duration
	// WarnLongQueries is a flag which set to true warns on long-running queries.
	WarnLongQueries bool
}

Config is the configuration for the database connection.

type Conn

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

Conn is the database connection.

func Connect

func Connect(driver database.Driver, dataSourceName string) (*Conn, error)

Connect establish a new database connection using provided driverName and given dataSourceName (DSN).

func (*Conn) As

func (c *Conn) As(in interface{}) error

As sets the input in to one of the following types:

  • *sqlx.DB
  • *sql.DB

func (*Conn) Begin

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

Begin starts a new transaction.

func (*Conn) BeginTx

func (c *Conn) BeginTx(ctx context.Context, options *sql.TxOptions) (*Tx, error)

BeginTx starts a new transaction. The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if this context is canceled.

func (*Conn) BindNamed

func (c *Conn) BindNamed(q string, arg interface{}) (query string, args []interface{}, err error)

BindNamed binds the named arguments.

func (*Conn) CanRetry added in v0.0.16

func (c *Conn) CanRetry(err error) bool

CanRetry checks if the query done by given connection could be retried.

func (*Conn) DriverName

func (c *Conn) DriverName() string

DriverName gets the name of the driver provided during establishing connection.

func (*Conn) ErrorCode added in v0.0.16

func (c *Conn) ErrorCode(err error) cgerrors.ErrorCode

ErrorCode gets the error code related to given database result error.

func (*Conn) Exec

func (c *Conn) Exec(query string, args ...interface{}) (sql.Result, error)

Exec execute provided query with the input arguments.

func (*Conn) ExecContext

func (c *Conn) ExecContext(ctx context.Context, query string, args ...interface{}) (res sql.Result, err error)

ExecContext executes provided query with the input arguments. The connection is aware of given context.

func (*Conn) Ping

func (c *Conn) Ping() error

Ping verifies a connection to the database is still alive, establishing a connection if necessary.

func (*Conn) PingContext

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

PingContext verifies a connection to the database is still alive, establishing a connection if necessary.

func (*Conn) Prepare

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

Prepare creates a prepared statement.

func (*Conn) PrepareContext

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

PrepareContext creates a prepared statement. Provided context is used for the preparation of the statement, not for the execution of the statement.

func (*Conn) Query

func (c *Conn) Query(query string, args ...interface{}) (*Rows, error)

Query queries the database and returns an *xsql.Rows. Any placeholder parameters are replaced with supplied args.

func (*Conn) QueryContext

func (c *Conn) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)

QueryContext queries the database and returns an *xsql.Rows. Any placeholder parameters are replaced with supplied args.

func (*Conn) QueryRow

func (c *Conn) QueryRow(query string, args ...interface{}) *Row

QueryRow queries the database and returns an *xsql.Row. Any placeholder parameters are replaced with supplied args.

func (*Conn) QueryRowContext

func (c *Conn) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row

QueryRowContext queries the database and returns an *xsql.Row. Any placeholder parameters are replaced with supplied args.

func (*Conn) Rebind

func (c *Conn) Rebind(query string) string

Rebind change input query bindings to the ones that matches given database driver.

func (*Conn) RunInTransaction

func (c *Conn) RunInTransaction(ctx context.Context, fn func(tx *Tx) error) error

RunInTransaction runs provided function within a new database transaction.

func (*Conn) SetLongQueriesTime added in v0.0.16

func (c *Conn) SetLongQueriesTime(d time.Duration)

SetLongQueriesTime sets the duration at which the queries would become treated as long-running.

func (*Conn) WarnOnLongQueries added in v0.0.16

func (c *Conn) WarnOnLongQueries(warn bool)

WarnOnLongQueries logs warnings if the queries are long-running.

type DB

type DB interface {
	// QueryContext queries the database and returns an *xsql.Rows. Any placeholder parameters are replaced with supplied args.
	QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)
	// QueryRowContext queries the database and returns an *xsql.Row. Any placeholder parameters are replaced with supplied args.
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row

	// Query queries the database and returns an *xsql.Rows. Any placeholder parameters are replaced with supplied args.
	Query(query string, args ...interface{}) (*Rows, error)
	// QueryRow queries the database and returns an *xsql.Row. Any placeholder parameters are replaced with supplied args.
	QueryRow(query string, args ...interface{}) *Row

	// ExecContext executes provided query with the input arguments.
	// The connection is aware of given context.
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	// Exec execute provided query with the input arguments.
	Exec(query string, args ...interface{}) (sql.Result, error)

	// PrepareContext creates a prepared statement.
	// Provided context is used for the preparation of the statement, not for the execution of the statement.
	PrepareContext(ctx context.Context, query string) (*Stmt, error)
	// Prepare creates a prepared statement.
	Prepare(query string) (*Stmt, error)

	// As extracts the types on which given implementation is based on.
	// I.e: Tx accepts: **sqlx.Tx or **sql.Tx.
	As(in interface{}) error

	// ErrorCode gets the error code for related database error.
	ErrorCode(err error) cgerrors.ErrorCode
	// CanRetry checks if the query could be retried for given error.
	CanRetry(err error) bool

	// Rebind changes argument format in provided query.
	Rebind(query string) string

	// DriverName gets the driver name.
	DriverName() string
}

DB is the common interface for both the Conn and Tx.

type Row

type Row sqlx.Row

Row is the row type wrapper of the sqlx.Row.

func (*Row) As

func (r *Row) As(in interface{}) error

As extracts the *sqlx.Row from given Row type.

func (*Row) ColumnTypes

func (r *Row) ColumnTypes() ([]*sql.ColumnType, error)

ColumnTypes returns the underlying sql.Rows.ColumnTypes(), or the deferred error.

func (*Row) Columns

func (r *Row) Columns() ([]string, error)

Columns returns the underlying sql.Rows.Columns(), or the deferred error usually returned by Row.Scan()

func (*Row) Err

func (r *Row) Err() error

Err returns the error encountered while scanning.

func (*Row) MapScan

func (r *Row) MapScan(dest map[string]interface{}) error

MapScan scans the row into an input dest map.

func (*Row) Scan

func (r *Row) Scan(dest ...interface{}) error

Scan is a fixed implementation of sql.Row.Scan, which does not discard the underlying error from the internal rows object if it exists.

func (*Row) SliceScan

func (r *Row) SliceScan() ([]interface{}, error)

SliceScan scans the row into a slice.

func (*Row) StructScan

func (r *Row) StructScan(dest interface{}) error

StructScan scans the row as a structure provided as argument.

type Rows

type Rows sqlx.Rows

Rows is the simple type wrapper for the sqlx.Rows that provides minor enhancement.

func (*Rows) As

func (r *Rows) As(in interface{}) error

As extracts the *sqlx.Rows or *sql.Rows implementation by providing a pointer to it.

func (*Rows) Close

func (r *Rows) Close() error

Close closes the Rows, preventing further enumeration. If Next is called and returns false and there are no further result sets, the Rows are closed automatically, and it will suffice to check the result of Err. Close is idempotent and does not affect the result of Err.

func (*Rows) ColumnTypes

func (r *Rows) ColumnTypes() ([]*sql.ColumnType, error)

ColumnTypes returns column information such as column type, length, and nullable. Some information may not be available from some drivers.

func (*Rows) Columns

func (r *Rows) Columns() ([]string, error)

Columns returns the column names. It returns an error if the rows are closed

func (*Rows) Err

func (r *Rows) Err() error

Err returns the error, if any, that was encountered during iteration. It may be called after an explicit or implicit Close.

func (*Rows) MapScan

func (r *Rows) MapScan(dest map[string]interface{}) error

MapScan using this Rows.

func (*Rows) Next

func (r *Rows) Next() bool

Next prepares the next result row for reading with the Scan method. It returns true on success, or false if there is no next result row, or an error happened while preparing it. Err should be consulted to distinguish between the two cases. Every call to Scan, even the first one, must be preceded by a call to Next.

func (*Rows) NextResultSet

func (r *Rows) NextResultSet() bool

NextResultSet prepares the next result set for reading. It reports whether there is further result sets, or false if there is no further result set or if there is an error advancing to it. The Err method should be consulted to distinguish between the two cases. After calling NextResultSet, the Next method should always be called before scanning. If there are further result sets they may not have rows in the result set.

func (*Rows) Scan

func (r *Rows) Scan(dest ...interface{}) error

Scan copies the columns in the current row into the values pointed at by dest. The number of values in dest must be the same as the number of columns in Rows. It converts columns read from the database into the following common Go types and special types provided by the sql package: - *string - *[]byte - *int, *int8, *int16, *int32, *int64 - *uint, *uint8, *uint16, *uint32, *uint64 - *bool - *float32, *float64 - *interface{} - *RawBytes - *Rows (cursor value) any type implementing Scanner (see Scanner docs) In the most simple case, if the type of the value from the source column is an integer, bool or string type T and dest is of type *T, Scan simply assigns the value through the pointer. Scan also converts between string and numeric types, as long as no information would be lost. While Scan stringifies all numbers scanned from numeric database columns into *string, scans into numeric types are checked for overflow. For example, a float64 with value 300, or a string with value "300" can scan into a uint16, but not into a uint8, though float64(255) or "255" can scan into an uint8. One exception is that scans of some float64 numbers to strings may lose information when stringifying. In general, scan floating point columns into *float64. If a dest argument has type *[]byte, Scan saves in that argument a copy of the corresponding data. The copy is owned by the caller and can be modified and held indefinitely. The copy can be avoided by using an argument of type *RawBytes instead; see the documentation for RawBytes for restrictions on its use. If an argument has type *interface{}, Scan copies the value provided by the underlying driver without conversion. When scanning from a source value of type []byte to *interface{}, a copy of the slice is made, and the caller owns the result. Source values of type time.Time may be scanned into values of type *time.Time, *interface{}, *string, or *[]byte. When converting to the latter two, time.RFC3339Nano is used. Source values of type bool may be scanned into types *bool, *interface{}, *string, *[]byte, or *RawBytes. For scanning into *bool, the source may be true, false, 1, 0, or string inputs parseable by strconv.ParseBool. Scan can also convert a cursor returned from a query, such as "select cursor(select * from my_table) from dual", into a *Rows value that can itself be scanned from. The parent select query will close any cursor *Rows if the parent *Rows is closed. If any of the first arguments implementing Scanner returns an error, that error will be wrapped in the returned error.

func (*Rows) SliceScan

func (r *Rows) SliceScan() ([]interface{}, error)

SliceScan using this Rows.

func (*Rows) StructScan

func (r *Rows) StructScan(dest interface{}) error

StructScan is like sql.Rows.Scan, but scans a single Row into a single Struct. Use this and iterate over Rows manually when the memory load of Select() might be prohibitive. *Rows.StructScan caches the reflection work of matching up column positions to fields to avoid that overhead per scan, which means it is not safe to run StructScan on the same Rows instance with different struct types.

type Stmt

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

Stmt is the xsql type wrapper for the sqlx.Stmt.

func (*Stmt) As

func (s *Stmt) As(in interface{}) error

As extracts direct implementation of the stmt in the *sqlx.Stmt or *sql.Stmt.

func (*Stmt) Close

func (s *Stmt) Close() error

Close closes given statement.

func (*Stmt) Exec

func (s *Stmt) Exec(args ...interface{}) (sql.Result, error)

Exec executes the statement with provided arguments.

func (*Stmt) ExecContext

func (s *Stmt) ExecContext(ctx context.Context, args ...interface{}) (res sql.Result, err error)

ExecContext executes the statement with provided arguments.

func (*Stmt) Query

func (s *Stmt) Query(args ...interface{}) (*Rows, error)

Query executes statement query with provided arguments.

func (*Stmt) QueryContext

func (s *Stmt) QueryContext(ctx context.Context, args ...interface{}) (*Rows, error)

QueryContext executes statement query with provided arguments.

func (*Stmt) QueryRow

func (s *Stmt) QueryRow(args ...interface{}) *Row

QueryRow executes a query with provided arguments and creates a new Row.

func (*Stmt) QueryRowContext

func (s *Stmt) QueryRowContext(ctx context.Context, args ...interface{}) *Row

QueryRowContext executes a query with provided arguments and creates a new Row. The connection is based on given context.

type Tx

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

Tx is the database connection.

func (*Tx) As

func (tx *Tx) As(in interface{}) error

As extracts the transaction base types like: - *sqlx.Tx - *sql.Tx

func (*Tx) BindNamed

func (tx *Tx) BindNamed(s string, i interface{}) (string, []interface{}, error)

BindNamed binds the named arguments.

func (*Tx) CanRetry added in v0.0.16

func (tx *Tx) CanRetry(err error) bool

CanRetry checks if the query done within given transaction could be retried.

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit commits this transaction.

func (*Tx) DriverName

func (tx *Tx) DriverName() string

DriverName gets the name of the driver provided during establishing connection.

func (*Tx) ErrorCode added in v0.0.16

func (tx *Tx) ErrorCode(err error) cgerrors.ErrorCode

ErrorCode gets the error code related to given database error.

func (*Tx) Exec

func (tx *Tx) Exec(query string, args ...interface{}) (sql.Result, error)

Exec execute provided query with the input arguments.

func (*Tx) ExecContext

func (tx *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (res sql.Result, err error)

ExecContext execute provided query with the input arguments.

func (*Tx) Prepare

func (tx *Tx) Prepare(query string) (*Stmt, error)

Prepare creates a prepared statement.

func (*Tx) PrepareContext

func (tx *Tx) PrepareContext(ctx context.Context, query string) (*Stmt, error)

PrepareContext creates a prepared statement. Provided context is used for the preparation of the statement, not for the execution of the statement.

func (*Tx) Query

func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error)

Query queries the database within given transaction and returns an *xsql.Rows. Any placeholder parameters are replaced with supplied args.

func (*Tx) QueryContext

func (tx *Tx) QueryContext(ctx context.Context, query string, args ...interface{}) (*Rows, error)

QueryContext queries the database within given transaction and returns an *xsql.Rows. Any placeholder parameters are replaced with supplied args.

func (*Tx) QueryRow

func (tx *Tx) QueryRow(query string, args ...interface{}) *Row

QueryRow queries the database within given transaction and returns an *xsql.Row. Any placeholder parameters are replaced with supplied args.

func (*Tx) QueryRowContext

func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *Row

QueryRowContext queries the database within given transaction and returns an *xsql.Row. Any placeholder parameters are replaced with supplied args.

func (*Tx) Rebind

func (tx *Tx) Rebind(s string) string

Rebind change input query bindings to the ones that matches given database driver.

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback aborts this transaction.

func (*Tx) RunInTransaction

func (tx *Tx) RunInTransaction(fn func(tx *Tx) error) error

RunInTransaction runs a function in the transaction. If function returns an error transaction is rolled back, otherwise transaction is committed.

Jump to

Keyboard shortcuts

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