sqlm

package module
v0.0.0-...-662fd6f Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2023 License: MIT Imports: 7 Imported by: 0

README

SQL Management

GoDoc Build Status Code Coverage Go Report Card

sqlm is a Go package providing facilities to deal with SQL database.

Features

  1. todo

Examples


Documentation

Index

Constants

View Source
const (
	// MaxConn is the maximum number of open connections to the database.
	MaxConn = 25
	// MaxLifetime is the maximum amount of time a connection may be reused.
	MaxLifetime = 5 * time.Minute
	// Timeout is the default timeout duration.
	Timeout = 5 * time.Second
)
View Source
const MySQLDriver = "mysql"

MySQLDriver is name of MySQL driver.

Variables

This section is empty.

Functions

func ExecAndLastInsertID

func ExecAndLastInsertID(ctx context.Context, conn Tx, query string, args ...any) (id int64, err error)

ExecAndLastInsertID executes a prepared statement with the given arguments and returns the integer generated by the database in response to a command. Typically, this will be from an "auto increment" column when inserting a new row. Not all databases support this feature, and the syntax of such statements varies.

func ExecAndRowsAffected

func ExecAndRowsAffected(ctx context.Context, conn Tx, query string, args ...any) (id int64, err error)

ExecAndRowsAffected executes a prepared statement with the given arguments and returns the number of rows affected by an update, insert, or delete. Not every database or database driver may support this.

func MySQLOpen

func MySQLOpen(dataSourceName string) (*sql.DB, error)

MySQLOpen opens and validates a MySQL database pool of connections.

func Open

func Open(driverName, dataSourceName string, maxConn int, maxLifetime, pingTimeout time.Duration) (*sql.DB, error)

Open opens a database specified by its database driver name and a driver-specific data source name, usually consisting of at least a database name and connection information. It also validates the data source name by calling a Ping.

func WithTx

func WithTx(ctx context.Context, db BeginTx, f func(Tx) error) (err error)

WithTx creates a new transaction and handles commit/rollback based on the returned error. It uses db to handle the transaction and ctx to isolate it. See https://golang.org/doc/go1.8#database_sql.

Types

type Any

type Any map[string]any

Any is map of string containing any values.

func QueryAny

func QueryAny(ctx context.Context, conn Tx, query string, args ...any) (Any, error)

QueryAny executes a query that is expected to return at most one row, so one Any. The args are for any placeholder parameters in the query. If multiple rows are returned by the query, the Scan method discards all but the first.

func QueryAnyRows

func QueryAnyRows(ctx context.Context, conn Tx, query string, args ...any) ([]Any, error)

QueryAnyRows executes a query that returns rows as slice of Any, typically a SELECT. The args are for any placeholder parameters in the query.

func (Any) String

func (m Any) String(key string) string

String returns the string representation of the value behind this key. If it's a nil pointer, we return a blank string instead.

type BeginTx

type BeginTx interface {
	// BeginTx starts a transaction.
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
}

BeginTx represents the database connection who handles transactions.

type Tx

type Tx interface {
	// ExecContext executes a prepared statement with the given arguments and
	// returns a Result summarizing the effect of the statement.
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	// PrepareContext creates a prepared statement for use within a transaction.
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
	// QueryContext executes a query that returns rows, typically a SELECT.
	// The args are for any placeholder parameters in the query.
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	// QueryRowContext executes a query that is expected to return at most one row.
	// It always returns a non-nil value.
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

Tx lists all the methods used inside a SQL transaction.

Jump to

Keyboard shortcuts

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