rice

package module
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2022 License: MIT Imports: 20 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 FmtSprintfByte added in v0.0.9

func FmtSprintfByte(b []byte) string

FmtSprintfByte []byte to string

func NewRedis added in v0.0.12

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

func NewRedisDB added in v0.0.12

func NewRedisDB() *redis.Client

func SliceRemoveIndex added in v0.0.10

func SliceRemoveIndex(slice []int, s int) []int

SliceRemoveIndex 移除 slice 中的一个元素

func SliceRemoveIndexUnOrder added in v0.0.10

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

SliceRemoveIndexUnOrder 移除 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 TimeFormat

func TimeFormat(tm time.Time) string

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

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 TimeUnix

func TimeUnix(sec int64) time.Time

TimeUnix 1645270804 to time.Time

func UUIDNewString added in v0.0.10

func UUIDNewString() string

UUIDNewString creates a new random UUID

Types

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(url 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(url 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 ...interface{}) (sql.Result, error)
	Exec(query string, args ...interface{}) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
	QueryRow(query string, args ...interface{}) *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 RedisOption added in v0.0.10

type RedisOption func(*redis.Options)

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

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