dbhelper

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2024 License: MPL-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	SQL_ARG_PATTERN = regexp.MustCompile(`:[1|2|3|4|5|6|7|8|9](0|1|2|3|4|5|6|7|8|9)?`)
)

Functions

func BeginTx added in v0.0.4

func BeginTx(ctx context.Context, serializable bool) context.Context

func CloseTx added in v0.0.5

func CloseTx(ctx context.Context)

func CoalseceBool added in v0.0.8

func CoalseceBool(v sql.NullBool, dv bool) bool

func CoalseceByte added in v0.0.11

func CoalseceByte(v sql.NullByte, dv byte) byte

func CoalseceFloat64 added in v0.0.8

func CoalseceFloat64(v sql.NullFloat64, dv float64) float64

func CoalseceInt16 added in v0.0.8

func CoalseceInt16(v sql.NullInt16, dv int16) int16

func CoalseceInt32 added in v0.0.8

func CoalseceInt32(v sql.NullInt32, dv int32) int32

func CoalseceInt64 added in v0.0.8

func CoalseceInt64(v sql.NullInt64, dv int64) int64

func CoalseceString added in v0.0.8

func CoalseceString(v sql.NullString, dv string) string

func CoalseceTime added in v0.0.8

func CoalseceTime(v sql.NullTime, dv time.Time) time.Time

func CommitTx added in v0.0.5

func CommitTx(ctx context.Context)

func EscapeLikeString added in v0.0.13

func EscapeLikeString(s utils.String) utils.String

EscapeLikeString 对LIKE参数进行转义。

func Exec

func Exec[T int | int8 | int16 | int32 | int64](ctx context.Context, query string, args ...any) (T, error)

Exec 执行指定的sql并返回受影响的行数。

func ExecLastInsertId

func ExecLastInsertId[T int | int8 | int16 | int32 | int64](ctx context.Context, query string, args ...any) (T, error)

ExecLastInsertId 执行指定的sql并返回插入的ID值。只要sql执行成功,即使未插入任何记录也不会返回错误。

func InitMySqlDb

func InitMySqlDb(addr, username, password, dbname string) error

InitMySqlDb 初始化MySql数据源。

func InsertBatch

func InsertBatch(ctx context.Context, query string, rows ...[]any) (int64, error)

insertBatch 批量插入记录。 返回成功插入的记录数。

func JoinInInt

func JoinInInt[T int | int8 | int16 | int32 | int64](args []T) string

func JoinInString

func JoinInString(args []string) string

func ParseNullInt16 added in v0.0.11

func ParseNullInt16(s string) sql.NullInt16

func ParseNullInt32 added in v0.0.11

func ParseNullInt32(s string) sql.NullInt32

func ParseNullInt64 added in v0.0.11

func ParseNullInt64(s string) sql.NullInt64

func Query

func Query[T bool | int | int8 | int16 | int32 | int64 | string | time.Time | sql.NullInt64 | sql.NullBool | sql.NullTime](ctx context.Context, query string, args ...any) (T, error)

func QueryKeyValueMap

func QueryKeyValueMap[T bool | int | int8 | int16 | int32 | int64 | string](ctx context.Context, query string, args ...any) (map[string]T, error)

func QueryList

func QueryList[T bool | int | int8 | int16 | int32 | int64 | string | time.Time | sql.NullInt64 | sql.NullBool | sql.NullTime](ctx context.Context, query string, args ...any) ([]T, error)

func QueryObj

func QueryObj[T any](ctx context.Context, query string, rh RowHandler[T], args ...any) (*T, error)

func QueryObjList

func QueryObjList[T any](ctx context.Context, query string, rh RowHandler[T], args ...any) ([]*T, error)

QueryObjList 查询对象列表。

func Version

func Version() string

Version 获取包的版本。

Types

type DbRow

type DbRow interface {
	Scan(dest ...any) error
}

type DynamicSqlBuilder added in v0.0.7

type DynamicSqlBuilder struct {
	// contains filtered or unexported fields
}

func (*DynamicSqlBuilder) Append added in v0.0.7

func (d *DynamicSqlBuilder) Append(sql string) *DynamicSqlBuilder

func (*DynamicSqlBuilder) AppendIf added in v0.0.7

func (d *DynamicSqlBuilder) AppendIf(sql string, p bool) *DynamicSqlBuilder

func (*DynamicSqlBuilder) End added in v0.0.7

func (d *DynamicSqlBuilder) End() *SqlBuilder

type InsertSqlBuilder added in v0.0.17

type InsertSqlBuilder struct {
	// contains filtered or unexported fields
}

func (*InsertSqlBuilder) Append added in v0.0.17

func (d *InsertSqlBuilder) Append(col string) *InsertSqlBuilder

func (*InsertSqlBuilder) AppendIf added in v0.0.17

func (d *InsertSqlBuilder) AppendIf(col string, p bool) *InsertSqlBuilder

func (*InsertSqlBuilder) End added in v0.0.17

func (d *InsertSqlBuilder) End() *SqlBuilder

type KeyValuePairMapper

type KeyValuePairMapper[T bool | int | int8 | int16 | int32 | int64 | string] struct{}

func (*KeyValuePairMapper[T]) Scan

func (m *KeyValuePairMapper[T]) Scan(r DbRow) (*KeyValuePairPo[T], error)

type KeyValuePairPo

type KeyValuePairPo[T bool | int | int8 | int16 | int32 | int64 | string] struct {
	Key   string
	Value T
}

type RowHandler

type RowHandler[T any] interface {
	Scan(sc DbRow) (*T, error)
}

type SqlBuilder added in v0.0.7

type SqlBuilder struct {
	// contains filtered or unexported fields
}

func NewSqlBuilder added in v0.0.7

func NewSqlBuilder(sql string) *SqlBuilder

func (*SqlBuilder) Append added in v0.0.7

func (b *SqlBuilder) Append(sql string) *SqlBuilder

func (*SqlBuilder) AppendIf added in v0.0.7

func (b *SqlBuilder) AppendIf(sql string, p bool) *SqlBuilder

func (*SqlBuilder) Dynamic added in v0.0.7

func (b *SqlBuilder) Dynamic(prefix, suffix, joint string) *DynamicSqlBuilder

func (*SqlBuilder) Inserter added in v0.0.17

func (b *SqlBuilder) Inserter(quote string) *InsertSqlBuilder

func (*SqlBuilder) Limit added in v0.0.7

func (b *SqlBuilder) Limit(startRowIndex, maximumRows int) *SqlBuilder

Limit 创建分页参数SQL。 startRowIndex 起始行索引,从0开始。 maximumRows 最大行数,如果小于等于0,则不添加LIMIT子句。

func (*SqlBuilder) OrderBy added in v0.0.7

func (b *SqlBuilder) OrderBy(sql ...string) *SqlBuilder

func (*SqlBuilder) Set added in v0.0.17

func (b *SqlBuilder) Set() *DynamicSqlBuilder

func (*SqlBuilder) String added in v0.0.7

func (b *SqlBuilder) String() string

func (*SqlBuilder) Where added in v0.0.7

func (b *SqlBuilder) Where() *DynamicSqlBuilder

Jump to

Keyboard shortcuts

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