gorms

package module
v0.0.0-...-e72caec Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2023 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

gorms - util for gorm

Looking forward to your pull requests!

Alias

gorms.AS("user", "u")    // "user AS u"
gorms.Alias("user", "u") // "user u"

Connection

db, err := gorms.InMemorySqlite()
db, err := gorms.InMemorySqliteWithInitSQL("CREATE TABLE ...")

Like

gorms.PercentagePrefix("Tim") // "%Tim"
gorms.PercentageSuffix("Tim") // "Tim%"
gorms.PercentageAround("Tim") // "%Tim%"

gorms.Like(gorms.column, pattern)         // db.Where("? LIKE ?", column, %pattern%)
gorms.BeforeLike(gorms.column, pattern)   // db.Where("? LIKE ?", column, %pattern)
gorms.AfterLike(gorms.column, pattern)    // db.Where("? LIKE ?", column, pattern%)

Order

gorms.Desc("id") // "id DESC"
gorms.Asc("id")  // "id ASC"
gorms.Orders("id DESC", "create_time ASC") // "id DESC, create_time ASC"

Paginate

gorms.Paginate(3, 10) // db.Offset(20).Limit(10)
gorms.PageParamConvert(2, 10) // offset=10 & limit=10

// pageSizeBoundFn is used for the constraints of pageSize
// check paginate_test.go for usage in detail 
gorms.PaginateWith(pageNum, pageSize, pageSizeBoundFn)
gorms.PageParamConvertWith(pageNum, pageSize, pageSizeBoundFn)

String

gorms.SignedIntegerToString(123) // "123"

Documentation

Index

Constants

View Source
const (
	WildCardPercentage = "%"
	WildCardUnderLine  = "_"
)
View Source
const (
	OrderDesc = "DESC"
	OrderAsc  = "ASC"
)
View Source
const (
	AliasAs = "AS"
)

Variables

This section is empty.

Functions

func AS

func AS(v, alias string) string

AS set alias with "AS"

If reserved word "AS" is not need, use Alias

AS("user", "u") -> "user AS u"

func AfterLike

func AfterLike(column, pattern string) func(*gorm.DB) *gorm.DB

AfterLike generate `column LIKE pattern%`

func Alias

func Alias(v, alias string) string

Alias the value

If reserved word "AS" is need, use AS

Alias("user", "u") -> "user u"

func Asc

func Asc(order string) string

Asc order

Asc("id") -> "id ASC"

func BeforeLike

func BeforeLike(column, pattern string) func(*gorm.DB) *gorm.DB

BeforeLike generate `column LIKE %pattern`

func Desc

func Desc(order string) string

Desc order

Desc("id") -> "id DESC"

func InMemorySqlite

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

InMemorySqlite create in memory sqlite database

InMemorySqlite can be used for mocking database while unit testing

func InMemorySqliteWithInitSQL

func InMemorySqliteWithInitSQL(sql string, values ...interface{}) (*gorm.DB, error)

InMemorySqliteWithInitSQL create in memory sqlite database with init SQL

func Like

func Like(column, pattern string) func(*gorm.DB) *gorm.DB

Like generate `column LIKE %pattern%`

Like(column, pattern) -> db.Where("? LIKE ?", column, %+pattern+%)

The order of the parameters (column & pattern) is consistent with the order of LIKE clause

func Max0

func Max0[T constraints.Integer](value T) T

func Max1

func Max1[T constraints.Integer](value T) T

func MaxInteger

func MaxInteger[T constraints.Integer](lhs, rhs T) T

func Orders

func Orders(orders ...string) string

Orders make a concatenation

Orders("id DESC", "create_time ASC") -> "id DESC, create_time ASC"

func PageParamConvert

func PageParamConvert[T constraints.Integer](pageNum, pageSize T) (offset, limit T)

PageParamConvert convert the num and size of a page to the param offset and limit

PageParamConvert will fix the value of the pageNum and pageSize if the value is less than 1

func PageParamConvertWith

func PageParamConvertWith[T constraints.Integer](pageNum, pageSize T, pageSizeFn func(T) T) (offset, limit T)

PageParamConvertWith convert the num and size of a page to the param offset and limit with the adjust function for page size

func Paginate

func Paginate[T constraints.Integer](pageNum, pageSize T) func(*gorm.DB) *gorm.DB

Paginate convert pageNum and pageSize to offset and limit

Paginate(3, 10) -> db.Offset(20).Limit(10)

If offset or limit is less than 1, then Paginate will fix it

If some constraints are required for pageSize, please use PaginateWith

func PaginateWith

func PaginateWith[T constraints.Integer](pageNum, pageSize T, pageSizeFn func(T) T) func(*gorm.DB) *gorm.DB

PaginateWith convert pageNum and pageSize to offset and limit with the constraint of pageSize

func PercentageAround

func PercentageAround(v string) string

PercentageAround add % before and after the string

PercentageAround("Tim") -> "%Tim%"

PercentageAround use string concatenation rather than fmt.Sprintf("%%%s%%", v), which is confusing and slow (no need to use format)

func PercentagePrefix

func PercentagePrefix(v string) string

PercentagePrefix add "%" before the value

PercentagePrefix("Tim") -> "%Tim"

func PercentageSuffix

func PercentageSuffix(v string) string

PercentageSuffix add "%" after the value

PercentageSuffix("Tim") -> "Tim%"

func SignedIntegerToString

func SignedIntegerToString[T constraints.Signed](i T) string

SignedIntegerToString convert signed integer (int, int8, int16, int32, int64) to string

Refer to https://stackoverflow.com/questions/39442167/convert-int32-to-string-in-golang

Types

This section is empty.

Jump to

Keyboard shortcuts

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