sqlitebuilder

package
v1.15.4 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	RegCount      = regexp.MustCompile("(?is)^(SELECT).*?(FROM)")
	RegLimit      = regexp.MustCompile(`(?is)LIMIT\s+(\d+|\?)(?:\s*,\s*(\d+|\?))*\s*$`)
	RegOrderLimit = regexp.MustCompile(`(?is)(ORDER BY \S+(\s+(ASC|DESC))?\s+)?LIMIT\s+(\d+|\?)(?:\s*,\s*(\d+|\?))*\s*$`)
)

Functions

func BuildDBDriver

func BuildDBDriver(opts *Options) string

BuildDBDriver return a driver like "user:pass@tcp(host:port)/dbName?charset=utf8&loc=Asia%%2FShanghai"

Types

type Count

type Count struct {
	Count int64 `db:"count"`
}

type DBConnect

type DBConnect struct {
	*sql.DB
	Options   *Options
	ContextTx contextKey
	// contains filtered or unexported fields
}

func New

func New(opts ...Option) (*DBConnect, error)

New return a new mysql client, and try ping.

func (*DBConnect) Exec

func (db *DBConnect) Exec(ctx context.Context, query string, args ...any) (lastInsertId, rowsAffected int64, err error)

Exec exec query by prepare sql, eg: INSERT, UPDATE, DELETE

func (*DBConnect) Query

func (db *DBConnect) Query(ctx context.Context, query string, args ...any) (results []map[string]any, count int64, err error)

Query query by prepare sql, eg: SELECT

func (*DBConnect) Select

func (db *DBConnect) Select(ctx context.Context, dest any, query string, args ...any) (count int64, err error)

Select 查询数据

func (*DBConnect) SelectAll

func (db *DBConnect) SelectAll(ctx context.Context, dest any, query string, args ...any) (count int64, err error)

SelectAll 返回所有数据,如果最后有 limit 会删除

func (*DBConnect) SelectMany

func (db *DBConnect) SelectMany(ctx context.Context, dest any, query string, args ...any) (count int64, err error)

SelectMany 查询总数并返回指定数据

返回的字段名,不能有预处理的参数

func (*DBConnect) SelectOne

func (db *DBConnect) SelectOne(ctx context.Context, dest any, query string, args ...any) (err error)

SelectOne 查询单条数据,不是单条数据会报错

type MultiSelect

type MultiSelect func(ctx context.Context, dest any, query string, args ...any) (count int64, err error)

MultiSelect Select, SelectMany, SelectAll 函数签名

type Option

type Option func(opts *Options)

func WithCharset

func WithCharset(charset string) Option

func WithDBFilePath

func WithDBFilePath(DBFilePath string) Option

func WithDBName

func WithDBName(dbName string) Option

func WithHost

func WithHost(host string) Option

func WithLoc

func WithLoc(loc string) Option

func WithPass

func WithPass(pass string) Option

func WithPoolSize

func WithPoolSize(size int) Option

func WithPort

func WithPort(port int) Option

func WithUser

func WithUser(user string) Option

type Options

type Options struct {
	User     string
	Pass     string
	Host     string
	Port     int
	PoolSize int

	DBName  string
	Charset string
	Loc     string

	DBFilePath string
}

Jump to

Keyboard shortcuts

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