Documentation ¶
Index ¶
- Variables
- func AddSlashes(s string) string
- func ContentType(r *http.Request) string
- func CreateFile(filename string) (*os.File, error)
- func DebugLogger(options ...zap.Option) *zap.Logger
- func DetachContext(ctx context.Context) context.Context
- func IP2Long(ip string) uint32
- func Long2IP(ip uint32) string
- func MapForm(ptr any, form map[string][]string) error
- func MapFormByTag(ptr any, form map[string][]string, tag string) error
- func MapQuery(ptr any, m map[string][]string) error
- func MappingByPtr(ptr any, setter setter, tag string) error
- func MarshalNoEscapeHTML(v any) ([]byte, error)
- func MyTimeEncoder(t time.Time, e zapcore.PrimitiveArrayEncoder)
- func NewDB(cfg *DBConfig) (*sql.DB, error)
- func NewDBX(cfg *DBConfig) (*sqlx.DB, error)
- func NewLogger(cfg *LogConfig) *zap.Logger
- func Nonce(size uint8) string
- func OpenFile(filename string) (*os.File, error)
- func QuoteMeta(s string) string
- func SliceRand[T any](a []T, n int) []T
- func SliceUniq[T ~[]E, E cmp.Ordered](a T) T
- func StrToTime(datetime, layout string) time.Time
- func StripSlashes(s string) string
- func TimeToStr(timestamp int64, layout string) string
- func Transaction(ctx context.Context, db *sqlx.DB, ...) error
- func VersionCompare(rangeVer, curVer string) (bool, error)
- func WeekAround(timestamp int64, layout string) (monday, sunday string)
- type DBConfig
- type LogConfig
- type Mutex
- type Quantity
- type SQLBuilder
- type SQLClause
- type SQLOption
- func CrossJoin(table string) SQLOption
- func Distinct(columns ...string) SQLOption
- func FullJoin(table, on string) SQLOption
- func GroupBy(columns ...string) SQLOption
- func Having(query string, binds ...any) SQLOption
- func Join(table, on string) SQLOption
- func LeftJoin(table, on string) SQLOption
- func Limit(n int) SQLOption
- func Offset(n int) SQLOption
- func OrderBy(columns ...string) SQLOption
- func Returning(column string) SQLOption
- func RightJoin(table, on string) SQLOption
- func Select(columns ...string) SQLOption
- func Table(name string) SQLOption
- func Union(wrappers ...SQLWrapper) SQLOption
- func UnionAll(wrappers ...SQLWrapper) SQLOption
- func Where(query string, binds ...any) SQLOption
- func WhereIn(query string, binds ...any) SQLOption
- type SQLWrapper
- type TXBuilder
- type X
Constants ¶
This section is empty.
Variables ¶
var ( // ErrSQLDataType 不合法的插入或更新数据类型错误 ErrSQLDataType = errors.New("invaild data type, expects: struct, *struct, yiigo.X") // ErrSQLBatchDataType 不合法的批量插入数据类型错误 ErrSQLBatchDataType = errors.New("invaild data type, expects: []struct, []*struct, []yiigo.X") )
var GMT8 = time.FixedZone("CST", 8*3600)
GMT8 东八区时区
Functions ¶
func ContentType ¶ added in v1.6.5
func CreateFile ¶ added in v1.6.4
CreateFile 创建或清空指定的文件 文件已存在,则清空;文件或目录不存在,则以0775权限创建
func DetachContext ¶ added in v1.12.1
DetachContext returns a copy of parent that is not canceled when parent is canceled. The returned context returns no Deadline or Err, and its Done channel is nil. Calling [Cause] on the returned context returns nil. Use `context.WithoutCancel` if go version >= 1.21.0
func MapFormByTag ¶ added in v1.6.5
func MappingByPtr ¶ added in v1.6.5
func MarshalNoEscapeHTML ¶ added in v1.7.5
MarshalNoEscapeHTML 不带HTML转义的JSON序列化
func MyTimeEncoder ¶
func MyTimeEncoder(t time.Time, e zapcore.PrimitiveArrayEncoder)
MyTimeEncoder 自定义时间格式化
func Transaction ¶ added in v1.9.0
func Transaction(ctx context.Context, db *sqlx.DB, fn func(ctx context.Context, tx *sqlx.Tx) error) error
Transaction 执行数据库事物
func VersionCompare ¶
VersionCompare 语义化的版本比较,支持:>, >=, =, !=, <, <=, | (or), & (and). 参数 `rangeVer` 示例:1.0.0, =1.0.0, >2.0.0, >=1.0.0&<2.0.0, <2.0.0|>3.0.0, !=4.0.4
func WeekAround ¶
WeekAround 返回给定时间戳所在周的「周一」和「周日」时间字符串
Types ¶
type DBConfig ¶ added in v1.6.0
type DBConfig struct { // Driver 驱动名称 Driver string // DSN 数据源名称 // [-- MySQL] username:password@tcp(localhost:3306)/dbname?timeout=10s&charset=utf8mb4&collation=utf8mb4_general_ci&parseTime=True&loc=Local // [Postgres] host=localhost port=5432 user=root password=secret dbname=test connect_timeout=10 sslmode=disable // [- SQLite] file::memory:?cache=shared DSN string // MaxOpenConns 设置最大可打开的连接数 MaxOpenConns int // MaxIdleConns 连接池最大闲置连接数 MaxIdleConns int // ConnMaxLifetime 连接的最大生命时长 ConnMaxLifetime time.Duration // ConnMaxIdleTime 连接最大闲置时间 ConnMaxIdleTime time.Duration }
DBConfig 数据库初始化配置
type LogConfig ¶ added in v1.11.2
type LogConfig struct { // Filename 日志名称 Filename string // Level 日志级别 Level zapcore.Level // MaxSize 当前文件多大时轮替;默认:100MB MaxSize int // MaxAge 轮替的旧文件最大保留时长;默认:不限 MaxAge int // MaxBackups 轮替的旧文件最大保留数量;默认:不限 MaxBackups int // Compress 轮替的旧文件是否压缩;默认:不压缩 Compress bool // Stderr 是否输出到控制台 Stderr bool // Options Zap日志选项 Options []zap.Option }
LogConfig 日志初始化配置
type Mutex ¶ added in v1.6.5
type Mutex interface { // Lock 尝试获取锁;interval - 每隔指定时间尝试获取一次锁;timeout - 获取锁的超时时间 Lock(ctx context.Context, interval, timeout time.Duration) error // UnLock 释放锁 UnLock(ctx context.Context) error }
Mutex 分布式锁
type SQLBuilder ¶ added in v1.0.4
type SQLBuilder interface { TXBuilder // Transaction 启用事务 Transaction(ctx context.Context, f func(ctx context.Context, tx TXBuilder) error) error }
SQLBuilder SQL构造器
func NewSQLBuilder ¶ added in v1.0.4
func NewSQLBuilder(db *sqlx.DB, logFn func(ctx context.Context, query string, args ...any)) SQLBuilder
NewSQLBuilder 生成SQL构造器
type SQLClause ¶ added in v1.0.4
type SQLClause struct {
// contains filtered or unexported fields
}
SQLClause SQL语句
type SQLOption ¶ added in v1.12.0
type SQLOption func(w *sqlWrapper)
SQLOption SQL查询选项
func Returning ¶ added in v1.12.1
Returning 指定 `RETURNING` 子句; 用于 PostgresSQL 和 SQLite(3.35.0) `INSERT` 语句
func UnionAll ¶ added in v1.1.2
func UnionAll(wrappers ...SQLWrapper) SQLOption
UnionAll 指定 `UNION ALL` 子句
type SQLWrapper ¶ added in v1.1.2
type SQLWrapper interface { // One 查询一条数据 One(ctx context.Context, data any) error // All 查询多条数据 All(ctx context.Context, data any) error // Insert 插入数据 (数据类型:`struct`, `*struct`, `yiigo.X`) Insert(ctx context.Context, data any) (sql.Result, error) // BatchInsert 批量插入数据 (数据类型:`[]struct`, `[]*struct`, `[]yiigo.X`) BatchInsert(ctx context.Context, data any) (sql.Result, error) // Update 更新数据 (数据类型:`struct`, `*struct`, `yiigo.X`) Update(ctx context.Context, data any) (sql.Result, error) // Delete 删除数据 Delete(ctx context.Context) (sql.Result, error) // Truncate 清空表 Truncate(ctx context.Context) (sql.Result, error) }
SQLWrapper SQL包装器
type TXBuilder ¶ added in v1.12.0
type TXBuilder interface { // Wrap 包装查询选项 Wrap(opts ...SQLOption) SQLWrapper // contains filtered or unexported methods }
TXBuilder 事务构造器