storage

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMalformedConnectionString = errors.New(
		"malformed database connection parameters provided",
	)
	ErrUnableToConnect   = errors.New("unable to open connection to database")
	ErrUnsupportedDriver = fmt.Errorf(
		"unsupported driver, supported drivers are %s",
		[]string{
			postgresqlDriverName,
			mysqlDriverName,
			mssqlDriverName,
			sqliteDriverName,
		},
	)
)

Functions

This section is empty.

Types

type DB added in v0.5.0

type DB interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
	Tx(fn TxFunc) error
	Close() error
	TableExists(tableName string) (bool, error)
}

func NewDB added in v0.5.0

func NewDB(cfg configloader.ConnectionConfig) (DB, error)

NewDB establishes a connection to the database using the connection configuration.

The following errors may be returned:

  • ErrMalformedConnectionString: The provided connection parameters are not in a valid format.
  • ErrUnableToConnect: Unable to make a connection to the database with the provided connection parameters.
  • ErrUnsupportedDriver: The provided driver is not supported.

type DBAdapter added in v0.5.0

type DBAdapter interface {
	// ConvertGenericPlaceholders replaces any generic `?` placeholders
	// in `query` for the database driver specific placeholders and returns
	// the updated query.
	ConvertGenericPlaceholders(query string, argsCount int) string
	// TableExists checks if the tableName exists within the
	// database currently connected to.
	TableExists(executor sqlExecutor, tableName string) (bool, error)
	// DatabaseName retrieves the currently selected database name.
	DatabaseName(executor sqlExecutor) (string, error)
	// CreateDSN creates a DSN to be used with sql.Open in the database
	// driver specific format.
	CreateDSN(cfg configloader.ConnectionConfig) string
}

type MSSQLAdapter added in v0.6.0

type MSSQLAdapter struct{}

func (MSSQLAdapter) ConvertGenericPlaceholders added in v0.6.0

func (m MSSQLAdapter) ConvertGenericPlaceholders(
	query string,
	argsCount int,
) string

func (MSSQLAdapter) CreateDSN added in v0.6.0

func (MSSQLAdapter) DatabaseName added in v0.6.0

func (m MSSQLAdapter) DatabaseName(executor sqlExecutor) (string, error)

func (MSSQLAdapter) TableExists added in v0.6.0

func (m MSSQLAdapter) TableExists(
	executor sqlExecutor,
	tableName string,
) (bool, error)

type MySQLAdapter added in v0.5.0

type MySQLAdapter struct{}

func (MySQLAdapter) ConvertGenericPlaceholders added in v0.5.0

func (m MySQLAdapter) ConvertGenericPlaceholders(query string, argsCount int) string

func (MySQLAdapter) CreateDSN added in v0.5.0

func (MySQLAdapter) DatabaseName added in v0.5.0

func (m MySQLAdapter) DatabaseName(executor sqlExecutor) (string, error)

func (MySQLAdapter) TableExists added in v0.5.0

func (m MySQLAdapter) TableExists(
	executor sqlExecutor,
	tableName string,
) (bool, error)

type PostgresqlAdapter added in v0.5.0

type PostgresqlAdapter struct{}

func (PostgresqlAdapter) ConvertGenericPlaceholders added in v0.5.0

func (p PostgresqlAdapter) ConvertGenericPlaceholders(
	query string,
	argsCount int,
) string

func (PostgresqlAdapter) CreateDSN added in v0.5.0

func (PostgresqlAdapter) DatabaseName added in v0.5.0

func (p PostgresqlAdapter) DatabaseName(executor sqlExecutor) (string, error)

func (PostgresqlAdapter) TableExists added in v0.5.0

func (p PostgresqlAdapter) TableExists(
	executor sqlExecutor,
	tableName string,
) (bool, error)

type SqlDB added in v0.10.0

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

func (SqlDB) Close added in v0.10.0

func (db SqlDB) Close() error

Close closes the database connection. Any further queries will result in errors, and you should call NewDB again after if you'd like to run more.

func (SqlDB) Exec added in v0.10.0

func (db SqlDB) Exec(query string, args ...any) (sql.Result, error)

Exec is a wrapper around the sql.DB Exec.

func (SqlDB) Query added in v0.10.0

func (db SqlDB) Query(query string, args ...any) (*sql.Rows, error)

Query is a wrapper around the sql.DB Query.

func (SqlDB) QueryRow added in v0.10.0

func (db SqlDB) QueryRow(query string, args ...any) *sql.Row

QueryRow is a wrapper around the sql.DB QueryRow.

func (SqlDB) TableExists added in v0.10.0

func (db SqlDB) TableExists(tableName string) (bool, error)

TableExists checks if the tableName exists within the database currently connected to.

func (SqlDB) Tx added in v0.10.0

func (db SqlDB) Tx(fn TxFunc) error

Tx executes fn within a transaction block. If fn returns an error, the transaction will be rolled back. Otherwise, it will be committed.

type SqliteAdapter added in v0.6.0

type SqliteAdapter struct{}

func (SqliteAdapter) ConvertGenericPlaceholders added in v0.6.0

func (s SqliteAdapter) ConvertGenericPlaceholders(
	query string,
	argsCount int,
) string

func (SqliteAdapter) CreateDSN added in v0.6.0

func (SqliteAdapter) DatabaseName added in v0.6.0

func (s SqliteAdapter) DatabaseName(executor sqlExecutor) (string, error)

func (SqliteAdapter) TableExists added in v0.6.0

func (s SqliteAdapter) TableExists(
	executor sqlExecutor,
	tableName string,
) (bool, error)

type TxFunc added in v0.10.0

type TxFunc func(db DB) error

Jump to

Keyboard shortcuts

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