dbutils

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultPageSize ...
	DefaultPageSize = 100
	// MaxPageSize ...
	MaxPageSize = 1000
)

Variables

This section is empty.

Functions

func ApplyDbQueryPagination

func ApplyDbQueryPagination(ginctx *gin.Context, tx *gorm.DB) (*gorm.DB, error)

ApplyDbQueryPagination applies pagination settings on the given Gorm database query object. Pagination settings are taken from the Gin context (HTTP query strings). Returns the modified Gorm query object, or an error if settings parsing failed.

func ApplyDbQueryPaginationOptions

func ApplyDbQueryPaginationOptions(tx *gorm.DB, options PaginationOptions) *gorm.DB

func ClearDatabaseFast

func ClearDatabaseFast(context context.Context, db *gorm.DB, tableNames []string) error

ClearDatabaseFast clears all tables and sequences in the current database, using DELETE. This is usually faster than ClearDatabaseSlow and is concurrency-safe, but does not reclaim disk space and does not reset sequence numbers.

func ClearDatabaseSlow

func ClearDatabaseSlow(context context.Context, db *gorm.DB, tableNames []string) error

ClearDatabaseSlow clears all tables and sequences in the current database, using TRUNCATE. This is usually slower than ClearDatabaseFast and is not concurrency-safe, but it reclaims disk space and resets sequence numbers.

func CreateFindOperationError

func CreateFindOperationError(tx *gorm.DB) error

CreateFindOperationError is to be used by `dbmodel.FindXxxByYyyy()` functions to ensure that, when a record is not found, a `gorm.ErrRecordNotFound` error is returned.

Implementors are supposed to use this in combination with GORM's `tx.Take()`, like so:

var result MyModel
tx := db.Where(...).Take(&result)
return result, dbutils.CreateFindOperationError(tx)

func EstablishDatabaseConnection

func EstablishDatabaseConnection(dbtype string, connString string, config *gorm.Config) (*gorm.DB, error)

EstablishDatabaseConnection establishes a gorm database connection.

func IsUniqueConstraintError

func IsUniqueConstraintError(err error, constraintName string) bool

IsUniqueConstraintError checks whether the given gorm error represents a unique key constraint error, on the given constraint name.

func QueryStringList

func QueryStringList(db *gorm.DB, sql string, values ...interface{}) ([]string, error)

QueryStringList performs a query that only selects a single column of type string. It returns all rows (as an array of strings), or an error.

QueryStringList(db, "SELECT name FROM users WHERE age > ?", 12)

func ResetDatabase

func ResetDatabase(context context.Context, db *gorm.DB) error

ResetDatabase drops all tables and user-defined types in the current database.

func SetupTestDatabase

func SetupTestDatabase() (*gorm.DB, error)

SetupTestDatabase, when called for the first time, connects to the test database and clears it with `ClearDatabaseSlow`. On subsequent calls, it returns the initial database connection instead of making a new one, and clears the database with `ClearDatabaseFast`.

Types

type PaginationOptions

type PaginationOptions struct {
	Page    uint
	PerPage uint
}

func ParsePaginationOptions

func ParsePaginationOptions(ginctx *gin.Context) (PaginationOptions, error)

type TestDatabaseContext

type TestDatabaseContext struct {
	Db          *gorm.DB
	TableNames  []string
	ClearedOnce uint32
	SetupError  error
}

type UnsupportedDatabaseTypeError

type UnsupportedDatabaseTypeError struct {
	Type string
}

UnsupportedDatabaseTypeError means that the given database type is not supported.

func (*UnsupportedDatabaseTypeError) Error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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