core

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package core 定义了 sqlx 的领域层接口与核心类型。 它提供了纯抽象接口、Row/Rows 数据结构、绑定变量工具 以及不依赖具体数据库实现的扫描与查询函数。

Index

Constants

View Source
const (
	UNKNOWN = iota
	QUESTION
	DOLLAR
	NAMED
	AT
)

Variables

View Source
var NameMapper = strings.ToLower

NameMapper 用于将列名映射到结构体字段名。默认情况下, 它使用 strings.ToLower 将结构体字段名转为小写。

Functions

func BindDriver

func BindDriver(driverName string, bindType int)

BindDriver 将 driverName 的 BindType 设置为 bindType。

func BindType

func BindType(driverName string) int

BindType 根据驱动程序名称返回数据库的绑定类型。

func FieldsByTraversal

func FieldsByTraversal(v reflect.Value, traversals [][]int, values []any, ptrs bool) error

FieldsByTraversal 根据 traversals 中的路径,用传递的 value 的字段填充 values 接口切片。

func Get

func Get(q Queryer, dest any, query string, args ...any) error

Get 使用提供的 Queryer 执行 QueryRow,并将结果行扫描到 dest 中。

func In

func In(query string, args ...any) (string, []any, error)

In 展开 args 中的切片值,返回修改后的查询字符串和新的参数列表。

func IsScannable

func IsScannable(t reflect.Type) bool

IsScannable 根据 reflect.Type 来判断目标是否可扫描。

func MapScan

func MapScan(r ColScanner, dest map[string]any) error

MapScan 将单行扫描到 dest map[string]any 中。

func Mapper

func Mapper() *reflectx.Mapper

Mapper 返回使用已配置 NameMapper 函数的有效 Mapper。

func MustExec

func MustExec(e Execer, query string, args ...any) sql.Result

MustExec 使用 e 执行查询,如果发生错误则 panic。

func Rebind

func Rebind(bindType int, query string) string

Rebind 将查询从默认绑定类型(QUESTION)重绑定到目标绑定类型。

func RebindBuff

func RebindBuff(bindType int, query string) string

RebindBuff 是 Rebind 的实验性实现,使用 bytes.Buffer。 代码更简单,应当更能抵抗奇特的 unicode,但速度慢两倍。

func ScanAll

func ScanAll(rows Rowsi, dest any, structOnly bool, unsafe bool) error

ScanAll 将所有行扫描到目标切片中。

func Select

func Select(q Queryer, dest any, query string, args ...any) error

Select 使用提供的 Queryer 执行查询,并将每行通过 StructScan 扫描到 dest 中。

func SliceScan

func SliceScan(r ColScanner) ([]any, error)

SliceScan 扫描一行,返回类似 MapScan 的 []any 值。

func StructScan

func StructScan(rows Rowsi, dest any) error

StructScan 将所有行扫描到 dest 切片中。

Types

type Binder

type Binder interface {
	DriverName() string
	Rebind(string) string
	BindNamed(string, any) (string, []any, error)
}

Binder 是能够绑定查询的接口。

type ColScanner

type ColScanner interface {
	Columns() ([]string, error)
	Scan(dest ...any) error
	Err() error
}

ColScanner 是 MapScan 和 SliceScan 使用的接口。

type Execer

type Execer interface {
	Exec(query string, args ...any) (sql.Result, error)
}

Execer 是 MustExec 和 LoadFile 使用的接口。

type Ext

type Ext interface {
	Binder
	Queryer
	Execer
}

Ext 是一个联合接口,可以绑定、查询和执行。

type Preparer

type Preparer interface {
	Prepare(query string) (*sql.Stmt, error)
}

Preparer 是 Preparex 使用的接口。

type Queryer

type Queryer interface {
	Query(query string, args ...any) (*sql.Rows, error)
	Queryx(query string, args ...any) (*Rows, error)
	QueryRowx(query string, args ...any) *Row
}

Queryer 是 Get 和 Select 使用的接口。

type Row

type Row struct {
	Unsafe bool
	Rows   *sql.Rows
	Mapper *reflectx.Mapper
	// contains filtered or unexported fields
}

Row 是对 sql.Row 的重新实现,目的是能够访问底层 sql.Rows.Columns() 数据, 这是 StructScan 所必需的。

func NewRow

func NewRow(rows *sql.Rows, err error, unsafe bool, mapper *reflectx.Mapper) *Row

NewRow 创建一个新的 Row 实例。供基础设施层使用。

func (*Row) ColumnTypes

func (r *Row) ColumnTypes() ([]*sql.ColumnType, error)

ColumnTypes 返回底层 sql.Rows.ColumnTypes(),或延迟错误。

func (*Row) Columns

func (r *Row) Columns() ([]string, error)

Columns 返回底层 sql.Rows.Columns(),或延迟错误。

func (*Row) Err

func (r *Row) Err() error

Err 返回扫描过程中遇到的错误。

func (*Row) MapScan

func (r *Row) MapScan(dest map[string]any) error

MapScan 使用此 Row 进行映射扫描。

func (*Row) Scan

func (r *Row) Scan(dest ...any) error

Scan 是 sql.Row.Scan 的修正实现,如果内部的 Rows 对象存在错误, 它不会丢弃该错误。

func (*Row) ScanAny

func (r *Row) ScanAny(dest any, structOnly bool) error

ScanAny 将单行扫描到 dest 中。structOnly 为 true 时要求目标必须是结构体。

func (*Row) SliceScan

func (r *Row) SliceScan() ([]any, error)

SliceScan 使用此 Row 进行切片扫描。

func (*Row) StructScan

func (r *Row) StructScan(dest any) error

StructScan 将单行扫描到 dest 中。

type Rows

type Rows struct {
	*sql.Rows
	Unsafe bool
	Mapper *reflectx.Mapper
	// contains filtered or unexported fields
}

Rows 是 sql.Rows 的封装,在循环 StructScan 期间缓存高开销的反射操作。

func (*Rows) MapScan

func (r *Rows) MapScan(dest map[string]any) error

MapScan 使用此 Rows 进行映射扫描。

func (*Rows) SliceScan

func (r *Rows) SliceScan() ([]any, error)

SliceScan 使用此 Rows 进行切片扫描。

func (*Rows) StructScan

func (r *Rows) StructScan(dest any) error

StructScan 将单行扫描到单个结构体中。 当 Select() 的内存负载可能过高时,使用此方法手动迭代 Rows。

type Rowsi

type Rowsi interface {
	Close() error
	Columns() ([]string, error)
	Err() error
	Next() bool
	Scan(...any) error
}

Rowsi 是统一的行迭代接口。

Jump to

Keyboard shortcuts

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