sql

package
v0.0.0-...-4125756 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package sql provides function for managing connection to various database. You can open a database connection by calling Open function.

connection := "postgresql://user:password@host/db[?options]"
db, _ := sql.Open(connection)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(db *sqlx.DB, service string, fs fs.FS) error

Migrate looks at the currently active migration version of the service and will migrate all the way up (applying all up migrations). Migrate will look at the folder `db` by default (generally assets/db).

func MigrateToVersion

func MigrateToVersion(db *sqlx.DB, service string, fs fs.FS, version uint) error

MigrateToVersion should be use to apply down or up script to a given version

func MigrateWithPath

func MigrateWithPath(db *sqlx.DB, fs fs.FS, service string, path string) error

MigrateWithPath looks at the currently active migration version of the service and will migrate all the way up (applying all up migrations) from the given fs path.

func Open

func Open(connection string, ops ...Option) (*sqlx.DB, error)

Open knows how to open a database connection based on connection string.

connection string should follow this format postgresql://user:password@host/db[?options]

func StatusCheck

func StatusCheck(ctx context.Context, db *sqlx.DB) error

StatusCheck returns nil if it can successfully talk to the database. It returns a non-nil error otherwise.

Types

type Option

type Option func(*options)

Option defines a SQL option.

func WithMaxConnIdleTime

func WithMaxConnIdleTime(d time.Duration) Option

WithMaxConnIdleTime defines the maximum amount of time a connection may be idle.

func WithMaxConnLifeTime

func WithMaxConnLifeTime(d time.Duration) Option

WithMaxConnLifeTime defines the maximum amount of time a connection may be reused.

func WithMaxIdleConns

func WithMaxIdleConns(n int) Option

WithMaxIdleConns defines the maximum number of connections in the idle connection pool.

func WithMaxOpenConns

func WithMaxOpenConns(n int) Option

WithMaxOpenConns defines the maximum number of open connections to the database.

func WithStatementCacheMode

func WithStatementCacheMode(mode string) Option

WithStatementCacheMode defines the maximum amount of time a connection may be idle.

type Queryable

type Queryable interface {
	sqlx.Ext
	sqlx.ExecerContext
	sqlx.PreparerContext
	sqlx.QueryerContext
	sqlx.Preparer

	GetContext(context.Context, interface{}, string, ...interface{}) error
	SelectContext(context.Context, interface{}, string, ...interface{}) error
	Get(interface{}, string, ...interface{}) error
	MustExecContext(context.Context, string, ...interface{}) sql.Result
	PreparexContext(context.Context, string) (*sqlx.Stmt, error)
	QueryRowContext(context.Context, string, ...interface{}) *sql.Row
	Select(interface{}, string, ...interface{}) error
	QueryRow(string, ...interface{}) *sql.Row
	PrepareNamedContext(context.Context, string) (*sqlx.NamedStmt, error)
	PrepareNamed(string) (*sqlx.NamedStmt, error)
	Preparex(string) (*sqlx.Stmt, error)
	NamedExec(string, interface{}) (sql.Result, error)
	NamedExecContext(context.Context, string, interface{}) (sql.Result, error)
	MustExec(string, ...interface{}) sql.Result
	NamedQuery(string, interface{}) (*sqlx.Rows, error)
}

Queryable includes all methods shared by sqlx.DB and sqlx.Tx, allowing either type to be used interchangeably.

type TestingDB

type TestingDB struct {
	*sqlx.DB
	DSN string
}

TestingDB abstracts a database that can be used in tests. defaults to connecting to localhost:5432 with username "postgres" and password "postgres".

The environment variable TESTINGDB_URL can be used to control the connection settings.

Example usage:

	func TestMe(t *testing.T) {
		var db sql.TestingDB
		err := db.Open()
		if !assert.NoError(t, err) {
			return
		}
		defer db.Close()

     // connecting to the test database via generated dsn for this test purpose.
     database.Open(db.DSN)
	}

func (*TestingDB) Close

func (tdb *TestingDB) Close() error

Close cleanup and close the testing database.

func (*TestingDB) Open

func (tdb *TestingDB) Open() error

Open creates and initializes the testing database.

Jump to

Keyboard shortcuts

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