Documentation ¶
Index ¶
- Constants
- func ApplyDbQueryPagination(ginctx *gin.Context, tx *gorm.DB) (*gorm.DB, error)
- func ApplyDbQueryPaginationOptions(tx *gorm.DB, options PaginationOptions) *gorm.DB
- func ClearDatabaseFast(context context.Context, db *gorm.DB, tableNames []string) error
- func ClearDatabaseSlow(context context.Context, db *gorm.DB, tableNames []string) error
- func CreateFindOperationError(tx *gorm.DB) error
- func EstablishDatabaseConnection(dbtype string, connString string, config *gorm.Config) (*gorm.DB, error)
- func IsUniqueConstraintError(err error, constraintName string) bool
- func QueryStringList(db *gorm.DB, sql string, values ...interface{}) ([]string, error)
- func ResetDatabase(context context.Context, db *gorm.DB) error
- func SetupTestDatabase() (*gorm.DB, error)
- type PaginationOptions
- type TestDatabaseContext
- type UnsupportedDatabaseTypeError
Constants ¶
const ( // DefaultPageSize ... DefaultPageSize = 100 // MaxPageSize ... MaxPageSize = 1000 )
Variables ¶
This section is empty.
Functions ¶
func ApplyDbQueryPagination ¶
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 ¶
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 ¶
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 ¶
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 ¶
IsUniqueConstraintError checks whether the given gorm error represents a unique key constraint error, on the given constraint name.
func QueryStringList ¶
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 ¶
ResetDatabase drops all tables and user-defined types in the current database.
func SetupTestDatabase ¶
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 ¶
func ParsePaginationOptions ¶
func ParsePaginationOptions(ginctx *gin.Context) (PaginationOptions, error)
type TestDatabaseContext ¶
type UnsupportedDatabaseTypeError ¶
type UnsupportedDatabaseTypeError struct {
Type string
}
UnsupportedDatabaseTypeError means that the given database type is not supported.
func (*UnsupportedDatabaseTypeError) Error ¶
func (e *UnsupportedDatabaseTypeError) Error() string