rice

package module
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2022 License: MIT Imports: 28 Imported by: 0

README

rice

import "github.com/woxingliu/rice"
go mod tidy

Usage

Database

Postgresql 初始化

pg, err := rice.NewPostgres("postgres://postgres:123456@localhost:5432/test?sslmode=disable")
if err != nil {
	return err
}
defer pg.Close()

MariaDB 初始化

mariadb, err := rice.NewMaria("root:root@tcp(localhost:3306)/test?parseTime=True&loc=Local&charset=utf8mb4")
if err != nil {
	return err
}
defer mariadb.Close()

Redis 初始化

rdb, err := rice.NewRedis("localhost:6379")
if err != nil {
	return err
}
defer rdb.Close()

Pretty 封装了 database/sql 标准库里 sql.DBsql.Tx 共有的一些方法,使用 Pretty 时可以在 repo 的上一层初始化,使 repo 层既可以执行 sql.DB 的方法,也可以执行 sql.Tx 的方法,下面是示例。

package repo

type Repo struct {
	Pretty
}

func NewRepo(db Pretty) Repo {
	return Repo{Pretty: db}
}

func (r Repo) Create() error { return nil }

func (r Repo) Update() error { return nil }

func (r Repo) Find() error { return nil }
package usecase

func UseCaseDB() {

	db := NewMariaDB()

	dbRepo := NewRepo(db)

	dbRepo.Find()
}

func UseCaseTx() error {

	tx, _ := NewMariaTx()
	defer tx.Rollback()

	txRepo := NewRepo(tx)

	err := txRepo.Create()
	if err != nil {
		return err
	}

	err = txRepo.Update()
	if err != nil {
		return err
	}

	if err := tx.Commit(); err != nil {
		return err
	}

	return nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AESDecrypt added in v0.0.9

func AESDecrypt(key []byte, s string) (string, error)

func AESEncrypt added in v0.0.9

func AESEncrypt(key []byte, s string) (string, error)

func AESNewCBCDecrypter added in v0.0.9

func AESNewCBCDecrypter(keyString, cipherString string) (string, error)

func AESNewCBCEncrypter added in v0.0.9

func AESNewCBCEncrypter(keyString, plainString string) (string, error)

func AESNewCFBDecrypter added in v0.0.9

func AESNewCFBDecrypter()

func AESNewCFBEncrypter added in v0.0.9

func AESNewCFBEncrypter()

func AESNewGCMDecrypt added in v0.0.9

func AESNewGCMDecrypt(keyString, nonceString, ciphertext string) (string, error)

func AESNewGCMEncrypt added in v0.0.9

func AESNewGCMEncrypt(keyString, plaintext string) (string, error)

func BCryptCompareHashAndPassword added in v0.0.9

func BCryptCompareHashAndPassword(pwd, hash string) bool

BCryptCompareHashAndPassword true or false

func BCryptGenerateFromPassword added in v0.0.9

func BCryptGenerateFromPassword(pwd string) (string, error)

BCryptGenerateFromPassword generate hash from password

func ByteString

func ByteString(b []byte) string

ByteString []byte to string

func BytesNewBufferString added in v0.0.9

func BytesNewBufferString(b []byte) string

BytesNewBufferString []byte to string

func CronRun added in v0.0.14

func CronRun(ctx context.Context, corn string, task ...func()) error

func FmtSprintfByte added in v0.0.9

func FmtSprintfByte(b []byte) string

FmtSprintfByte []byte to string

func HttpGet added in v0.0.14

func HttpGet(url string) ([]byte, error)

func NewLumberjackLogger added in v0.0.13

func NewLumberjackLogger(fileName string, opts ...LumberjackOption) *lumberjack.Logger

func NextId added in v0.0.13

func NextId() string

func RandInt added in v0.0.13

func RandInt(max int64) (int64, error)

RandInt 生成一个真随机数

func SliceDifference added in v0.0.13

func SliceDifference[T comparable](a, b []T) []T

SliceDifference 取 a 中有,而 b 中没有的

func SliceIntersection added in v0.0.13

func SliceIntersection[T comparable](s1, s2 []T) (inter []T)

SliceIntersection 两个 slice 的交集

func SliceRemoveIndex added in v0.0.10

func SliceRemoveIndex[T any](s []T, index int) []T

SliceRemoveIndex 移除 slice 中的一个元素

func SliceRemoveIndexUnOrder added in v0.0.10

func SliceRemoveIndexUnOrder[T any](s []T, i int) []T

SliceRemoveIndexUnOrder 移除 slice 中的一个元素(无序,但效率高)

func SliceSet added in v0.0.13

func SliceSet[T comparable](s1 []T) []T

SliceSet slice 去重

func StrconvFormatInt

func StrconvFormatInt(i int64) string

StrconvFormatInt 1645270804 to "1645270804"

func StrconvParseFloat added in v0.0.8

func StrconvParseFloat(i int) (float64, error)

StrconvParseFloat int/100 to float64 保留 2 位小数点 example: 2333 -> 23.33

func StrconvParseInt

func StrconvParseInt(s string) (int64, error)

StrconvParseInt "1645270804" to 1645270804

func StringByte

func StringByte(s string) []byte

StringByte string to []byte

func StringByteUnsafe added in v0.0.13

func StringByteUnsafe(s string) []byte

StringByteUnsafe string to []byte

func TickerRun added in v0.0.14

func TickerRun(ctx context.Context, d time.Duration, task ...Task) error

func TimeExistIntersection added in v0.0.13

func TimeExistIntersection(startTime, endTime time.Time, anotherStartTime, anotherEndTime time.Time) bool

TimeExistIntersection 两个时间段是否有交集 false 没有交集,true 有交集

func TimeFormat

func TimeFormat(tm time.Time) string

TimeFormat time.Time to "2006-01-02 15:04:05"

func TimeFormatDate added in v0.0.13

func TimeFormatDate(tm time.Time) string

TimeFormatDate time to date

func TimeNowFormat

func TimeNowFormat() string

TimeNowFormat time.Time to "2006-01-02 15:04:05"

func TimeParse

func TimeParse(s string) (time.Time, error)

TimeParse "2006-01-02 15:04:05" to time.Time

func TimeParseDate added in v0.0.13

func TimeParseDate(date string) (time.Time, error)

TimeParseDate date to time

func TimeUnix

func TimeUnix(sec int64) time.Time

TimeUnix 1645270804 to time.Time

func TimerRun added in v0.0.14

func TimerRun(ctx context.Context, tm time.Time, task ...Task) error

func TimestampExistIntersection added in v0.0.13

func TimestampExistIntersection(startTime, endTime int64, anotherStartTime, anotherEndTime int64) bool

IsHaveIntersectionTimestamp 两个时间段是否有交集 false 没有交集,true 有交集

func UUIDNewString added in v0.0.10

func UUIDNewString() string

UUIDNewString creates a new random UUID

func XidNewString added in v0.0.13

func XidNewString() string

Types

type Data added in v0.0.13

type Data interface {
	Marshal() ([]byte, error)
	Unmarshal(data []byte) error
}

type LumberjackOption added in v0.0.13

type LumberjackOption func(*lumberjack.Logger)

func Compress added in v0.0.13

func Compress(b bool) LumberjackOption

func LocalTime added in v0.0.13

func LocalTime(b bool) LumberjackOption

func MaxAge added in v0.0.13

func MaxAge(i int) LumberjackOption

func MaxBackups added in v0.0.13

func MaxBackups(i int) LumberjackOption

func MaxSize added in v0.0.13

func MaxSize(i int) LumberjackOption

type MariaDB added in v0.0.8

type MariaDB struct {
	*sql.DB
	// contains filtered or unexported fields
}

func NewMaria added in v0.0.12

func NewMaria(dsn string, opts ...Option) (*MariaDB, error)

func (*MariaDB) Close added in v0.0.8

func (db *MariaDB) Close()

type Option added in v0.0.12

type Option func(*sql.DB)

func ConnMaxIdleTime added in v0.0.12

func ConnMaxIdleTime(d time.Duration) Option

ConnMaxIdleTime 一个连接空闲的最大时长

func ConnMaxLifetime added in v0.0.12

func ConnMaxLifetime(d time.Duration) Option

ConnMaxLifetime 一个连接使用的最大时长

func MaxIdleConns added in v0.0.12

func MaxIdleConns(size int) Option

MaxIdleConns 最大闲置的连接数

func MaxOpenConns added in v0.0.12

func MaxOpenConns(size int) Option

MaxOpenConns 最大打开的连接数

type Postgres added in v0.0.8

type Postgres struct {
	*sql.DB
}

func NewPostgres added in v0.0.12

func NewPostgres(dsn string, opts ...Option) (*Postgres, error)

func (*Postgres) Close added in v0.0.8

func (db *Postgres) Close()

type Pretty added in v0.0.12

type Pretty interface {
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
	Prepare(query string) (*sql.Stmt, error)
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	Exec(query string, args ...any) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	Query(query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
	QueryRow(query string, args ...any) *sql.Row
}

type PrettyDB added in v0.0.12

type PrettyDB interface {
	Pretty
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
	Begin() (*sql.Tx, error)
}

func NewMariaDB added in v0.0.8

func NewMariaDB() PrettyDB

func NewPostgresDB added in v0.0.8

func NewPostgresDB() PrettyDB

type PrettyTx added in v0.0.12

type PrettyTx interface {
	Pretty
	Commit() error
	Rollback() error
	StmtContext(ctx context.Context, stmt *sql.Stmt) *sql.Stmt
	Stmt(stmt *sql.Stmt) *sql.Stmt
}

func NewMariaTx added in v0.0.12

func NewMariaTx() (PrettyTx, error)

func NewMariaTxContext added in v0.0.12

func NewMariaTxContext(ctx context.Context) (PrettyTx, error)

func NewPostgresTx added in v0.0.12

func NewPostgresTx() (PrettyTx, error)

func NewPostgresTxContext added in v0.0.12

func NewPostgresTxContext(ctx context.Context) (PrettyTx, error)

type Redis added in v0.0.8

type Redis struct{ *redis.Client }

func NewRedis added in v0.0.12

func NewRedis(addr string, opts ...RedisOption) (*Redis, error)

func NewRedisDB added in v0.0.12

func NewRedisDB() *Redis

func (*Redis) GetStruct added in v0.0.13

func (rdb *Redis) GetStruct(key string, data Data) error

func (*Redis) HGetStruct added in v0.0.13

func (rdb *Redis) HGetStruct(key string, field string, data Data) error

func (*Redis) HSetStruct added in v0.0.13

func (rdb *Redis) HSetStruct(key string, field string, data Data) error

func (*Redis) SetStruct added in v0.0.13

func (rdb *Redis) SetStruct(key string, data Data) error

type RedisOption added in v0.0.10

type RedisOption func(*redis.Options)

func RedisAddr added in v0.0.13

func RedisAddr(addr string) RedisOption

func RedisDB added in v0.0.10

func RedisDB(db int) RedisOption

func RedisPassword added in v0.0.10

func RedisPassword(pwd string) RedisOption

func RedisPoolSize added in v0.0.10

func RedisPoolSize(poolSize int) RedisOption

type Task added in v0.0.14

type Task func() error

Directories

Path Synopsis
Package httpserver implements HTTP server.
Package httpserver implements HTTP server.
rabbitmq

Jump to

Keyboard shortcuts

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